$(document).ready(function() {
	
	if (validate == "yes" ) { $(".validate").validate(); }
	
	if (lightbox == "yes" ) { $(".lightBox").lightBox(); }
	
	// fix for target='_blank'
	$('a[href^="http://"], a[href^="https://"], .external').attr({
		target: "_blank", 
		title: "Opens in a new window"
	});	
	
	//search clear
	$(".search").click(function(){
		var val = $(this).val();
		if(val == "search..."){
			$(this).val("");
		}	
	});
	
	//search highlight
	$(".searchHighlight p").each(function(){
		var content = $(this).html();
		var searchVal = $(".search").val();
		var content = content.replace(searchVal,"<span class='highlight'>"+searchVal+"</span>");
		$(this).html(content);
	});
			
	//slider
	$("#slider").edSlider();
	
	//grey min pod
	var newsHeight = $(".newsList").height();
	if(newsHeight < 300){
		$(".newsList").height(300);
	}
	
	//equal pods
	equal(".pod");
	
	$(".featureItems").edFade({
		auto:true,
		pause:4000
	});
	
	//disable href
	$(".nogo").click(function(e){
		e.preventDefault();
	});
	
	//drop down	
	$(".hasSubNav").hover(  
		function () {  
			$("ul", this).slideDown(100);  
		   
		},   
        function () {  	            
			$("ul", this).slideUp(100);           
        }  
    ); 
	
			
	$(".slidingForm").slidingForm({
		openAll:false,
		openOn:0,
		openClass:"open",
		closeClass:"close"
	 });
	
	//news hover
	$(".newsItem").mouseenter(function(){
		$(this).css("background-color","#fff");
	});
	$(".newsItem").mouseleave(function(){
		$(this).css("background-color","#ebebeb");
	});
 
});


function equal(pod){
	
	$("body").append("<input type='hidden' value='0' class='equal'/>");
	
	$(pod).each(function(){
		var equal = $(".equal").val();
		var podHeight = $(this).height();		
		if(equal < podHeight){			
			$(".equal").val(podHeight);						
		}		
	});
	setTimeout(function(){
		var equal = $(".equal").val();
		$(".pod").height(""+equal+"px");
	},500);
	
}

$.fn.edFade = function(options) {  

	var defaults = {  
		pause:2000,
		auto:true,
		nextMessage:"Click for Next"
	};  
 
	var options = $.extend(defaults, options);  
  
	return this.each(function() {  
		
		//vars
		var element = $(this);
		var item = element.find("li");
		var itemNo = item.length;
		var elementCss = {
			"position":"relative"
		};
		var itemCss = {
			"position":"absolute",
			"top":"0px",
			"left":"0px",
			"display":"none",
			"z-index":"2"
		};
		
		//setup
		element.css(elementCss);
		item.css(itemCss);
		
		item.each(function(i){
			i++;		
			$(this).attr("id","item"+i);
		});	
		
		if(options.auto == true){
			element.find("li:first").clone().appendTo(element).css("z-index","1").show("");
			element.attr("paused",false);
			var link = element.find("li:first").children("a").attr("href");
			$(".featureMask").find("a").hide("");
			$(".featureLink").attr("href",link);
		}else{
			element.find("li:first").show("");
		}
							
		//call functions
		if(options.auto == true){
			auto();
		}else{
			clicker();
		}
				
		$(".featureMask").mouseover(function(){
			element.attr("paused",true);
		});
		
		$(".featureMask").mouseout(function(){
			element.attr("paused",false);
		});
				
		//functions
		function fade(i){
			var fade = "#item"+i;
			var link = $(fade).find("a").attr("href");
			$(".featureLink").attr("href",link);
			item.fadeOut("slow");
			$(fade).fadeIn("slow");
		}
		
		function auto (){							
			item.each(function(i){
				i++;		
				setTimeout(function(){									
					var paused = element.attr("paused");
					if(paused == "false"){
						fade(i);
					}		
					if(i == itemNo){
						auto();
					}
					return false;
				},options.pause * i);
			});
		}
		
		function clicker(){
			var infoCss = {
				"position":"absolute",
				"bottom":"-10px",
				"right":"0px",
				"z-index":"3",
				"font-size":"10px",
				"color":"#000"
			};
			item.append("<span class='info'>"+options.nextMessage+"</span>").css("cursor","pointer");
			element.find(".info").css(infoCss);
			item.click(function(){
				var next = $(this).next(item).attr("id");
				var next = "#"+next;
				item.fadeOut("slow");
				if(next != "#undefined"){
					$(next).fadeIn("slow");
				}else{
					element.find("li:first").fadeIn("slow");
				}
			});
		}
						
	});  
};


$.fn.slidingForm = function(options) {  

	var defaults = {  
		openOn:1,
		openAll:false,
		openClass:"edit",
		closeClass:"close"
	};  
 
	var options = $.extend(defaults, options);  
  
	return this.each(function() {  
		
		
		var details = $(this).find(".details");
		var header = $(this).find("h2");
		details.attr("id", function (number) {
			return "details" + number;		  
		});
		if(options.openAll == false){
			details.hide("");
			var openOn = options.openOn - 1;
			//alert(openOn);
			var openOn = "#details" + openOn;
			//alert(openOn);
			$(openOn).show("");
			$(openOn).parents(".group").find("h2:first").addClass("active");
			header.append("<span class='open'></span>")
			$(".open").addClass(options.openClass);	
			$(".active span").addClass(options.closeClass);	
			header.click(function(){
				if($(this).hasClass("active")){
					$(".open").removeClass(options.closeClass);
					details.slideUp("slow");
					header.removeClass("active");
				}else{
					$(".open").removeClass(options.closeClass);
					$(this).find("span").addClass(options.closeClass);
					details.slideUp("slow");
					$(this).next(".details").slideDown("slow");
					header.removeClass("active");
					$(this).addClass("active");
				}
			});
		}else{
			details.show("");
		}		
	});  
};
