// JavaScript Document

$(document).ready(function() {   		   
						   
	// Add striping to datatables
	$('.datatable tr:odd').addClass("odd");
			
	// Allow users to tab through different news categories on home page
	homeNewsTabs();
	
	// Removes default text from fields when they are focused on
	clearonfocus();
	
	// Prepares masthead images for cycling through
	slideShow();
	
	// Adds quote button when user hovers over product tool table
	addQuoteButton();
	//addHeaderQuoteButton();
	
	// Hides or shows an element based on which radio button is currently selected
	setFormEvent("input[name='selReason']", "Damaged in Transit", "#damagedintransit");
	setFormEvent("input[name='selReason']", "Defective", "#defective");
	
	// Launch links in new window where rel="popup"
	$('a[rel=popup]').click(function(){
		window.open(this.href);
		return false;
	});
	
	autoComplete();
	
	if($("body").hasClass("product")) {
		productTabs($("#producttabs li a"), $("#producttabtargets div"));
		productTabs($("#optionstabs li a"), $("#optionstabtargets div"));
		$('#productgallery a').lightBox(); 		
		$('.enlarge').lightBox(); 	
	}
	
	
	
	if($('#map').length > 0) {
	//	loadMapScript();
	}
	
	$('.mainmenu').superfish();
	
	loadMap();
	enlargeImageHeader();

});

function loadMapScript() {
	  var url = "http://maps.google.com/maps?file=api&v=2&key=ABQIAAAAKi0tWWfW5dUH8pbvw2cGnRSFoJaPm_50jpOMBTB9qoL2qBqdMRSGx2-hIfuw6kZbozHx4ECdHsO3fQ&async=2&callback=loadMap";
      var script = document.createElement("script");
      script.type = "text/javascript";
      script.src = url;
      document.body.appendChild(script);
}

function loadMap() {

}

function enlargeImageHeader() {
	
	$('.h_thumb').bind("mouseover", function() {
		$(this).next('.h_large').show();
	});
	
	$('.h_thumb').bind("mouseout", function() {
		$(this).next('.h_large').hide();
	});
	
	$('.h_large').bind("mouseover", function() {
		$(this).show();
	});
	
	$('.h_large').bind("mouseout", function() {
		$(this).hide();
	});
	
		
}


/*---- homeNewsTabs() ---------------------*/
/*     Allow users to tab through different news categories on home page
/*-------------------------------------------------------------------------------------------*/

function homeNewsTabs() {

	var curr = 0,
		$newstype = $("#newstype"),
		$homenews = $("#home-news"),
		types = new Array("blog", "newproducts", "security");
		
	$('li', $homenews).hide();
	showCurrNews(curr);
	
	function showCurrNews(a) {
		var start = a*3, end = start+3;
		$('li', $homenews).hide();
		$('li', $homenews).slice(start,end).show();
	}
	
	$('li a', $newstype).bind("click", function() {
		curr = $(this).parent().index();
		
		showCurrNews(curr);
		
		$('li', $newstype).removeClass("current");
		$('li:eq(' + curr + ')', $newstype).addClass("current");
		
		return false;
	});

}

/*---- clearonfocus() ---------------------*/
/*     Removes default text from fields when they are focused on
/*-------------------------------------------------------------------------------------------*/

function clearonfocus() {

	var $cof = $('.clearonfocus');

	$cof.each(function() {
		$(this).data("defaulttxt", $(this).val());
	});
	
	$cof.bind("focus", function() {					
		var val = $(this).val();
		var dflt = $(this).data("defaulttxt");
		if(val==dflt) $(this).val("");						
	});
	
	$cof.bind("blur", function() {					
		var val = $(this).val();
		var dflt = $(this).data("defaulttxt");
		if(val == "") $(this).val(dflt);		
	});
}
  
/*---- slideshow() ---------------------*/
/*     Prepares masthead images for cycling through
/*-------------------------------------------------------------------------------------------*/  
  
function slideShow() {   

	var nav="";
	$('.mastheadimage').each(function(i) {
		nav += "<span>" + (i+1) + "</span>";
	});
	
  	$('#masthead-main').prepend('<div id="mastheadnav">' + nav + '</div>');
	$('#masthead-main').append('<div id="progress">&nbsp;</div>');
	
	navwidth = $('#mastheadnav').width();
	
	$("#progress").css({'opacity' : 0.5, 'width' : 0}).animate({'width' : 618-navwidth}, 7500).animate({'opacity' : 0}, 400);
	
    $('.mastheadimage').css({opacity: 0.0});   
    $('.mastheadimage:eq(0)').css({opacity: 1.0}).addClass("show");
	$('#mastheadnav span:eq(0)').addClass("current");
	  
    setInterval('gallery()',8000);   
       
	$('#mastheadnav span').css({'opacity' : 0.5 });
	$('#mastheadnav span.current').css({'opacity' : 0.8 });
} 

/*---- gallery() ---------------------*/
/*     Cycles through masthead images and text
/*-------------------------------------------------------------------------------------------*/ 
  
