jQuery(function(){
	//set some global vars
		$UA = jQuery.browser;
	//
	
	//fixes for internet explorer
		if($UA.msie)
		{
			//disable ie6
			if(parseInt($UA.version) == 6)
			{
				ieWarning();
			}
		
			ieFixes($UA);
		}
	//
	
	//css crossbrowser fixes for search area
		searchFix($UA);
	//
	
	//navigation controller
		jQuery('#topNav ul li').not('.submenu li').hover(function(){
			var $link = jQuery(this).children('a');
			$link.addClass('selectedMenuItem');
			var $submenu = $link.siblings('ul.submenu');
			$submenu.append("<li class='parentDeptName'><a href=" + $link.attr('href') + ">" + $link.text() + "</a></li>");
			$submenu.css({
				'z-index': '10000 !important'
			});
			$submenu.show();
		}, function(){
			jQuery('li.parentDeptName').remove();
			var $link = jQuery(this).children('a');
			$link.removeClass('selectedMenuItem');
			$link.siblings('ul.submenu').hide();
		});
	//
	
	//pretty things up
		//uniform is for form elements
		jQuery('#product-options-wrapper select').uniform();
		jQuery('input:checkbox').uniform();
		jQuery('input:radio').uniform();
		
		//product titles
		if(jQuery('body').hasClass('catalog-product-view')){
			var $pn = jQuery('#itemSku');
			var $heading = jQuery('.product-name h1');
			var $price = jQuery('.price-box');
			
			var combinedHeight = $pn.height() + $heading.height() + $price.height();
			var newHeight = 100-(100-combinedHeight);
			var offset = (100-combinedHeight)/2;
			
			jQuery('.product-name').css({
				'padding-top' : offset + 'px',
				'padding-bottom' : offset + 'px',
				'height' : newHeight + 'px'
			});
		}
	//
	
	//contact controller
		jQuery('#sendUserMsg').click(function(e){
			e.preventDefault();
			
			var $nameField = jQuery('#customerName');
			var $emailField = jQuery('#customerEmail');
			var $messageField = jQuery('#customerMessage');
			var $btn = jQuery(this);
			
			var userName = $nameField.val();
			var userEmail = $emailField.val();
			var userMessage = $messageField.val();
			var action = $btn.val();
			
			var submitForm = false;
			
			var data = new Array(
				Array(userName, 'name'),
				Array(userEmail, 'email'),
				Array(userMessage, 'text')
			);
			
			var errors = validate(data);
			
			if(errors[0] != '')
			{
				$nameField.val(errors[0]).css('color', 'red');
			}
			
			if(errors[1] != '')
			{
				$emailField.val(errors[1]).css('color', 'red');
			}
			
			if(errors[2] != '')
			{
				$messageField.val(errors[2]).css('color', 'red');
			}
			
			$nameField.focus(function(){
				var field = jQuery(this);
				field.css('color', 'black');
				field.val('');
			});
			
			$emailField.focus(function(){
				var field = jQuery(this);
				field.css('color', 'black');
				field.val('');
			});
			
			$messageField.focus(function(){
				var field = jQuery(this);
				field.css('color', 'black');
				field.val('');
			});
			
			for(var i=0; i<errors.length; i++)
			{
				if(errors[i] != '')
				{
					submitForm = false;
				}
				else
				{
						submitForm = true;
				}
			}
			
			if(submitForm == true)
			{
				alert(action);
				jQuery.ajax({
					url: '/sendMessage.php',
					type: "POST",
					data: { action: action, customerName: userName, customerEmail: userEmail, customerMessage: userMessage },
					success: function(data){
						alert(data);
						$nameField.val('');
						$emailField.val('');
						$messageField.val('');
					},
					error: function(){
						alert("There was an error sending your message. Please try again.");
					}
				});
			}
			
		});
	
	//tabs interface
		jQuery('#tabs').tabs();
	//
	
	//compare with 2 sets of tabs
		jQuery('.tabs').each(function(){
			jQuery(this).tabs();
		});
	//
	
	//accordion interface
		jQuery('#accordion').accordion({collapsible:true, autoHeight:false});
	//
	
	//homepage controller
		jQuery('li.promo').hide();
		jQuery('#promo1').show();
	
		var $rightLinks = jQuery('#featureRight ul li a');
	
		$rightLinks.each(function(){
			jQuery(this).addClass('promoLink');
		});
		
		if($UA.msie)
		{
			jQuery('#promoLink1').addClass('selectedPromoLinkIE');
		}
		else
		{
			jQuery('#promoLink1').addClass('selectedPromoLink');
		}
		
	
		var openPromo = 'promo1';
		$rightLinks.click(function(e){
			e.preventDefault();
			var $promoLink = jQuery(this);
			openPromo = promoController($promoLink, openPromo, $UA);
		});
		
		//rotate promos
		/*for(var i=1; i<5; i++)
		{
			var $currentPromo = 'promo' + i;
			if(i < 4)
			{
				var $nextPromo = jQuery('#promoLink' + (i+1));
			}
			else if(i == 4)
			{
				var $nextPromo = jQuery('#promoLink1');
			}
			
			setTimeout("$currentPromo = promoController($nextPromo, $currentPromo, $UA)", 4000);
			
		}*/
		
	//
	
	//minicart controller
		var $minicartLink = jQuery('.top-link-cart');
		var isMiniCartOpen = false;
		$minicartLink.click(function(e){
			e.preventDefault();
			isMiniCartOpen = minicartController($minicartLink, isMiniCartOpen);
		});
		
		//minicart close button
		jQuery('#closeMiniCart').click(function(e){
			e.preventDefault();
			isMiniCartOpen = minicartController($minicartLink, isMiniCartOpen);
		});
	//
	
	//brand slider
		jQuery('#brand-lower ul').cycle({
			fx:'scrollHorz',
			pause: 0,
			timeout: 0,
			prev: '#brand-left',
			next: '#brand-right'
		});
	//
	
	//one page checkout fix
		onePageCheckoutFix()
	//
	
	//trade show page
	if(jQuery('body').hasClass('cms-trade-shows'))
	{
		initTradeShow();

		jQuery('.show').hide();
		jQuery('.current').show();

		var show = getShowTitle();
		setShowTitle(show);

		jQuery('.col-left ul li a').click(function(e){
			e.preventDefault();
			
			var link = jQuery(this);
			
			tradeShowController(link);
		});
	}
});

