$(document).ready(function() 
{

	function megaHoverOver(){
		$(this).find(".sub").stop().fadeTo('fast', 1).show();
		$(this).find(".sub-right").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(".sub-right").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});
			$(this).find(".sub-right").css({'width' : rowWidth});			
			
		}
	}
	
	function megaHoverOut(){ 
	  $(this).find(".sub").stop().fadeTo('fast', 0, function() {
		  $(this).hide(); 
	  });
	  $(this).find(".sub-right").stop().fadeTo('fast', 0, function() {
		  $(this).hide(); 
	  });	  
	}


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

	$("#menu-bar ul li .sub").css({'opacity':'0'});
	$("#menu-bar ul li .sub-right").css({'opacity':'0'});	
	$("#menu-bar ul li").hoverIntent(config);
});

function initHome()
{
	fadeAllStars();
	
	initTweets();
}

function initTweets()
{
	$("#tweet-list").tweet({
		avatar_size: 24,
		count: 3,
		username: "VictoryGym",
		loading_text: "loading list..."
	});	
}

function viewEventInfo(event_id)
{
	
	var error_count = 0;
	var ajaxRequest;
		try{
			// Opera 8.0+, Firefox, Safari
			ajaxRequest = new XMLHttpRequest();
		} catch (e){
			// Internet Explorer Browsers
			try{
				ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try{
					ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e){
					// Something went wrong
					alert("Your browser broke!");
					return false;
				}
			}
		}
		// Create a function that will receive data sent from the server
		ajaxRequest.onreadystatechange = function()
		{
			if(ajaxRequest.readyState == 4)
			{
				/*
				var r = ajaxRequest.responseText;
				$("#calendar_details").html(r);
				var current_left = "-500";
				$("#calendar_outer").animate({ 
					left: current_left+"px"
			      }, 650);  
				  */
				  
				  var r = ajaxRequest.responseText;
				$("#calendar_details").html(r);
				$("#calendar_details").fadeIn("slow");
			}
		}
		
		var values = "";
		values += "&news_id="+escape(encodeURI(event_id));
		var queryString = "?action=view"+values;
		ajaxRequest.open("GET", "/assets/ajax/news.php" + queryString, true);
		ajaxRequest.send(null); 
}



function closeEventInfo()
{
	closeTellAFriend();
	$("#calendar_details").html("");
	$("#calendar_details").fadeOut("slow");
}

function openTellAFriend(news_id)
{
	$("#calendar_tell").fadeIn("fast");
	$("#tell_news_id").val(news_id);
}


function closeTellAFriend()
{
	$("#calendar_tell").fadeOut("fast");
	$("#gallery_tell").fadeOut("fast");
	$("#tell_your_name").val("");
	$("#tell_your_name_label").removeClass("error");
	
	$("#tell_your_email").val("");
	$("#tell_your_email_label").removeClass("error");
	
	$("#tell_friend_name").val("");
	$("#tell_friend_name_label").removeClass("error");
	
	$("#tell_friend_email").val("");
	$("#tell_friend_email_label").removeClass("error");
	
	$("#tell_comments").val("");
	
}

function checkEventsTell()
{
	var e = 0;
	if ($("#tell_your_name").val()=="")
	{
		$("#tell_your_name_label").addClass("error");
		
		e++;
	} else 
	{
		$("#tell_your_name_label").removeClass("error");
	}
	
	var em = $("#tell_your_email").val();
	if (em=="" || em.indexOf(".")==-1 || em.indexOf("@")==-1)
	{
		$("#tell_your_email_label").addClass("error");
		e++;
	} else 
	{
		$("#tell_your_email_label").removeClass("error");
	}
	
	if ($("#tell_friend_name").val()=="")
	{
		$("#tell_friend_name_label").addClass("error");
		
		e++;
	} else 
	{
		$("#tell_friend_name_label").removeClass("error");
	}
	
	var em = $("#tell_friend_email").val();
	if (em=="" || em.indexOf(".")==-1 || em.indexOf("@")==-1)
	{
		$("#tell_friend_email_label").addClass("error");
		e++;
	} else 
	{
		$("#tell_friend_email_label").removeClass("error");
	}
	
	
	
	if(e>0)
	{
		return false;
	} else 
	{
		sendEventsTell();
		return false;
	}
}

function sendEventsTell()
{
	var error_count = 0;
	var ajaxRequest;
		try{
			// Opera 8.0+, Firefox, Safari
			ajaxRequest = new XMLHttpRequest();
		} catch (e){
			// Internet Explorer Browsers
			try{
				ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try{
					ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e){
					// Something went wrong
					alert("Your browser broke!");
					return false;
				}
			}
		}
		// Create a function that will receive data sent from the server
		ajaxRequest.onreadystatechange = function()
		{
			if(ajaxRequest.readyState == 4)
			{
				var r = ajaxRequest.responseText;
				closeTellAFriend();
			}
		}
		
		var values = "";
		values += "&news_id="+escape(encodeURI($("#tell_news_id").val()));
		values += "&your_name="+escape(encodeURI($("#tell_your_name").val()));
		values += "&your_email="+escape(encodeURI($("#tell_your_email").val()));
		values += "&friend_name="+escape(encodeURI($("#tell_friend_name").val()));
		values += "&friend_email="+escape(encodeURI($("#tell_friend_email").val()));
		values += "&comments="+escape(encodeURI($("#tell_comments").val()));
		var queryString = "?action=tell"+values;
		
		ajaxRequest.open("GET", "/assets/ajax/news.php" + queryString, true);
		ajaxRequest.send(null); 
}




