$(document).ready(function() {
	
	function getUrlVars()
	{
	    var getVars = [], hash;
	    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	    for(var i = 0; i < hashes.length; i++)
	    {
	        hash = hashes[i].split('=');
	        getVars.push(hash[0]);
	        getVars[hash[0]] = hash[1];
	    }
	    return getVars;
	}
	
	var urlvar = getUrlVars()["LB"];
	
	$('#scrollbar1').tinyscrollbar({ axis: 'x', sizethumb: 30 });
		
	//Config vars
	//image dimensions in pixels, number of slides, etc
	var bgWidth = 1024,
		bgHeight = 506,
		aspectRatio = bgWidth / bgHeight,
		theWindow = $(window),
		sliderPos = 0,
		duration = 1500,
		numSlides_women = 11,
		numSlides_men = 8,
		numSlides_kids = 3
		numSlides_fearless = 3;

	function execSlide(slideNum,lbid) {
		leftPos = -theWindow.width()*slideNum;
		$('#'+lbid+'LookBookSlideWrapper').animate({left:leftPos}, duration);
		marLeft = slideNum*12;
		$("#navDotTick").animate({marginLeft:marLeft}, duration);
		//kill any flash OBJECT tags
		$('object').remove();
		//check for missing embed placeholders and replace missing
		if($('#flashmediaslide').length == 0) {
			$('#fearlessVideoWrapper').prepend('<div id="flashmediaslide"></div>');
		}
	}
	
	function constructSlider() {
		$bg = $('.slideBG');
		if((theWindow.width() / theWindow.height()) < aspectRatio) {
			$bg.removeAttr("style");
			$bg.css({"height":theWindow.height()});
		}
		else {
			$bg.removeAttr("style");
			$bg.css({"width":theWindow.width()});
		}
		$(".LBSlide").css({"width":theWindow.width(),"height":theWindow.height()});
		$('#womenLookBookSlideWrapper').css({"width":theWindow.width() * numSlides_women});
		$('#menLookBookSlideWrapper').css({"width":theWindow.width() * numSlides_men});
		$('#kidsLookBookSlideWrapper').css({"width":theWindow.width() * numSlides_kids});
		$('#fearlessLookBookSlideWrapper').css({"width":theWindow.width() * numSlides_fearless});
	}
	
	$bg = $('.slideBG');
	
	theWindow.resize(function(){
		constructSlider();
	}).trigger("resize");
	
	//FEARLESS SLIDER FLASH
	function embedVideo() {
		// load SWF object
		var vars = {firstVideo:"SOREL_MANOR_60_ZBGG-0071_1080i_1.flv", autoPlay:"true", loopVideo:"false"};
		var params = { scale:'noScale', salign:'lt', menu:'true', allowFullScreen:'true', allowScriptAccess:'always', wmode:'transparent' };
		var attributes = { id:'flash', name:'flash' }; // give an id to the flash object

		swfobject.embedSWF("/on/demandware.static/Sites-Sorel_US-Site/Sites-Sorel_US-Library/default/Home/2011LookBook/flash/main.swf", "flashmediaslide", "100%", "100%", "10.0.0", "", vars, params, attributes );
	}
	
	//*********************
	//Click events MAIN LB NAV
	//*********************
	$('#womenLBButton').live('click',function() {
		$('#womenLookBookFrame').show();
		$('#womenLookBookFrame').siblings().hide();
		$('#womenTextNav').removeClass('activeNavBox nonactiveNavBox').addClass('activeNavBox');
		$('#womenTextNav').siblings().removeClass('activeNavBox nonactiveNavBox').addClass('nonactiveNavBox');
	});
	$('#menLBButton').live('click',function() {
		$('#menLookBookFrame').show();
		$('#menLookBookFrame').siblings().hide();
		$('#menTextNav').removeClass('activeNavBox nonactiveNavBox').addClass('activeNavBox');
		$('#menTextNav').siblings().removeClass('activeNavBox nonactiveNavBox').addClass('nonactiveNavBox');
		$('#scrollbar2').tinyscrollbar({ axis: 'x', sizethumb: 30 });
	});
	$('#kidsLBButton').live('click',function() {
		$('#kidsLookBookFrame').show();
		$('#kidsLookBookFrame').siblings().hide();
		$('#kidsTextNav').removeClass('activeNavBox nonactiveNavBox').addClass('activeNavBox');
		$('#kidsTextNav').siblings().removeClass('activeNavBox nonactiveNavBox').addClass('nonactiveNavBox');
		$('#scrollbar3').tinyscrollbar({ axis: 'x', sizethumb: 30 });
	});
	$('#fearlessLBButton').live('click',function() {
		$('#fearlessLookBookFrame').show();
		$('#fearlessLookBookFrame').siblings().hide();
		$('#fearlessTextNav').removeClass('activeNavBox nonactiveNavBox').addClass('activeNavBox');
		$('#fearlessTextNav').siblings().removeClass('activeNavBox nonactiveNavBox').addClass('nonactiveNavBox');
		$('#scrollbar4').tinyscrollbar({ axis: 'x', sizethumb: 30 });
		embedVideo();
	});
	
	//*********************
	//Click events WOMENS
	//*********************
	$womensThumb = $('.womenThumb');
	lbid_w = 'women';
	$('#womenSlide1').live('click',function() {
		sliderPos = 0;
		execSlide(sliderPos,lbid_w);
		//remove active thumb class from other thumb and apply nonactive to them and active to this
		$womensThumb.removeClass('activeThumb nonactiveThumb').addClass('nonactiveThumb');
		$(this).removeClass('nonactiveThumb').addClass('activeThumb');
	});
	$('#womenSlide1b').live('click',function() {
		sliderPos = 1;
		execSlide(sliderPos,lbid_w);
		//remove active thumb class from other thumb and apply nonactive to them and active to this
		$womensThumb.removeClass('activeThumb nonactiveThumb').addClass('nonactiveThumb');
		$(this).removeClass('nonactiveThumb').addClass('activeThumb');
	});
	$('#womenSlide2').live('click',function() {
		sliderPos = 2;
		execSlide(sliderPos,lbid_w);
		//remove active thumb class from other thumb and apply nonactive to them and active to this
		$womensThumb.removeClass('activeThumb nonactiveThumb').addClass('nonactiveThumb');
		$(this).removeClass('nonactiveThumb').addClass('activeThumb');
	});
	$('#womenSlide3').live('click',function() {
		sliderPos = 3;
		execSlide(sliderPos,lbid_w);
		//remove active thumb class from other thumb and apply nonactive to them and active to this
		$womensThumb.removeClass('activeThumb nonactiveThumb').addClass('nonactiveThumb');
		$(this).removeClass('nonactiveThumb').addClass('activeThumb');
	});
	$('#womenSlide4').live('click',function() {
		sliderPos = 4;
		execSlide(sliderPos,lbid_w);
		//remove active thumb class from other thumb and apply nonactive to them and active to this
		$womensThumb.removeClass('activeThumb nonactiveThumb').addClass('nonactiveThumb');
		$(this).removeClass('nonactiveThumb').addClass('activeThumb');
	});
	$('#womenSlide5').live('click',function() {
		sliderPos = 5;
		execSlide(sliderPos,lbid_w);
		//remove active thumb class from other thumb and apply nonactive to them and active to this
		$womensThumb.removeClass('activeThumb nonactiveThumb').addClass('nonactiveThumb');
		$(this).removeClass('nonactiveThumb').addClass('activeThumb');
	});
	$('#womenSlide6').live('click',function() {
		sliderPos = 6;
		execSlide(sliderPos,lbid_w);
		//remove active thumb class from other thumb and apply nonactive to them and active to this
		$womensThumb.removeClass('activeThumb nonactiveThumb').addClass('nonactiveThumb');
		$(this).removeClass('nonactiveThumb').addClass('activeThumb');
	});
	$('#womenSlide7').live('click',function() {
		sliderPos = 7;
		execSlide(sliderPos,lbid_w);
		//remove active thumb class from other thumb and apply nonactive to them and active to this
		$womensThumb.removeClass('activeThumb nonactiveThumb').addClass('nonactiveThumb');
		$(this).removeClass('nonactiveThumb').addClass('activeThumb');
	});
	$('#womenSlide8').live('click',function() {
		sliderPos = 8;
		execSlide(sliderPos,lbid_w);
		//remove active thumb class from other thumb and apply nonactive to them and active to this
		$womensThumb.removeClass('activeThumb nonactiveThumb').addClass('nonactiveThumb');
		$(this).removeClass('nonactiveThumb').addClass('activeThumb');
	});
	$('#womenSlide9').live('click',function() {
		sliderPos = 9;
		execSlide(sliderPos,lbid_w);
		//remove active thumb class from other thumb and apply nonactive to them and active to this
		$womensThumb.removeClass('activeThumb nonactiveThumb').addClass('nonactiveThumb');
		$(this).removeClass('nonactiveThumb').addClass('activeThumb');
	});
	$('#womenSlide10').live('click',function() {
		sliderPos = 10;
		execSlide(sliderPos,lbid_w);
		//remove active thumb class from other thumb and apply nonactive to them and active to this
		$womensThumb.removeClass('activeThumb nonactiveThumb').addClass('nonactiveThumb');
		$(this).removeClass('nonactiveThumb').addClass('activeThumb');
	});
	
	//*********************
	//Click events MENS
	//*********************
	$mensThumb = $('.menThumb');
	lbid_m = 'men';
	$('#menSlide1').live('click',function() {
		sliderPos = 0;
		execSlide(sliderPos,lbid_m);
		//remove active thumb class from other thumb and apply nonactive to them and active to this
		$mensThumb.removeClass('activeThumb nonactiveThumb').addClass('nonactiveThumb');
		$(this).removeClass('nonactiveThumb').addClass('activeThumb');
	});
	$('#menSlide1b').live('click',function() {
		sliderPos = 1;
		execSlide(sliderPos,lbid_m);
		//remove active thumb class from other thumb and apply nonactive to them and active to this
		$mensThumb.removeClass('activeThumb nonactiveThumb').addClass('nonactiveThumb');
		$(this).removeClass('nonactiveThumb').addClass('activeThumb');
	});
	$('#menSlide2').live('click',function() {
		sliderPos = 2;
		execSlide(sliderPos,lbid_m);
		//remove active thumb class from other thumb and apply nonactive to them and active to this
		$mensThumb.removeClass('activeThumb nonactiveThumb').addClass('nonactiveThumb');
		$(this).removeClass('nonactiveThumb').addClass('activeThumb');
	});
	$('#menSlide3').live('click',function() {
		sliderPos = 3;
		execSlide(sliderPos,lbid_m);
		//remove active thumb class from other thumb and apply nonactive to them and active to this
		$mensThumb.removeClass('activeThumb nonactiveThumb').addClass('nonactiveThumb');
		$(this).removeClass('nonactiveThumb').addClass('activeThumb');
	});
	$('#menSlide4').live('click',function() {
		sliderPos = 4;
		execSlide(sliderPos,lbid_m);
		//remove active thumb class from other thumb and apply nonactive to them and active to this
		$mensThumb.removeClass('activeThumb nonactiveThumb').addClass('nonactiveThumb');
		$(this).removeClass('nonactiveThumb').addClass('activeThumb');
	});
	$('#menSlide5').live('click',function() {
		sliderPos = 5;
		execSlide(sliderPos,lbid_m);
		//remove active thumb class from other thumb and apply nonactive to them and active to this
		$mensThumb.removeClass('activeThumb nonactiveThumb').addClass('nonactiveThumb');
		$(this).removeClass('nonactiveThumb').addClass('activeThumb');
	});
	$('#menSlide6').live('click',function() {
		sliderPos = 6;
		execSlide(sliderPos,lbid_m);
		//remove active thumb class from other thumb and apply nonactive to them and active to this
		$mensThumb.removeClass('activeThumb nonactiveThumb').addClass('nonactiveThumb');
		$(this).removeClass('nonactiveThumb').addClass('activeThumb');
	});
	$('#menSlide7').live('click',function() {
		sliderPos = 7;
		execSlide(sliderPos,lbid_m);
		//remove active thumb class from other thumb and apply nonactive to them and active to this
		$mensThumb.removeClass('activeThumb nonactiveThumb').addClass('nonactiveThumb');
		$(this).removeClass('nonactiveThumb').addClass('activeThumb');
	});
	
	//*********************
	//Click events KIDS
	//*********************
	$kidsThumb = $('.kidsThumb');
	lbid_k = 'kids';
	$('#kidsSlide1').live('click',function() {
		sliderPos = 0;
		execSlide(sliderPos,lbid_k);
		//remove active thumb class from other thumb and apply nonactive to them and active to this
		$kidsThumb.removeClass('activeThumb nonactiveThumb').addClass('nonactiveThumb');
		$(this).removeClass('nonactiveThumb').addClass('activeThumb');
	});
	$('#kidsSlide2').live('click',function() {
		sliderPos = 1;
		execSlide(sliderPos,lbid_k);
		//remove active thumb class from other thumb and apply nonactive to them and active to this
		$kidsThumb.removeClass('activeThumb nonactiveThumb').addClass('nonactiveThumb');
		$(this).removeClass('nonactiveThumb').addClass('activeThumb');
	});
	$('#kidsSlide3').live('click',function() {
		sliderPos = 2;
		execSlide(sliderPos,lbid_k);
		//remove active thumb class from other thumb and apply nonactive to them and active to this
		$kidsThumb.removeClass('activeThumb nonactiveThumb').addClass('nonactiveThumb');
		$(this).removeClass('nonactiveThumb').addClass('activeThumb');
	});
	
	//*********************
	//Click events FEARLESS
	//*********************
	$fearlessThumb = $('.fearlessThumb');
	lbid_f = 'fearless';
	$('#fearlessSlide1').live('click',function() {
		sliderPos = 0;
		execSlide(sliderPos,lbid_f);
		//remove active thumb class from other thumb and apply nonactive to them and active to this
		$fearlessThumb.removeClass('activeThumb nonactiveThumb').addClass('nonactiveThumb');
		$(this).removeClass('nonactiveThumb').addClass('activeThumb');
		embedVideo();
	});
	$('#fearlessSlide2').live('click',function() {
		sliderPos = 1;
		execSlide(sliderPos,lbid_f);
		//remove active thumb class from other thumb and apply nonactive to them and active to this
		$fearlessThumb.removeClass('activeThumb nonactiveThumb').addClass('nonactiveThumb');
		$(this).removeClass('nonactiveThumb').addClass('activeThumb');
	});
	$('#fearlessSlide3').live('click',function() {
		sliderPos = 2;
		execSlide(sliderPos,lbid_f);
		//remove active thumb class from other thumb and apply nonactive to them and active to this
		$fearlessThumb.removeClass('activeThumb nonactiveThumb').addClass('nonactiveThumb');
		$(this).removeClass('nonactiveThumb').addClass('activeThumb');
	});
	
	//hide men and kids look books, defered to here to fix ie9 issue
	$("#menLookBookFrame").hide();
	$("#kidsLookBookFrame").hide();
	$("#fearlessLookBookFrame").hide();
	
	if(urlvar == 'men') {
		$("#menLBButton").trigger('click',[true]);
	}
	
	if(urlvar == 'kids') {
		$("#kidsLBButton").trigger('click',[true]);
	}
	
	if(urlvar == 'fearless') {
		$("#fearlessLBButton").trigger('click',[true]);
	}
	
	//check for Flash and set flag
	var hasFlash = false;
	try {
		var fo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
		if(fo) hasFlash = true;
	}catch(e){
		if(navigator.mimeTypes ["application/x-shockwave-flash"] != undefined) hasFlash = true;
	}
	//load non-flash modals
	if(hasFlash == false) {
		$(".modalContentWrapper").show();
	}
	
});
