jQuery(document).ready(function(){
	
	contentRepos.init();// call this first to ensure content is moved to correct place in DOM
	
	productCycler.init();
	$('.shProductPager a').click(function(){ $(this).blur(); });
	
	signInFrom();
	registerFormBtn();
	headerSlider();
	mediaPanel();
	mediaPanelLightBox.init();
	homeCycle.init();
	stockistSearch.init();
	
	$('#header ul li:last-child a').addClass('on');	
	$('#searchInput').focus().css('marginTop','3px');//stops scroll to top
	$('#search div a').removeAttr('title');
	
	featureTagImageHover();
	
	showLoginForm.init();
	logoutAndChangePasswordBtnEvents.init();
	
	// analytics tracking scripts
		analyticMenuClick.init();
		analyticRelatedProductsExist.init();
		analyticsTrackSubCarousel.init();
		analyticsDownloadLinks.init();
		analyticsUserLogin.init();
		analyticsDownloadProAssets.init();
		
});

// start of analytics tracking scripts

var analyticMenuClick = {
	init: function() {
		
		var obj = this;
		$('#nav .pop ul li h3 a').click(function(e) {
			e.preventDefault();
			var pos1 = (parseInt($(this).parents('.pop').find('ul:eq(0) > li:not(.split)').index($(this).parents('li:first')), 10)+1);
			var item = $(this).text();
			obj.trackClick(this, 'Products', item, pos1);
		});
		
		$('#nav .pop ul ul li a').click(function(e) {
			e.preventDefault();
			var pos1 = (parseInt($(this).parents('.pop').find('ul:eq(0) > li:not(.split)').index($(this).parents('li:first').parents('li:first')), 10)+1);
			var pos2 = (parseInt($(this).parents('ul').find('li:not(.split)').index($(this).parents('li')), 10)+1);
			var item = $(this).parents('li:first').parents('li:first').find('a:eq(0)').text() + ' > ' + $(this).text();
			obj.trackClick(this, 'Products', item, pos1 + '.' + pos2);
		});	
	},
	
	trackClick: function(elem, category, item, pos) {
		//console.log(category + ' - ' + item + ' - ' + parseFloat(pos));
		_gaq.push(['_trackEvent', 'Menu Click', category, parseFloat(pos) + ' ' + item, parseInt(pos, 10)]);
		setTimeout('document.location = "' + $(elem).attr('href') + '"', 100);
	}
}

var analyticRelatedProductsExist = {
	init:function() {
		if($('#productVVPageWrapper').length < 1) { return; }

		var obj = this;
		_gaq.push(['_trackEvent', 'Related products', 'impression']);

		$('#productVVPageWrapper').delegate(".panel a", "click", function(e) {
			e.preventDefault();
			_gaq.push(['_trackEvent', 'Related products', 'click']);
			setTimeout('document.location = "' + $(this).attr('href') + '"', 100);
		});
	}
}

var analyticsTrackSubCarousel = {

	init:function() {
		if($('.shProductCycle').length < 1) { return; }
		
		var obj = this;
		$('.shProductCycle .product').delegate('a', 'click', function(e) {
			e.preventDefault();
			obj.trackClick($(this), 'Sub Carousel Click', $(this).parents('.product:first').find('h3 a').text(), (parseInt($(this).parents('.shProductCycle:first').find('.product').index($(this).parents('.product:first')), 10)+1));
		});
	},
	
	trackClick: function(elem, category, item, pos) {
		_gaq.push(['_trackEvent', category, item, '', pos]);
		setTimeout('document.location = "' + $(elem).attr('href') + '"', 100);
	}

}

var analyticsDownloadLinks = {
	init: function() {
		if($('#downloadAssetLinks').length < 1) { return; }
		
		$('#downloadAssetLinks .bottom').not(':last').find('p a').click(function(e) {
			e.preventDefault();
			_gaq.push(['_trackEvent', 'Download', $(this).text()]);
			setTimeout('document.location = "' + $(this).attr('href') + '"', 100);		
		});
	}
}

var analyticsUserLogin = {
	
	init: function() {
		if($.cookie('vanityLogin') && $.cookie('vanityLogin') == 'true') {
			if(!$.cookie('vanityLoginTracked ') || $.cookie('vanityLoginTracked ') != 'true') {
				// track user logged in - set a session cookie so this doesn't get tracked twice
				_gaq.push(['_setCustomVar', 1, 'User Type', 'Member', 2]);
				$.cookie('vanityLoginTracked ', 'true');
			}
		}
	}
}