function ieWarning()
{
	if(jQuery('body').is('.cms-home'))
	{
		var $window = jQuery(window);
		var $body = jQuery('body');
		var $cover = $body.children('#cover');
		var $warning = $body.children('#warning');
		var windowWidth = $window.width();
		var windowHeight = $window.height();
		
		$cover.show().css({
			'height':windowHeight+"px",
			'width':windowWidth+"px"
		});
		$body.css({
			'height':windowHeight+'px',
			'overflow':'hidden'
		});
		$warning.show();
		var top = (windowHeight-250)/2;
		var left = (windowWidth-820)/2;
		$warning.css({
			'top':top+'px',
			'left':left+'px'
		});
		
		jQuery('#warning a').click(function(e){
			e.preventDefault();
			var $link = jQuery(this);
			window.open($link.attr('href'));
		});
	}
}

function searchFix($ua)
{	
	var $search = jQuery('#search');
	var $searchBox = jQuery('#searchBox');
	var $searchSubmit = jQuery('#searchSubmit');
	
	//webkit (chrome, safari)
	if($ua.webkit)
	{
		$search.css('top','104px');
		
		$searchBox.css({
			'top':'5px',
			'border-radius':'3px 0px 0px 3px',
			'border':'1px solid #000'
		});
		
		$searchSubmit.css('top', '5px');
	}
	
	//mozilla
	if($ua.mozilla)
	{
		$searchSubmit.css({
			'top':'0px'
		});
	}
	
	//ie(6-9)
	if($ua.msie)
	{
		if(parseInt($ua.version) == 9) //IE9
		{
			$searchSubmit.css('top','0px');
		}
		
		if(parseInt($ua.version) == 8) //IE8
		{
			$searchSubmit.css('top','0px');
			
			$search.css({
				'margin':'0 15px',
				'top':'110px',
				'right':'0px'
			});
			
			$searchBox.css({
				'top':'10px !important'
			});
		}
		
		if(parseInt($ua.version) < 8) //IE6-7
		{
			$searchSubmit.css('top','0px');
		}
	}
}

function ieFixes($ua)
{
	if(parseInt($ua.version) == 6)
	{
		jQuery('#featureRight ul li a').each(function(i){
			var link = jQuery(this);
			link.css({
				'border-left':'1px solid #000',
				'width':'308px',
				'height':'98px',
				'border-top':'1px solid #000',
				'border-bottom':'1px solid #000'
			}).removeClass('selectedPromoLink');
		});
	}
		
	jQuery('a.selectedMenuItem').addClass('pie');
	jQuery('#featureRight a').each(function(i){
		jQuery('#promoLink'+(i+1)+ '.selectedPromoLink, pie').each(function(){
			PIE.attach(this);
		});
	});
	
	jQuery('.pie').each(function(){
		PIE.attach(this);
	});
	
	jQuery('.btn-cart').each(function(){
		PIE.attach(this);
	});
	
	jQuery('.checkout-btn').each(function(){
		PIE.attach(this);
	});
	
	jQuery('.submenu').each(function(){
		PIE.attach(this);
	});
}

