// JavaScript Document

$(document).ready(function() {  
						   
	$('#youtubelink').click(function() {
		$.fancybox({
				'padding'		: 0,
				'autoScale'		: false,
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				'title'			: this.title,
				'width'		: 600,
				'height'		: 370,
				'href'			: 'http://www.youtube.com/v/kXO3wVda91M?autoplay=1',
				'type'			: 'swf',
				'swf'			: {
					 'wmode'		: 'transparent',
					'allowfullscreen'	: 'true'
				}
			});
	
		return false;
	});
	
	// 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
	if($("table.enq").length > 0) {
		addQuoteButton();
	}
	
	// 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");
	
	setFormEvent("input[name='collectionadd']", "yes", "");
	setFormEvent("input[name='collectionadd']", "no", "#newcollectionaddress");
	
	// Launch links in new window where rel="popup"
	$('a[rel=popup], #datasheets a, a.pdflink').click(function(){
		window.open(this.href);
		return false;
	});

	if($("body").hasClass("product")) {
		productTabs($("#producttabs li a"), $("#producttabtargets div"));
		productTabs($("#optionstabs li a"), $("#optionstabtargets div"));
		$('#gallery a, #image a').lightBox(); 		
		
		$('#modelrange dl').accordion({'autoHeight' : false}); 
		addModelQuoteButton();	
	}

	$('.mainmenu').superfish();

	enlargeImageHeader();

});



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() {

	$(".clearonfocus").focus(function() {
		if(this.value == this.defaultValue) {
			this.value = "";
		}
	}).blur(function() {
		if(!this.value.length) {
			this.value = this.defaultValue;
		}
	});
}
  
/*---- slideshow() ---------------------*/
/*     Prepares masthead images for cycling through
/*-------------------------------------------------------------------------------------------*/  
  
function slideShow() {   

	var nav="";
	$('.mastheadimage').each(function(i) {
		nav += "<span>" + (i+1) + "</span>";
	});
	
  	$('body:not(.home) #masthead-main').prepend('<div id="mastheadnav">' + nav + '</div>');
	$('body:not(.home) #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;
	});
	
}

/*---- addModelQuoteButton() ---------------------*/
/*     Displays quote button when user hovers over model accordion on product page
/*-------------------------------------------------------------------------------------------*/ 

function addModelQuoteButton() {
	
	$("body").append('<form style="display:none" id="codeform" action="quote.html" method="post"><input type="hidden" name="enqcode" /></form>');
	
	$('#modelrange dt').append('<a style="display:none" href="#" class="modelenquire button orange">Get Quote</a>');
	
	$('#modelrange dt.ui-state-active .modelenquire').show();
	
	$('#modelrange').delegate("dt", "click", function() {
		$('#modelrange .modelenquire').hide();
		$('#modelrange dt.ui-state-active .modelenquire').show();
	});
	
	$('#modelrange').delegate("dt:not('.ui-state-active')", "mouseenter", function() {
		$(".modelenquire", this).show();
	});
	
	$('#modelrange').delegate("dt:not('.ui-state-active')", "mouseleave", function() {
		$(".modelenquire", this).hide();
	});
	
	$('.modelenquire').bind("click", function() {
		$(this).hide();
		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;
	});

}

/*---- 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();

}

/*----------------------------------------------------------------------------------------------------------------------*/

/*---- 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;
	});

}


