// Declare variables
var isIE = jQuery.browser.msie;
var sensitive;
var interval;
var timeout;

// Speed for hover-over effect
var hoverSpeed = 250;
var flagHoverShowStarted = false;
var flagHoverHideStarted = false;

// Needed for image expand/contract on testimonials video page
var imgHeight = 68;
var percentUp = 250;
var percentDown = 40;
var speed = 0.5;
var firstImg = "";
var secondImg = "";
var isFirstBig = false;
var isSecondBig = false;

// Hover Variables for menu
sensitivity = 3;	// sensitivity threshold (must be 1 or higher)
interval = 50;		// milliseconds for onMouseOver polling interval
timeout = 300;		// milliseconds delay before onMouseOut

// Set jQuery in NoConflict mode
jQuery.noConflict();

function validateTestimonialUploadForm(obj)
{
	var error = true;
	
	if (obj.firstName.value == "")
	{
		jQuery("div#errorMessageFirstName").text("Please enter your first name");
		error = false;
	}
	if (obj.lastName.value == "")
	{
		jQuery("div#errorMessageLastName").text("Please enter your last name");
		error = false;
	}
	if (obj.lastName.value == "")
	{
		jQuery("div#errorMessageEmail").text("Please enter your last name");
		error = false;
	}
	
	
	return error;
}

// Checks to see if the user has corectly marked the image/video upload form and if so, enables the upload button
// The selector ":filled" requires the Validate plugin
function EnableAssetUploadForm(evt, force)
{
	jQuery("input#upload").get(0).disabled = 
		(force != null ? !force : !(jQuery("input#termsConditions").is(":checked") && jQuery("input#userAsset").is(":filled")));
}

// Fade in an image wraped in a div when called
function hoverOverDisplay(objId)
{
	if (!flagHoverShowStarted && !flagHoverHideStarted)
	{
		flagHoverFadeStarted = true;
		jQuery("div#"+objId).hide().fadeIn(hoverSpeed, function(){flagHoverShowStarted = false;});
	}

}

// Fade out an image wraped in a div when called
function hoverOutDisplay(objId)
{
	if (!flagHoverHideStarted)
	{
		flagHoverHideStarted = true;
		jQuery("div#"+objId).fadeOut(hoverSpeed, function(){flagHoverHideStarted = false;});
	}
}


function menuDisplay(tabId, menuId)
{
	tabMove(tabId);
	if (menuId) {
		jQuery(menuId).prev().slideDown(400);
		jQuery(menuId).css({opacity: 0}).animate({opacity: 1}, {duration: 500, queue: false}).slideDown(400);
	}
}

function menuHide(tabId, menuId)
{
	if (menuId) jQuery(menuId).prev().hide().end().animate({opacity: 0}, {duration: 800, queue: false}).slideUp(400, function(){tabUp(tabId);});
	else tabUp(tabId);
}

function tabProductsDisplay()
{
	menuDisplay('tabProducts', 'div#subProducts');
}

function tabProductsHide()
{
	menuHide('tabProducts', 'div#subProducts');
}

function tabGettingstartedDisplay()
{

	menuDisplay('tabStart', 'div#subStart');
}

function tabGettingstartedHide()
{
	menuHide('tabStart', 'div#subStart');
}

function tabTestimonialsDisplay()
{

	menuDisplay('tabTestimonials', 'div#subTestimonials');
}

function tabTestimonialsHide()
{
	menuHide('tabTestimonials', 'div#subTestimonials');
}

function tabLocatorDisplay()
{

	menuDisplay('tabLocator');
}

function tabLocatorHide()
{
	menuHide('tabLocator');
}

function tabTellDisplay()
{

	menuDisplay('tabTell');
}

function tabTellHide()
{
	menuHide('tabTell');
}

function tabHelpDisplay()
{

	menuDisplay('tabHelp');
}

function tabHelpHide()
{
	menuHide('tabHelp');
}

// Executes the specified effect on the current node and all sibling nodes that
// follow it, but waits until the current effect is finished first before
// starting the next. Support for more effects can easily be added.
function chainEffect(jq, effect, options) {
	if (jq.length)
		switch (effect) {
			case "fadeIn": jq.fadeIn(options.speed, function(){chainEffect(jQuery(this).next(), effect, options);}); break;
			default: break;
		}
}

// Script to execute accross EVERY page when the DOM is ready
jQuery(document).ready(function(){
	jQuery("div#products").hoverIntent({
		sensitivity: sensitivity,
		interval: interval,
		over: tabProductsDisplay,
		timeout: timeout,
		out: tabProductsHide
	});

	jQuery("div#getStarted").hoverIntent({
		sensitivity: sensitivity,
		interval: interval,
		over: tabGettingstartedDisplay,
		timeout: timeout,
		out: tabGettingstartedHide
	});
	
	jQuery("div#testimonials").hoverIntent({
		sensitivity: sensitivity,
		interval: interval,
		over: tabTestimonialsDisplay,
		timeout: timeout,
		out: tabTestimonialsHide
	});
	
	jQuery("div#storeLocator").hoverIntent({
		sensitivity: sensitivity,
		interval: interval,
		over: tabLocatorDisplay,
		timeout: timeout,
		out: tabLocatorHide
	});
	
	jQuery("div#tellAfriend").hoverIntent({
		sensitivity: sensitivity,
		interval: interval,
		over: tabTellDisplay,
		timeout: timeout,
		out: tabTellHide
	});
	
	jQuery("div#help").hoverIntent({
		sensitivity: sensitivity,
		interval: interval,
		over: tabHelpDisplay,
		timeout: timeout,
		out: tabHelpHide
	});
	
	// Set opacity of menu backgrounds
	jQuery("#navMod .subNavBg").css({opacity: 0.75});
	
	// Fix PNG display for IE
	correctPNG();
});