function checkOpenMenu($submenu)
{
	if($submenu.attr('style') == 'display: none; ')
	{
		return false;
	}
	else
	{
		return true;
	}
}

function navigationController($link, $submenu)
{	
	if(!isMenuOpen)
	{
		$submenu.append("<li class='parentDeptName'><a href=" + $link.attr('href') + ">" + $link.text() + "</a></li>");
		$submenu.show();
		isMenuOpen = true;
		$link.addClass('selectedMenuItem');
	}
	else
	{
		$submenu.hide();
		jQuery('li.parentDeptName').remove();
		isMenuOpen = false;
		$link.removeClass('selectedMenuItem');
	}
	
	return isMenuOpen;
}

function promoController($promoLink, openPromo, $ua)
{
	var promoToOpen = $promoLink.attr('href');
	jQuery('#'+openPromo).fadeOut(300);
	if(jQuery('.promoLink').hasClass('selectedPromoLink') || jQuery('.promoLink').hasClass('selectedPromoLinkIE'))
	{
		jQuery('.promoLink').removeClass('selectedPromoLink');
		jQuery('.promoLink').removeClass('selectedPromoLinkIE');
	}
	jQuery(promoToOpen).delay(300);
	jQuery(promoToOpen).fadeIn(300);
	if($ua.msie)
	{
			$promoLink.addClass('selectedPromoLinkIE');
	}
	else
	{
		$promoLink.addClass('selectedPromoLink');
	}
	return promoToOpen.substr(1);
}

function minicartController($minicartLink, isMiniCartOpen)
{
	$minicart = jQuery('#minicart');
	
	//is minicart is open, close it
	if(isMiniCartOpen)
	{
		$minicart.hide('blind', 400);
		isMiniCartOpen = false;
	}
	else //close minicart
	{
		$minicart.show('blind', 400);
		isMiniCartOpen = true;
	}
	
	return isMiniCartOpen;
}

function onePageCheckoutFix(){
	if(jQuery('body').attr('class') == " checkout-onepage-index" || jQuery('body').attr('class') == " checkout-onepage-success"){
		var pageTitle = jQuery('div.page-title');
		var destination = jQuery('div.main');
		pageTitle.remove();
		destination.prepend(pageTitle);
	}
}

function validate(data)
{
	var textRegex = /^[A-Za-z]+$/
	var emailRegex = /^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/
	
	var errors = new Array();
	
	for(var i=0; i<data.length; i++)
	{
		if(data[i][1] == 'name')
		{
			if(data[i][0] == '' || !data[i][0].match(textRegex) || data[i][0] == 'Please enter a valid name!')
			{
				errors[i] = "Please enter a valid name!";
			}
			else
			{
				errors[i] = '';
			}
		}
		
		if(data[i][1] == "email")
		{
			if(data[i][0] == '' || !data[i][0] .match(emailRegex) || data[i][0] == 'Please enter a valid email address!')
			{
				errors[i] = "Please enter a valid email address!";
			}
			else
			{
				errors[i] = '';
			}
		}
		
		if(data[i][1] == 'text')
		{
			if(data[i][0] == '' || data[i][0] == 'Please enter valid text!')
			{
				errors[i] = "Please enter valid text!";
			}
			else
			{
				errors[i] = '';
			}
		}
	}
	
	return errors;
}

function initTradeShow()
{
	var url = window.location.href;
	url = url.substring(7);
	
	var sections = url.split('/');
	
	var start = sections[1].indexOf('#')
	
	var show = sections[1].substring(start);
	
	var link;
	
	if(show != 'trade-shows')
	{
		link = jQuery(show + '_link');
		tradeShowController(link);
	}
	else
	{
		return;
	}
}

function tradeShowController(link)
{
	jQuery('.col-left ul li a').each(function(i){
		if(jQuery(this).hasClass('active'))
		{
			jQuery(this).removeClass('active');
		}
	});

	jQuery('.show').each(function(){
		if(jQuery(this).hasClass('current'))
		{
			jQuery(this).removeClass('current');
		}
	});

	link.addClass('active');
			
	var loadShow = link.attr('href');

	jQuery('.show').hide();
	jQuery(loadShow).show().addClass('current');

	var show = getShowTitle();
	setShowTitle(show);
}

function getShowTitle()
{
	if(jQuery('.col-left ul li a').hasClass('active'))
	{
		var currentShow = jQuery('a.active').text();
	}

	return currentShow;
}

function setShowTitle(show)
{

	if(jQuery('#breadcrumbs .show-title').length == 0)
	{
		var showTitle = '<span class="show-title">' + show + '</span>';
		jQuery('#breadcrumbs ul').after(showTitle);	
	}
	else
	{
		jQuery('#breadcrumbs .show-title').text(show);	
	}

}