var analyticsDownloadProAssets = {
	init: function() {
		
		if($('#docsList').length < 1) { return; }
		$('#docsList dd a').click(function(e) {
			e.preventDefault();
			_gaq.push(['_trackEvent', 'Download', $(this).text(), 'Professional Asset']);
			setTimeout('document.location = "' + $(this).attr('href') + '"', 100);		
		});
	}
}

// end of analytics tracking scripts





var logoutAndChangePasswordBtnEvents = {

	init:function(){
		$('#logoutButton').click(function (){
			window.location = '/your-account/login/logout/';
		});
		$('#changePasswordButton').click(function (){
			window.location = '/your-account/change-password/';
		});
	}

}

var showLoginForm = {
	init:function() {
		
		if($.cookie('vanityLogin') && $.cookie('vanityLogin') == 'true') {	// Logged in
			$('.notLoggedIn').hide();
			$('.loggedIn').show();
			$('#search').css({ width: '570px' });
			
			// Hide login link under downloads section on right hand column
			$('.downloadsLoginLink').hide();
			
		} else {	// Not logged in
			$('.notLoggedIn').show();
			$('.loggedIn').hide();
		}
	}
}

var stockistSearch = {
	init: function() {
		$.each(['#locationPostcode', '#rightColSearch'], function(ind, val) { 
			var obj = $(val);
			obj.data('state', {  txt : jQuery.trim(obj.attr('value')), clr : obj.css('color') });
			obj.focus(function() {
				var txt = jQuery.trim($(this).attr('value'));
				if(txt == obj.data('state').txt) { 
					$(this).attr('value','');
					$(this).css('color','#000');
				}
			});
			obj.blur(function() {
				var txt = jQuery.trim($(this).attr('value'));
				if(txt == '') { 
					$(this).attr('value',obj.data('state').txt); 
					$(this).css('color',obj.data('state').clr);
				}
			});
		});
	}
}


var productCycler = {
	init:function() {
		$('.shProductCycle .slideshow').each(function() {
			var that = $(this);
			that.cycle({
				fx: 		'scrollHorz',			// Transition effect: fade, scrollHorz, scrollRight, etc
				speed:		'slow',
				pause:		1,
				timeout:	5000,
				next:		that.parents('.shProductCycleContainer').find('.next').eq(0),
				prev:		that.parents('.shProductCycleContainer').find('.prev').eq(0),
				sync:		true,
				pager:		that.parents('.shProductCycleContainer').find('.shProductPager ul').eq(0),
				pagerAnchorBuilder: function(index, DOMelement) {
					return '<li><a href="#">&nbsp;</a></li>';
				}
			});	
		});	
	}
}



var contentRepos = {

	init: function() {

		// move sliders into correct position and set container to display block
		$('div.shProductCycleContainer').each(function() {
			if($(this).parents('#featuredRow').find('.container').length < 1) {
				$('#featuredRow .container').append($(this));
			}
		});	
		if($('#featuredRow .shProductCycleContainer').length > 0) { $('#featuredRow').css('display','block'); }
		
		// move sidebar content into correct position
		if($('.sidebarContent').length > 0 && $('#rightCol').length > 0) {
			$('#rightCol').prepend($('.sidebarContent').html());
		}

	}
}


var mediaPanelLightBox = {
	
	init: function() {
		if($('#mediaPanelWrapper').length < 1) { return; }
		$('#mediaPanelWrapper a[rel="imageshow"]').colorbox();

		if($('#colorbox_target').length < 1) {
			$('body').append('<div style="display: none"><div id="colorbox_target" style="width: 640px; padding-top: 5px; padding-left: 6px; padding-right: 6px;height: 386px;"><div id="colorbox_target_video"></div></div></div>'); // originally "580", "400"
		}

		$('#mediaPanelWrapper a[rel="videoshow"]').colorbox({
			slideshow : false,
			current:'',
			inline:true, 
			href: "#colorbox_target",
			onComplete:function(){ 
				var flashvars = { 
					file:$(this).attr('href'), 
					autostart:true
				}
				var params = { wmode:'transparent', allowfullscreen:'true', allowscriptaccess:'always' }, attributes = {};
				swfobject.embedSWF("/javascript/player.swf", 'colorbox_target_video', "640", "386", "9.0.0", false, flashvars, params, attributes);	
			}
		});


	}
}

