/* Javascript stylesheet */
document.write("<link rel=\"StyleSheet\" href=\"/baseAssets/css/baseJs.css\" type=\"text/css\" media=\"screen\" />");

// e3 javascript for Base London

// when the document is loaded
$(document).ready(function() {	
	
	
	
	//jQuery functions
	fnBrowser();
	fnAccordionInit();
	fnSizeGuide();
	fnSliderInit();
	fnSignUp();			// homepage email sign up
	fnHomeHozTabs();	// homepage gents club horizontal tabs
	//fnHomeFlashHero();	// homepage hero flash area // Written into page by E3 CMS
});

/**************************************************************
Function:	fnBrowser
Purpose:	Check we're in a supported browser
Notes:		
**************************************************************/
// Global Variables
var iepc,firefox,ns7,ns8,mozilla,safari;
function fnBrowser() {	
	var ua = navigator.userAgent.toLowerCase();
	
	iepc = (((ua.indexOf("msie 7") != -1)||(ua.indexOf("msie 6") != -1)||(ua.indexOf("msie 5.5") != -1))&&(ua.indexOf("windows") != -1)&&(ua.indexOf("opera") == -1))? true:false;
	firefox = ua.indexOf("firefox") != -1 ? true:false; // pc or mac
	ns7 = ua.indexOf("netscape/7") != -1 ? true:false; // firefox mode
	ns8 = ua.indexOf("netscape/8") != -1 ? true:false; // firefox mode
	mozilla = (ua.indexOf("gecko") != -1) && (ua.indexOf("netscape") == -1) ? true:false; 
	safari = ua.indexOf("safari") != -1 ? true:false;	
	opera = ua.indexOf("opera/9") != -1 ? true:false;
	if (iepc||firefox||mozilla||safari||opera) {
		return true;
	} else {
		return false;
	}
}


// Flash detect. boolean true/false if installed and version number accessed via flash.version
var flash = new Object();
flash.installed = false;

if (navigator.plugins && navigator.plugins.length) {
	for (x=0; x < navigator.plugins.length; x++) {

		if (navigator.plugins[x].name.indexOf('Shockwave Flash') != -1) {
			flash.installed = true;
			flash.version = eval(navigator.plugins[x].description.split('Shockwave Flash ')[1].split('.')[0]);
			break;
		}
	}
}
else if (window.ActiveXObject) {
	for (x = 2; x <= 20; x++) {
		try {
			oFlash = eval("new ActiveXObject('ShockwaveFlash.ShockwaveFlash." + x + "');");
			if(oFlash) {
				flash.installed = true;
				flash.version = x;
			}
		}
		catch(e) {}
	}
}

/********************************************************************************

Name: 				Flash embed
Description:		Writeout flash code. Fixes Eolas update for IE as well.
@param	swf			String path to swf file
@param	width		Number value of movie width
@param	height		Number value of movie height
@param  wmode		Window mode to use for the movie

*********************************************************************************/
flash.insert = function(swf,width,height,flashVer,nonFlashId) {

	if(!flashVer){minVer = 6;}
	else {minVer = flashVer;}
	if(!nonFlashId){nonFlashId = "nonFlashContent";}
	
// Handle errors
	var errMsg ="";
	if (!fnBrowser()) { // NOT supported browser
		errMsg += '<p class="err">Unfortunately, this website cannot be viewed by your browser. Please go to <a href="http://www.getfirefox.com">www.getfirefox.com</a> or <a href="http://www.microsoft.com/windows/ie/">www.microsoft.com/</a> to download a recommended browser.</p>';
	}
	if (!flash.installed) { // NOT flash
		errMsg += '<p class="err">This website requires the adobe&trade; Flash player. Go to the adobe website to install the <a href="http://www.adobe.com/products/flashplayer/">latest version of flash</a></p>';
	}

	swf = swf.replace(/\./g,"%2E"); // encode periods as %2e

	if(flash.installed && fnBrowser() && flash.version >= minVer) {
		document.write('<embed src="'+swf+'" menu="false" width="'+width+'" height="'+height+'" scale="noscale" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" salign="T" />');
		
		fnHide(nonFlashId);
	}
	else {
    fnShow(nonFlashId);
	}
};

