
$(document).ready(function() {
	
	/* HOVER & SUB NAV */
	function megaHoverOver(){$(this).addClass("active");$(this).find(".sub").stop().fadeTo('fast', 1).show();}
	function megaHoverOut(){$(this).removeClass("active");$(this).find(".sub").stop().fadeTo('fast', 0, function() {$(this).hide();});}
	var config = { sensitivity: 2, interval: 0, over: megaHoverOver, timeout: 200, out: megaHoverOut };
	$("#header .top #nav li .sub").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
	$("#header .top #nav li").hoverIntent(config); //Trigger Hover intent with custom configurations
		
	/* TOP SEARCH BAR */
	$('#navsearch').focus(function() {if(($(this).val() == '')||($(this).val() == 'Enter Your Search Here')){$(this).val('');}});
	$('#navsearch').blur(function() {if($(this).val() == ''){$(this).val('Enter Your Search Here');}});
	
	/* LOGIN AREA */
	$('#loginemail').focus(function() {$(this).val('');});
	$('#loginemail').blur(function() {if($(this).val() == ''){$(this).val('Email Address');}});
	
	$('#loginpasscover').blur(function() {if($(this).val() == ''){$(this).val('Password');}});
	$("#loginpasscover").focus(function(){$(this).css("display","none");$("#loginpass").css("display","block");$("#loginpass").focus();});
	
	$('#loginpass').blur(function() {if($(this).val() == ''){$("#loginpass").css("display","none");$("#loginpasscover").css("display","block");}});
	
	$('#account_update_submit').click(function(){
		
		var errors = 0;
		var alertString = "";
		if($('#firstname').val()=="")
		{
			alertString+="Enter your First Name";
			errors++;
		}
		
		if($('#lastname').val()=="")
		{
			if(errors>0)
			{
				alertString+="\n";
			}
			alertString+="Enter your Last Name";
			errors++;
		}
				
		if(errors!=0)
		{
			alertString = "Please correct the following: \n\n"+alertString;
			alert(alertString);
			return false;
		}
		
	});
	$('#send_content_sub').click(function(){
	
		var errors = 0;
		
		if($('#val_category').val()=="")
		{
			alert("Please choose a News Category");
			errors++;
		}
		else if($('#val_name').val()=="")
		{
			alert("Please enter your Full Name");
			errors++;
		}
		else if($('#val_email').val()=="")
		{
			alert("Please enter your Email Address");
			errors++;
		}
		
		if(errors!=0)
		{
			return false;
		}
	});
	
	
	function slideSwitch()
	{
		var $active = $('#footer .main #subscribers #fader div.active');

		if ( $active.length == 0 ) $active = $('#footer .main #subscribers #fader div:last');

		// use this to pull the images in the order they appear in the markup
		var $next =  $active.next().length ? $active.next()
		: $('#footer .main #subscribers #fader div:first');

		//pull random image
		var $sibs  = $active.siblings();
		var rndNum = Math.floor(Math.random() * $sibs.length );
		var $next  = $( $sibs[ rndNum ] );
		
		$active.addClass('last-active');
		$next.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 1000, function(){$active.removeClass('active last-active');});
	}
	
	setInterval(slideSwitch, 5000);

	$("#newsalerts, #closeformbtn").click(function()
	{
		
		if($("#submitenewslayer").css("display")=="none")
		{
			//$("#submitenewslayer").css("display","block");
			$("#submitenewslayer").fadeIn("fast");
		}
		else
		{	
			//$("#submitenewslayer").show();
			//$("#submitenewslayer").css("display","none");
			$("#submitenewslayer").fadeOut("fast");
		}
		return false;
	});
	/* tabbing system on homepage */
	$("#home1 .news ul li, .itemcontainer .months ul li").click(function()
	{
		$("#home1 .news ul li, .itemcontainer .months ul li").removeClass("active");
		$("#home1 .news ul li, .itemcontainer .months ul li").addClass("inactive");
		$(this).removeClass("inactive");
		$(this).addClass("active");
		
		var contentID = $(this).attr("id").split("_");
		
		var contentIt = 0;
		var numChildren = $("#home1 .news .content > .content_block, .itemcontainer .content > .content_block").size()
		
		$("#home1 .news .content .content_block, .itemcontainer .content .content_block").fadeOut(300, function()
		{
			contentIt++;
			if(contentIt==numChildren)
			{//this is to ensure we only start the fade in once the last element has faded out - otherwise there can be an overlap
				$("#home1 .news .content #block_"+contentID[1]+", .itemcontainer .content #block_"+contentID[1]).fadeIn(300);
			}
		});
		return false;
	});

	
	jQuery.fn.autoEllipseText = function(options)
	{
		//initalise variables
		var width = 0;
		var text = "";
		var offset = 0;
		
		var twidth = "";
		if(options["width"]){twidth = options["width"];}
		if(twidth!=""){width = options["width"];}
		
		var toffset = "";
		if(options["offset"]){toffset = options["offset"];}
		if(toffset!=""){offset = toffset;}
		
		//loop through each occurence
		this.each(function()
		{
			if(width==0)
			{
				width = $(this).width()+offset;
				
			}
			
			text = $(this).html();
			$(this).html('<span title="' + text + '" class="ellipsisSpan" style="white-space:nowrap;">' + text + '</span>');
			inSpan = $(this).find('span');
			
			if(inSpan.width() > width)
			{
				var i = 1;
				inSpan.html("");
				while(inSpan.width() < (width) && i < text.length)
				{
					inSpan.html(text.substr(0,i) + '...');
					i++;
				}
				returnText = inSpan.html();
				
				
			}
		});
		return this;
	};
	
	$("#content #home2 .newsitem h3 a").autoEllipseText({offset:-20});
});