function gallery() {   

	navwidth = $('#mastheadnav').width();
	$("#progress").css({'opacity' : 0.5, 'width' : 0}).animate({'width' : 618-navwidth}, 7500).animate({'opacity' : 0}, 400);
       
    var current = $('.mastheadimage.show'); 
    var next = (current.next('.mastheadimage').length) ? current.next('.mastheadimage') : $('.mastheadimage:eq(0)');
	var cur = (current.index() < $('.mastheadimage').length) ? current.index() : 0;
	
	$('#mastheadnav span').removeClass("current");
	$('#mastheadnav span').eq(cur).addClass("current"); 
	
	next.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 500);
    current.animate({opacity: 0.0}, 500).removeClass('show'); 
	
	$('#mastheadnav span').css({'opacity' : 0.5 });
	$('#mastheadnav span.current').css({'opacity' : 0.8 });
}  

/*---- addQuoteButton() ---------------------*/
/*     Adds quote button when user hovers over product tool table
/*-------------------------------------------------------------------------------------------*/ 

function addQuoteButton() {
	
	$("body").append('<form style="display:none" id="codeform" action="quote.html" method="post"><input type="hidden" name="enqcode" /></form>');
	
	$('table.enq tr').each(function() {
		$('td:eq(0)', this).append('<a style="display:none" href="#" class="enquire button orange">Get Quote</a>').wrapInner("<span />");
	});
	
	$('table.enq').delegate("tbody tr", "mouseenter", function() {
		$(".enquire", this).show();
	});
	
	$('table.enq').delegate("tbody tr", "mouseleave", function() {
		$(".enquire", this).hide();
	});
	
	$('.enquire').bind("click", function() {
		value = $(this).parent().text();
		removevalue = $(this).parent().children("a").text();
		value = value.replace(removevalue, "");
		$("#codeform input[name='enqcode']").val(value);
		$("#codeform").trigger("submit");
		return false;
	});
	
}


/*
function addQuoteButton() {

		var offset=0, $value;
			
		$('table.enq:eq(0)').after('<a href="#" class="enquire button orange">Get Quote</a><form style="display:none" id="codeform" action="quote.html" method="post"><input type="hidden" name="enqcode" /></form>');
		
		
	
		$('table.enq').delegate("tbody tr", "mouseenter", function() {

			$value = $("td:first-child", this).text();
			
			offset = $(this).offset();
			var width = $("td:first-child", this).width();
			$(".enquire").css({'display' : 'block', 'position' : 'absolute', 'top' : offset.top, 'left' : offset.left + (width-47)});
			
			$('.enq tbody tr').each(function() {						
				if($(this).offset().top == offset.top) $(this).addClass("current");
				else $(this).removeClass("current");
			});
		});
		
		$('table.enq').delegate("th.enq", "mouseenter", function() {
			$value = $("span", this).text();
		});
				
		$('table.enq').bind("mouseleave", function() {
			$(".enquire").css({'display' : 'none'});
			$('.enq tbody tr').removeClass("current");
		});	
				
		$('.enquire').bind("mouseenter", function() {
			$('.enq tbody tr').each(function() {						
				if($(this).offset().top == offset.top) $(this).trigger("mouseover");
			});
		});
		
		$('.enquire').bind("click", function() {
			$("#codeform input[name='enqcode']").val($value);
			$("#codeform").trigger("submit");
			return false;
		});
		
}*/

/*---- addQuoteButton() ---------------------*/
/*     Adds quote button when user hovers over product tool table
/*-------------------------------------------------------------------------------------------*/ 

function addHeaderQuoteButton() {

		var h_offset=0;
				
		$('table.enq').delegate("th.enq", "mouseenter", function() {
			w1 = $('.enquire').width(); w2 = $(this).width(); width = (w1-w2)/2;
			h_offset = $(this).offset();
			$(".enquire").css({'display' : 'block', 'position' : 'absolute', 'top' : h_offset.top, 'left' : h_offset.left-width });
			$('.enq tbody tr').removeClass("current");
		});
		
		$('table.enq').delegate("th.enq", "mouseleave", function() {
			h_offset=0;
		});		
				
		$('table.enq').bind("mouseleave", function() {
			$(".enquire").css({'display' : 'none'});
		});	
				
		$('.enquire').bind("mouseenter", function() {
			$('th.enq').each(function() {						
				if($(this).offset().left == h_offset.left) $(this).trigger("mouseover");
			});
		});		
}

/*---- setFormEvent() ---------------------*/
/*     Hides or shows an element based on which radio button is currently selected
/*-------------------------------------------------------------------------------------------*/ 

function setFormEvent(el, value, target) {

	function execFormEvent() {
		selel = $(el + ":checked");
		if(selel.val() === value) {
			$(target).removeClass("hide");
		}
		else {
			$(target).addClass("hide");		
		}
	}

	$(el).bind("change", function() {
		execFormEvent();
	});

	execFormEvent();

}

function autoComplete() {

	if($("#cable").length > 0) {
		$("#cable").autocomplete("/assets/snippets/leadselector/leadselector.autocomplete.php", {
			width: 400,
			selectFirst: false,
			max: 5
		});
	}
		
}

/*----------------------------------------------------------------------------------------------------------------------*/

/*---- productTabs() ---------------------*/
/*     Allow users to tab through different product information
/*-------------------------------------------------------------------------------------------*/

function productTabs($nav, $target) {

	var curr = 0;
		
	showCurrNews(curr);
	
	function showCurrNews(a) {
		$target.hide();
		$target.eq(a).show();
		$($nav).parent().removeClass("current");
		$($nav).parent().eq(a).addClass("current");
	}
	
	$($nav).bind("click", function() {
		curr = $(this).parent().index();	
		showCurrNews(curr);
		return false;
	});

}