function fnHide(id) {
	if(!document.getElementById(id)){return false;};
	var oTarg = document.getElementById(id).style;
	oTarg.position = "absolute";
	oTarg.top = "-5000px";
	oTarg.left = "0";
}

function fnShow(id) {
	if(!document.getElementById(id)){return false;};
	var oTarg = document.getElementById(id).style;
	oTarg.position = "static";
	oTarg.top = "";
	oTarg.left = "";
}

/* SIFR Initiation */
	var din = {src: '/baseAssets/flash/dinmedium.swf'};

	// You probably want to switch this on, but read <http://wiki.novemberborn.net/sifr3/DetectingCSSLoad> first.
	sIFR.useStyleCheck = true;
	sIFR.activate();
	
	// specific to homepage sign up fb
	sIFR.replace(din, {
		selector: '#fbCol .homeNewsletterSignup h4'
		,css: {
			'.sIFR-root': { 
				'background-color': '#A91418',
		      	'color': '#ffffff',
		      	'font-size' : '13px',
		      	'padding' : '0',
		      	'margin' : '0'
	      	}			
		}	
	});
	
	// specific to homepage strap lines
	sIFR.replace(din, {
		selector: '#strap h2'
		,css: {
			'.sIFR-root': { 
				'background-color': '#ffffff',
		      	'color': '#A91418',
		      	'font-size' : '15px',
		      	'padding' : '0',
		      	'margin' : '0'
	      	}			
		}	
	});
	// specific to homepage sign up fb	
	
	sIFR.replace(din, {
		selector: '#strap p'
		,css: {
			'.sIFR-root': { 
				'background-color': '#ffffff',
		      	'color': '#333333',
		      	'font-size' : '12px',
		      	'padding' : '0',
		      	'margin' : '0'		      	
	      	}			
		}	
	});
	 
	// .frt
	sIFR.replace(din, {
		selector: '.frt'
		,selectable: false
		,css: {
			'.sIFR-root': {
				'background-color': '#FFFFFF',
		      	'color': '#333333',
		      	'font-size' : '14px',
		      	'padding' : '0',
		      	'margin' : '0'
	      	}
		},wmode: 'transparent'
		
	});
	
	sIFR.replace(din, {
		selector: '#headingImage .frtTitle'
		,css: {
			'.sIFR-root': { 
				'background-color': '#000000',
		      	'color': '#666666',
		      	'font-size' : '19px',
		      	'padding' : '0',
		      	'margin' : '0'
	      	}			
		},wmode: 'transparent'	
	});
	
	sIFR.replace(din, {
		selector: '#headingPlain .frtTitle'
		,css: {
			'.sIFR-root': { 
				'background-color': '#FFFFFF',
		      	'color': '#000000',
		      	'font-size' : '17px',
		      	'padding' : '0',
		      	'margin' : '0'
	      	}			
		}	
	});
	
	
	
var mImg;
var zLink;
var wrapper;






function fnDragImageInit(){

//if the wrapper has the zoomed class
if(wrapper.hasClass('zoomed')){
	//make the main image draggable
		mImg.draggable({
			//contained within the parent area which is a div larger than the image so it can move to all sides
			containment:'parent'
		}
			
		);
		//center it
		mImg.css({left:'320px', top:'140px'});
	}else{
		// do nothing	
	}
	
}



function fnAccordionInit(){
	if ($("#content.home").length > 0) {
	 $('.accList ul').accordion({   					
		        header: 'h5',
		        selectedClass: 'open',
		        event: 'mouseover',
		        autoHeight: false
		    });
	    }
	/* bind focus to same event
	$('.accList ul h5 a') .bind('focus', function(event) {
		console.log($(this));
		$(this).mouseover();
		
	});
	*/
}

