$.fn.extend({
	/**
	* Rollover for images with preloader 
	*/
 	rollOver : function(){
		
		/* Preload */ 		
 		$(this).each(function(){
 			var image = new Image();
 			image.src = $(this).attr('alt');
 		});

 		var onMouseOver = function(){
 			$(this).data('tmp', $(this).attr('src'));
			$(this).attr('src', $(this).attr('alt'));
		};
		
		var onMouseOut = function(){
			$(this).attr('src', $(this).data('tmp'));
		};

	 	$(this).hover(onMouseOver, onMouseOut);
	 	
	}

});


       
$(document).ready(function(){

	/** Menu **/
	
	$("ul.main-menu > li").hover(
		function(){
			$(".sub-menu").hide();
			$(this).children("a").attr('class','active');
			$(this).children("ul.sub-menu").show();
		},
		function(){
			$(".sub-menu").hide();
			$(this).children("a").attr('class','');
		}
	)
	
	$("#splash .nav a img").rollOver();
   
   	/** Splash **/

	var timer = setInterval(animate, 5000);
	var tmp = 1;

	function animate(){
		try{
			$(".slides div").each(function(){
			
				if($(this).attr('class') == 'active'){
					tmp++;
					
					if(tmp > 4){
						tmp = 1;
					}
					$(this).attr('class','');
					
					$("#slide" + tmp).attr('class','active');
					$("#slidepos span").each(function(){
						if($(this).attr('class') == 'underline'){
							$(this).attr('class','');
						}
					});
					$("#slidepos span[title='" + tmp + "']").attr('class','underline');
					
					throw "";
				}
			});
		}
		catch(e){
			
		}
	}
	
	$("#slidepos span").click(function(){
		
		clearInterval(timer);
		
		$(".slides div").each(function(){
			if($(this).attr('class') == 'active'){
				$(this).attr('class','');
			}
		});
		
		$("#slidepos span").each(function(){
			if($(this).attr('class') == 'underline'){
				$(this).attr('class','');
			}
		});
		
		$(this).attr('class','underline');
		
		$("#slide" + $(this).attr('title') ).attr('class','active'); 
			
	});
	
	/** Home page news / events **/
	
	$("#latest-news").click(function(){
		$(this).attr("class","blue");
		$("#upcoming-events").attr('class','');
		$("#home-events").hide();
		$("#home-news").show();
		
	});
	
	$("#upcoming-events").click(function(){
		$(this).attr("class","blue");
		$("#latest-news").attr('class','');
		$("#home-news").hide();
		$("#home-events").show();
	});




});