var numSlides = 8;
numSlides = numSlides -1;
var currentSlide = 0;
var timer= Number;
var interval = 5000;
var fadeSpeed;

/*
 * cakemailTrap: Capture newsletter subscription information from submitted forms 
 * and send it over to Cakemail.
 * 
 * usage: 
 * 		$(f).cakemailTrap(options);
 * where f is the submit input to monitor for a click
 *
 * options:
 * debug: true or false
 * email: Selector for email field (default is '#email')
 * name: Selector for subscriber's name field (default is '#name') 
 * list [{
 * 	name: Optional name of list 
 * 	optin: Selector for opt-in checkbox subscribe/unsubscribe (default is '#optin')
 * 	urlSub: address to submit subscribe info
 * 	urlUnsub: address to submit unsubscribe info
 * }]
 * passthru: Whether the form should be submitted after trapped (default is false)
 *
 */

(function($){
	$.fn.cakemailTrap = function(options) {
		// setup options
		var opts = $.extend({}, $.fn.cakemailTrap.defaultOptions, options);

		// support mutltiple elements
		return this.each(function() {
			$this = $(this);
			o = $.meta ? $.extend({}, options, $this.data()) : opts;
			listcount = o.list.length;
			debug('listcount is ' + listcount);
			$this.bind( 'click', submitted );
		});
	};

	$.fn.cakemailTrap.defaultOptions = {
		email : '#email',
		name : '#name',
		passthru: false,
		debug : false
	};

	$.fn.cakemailTrap.getOptions = function() {
		return o;
	};

	// SETUP private functions;
	var o = {};
	var listcount = 0;

	function submitted() { 
		if (listcount === 0){ return o.passthru;}
	  $this = $(this);
	  form = $this.parents('form');
		debug(o);
		debug($this);
		debug(form);
		email = $(o.email) ? encodeURIComponent($(o.email).val()) : '';
		name = $(o.name) ? encodeURIComponent($(o.name).val()) : '';
		debug(o.list);
		debug(o.list.length);
		$.each(o.list, function(i, list) {
			debug(list);
			var optin = $(list.optin).prop('checked');
			if (optin) {
				url = list.urlSub + '?email=' + email + '&name=' + name;
				result = 'Subscribed';
			} else {
				url = list.urlUnsub + '?email=' + email + '&name=' + name + '&unsubscribe=unsubscribe';
				result = 'Unsubscribed';
			}
			var cake = $('<iframe />').css('display', 'none');
			cake.attr('src', url);
			cake.load(function() {
			  if (o.passthru) {
					listcount--;
					$this.click();
				}
			});
			cake.prepend('<!-- ' + list.name + '-->');
			cake.insertAfter(form);
			form.prepend('<input type="hidden" name="newsletter" value="' + result + '" />');
			debug(result + ' list ' + list.name + ' for ' + name + ' ' + '<' + email + '>');
		});
		return false;
	}

	function debug($log) {
	  if (window.console && window.console.log && o.debug){
	    window.console.log($log);
	  }
	}

})(jQuery);



function fixLastChild(){
	$('ul.nav > li:last-child').addClass('last').css({'background':'none', 'border':'none'});	
	$('.footer li:last-child').addClass('last-child');
}

function initSlideshow(){
	$('ul.banner-images li').hide().eq(currentSlide).show();
	timer = window.setInterval(goToSlide, interval);
}


function advanceSlide(currentSlide) {
	
	$('ul.banner-images li').eq(currentSlide).fadeIn('slow');
}

function goToSlide(){
	$('ul.banner-images li').hide();
	if(currentSlide === numSlides){
		currentSlide = 0;
	}else{
		currentSlide++;
	}
	advanceSlide(currentSlide);	
}

function initMinHeightFix(){
	var contentDiv = $('.content');
	var contentHeight = contentDiv.height();
	if (contentHeight < 528) {
	    contentHeight = 528;
	    contentDiv.css('height', contentHeight + 'px');
	} 
$('.content.home').css('height', 'auto');

}