var current_allstar = -1;
var allstar_timer;

var slider_count;

function fadeAllStars()
{

	var sl = $("#all-star-of-the-month ul li");
	slider_count = sl.length;
	
	current_allstar++;
	if (current_allstar==slider_count) current_allstar =0;
	var current_allstar_slide; 
	$("#all-star-of-the-month ul li").css("display","none");
	current_allstar_slide = $("#all-star-of-the-month ul li")[current_allstar];
	current_allstar_slide_img = $("#all-star-of-the-month ul li img")[current_allstar];
	
	
	$("#all-star-of-the-month-title").html('<a href="/all-star-of-the-month">'+$(current_allstar_slide_img).attr("title")+'</a>');
	$("#all-star-of-the-month-cat").html($(current_allstar_slide_img).attr("alt"));
	$(current_allstar_slide).fadeIn("slow");
	allstar_timer = setTimeout("fadeAllStars()",5000);
	
	
}

function aomPrev()
{
	clearTimeout( allstar_timer );
	current_allstar = current_allstar - 2;
	if (current_allstar==-2) current_allstar = 2;
	
	fadeAllStars();
}


function aomNext()
{

	clearTimeout( allstar_timer );
	if (current_allstar>(slider_count-1)) current_allstar =0;
	
	fadeAllStars();
}



var tourSlides;
var tourSlideTotal = 0;
var tourSlideIndex = 1;
var tourTimer;
var tourTemp;

function loadTour(to_view)
{

	clearTimeout(tourTimer);
	$(".tour-box").css("display","none");
	
	$("#"+to_view).fadeIn("slow");
	
	tourSlides = $("#"+to_view+" .tour-image ul li");
	
	tourSlideTotal = tourSlides.length;
	if (tourSlideTotal>1)
	{
		$(".tour-image ul li").css("display","none");
		tourTemp = $("#"+to_view+" .tour-image ul li")[0];
		$(tourTemp).fadeIn("slow");
		tourTimer = setTimeout("tourSlide('"+to_view+"')",4000);
	}
}


function tourSlide(to_view)
{
	
	
	for (x=0;x<tourSlideTotal;x++)
	{
		if (x!=tourSlideIndex) $(tourSlides[x]).css("display","none");
	}
	
	for (x=0;x<tourSlideTotal;x++)
	{
		if (x==tourSlideIndex)
		{
			$(tourSlides[x]).fadeIn("slow");
		}
	}
	tourSlideIndex++;
	
	if (tourSlideIndex < tourSlideTotal)
	{
		
	} else 
	{
		tourSlideIndex = 0;
	}
	
	tourTimer = setTimeout("tourSlide('"+to_view+"')",4000);
}



/* FILTERING FOR EXHIBITORS */

(function($) {
	$.fn.filterable = function(settings) {
		settings = $.extend({
			useHash: true,
			animationSpeed: 800,
			show: { width: 'show', opacity: 'show' },
			hide: { width: 'hide', opacity: 'hide' },
			useTags: true,
			tagSelector: '#product-filter a',
			selectedTagClass: 'current',
			allTag: 'all'
		}, settings);
		
		return $(this).each(function(){
		
			/* FILTER: select a tag and filter */
			$(this).bind("filter", function( e, tagToShow ){
				if(settings.useTags){
					$(settings.tagSelector).removeClass(settings.selectedTagClass);
					$(settings.tagSelector + '[href=' + tagToShow + ']').addClass(settings.selectedTagClass);
				}
				$(this).trigger("filterportfolio", [ tagToShow.substr(1) ]);
			});
		
			/* FILTERPORTFOLIO: pass in a class to show, all others will be hidden */
			$(this).bind("filterportfolio", function( e, classToShow ){
				if(classToShow == settings.allTag){
					$(this).trigger("show");
				}else{
					$(this).trigger("show", [ '.' + classToShow ] );
					$(this).trigger("hide", [ ':not(.' + classToShow + ')' ] );
				}
				if(settings.useHash){
					location.hash = '#' + classToShow;
				}
			});
			
			/* SHOW: show a single class*/
			$(this).bind("show", function( e, selectorToShow ){
				$(this).children(selectorToShow).animate(settings.show, settings.animationSpeed);
			});
			
			/* SHOW: hide a single class*/
			$(this).bind("hide", function( e, selectorToHide ){
				$(this).children(selectorToHide).animate(settings.hide, settings.animationSpeed);	
			});
			
			/* ============ Check URL Hash ====================*/
			if(settings.useHash){
				if(location.hash != '')
					$(this).trigger("filter", [ location.hash ]);
				else
					$(this).trigger("filter", [ '#' + settings.allTag ]);
			}
			
			/* ============ Setup Tags ====================*/
			if(settings.useTags){
				$(settings.tagSelector).click(function(){
					$('.product-listing-tile').trigger("filter", [ $(this).attr('href') ]);
					
					$(settings.tagSelector).removeClass('current');
					$(this).addClass('current');
				});
			}
		});
	}
})(jQuery);