var items;
var step;
var w;
var h;
var speed;

function fnSliderInit(){
	//doesn't work properly in safari 1.3
	if ($.browser.safari && $.browser.version < 400) {
    	return false;
 	}
 	
// is there a slider on the page
if($(".fbSlider .sliderItem").length>1){
		
	// config variables - this version assumes there are only 3 max shoes
	homepage = ($("#home2Col #fbCol").length) ? true : false;
	items 	= 	$(".fbSlider .sliderItem").length;
	step 	= 	238; 			// pixel width of viewport
	totalW 	= 	items * 238;	// usually same as step
	h		= 	homepage ? '14.8em': '19.8em';		// sort the css overflow out - shorter if it's on the homepage (#home2Col #fbCol)		
	speed	= 	300;			// speed to slide at(ms to completion of movie)
	// end config
	
	$('.fbSlider .sliderThumbs').show();
	//prep the current html
	$(".fbSlider .slider")
		.css({
			overflow: 'hidden',
			height: h						
		});
	$(".fbSlider .sliderInr").css("width",totalW);
	if(!homepage){
		$(".fbSlider .sliderItem")
		.prepend('<img src="/baseAssets/images/arrowCirclePrev.gif" alt="Previous" class="prev" /><img src="/baseAssets/images/arrowCircleNext.gif" alt="Next" class="next" />');
		//bind slide
		$(".fbSlider .prev")						
			.click(function(){						
				$(".slider").animate({scrollLeft: "-="+step},{ complete: fnCheckSliderPos, duration:400 });						
				return false;
			});	
		$(".fbSlider .next")
			.click(function(){						
				$(".slider").animate({scrollLeft: "+="+step},{ complete: fnCheckSliderPos, duration:400 });
				return false;
			});			
	}
						
	$(".fbSlider .sliderThumbs a")
		.click(function() {						
			var index = $(".fbSlider .sliderThumbs a").index($(this)[0]);
			$(".slider").animate({scrollLeft: ""+ (index*step) +""},{ complete: fnCheckSliderPos, duration:400 });							 
			$(".fbSlider .sliderThumbs a img").css('border','1px solid #fff');
			$(this).find('img').css('border','1px solid #AD121E');
			return false;
	 	});	
	 	
	//hide buttons if not needed
	fnCheckSliderPos();
}
}

function fnCheckSliderPos(){
	var pos = $(".slider").attr("scrollLeft");	
	var oPrev = $(".fbSlider .prev");
	var oNext = $(".fbSlider .next");
	
	if(pos <= 0){
		fnShow(oNext);
		fnFade(oPrev);
		fnHighlight('0');
	}else if(pos >= (totalW-step) && items>2){
		fnFade(oNext);
		fnShow(oPrev);
		fnHighlight('2');												
	}else if(pos >= (totalW-step) && items<=2){
		fnFade(oNext);
		fnShow(oPrev);
		fnHighlight('1');												
	}else{
		fnShow(oNext);
		fnShow(oPrev);												
		fnHighlight('1');
	}
	
}

function fnShow(obj){
	obj.css('opacity','1.0');
	obj.css('cursor','pointer');
}
function fnFade(obj){
	obj.css('opacity','0.5');
	obj.css('cursor','default');
}
function fnHighlight(num){										
	$(".fbSlider .sliderThumbs a img").css('border','1px solid #fff');
	$(".fbSlider .sliderThumbs a:eq("+num+") img").css('border','1px solid #AD121E');					
}

var sG;
var sGT;

function fnSizeGuide(){
	if($('#shoeDetail').length > 0){ 
		
		
		
	    sGT = $('#sizeGuide h4');
    	
	    sGTitle = sGT.html();
	    sGT.remove();
	    sG = $('#sizeGuide table');
	    $('a[href=#sizeGuide]').boxy({title: sGTitle, closeText: 'close X', afterShow: function(){
	    	if($.browser.msie == true && $.browser.version <7){
	    	fnHideSelects()
    	}
	    	},
	    	afterHide: function(){
	    		fnShowSelects();
	    	}
    	});
    	
    	
	    $('#sizeGuide').hide();
	    
    }
}

