// JavaScript Document

// initialise plugins
var theInt = null;
var $crosslink, $navthumb;
var curclicked = 0;
var nNewsItemsToShow = 3;

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
  return false;
}

function RefreshImage(valImageId) {
	var objImage = document.images[valImageId];
	if (objImage == undefined) {
		return;
	}
	var now = new Date();
	objImage.src = objImage.src.split('?')[0] + '?x=' + now.toUTCString();
}

theInterval = function(cur){
	clearInterval(theInt);
	
	if( typeof cur != 'undefined' )
		curclicked = cur;
	
	$crosslink.removeClass("active-thumb");
	$navthumb.eq(curclicked).parent().addClass("active-thumb");
	$(".stripNav ul li a").eq(curclicked).trigger('click');
	$("div.panelContainer div.panel.hideelement").removeClass('hideelement');
	theInt = setInterval(function(){
		$crosslink.removeClass("active-thumb");
		$navthumb.eq(curclicked).parent().addClass("active-thumb");
		$(".stripNav ul li a").eq(curclicked).trigger('click');
		curclicked++;
		if( curclicked == $('#movers-row div a.cross-link').length )
			curclicked = 0;
		
	}, 3000);
};

$(document).ready(function(){
						   	

	$("#toptabs > ul").tabs({ fx: { opacity: 'toggle' } });
	$("#bottomtabs > ul").tabs({ fx: { opacity: 'toggle' } });
	$("#fourwaystabs > ul").tabs({ fx: { opacity: 'toggle' } });
	$("#fourways > ul").tabs({ 
		fx: { opacity: 'toggle' },
		select: function(event, ui){ 
			var sBenefitsID = ui.panel.id + "2";
			if ($("#"+sBenefitsID).length > 0){
				$(".jqBenefits").each(function(){
					if (this.id == sBenefitsID){						   
						$(this).fadeIn();
					} else { 
						$(this).hide();
					}
				});
			}
		}					 
	});
	$(".jqBenefits").each(function(nIndex){
		if (nIndex != 0){
			$(this).hide();
		}
	});
	$("#featuredproductstabs > ul").tabs({ fx: { opacity: 'toggle' } });
	jQuery('ul.sf-menu').superfish();
	$("#main-photo-slider").codaSlider();
	$("table.tabledata").each(function(nIndex){
		$(this).find("tr:even").css("background-color","#C7DCF2");
	});
	
	$navthumb = $(".nav-thumb");
	$crosslink = $(".cross-link");
	
	$navthumb.click(function() {
		var $this = $(this);
		theInterval($this.parent().attr('href').slice(1) - 1);
		return false;
	});
	
	$('#newsitems ul li').each(function(nIndex){
		if (nIndex >= nNewsItemsToShow){
			$(this).hide();
		}
	});
	$('#toggleViewAllNews').click(function(){
		$('#newsitems ul li').each(function(nIndex){
			if (nIndex >= nNewsItemsToShow){
				$(this).slideToggle("fast");
			}		
		});
		if(this.innerHTML == '[+] View All News'){
			this.innerHTML = '[-] Hide News';
		} else {
			this.innerHTML = '[+] View All News';
		}
	});
	if ($('#frmContact').length > 0){
		var container = $('div.errorcontainer');
		$("#frmContact").validate({
			errorClass: "invalid",
			errorContainer: container,
			errorLabelContainer: $("ol", container),
			wrapper: 'li',
			rules: {
				txtFirstName: {
					required: true
				},
				txtLastName: {
					required: true
				},
				txtCompany: {
					required: true
				},
				txtPhone: {
					required: true
				},
				txtImageText: {
					required: true,
					remote: "testcaptcha.asp"
				}
			},
			messages: {
				txtFirstName: "Your First Name is required.",
				txtLastName: "Your Last Name is required.",
				txtCompany: "Your Company is required.",	
				txtPhone: "Your Phone Number is required.",	
				txtImageText: "The text you typed does not match the image."	
			},
			onkeyup: false
		});
	}
	if ($('#frmBrochures').length > 0){
		var container = $('div.errorcontainer');
		$("#frmBrochures").validate({
			errorClass: "invalid",
			errorContainer: container,
			errorLabelContainer: $("ol", container),
			wrapper: 'li',
			rules: {
				txtFirstName: {
					required: true
				},
				txtLastName: {
					required: true
				},
				txtCompany: {
					required: true
				},
				txtPhone: {
					required: true
				},
				txtCountry: {
					required: true
				},
				txtEmail: {
					required: true,
					email: true
				},
				txtImageText: {
					required: true,
					remote: "testcaptcha.asp"
				}
			},
			messages: {
				txtFirstName: "Your First Name is required.",
				txtLastName: "Your Last Name is required.",
				txtCompany: "Your Company is required.",	
				txtPhone: "Your Phone Number is required.",	
				txtEmail: "A valid Email Address is required.",
				txtCountry: "Your country is required.",
				txtImageText: "The text you typed does not match the image."	
			},
			onkeyup: false
		});
	}
	if (document.getElementsByName('fourwaystabs').length){
		$('ul.columnize').makeacolumnlists({cols:3,colWidth:190,equalHeight:false,startN:1});
		$('ul.columnize li a.title').click(function(){
			var sHTML = $(this).next().html();
			var nHeight = $(this).next().height() + 60;
			var sTitle = $(this).html();
			$("#uidialog").html(sHTML).dialog({
				dialogClass:'flora',
				modal:true,
				draggable:false,
				resizable:true,
				hide:"slide",
				show:"slide",
				height:nHeight,
				width: 500,
				zIndex: 21000,
				title:sTitle
			});									
			/*
			var oTemp = this;													
			$('ul.columnize li div.content').each(function(nIndex){
				if($(this).css('display') != 'none' && oTemp != this){
					$(this).slideUp();
				}
			});
			$(this).next().slideDown();
			*/
		});
	}
	$('.jqCAInfo').click(function(){
		var sHTML = $("#CA"+this.alt).html();
		var sTitle = this.alt;
		var nHeight = $("#CA"+this.alt).height() + 60;
		$("#uidialog").html(sHTML).dialog({
			dialogClass:'flora',
			modal:true,
			draggable:false,
			resizable:true,
			hide:"slide",
			show:"slide",
			height:nHeight,
			width: 500,
			zIndex: 21000,
			title:sTitle
		});		
		pageTrackerEmail();
		return false;
	});
	$('.jqUSInfo').click(function(){
		var sHTML = $("#US"+this.alt).html();
		var sTitle = this.alt;
		var nHeight = $("#US"+this.alt).height() + 60;
		$("#uidialog").html(sHTML).dialog({
			dialogClass:'flora',
			modal:true,
			draggable:false,
			resizable:true,
			hide:"slide",
			show:"slide",
			height:nHeight,
			width: 500,
			zIndex: 21000,
			title:sTitle
		});			
		pageTrackerEmail();
		return false;
	});
	$("img[alt^='Site Search by Atomz']").css('display','none');
	$("h3.ad").css('display','none');
	$("div.ad_area").css('display','none');
	$('#frmSearch #sp_q').click(function(){
		this.select();
	});
	//Facebox - lightbox
	//$('a[rel*=facebox]').facebox();
	
	//if ($('#flashcontainer').attr('title') == 'palletrunner'){
	//	$('#flashcontainer').cycle('fade');
	//}
	theInterval();
	
	if ($('.tall').height) {
		$('.short').css('height',$('.tall').height()+'px');
	}
	
	function pageTrackerEmail() {
		$('a[href^="mailto"]').click(function (){
			pageTracker._trackEvent('Email', location.href.substring(location.href.lastIndexOf("/")+1),$(this).attr("href").replace("mailto:",""));
			pageTracker._setVar('Sent Email');
		});
	};
	pageTrackerEmail();
	
	
	// Copy Keyboard Shortcut
	function trackCopyEvent(type, selection) {
		pageTracker._trackEvent('Copy', type, selection);
	}
	
	$(document).bind('keypress', function(event) { // Safari and Firefox
		if( (event.charCode == 99 && (event.metaKey || event.ctrlKey) ) && window.getSelection ) {
			var selection = String(window.getSelection() + '').truncate(60);
			trackCopyEvent('text', selection);
		}
	});
	$(document).bind('keyup', function(event) { // Opera Windows and Internet Explorer
		if( event.keyCode == 67 && event.ctrlKey ) {
			if( document.getSelection ) { // Opera Windows
				var selection = document.getSelection().truncate(60);
				trackCopyEvent('text', selection);
			} else if( document.selection ) { // Internet Explorer
				var selection = document.selection.createRange().text.truncate(60);
				trackCopyEvent('text', selection);
			} 
		}
	});
	
	/* Context Menu
	$(document).bind('contextmenu', function(event) {
		if(event.target.nodeName == 'img') {
			var image = $(event.target).attr('src'));
			trackCopyEvent('image', image);
		}
	});*/
	
});