var featureTagImageHover = function (){
	if($('.featureTagImage').length > 0){
		$('.featureTagImage').tooltip();
	};
}/* featureTagImageHover */

var signInFrom = function (){
	var aniSpeed = 500;
	//$('#formRow').css('display','none');
	
	$('#signInButton').click(function (){
		$('#formRow').slideDown(aniSpeed);
	});
	$('#signClose').click(function (){
		$('#formRow').slideUp(aniSpeed);
	});
}/* signInFrom */

var registerFormBtn = function(){
	
	$('#registerButton').click(function(){
	
		window.location = '/your-account/sign-up/';
	
	});
	
}

//	sets up the header slider

var headerSlider = function (){
	if($('#headerWidget img').length > 1 && $('.homeCycleInner').length < 1){
		$(document).ready(function (){
			$('#headerWidget img.hidden').removeClass('hidden');
			$('#headerWidget').ceSlider({
				show		:	1,
				speed		:	1000
			});
		});
	};
};/* headerSlider */


var homeCycle = {

	curr: 0,
	trans: false,
	speed: 'normal',

	init:function() {
		if($('#homeCycleOuter .homeCycleInner').length < 1) { return; }
		var obj = this;
		
		$('#homeCycleOuter .homeCycleInner').eq(0).css({'display':'block', 'z-index':2});
		$('#homeCycleOuter .homeCycleInner').css('z-index',1);
		
		if($('#homeCycleOuter .homeCycleInner').length == 1) { 	
			$('#homeCycleOuter .prev').hide();
			$('#homeCycleOuter .next').hide();
		}
		
		$('#homeCycleOuter .homeCycleInner .link a').click(function(e) { e.preventDefault(); });
		
		$('#homeCycleOuter .homeCycleInner').click(function(e) {
			e.preventDefault();
			// fire analytics event
			_gaq.push(['_trackEvent', 'Homepage Carousel Click', $(this).find('h2').text(), '', (parseInt(obj.curr, 10)+1)]);
			setTimeout('document.location = "' + $(this).find('.content a').attr('href') + '"', 100);
		});
		
		$('#homeCycleOuter .prev a').click(function(e) {
			e.preventDefault();
			obj.fade(obj.curr-1);
		});

		$('#homeCycleOuter .next a').click(function(e) {
			e.preventDefault();
			obj.fade(obj.curr+1);
		});	
		
	},
	
	fade : function(curr) {
		var obj = this;
		
		if(obj.trans) { return; }
		obj.trans = true;

		if(curr < 0) { curr = $('#homeCycleOuter .homeCycleInner').length-1;} 
		else if(curr >= $('#homeCycleOuter .homeCycleInner').length) { curr = 0; }
		
		$('#homeCycleOuter .homeCycleInner').eq(curr).css('z-index',2);
		
		$('#homeCycleOuter .homeCycleInner').eq(curr).fadeIn(600, function() {
			$('#homeCycleOuter .homeCycleInner').css('z-index',1);
			$('#homeCycleOuter .homeCycleInner').eq(obj.curr).css('display','none');
			obj.trans = false;
			obj.curr = curr;
		});

	}
}




var mediaPanel = function(){
	if($('.shMediaPanel').length > 0){
		
		$('.shMediaPanel .control').click(function(){
			$('.shMediaPanel .media').animate({ left : '-23px' }, 1000);
		});
	
		$('.shMediaPanel a.closeBtn').click(function(){
			$('.shMediaPanel .media').animate({ left : '607px' }, 1000);
			return false;
		});
	
		$('.shMediaPanel .tabs ul').tabs();
		
		$('.shMediaPanel #images .thumbs').page({ items : 4, prevText : '' , nextText : '' });
		$('.shMediaPanel #video .thumbs').page({ items : 4, prevText : '' , nextText : '' });
		
		$('.shMediaPanel .tabs ul li a').click(function(){
			$(this).blur();
		});
	};
};/* mediaPanel */

	//isSafari3 = false; //if(window.devicePixelRatio) isSafari3 = true;