function fnHideSelects(){
	$('select').hide();
}

function fnShowSelects(){
	$('select').show();
}
	
/* Hide or show fields and address lookup on address pages */
function fnCountrySelected(countrySelect) {
    if(countrySelect == null) return false;
    if(countrySelect.options[countrySelect.selectedIndex].value == "UK") {
        $("#rowPostcode dfn").css("display","inline");
        $("#rowPostcode :image").css("display","inline");
        $("div[id$='divPostcodeNotFound']").removeClass("hide");
        if($("input[id$='txtAddress1']").val() == "" && !$("div[id$='divPostcodeNotFound']").hasClass('row'))
            $("div[id$='pnlAddressFields']").addClass("hide");
        
    } else {
        $("#rowPostcode dfn").css("display","none");
        $("#rowPostcode :image").css("display","none");
        $("div[id$='pnlAddressFields']").removeClass("hide");
        $("div[id$='divPostcodeNotFound']").addClass("hide");
    }    
}


/*
 * Catches keydown event on login box to submit Login button instead of Register button.
 */
function fnLoginRegisterCatchEnter (btn, event)
{
	btn = document.getElementById(btn);
	if (btn) {
		var iNum = 0;
		if (event.which == null)
		{
			iNum = event.keyCode;    // IE
		}
		else if (event.which > 0)
		{
			iNum = event.which;	  // All others
		}
		
		if (iNum == 13)		// Enter key
		{
			event.returnValue = false;
			event.cancel = true;
			try {
			btn.click();
			} catch (e) {}
		}
	}
}

// add bg with "example@email.com" to email sign up box. 
// Dont use a value as this needs to be deleted by users before they can enter text
function fnSignUp () {
	if($('#home2Col #fbCol div.homeNewsletterSignup input.txt')){
		var $targ = $('#home2Col #fbCol div.homeNewsletterSignup input.txt')	
		// if user has refreshed or gone back to this page - text box may be filled out
		if(!$targ.val()){
			$targ.addClass('bg');
		}
		$targ
			.bind('focus', function() {
				$(this).removeClass('bg');	
			})
			.bind('blur', function() {
				if(!$targ.val()){
					$(this).addClass('bg');
				}
			});
	}
}

// homepage gentlemens club show-hide content "tabs"
function fnHomeHozTabs() {	
	if($('#home2Col #gentsClub')){	
		$('#gentsClub').addClass('jsOn');			// add enhancing class
		var $gcA = $('#gentsClub ul li>a').not('.play');			// links to bind behaviour to
		var $gcImg = $('#gentsClub li img');			// links to bind behaviour to
				
		var $gcSpan = $('#gentsClub ul li>span'); 	// span is the "tab"		
		$gcSpan.not(":first").hide();				// hide all but first img ("tabs")		
		
		$gcA 
		
		.hover(function() {
			$gcSpan
				.filter(":visible").hide()
			.end()
				.eq($gcA.index(this)).show();				
			$gcA.removeClass('selected').eq($gcA.index(this)).addClass('selected');			
			
		}, function() {
			// do nothing on mouse-out
		}) .focus(function() {
			$gcSpan
				.filter(":visible").hide()
			.end()
				.eq($gcA.index(this)).show();				
			$gcA.removeClass('selected').eq($gcA.index(this)).addClass('selected');	
			
		}) .click(function() {			
			window.location = $("a.play",$(this).parents('li'))[0].href;			
			return false;
		});	
		
		$gcImg .click(function() {
			window.location = $("a.play",$(this).parents('li'))[0].href;						
		}); 
			
	}
}

function fnHomeFlashHero(path) {
	var so = new SWFObject(path, "FlashHero", "724", "300", "8");
	so.write("nonFlashContent");	
}