function initProdBorders(){
	var spans = $('body div.wrapper div.content-wrap div.content.product-category-page.product-page ul.category-row span#plc_lt_zoneContent_pageplaceholder_pageplaceholder_lt_zoneContentPage_cmsdatalist_lstElem > span');
	$('body div.wrapper div.content-wrap div.content.product-category-page.product-page ul.category-row span#plc_lt_zoneContent_pageplaceholder_pageplaceholder_lt_zoneContentPage_cmsdatalist_lstElem > span:nth-child(5n+4)').addClass('right').css({'border-right':'none'});
	var spanNum = spans.length;
	if((spanNum) % 4 !== 0){
		spans.eq(spanNum).addClass('last').css({'border-bottom':'none'});
		spans.eq(spanNum-1).addClass('last').css({'border-bottom':'none'});
		spans.eq(spanNum-2).addClass('last').css({'border-bottom':'none'});
		spans.eq(spanNum-3).addClass('last').css({'border-bottom':'none'});
	}
	
	if((spanNum-1) % 4 !== 0){
		spans.eq(spanNum).addClass('last').css({'border-bottom':'none'});
		spans.eq(spanNum-1).addClass('last').css({'border-bottom':'none'});
		spans.eq(spanNum-2).addClass('last').css({'border-bottom':'none'});
	}
	if((spanNum-2) % 4 !== 0){
		spans.eq(spanNum).addClass('last').css({'border-bottom':'none'});
		spans.eq(spanNum-1).addClass('last').css({'border-bottom':'none'});
	}
	if((spanNum-3) % 4 !== 0){
		spans.eq(spanNum).addClass('last').css({'border-bottom':'none'});
	}
	
	
	var spans2 = $('.sub-category span#plc_lt_zoneContent_pageplaceholder_pageplaceholder_lt_zoneContentPage_cmsdatalist_lstElem > span');
	$('.sub-category span#plc_lt_zoneContent_pageplaceholder_pageplaceholder_lt_zoneContentPage_cmsdatalist_lstElem > span:nth-child(4n+3)').addClass('right').css({'border-right':'none'});	
	var spanNum2 = spans2.length;
	if((spanNum2) % 3 === 0){
		spans2.eq(spanNum2).css({'border-bottom':'none'});
		spans2.eq(spanNum2 -1).css({'border-bottom':'none'});
		spans2.eq(spanNum2 -2).css({'border-bottom':'none'});
		spans2.eq(spanNum2 -3).css({'border-bottom':'none'});
	}
	if((spanNum2 - 1) % 3 === 0){
		spans2.eq(spanNum2).css({'border-bottom':'none'});
		spans2.eq(spanNum2-1).css({'border-bottom':'none'});
	}
	if((spanNum2 - 2) % 3 === 0){
		spans2.eq(spanNum2).css({'border-bottom':'none'});
		spans2.eq(spanNum2 -1).css({'border-bottom':'none'});
		spans2.eq(spanNum2 -2).css({'border-bottom':'none'});
	}
}

function initHandleForms(){
	// form validation
	$('#dist_login').validate();
	
	// option show/hide
	$('.userTypeBox').hide();
	$('#contact-opt-in').change(function(){
		if($('#contact-opt-in').prop('checked')){
			$('.userTypeBox').slideDown();
		}else{
			$('.userTypeBox').slideUp();
		}
	});
	///// cakemail traps
	/*  EXAMPLE
	$('#submitme').cakemailTrap({
		debug: 'True',
		email: '#email',
		name: '#name',
		passthru: true,
		list: [{
				name: 'Customer list',
				optin: '#optinC',
				urlSub : 'http://link.sayit-liveit.com/oi/1/0c3cdba106dff46d61fa02199ce04946',
				urlUnsub : 'http://link.sayit-liveit.com/oo/1/0c3cdba106dff46d61fa02199ce04946'
			}, {
				name: 'Distributor list',
				optin: '#optinD',
				urlSub : 'http://link.sayit-liveit.com/oi/1/0c3cdba106dff46d32354bef36381dd2',
				urlUnsub : 'http://link.sayit-liveit.com/oo/1/0c3cdba106dff46d32354bef36381dd2'
		}]
	});
	*/
	// home newsletter signup form
	$('.home .col .submit').cakemailTrap({
		debug: 'True',
		email : '#newsltr_email',
		name : '#newsltr_name',
		passthru: false,
		list: [{
				name: 'Customer list',
				optin: '#newsltr_eu_optin',
				urlSub : 'http://link.sayit-liveit.com/oi/1/0c3cdba106dff46d61fa02199ce04946',
				urlUnsub : 'http://link.sayit-liveit.com/oo/1/0c3cdba106dff46d61fa02199ce04946'
			}, {
				name: 'Distributor list',
				optin: '#newsltr_distributor',
				urlSub : 'http://link.sayit-liveit.com/oi/1/0c3cdba106dff46d32354bef36381dd2',
				urlUnsub : 'http://link.sayit-liveit.com/oo/1/0c3cdba106dff46d32354bef36381dd2'
		}]
	});

	// contact form
//	$('.contact-us #plc_lt_zoneContent_pageplaceholder_pageplaceholder_lt_zoneContentPage_BizForm_viewBiz_ctl00_btnOK').cakemailTrap({
//		debug: 'True',
//		email : '#plc_lt_zoneContent_pageplaceholder_pageplaceholder_lt_zoneContentPage_BizForm_viewBiz_ctl00_Email_txtEmailInput',
//		name : '#plc_lt_zoneContent_pageplaceholder_pageplaceholder_lt_zoneContentPage_BizForm_viewBiz_ctl00_Name',
//		passthru: false,
//		list: [{
//				name: 'Customer list',
//				optin: '#newsltr_eu_optin',
//				urlSub : 'http://link.sayit-liveit.com/oi/1/0c3cdba106dff46d61fa02199ce04946',
//				urlUnsub : 'http://link.sayit-liveit.com/oo/1/0c3cdba106dff46d61fa02199ce04946'
//			}, {
//				name: 'Distributor list',
//				optin: '#newsltr_distributor',
//				urlSub : 'http://link.sayit-liveit.com/oi/1/0c3cdba106dff46d32354bef36381dd2',
//				urlUnsub : 'http://link.sayit-liveit.com/oo/1/0c3cdba106dff46d32354bef36381dd2'
//		}]
//	});
	
	// become a distributor
//	$('.dist-contact #plc_lt_zoneContent_pageplaceholder_pageplaceholder_lt_zoneContentPage_BizForm_viewBiz_ctl00_btnOK').cakemailTrap({
//		debug: 'True',
//		email : '#plc_lt_zoneContent_pageplaceholder_pageplaceholder_lt_zoneContentPage_BizForm_viewBiz_ctl00_Email_txtEmailInput',
//		name : '#plc_lt_zoneContent_pageplaceholder_pageplaceholder_lt_zoneContentPage_BizForm_viewBiz_ctl00_Name',
//		list: [{
//				name: 'Distributor list',
//				optin: '#distributor-opt-in',
//				urlSub : 'http://link.sayit-liveit.com/oi/1/0c3cdba106dff46d32354bef36381dd2',
//				urlUnsub : 'http://link.sayit-liveit.com/oo/1/0c3cdba106dff46d32354bef36381dd2'
//		}]
//	});
	
	// how to buy
//	$('.end-user-contact #plc_lt_zoneContent_pageplaceholder_pageplaceholder_lt_zoneContentPage_BizForm_viewBiz_ctl00_btnOK').cakemailTrap({
//		debug: 'True',
//		email : '#plc_lt_zoneContent_pageplaceholder_pageplaceholder_lt_zoneContentPage_BizForm_viewBiz_ctl00_Email_txtEmailInput',
//		name : '#plc_lt_zoneContent_pageplaceholder_pageplaceholder_lt_zoneContentPage_BizForm_viewBiz_ctl00_Name',
//		list: [{
//				name: 'Customer list',
//				optin: '#how-to-buy-opt-in',
//				urlSub : 'http://link.sayit-liveit.com/oi/1/0c3cdba106dff46d61fa02199ce04946',
//				urlUnsub : 'http://link.sayit-liveit.com/oo/1/0c3cdba106dff46d61fa02199ce04946'
//		}]
//	});
}

