$(document).ready(function() {
	var activeMenuElemEnabled = 1;
	
	function activeMenuElemOn(){
		if( $("#active_menu_elem").length>0 ) {
			if( activeMenuElemEnabled == 1 ) {
		  		$("#active_menu_elem").find("a:first").removeClass("home");
	  			$("#active_menu_elem").find("a:first").addClass("topnav_hover");
	  			$("#active_menu_elem").find(".sub").stop().fadeTo('fast', 1).show();
	  		}
	  	}
	}
	
	function activeMenuElemOff(){
		if( $("#active_menu_elem").length>0 ) {
	  	  $("#active_menu_elem").find("a:first").addClass("home");
		  $("#active_menu_elem").find("a:first").removeClass("topnav_hover");
		  $("#active_menu_elem").find(".sub").stop().fadeTo('fast', 0, function() {
			  $(this).hide(); 
		 });
		}
	}
	

	function megaHoverOver(){
		if( $(this).hasClass( "topnav_li" ) && $(this).attr("id") != "active_menu_elem" ) {
			activeMenuElemEnabled = 0;
			activeMenuElemOff();
		}
	
		$(this).find("a:first").addClass("topnav_hover");
		$(this).find("a:first").removeClass("home");
		$(this).find(".sub").stop().fadeTo('fast', 1).show();
			
		//Calculate width of all ul's
		(function($) { 
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				$(this).find("ul").each(function() {					
					rowWidth += $(this).width(); 
				});	
			};
		})(jQuery); 
		
		if ( $(this).find(".row").length > 0 ) { //If row exists...
			var biggestRow = 0;	
			//Calculate each row
			$(this).find(".row").each(function() {							   
				$(this).calcSubWidth();
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
			//Set width
			$(this).find(".sub").css({'width' :biggestRow});
			$(this).find(".row:last").css({'margin':'0'});
			
		} else { //If row does not exist...
			
			$(this).calcSubWidth();
			//Set Width
			$(this).find(".sub").css({'width' : rowWidth});
			
		}
	}
	
	function megaHoverOut(){
	  if( $(this).hasClass( "topnav_li" ) && $(this).attr("id") != "active_menu_elem" ) { 
	  	$(this).find("a:first").addClass("home");
	  	$(this).find("a:first").removeClass("topnav_hover");
	  	$(this).find(".sub").stop().fadeTo('fast', 0, function() {
		  $(this).hide(); 
	  	});
	  	activeMenuElemEnabled = 1;
		setTimeout( activeMenuElemOn, 150 );
	  }
	}


	var config = {    
		 sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 0, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 0, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};

	$("ul#topnav li .sub").css({'opacity':'0'});
	$("ul#topnav li").hoverIntent(config);
	activeMenuElemOn();
	
	slideShow();

	popup();

});

function slideShow() {

	//Set the opacity of all images to 0
	$('#gallery a').css({opacity: 0.0});
	$('#gallery_thumbs div').css({opacity: 0.5});
	
	//Get the first image and display it (set it to full opacity)
	$('#gallery a:last').css({opacity: 1.0});
	$('#gallery_thumbs div:last').css({opacity: 1.0});
	
	if( $("#gallery a").length > 1 ) {
		//Call the gallery function to run the slideshow, 6000 = change to next image after 6 seconds
		setInterval('gallery()',5000);
	}
}

function gallery() {
	
	//if no IMGs have the show class, grab the first image
	var current = ($('#gallery a.show')?  $('#gallery a.show:last') : $('#gallery a:last'));
	var current_thumb = ($('#gallery_thumbs div.active-thumb')?  $('#gallery_thumbs div.active-thumb:last') : $('#gallery_thumbs div:last'));

	//Get next image, if it reached the end of the slideshow, rotate it back to the first image
	var next = ((current.prev().length) ? current.prev() : $('#gallery a:last'));	
	var next_thumb = ((current_thumb.prev().length) ? current_thumb.prev() : $('#gallery_thumbs div:last'));	
	
	//Set the fade in effect for the next image, show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);
	
	next_thumb.css({opacity: 0.5})
	.addClass('active-thumb')
	.animate({opacity: 1.0}, 500);

	//Hide the current image
	current.animate({opacity: 0.0}, 1500)
	.removeClass('show');
	current_thumb.animate({opacity: 0.5}, 500)
	.removeClass('active-thumb');
}

function popup() {
	var pObj = $('#popup');
	var p_left=0, p_top=0;
	
	if( pObj.length>0 ) {
		while( p_left<=0 || p_top<=0 ) {
			img_width = $('#popup > img').width();
			img_height = $('#popup > img').height();
			
			img_width = parseInt(img_width*0.90)-30;
			img_height = parseInt(img_height*0.90)-30;
			
			if( p_left<0||p_top<0 ) {
				$('#popup > img').width(img_width);
				$('#popup > img').height(img_height);
			}
			
			p_left = parseInt(($(window).width()-$('#popup > img').width())/2);
			p_top = parseInt(($(window).height()-$('#popup > img').height())/2);
		}
		
		pObj.css({'position':'fixed','left':p_left,'top':p_top,'z-index':2500});
		pObj.hide();
	
		
		setTimeout(function(){
        		pObj.fadeIn('slow');
        	},1000);
		
		pObj.find('a').click(function(){
		    $('#popup').fadeOut('slow');
		});
	}
}