function initImageSwap(){
	// product image swapping
//	$('.color-options').find('.blue').hover(function(){
//		$('.product-image').children('img').attr('src', 'images/img_product-display.jpg');
//	}, function(){
//		$('.product-image').children('img').attr('src', 'images/img_product-display.jpg');
//	});
//	$('.color-options').find('.red').hover(function(){
//		$('.product-image').children('img').attr('src', 'images/img_product-display_red.jpg');
//	}, function(){
//		$('.product-image').children('img').attr('src', 'images/img_product-display.jpg');
//	});
//	$('.color-options').find('.yellow').hover(function(){
//		$('.product-image').children('img').attr('src', 'images/img_product-display_ylw.jpg');
//	}, function(){
//		$('.product-image').children('img').attr('src', 'images/img_product-display.jpg');
//	});
}

function initFieldHint(){
	// field hint
	$('.fieldhint').each(function(){
		$(this).prev('label').hide(); // hide label
		
		$(this).addClass('blur');
		if ($(this).val() === ''){
			var hint_text = $(this).prev('label').html(); // use label text in field
			$(this).val(hint_text);
		}
	});
	
	$('.fieldhint').focus(function(){
		var hint_text = $(this).prev('label').html();
		if ($(this).val() === hint_text){
			$(this).val('');
		}
		$(this).removeClass('blur').addClass('focus');
	});
	
	$('.fieldhint').blur(function(){
		if (!$(this).val().length){
			var hint_text = $(this).prev('label').html();
			$(this).val(hint_text);
			$(this).removeClass('focus').addClass('blur');
		}
	});
		
}

function initSuperFishMenus(){
	// menus 
	$("ul#sidenav").addClass("sf-menu sf-vertical");
	$("ul.sf-menu").superfish({
	    delay:       1000,                            // one second delay on mouseout 
	    animation:   {opacity:'show',height:'show'},  // fade-in and slide-down animation 
	    speed:       'fast',                          // faster animation speed 
	    autoArrows:  false,                           // disable generation of arrow mark-up 
	    dropShadows: false                            // disable drop shadows 
	});
}


try{Typekit.load();}catch(e){}


$(document).ready(function(){

    //$('.content.sub-category').css('height', 'auto');
   	initSuperFishMenus();
	initSlideshow();
	initMinHeightFix();
	initProdBorders();
	initHandleForms();
	//initImageSwap();
	initFieldHint();
	
});

