
function print_page_setup() {
	blocks=new Array(
		"pageheader",
		"mainnav",
		"search",
		"breadcrumb",
		"prodColRight",
		"footer"
	);
	hide(blocks)
	document.getElementsByTagName("body")[0].className="blankBody";
	printID=window.setTimeout("print_win()",500); // gives window time to redraw
};

function pre_print(){
};

function print_win() {
	clearTimeout(printID);
	t=1000;
	if(confirm("Print now?")) window.print()
	else t=0;
	showID=window.setTimeout("show(blocks)",t);
};

function hide(b){
	for (var i=0; i<b.length; i++) {
		if(document.getElementById(b[i])) {
			document.getElementById(b[i]).style.display="none";
		}
	}
};

function show(b){
	for (var i=0; i<b.length; i++) {
		if(document.getElementById(b[i])) {		
			document.getElementById(b[i]).style.display="block";
		}
	}
	document.getElementsByTagName("body")[0].className="";
};
	
function imageClick() {
   	var href = $(this).attr("href");
   	if (/[\w]+:\/\/[\w.]+\/\S*\S*[.]swf|jsp\S*/.test(href)) {
		window.open(href,'enlarge','width=545,height=707,resizable=no');
   	}
   	else {
        $('#fullImage')
        	.attr('src',href)
			.attr('jqimg',href.replace('$prod$','$prodzoom$'));
   	}
	return false;
}
    
function validateQtyField(){
	var field = document.getElementById('WC_CachedProductOnlyDisplay_FormInput_quantity_In_OrderItemAddForm_1').value;
  	if (!isNaN(field) && field > 0 && field < 100){
		return true;
	}
  	else {
		alert(pageDetails.invalidInputMsg);
  		return false;
  	}
}

function carousel(jqObject,id) {
    jqObject
      .before('<img class="thumb-carousel-left" id="' + id + 'leftButton" src="' + pageDetails.jspStoreImgDir + 'images/thumb-carousel-button-left.jpg" alt="Shift items left" />')
      .after('<img class="thumb-carousel-right" id="' + id + 'rightButton" src="' + pageDetails.jspStoreImgDir + 'images/thumb-carousel-button-right.jpg" alt="Shift items right" />')
      .jCarouselLite({
        btnNext: 	"#" + id + "leftButton",
        btnPrev: 	"#" + id + "rightButton",
        afterEnd: 	function(elements) {
        				$(elements).find('a[class!=s7video]').click(imageClick);
        			}
      });
};
  
function bv_readall_reviews() {
	bv_readall("#tab3");
}
  
function bv_readall_qas() {
	bv_readall("#tab4");
}
  
function bv_readall(pTab) {
	$(pTab).click();
}

function showvideo(el) {

	// extract the flv name from the href
	var rx = new RegExp(/encodingXml=http:\/\/washford.scene7\.com\/e2\/Washford\/Videos\/(.*)\.flv/);
	var m = rx.exec($(el).attr("href"));
	if (!m || !m[1]) {
    	return true;
	}

	var videoName = m[1];
	var $imgDiv = $('#productFullImage');
	var $vidDiv = $('#scene7Video');

	$(el).unbind(
		'click'
	).click(function(){
		return hidevideo(el);
	}).attr(
		"videoactive", "true"
	).find("img").attr("src", "http://washford.scene7.com/is/image/Washford/returntoimage");

	$vidDiv.css({
		top:$imgDiv.position().top,
		left:$imgDiv.position().left,
		width:$imgDiv.width(),
		height:$imgDiv.height(),
		visibility:'visible',
		display:'block'
	});

	$imgDiv.css({
		visibility:'hidden',
		display:'none'
	});

	var viewerType = 'videoplayer';
	var myName = 's7player';
		
	var viewerUrl = "";  //Build Viewer Path to Viewer swf
 		viewerUrl += S7Config.isViewerRoot;
 		viewerUrl += "flash/" + viewerType + ".swf?";
 		viewerUrl += "&contentroot=" + S7Config.contentRoot + "e2/";
 		viewerUrl += "&instanceName=" + myName;
 		viewerUrl += "&encodingXml=" + S7Config.contentRoot + "e2/Washford/Videos/" + videoName + ".flv.xml";
 		viewerUrl += "&viewerXML=" + S7Config.contentRoot + "e2/Washford/DefaultViewerConfig.xml";

	// swfobject replaces rather than inserts, so create a temp child element
	var target = $vidDiv.html('<span id="swftgt" />');
	swfobject.embedSWF(
 		viewerUrl
 	    ,"swftgt"
 	    ,"290"
 	    ,"220"
 	    ,"7.0.14.0"
 	    ,"../resources/expressInstall/expressInstall.swf"
 	    ,{}//flashvars
 	    ,{//params
			allowScriptAccess:"Always"
			,menu:"false"
			,quality:"high"
			,salign:"LT"
			,wmode:"transparent"
			,method:"prog"
 	    }
 	    ,{//attributes
			id: myName
 			,name: myName
		}
	);

	return false;
}

function hidevideo(el) {

	$(el).unbind(
		'click'
	).click(function(){
		return showvideo(el);
	}).removeAttr(
		"videoactive"
	).find("img").attr("src", "http://washford.scene7.com/is/image/Washford/playvideo");
	
	var $imgDiv = $('#productFullImage');
	var $vidDiv = $('#scene7Video');

	try {
		S7Config.setFlashParam('s7player','videoPlayback','stop');
	} catch (err){
	}
	$('#s7player').remove(); // without this IE7 has some problems in some cases

	$vidDiv.css({
		visibility:'hidden',
		display:'none'
	});
	
	$imgDiv.css({
		visibility:'visible',
		display:'block'
	});

	return false;
}

function PDPz1_zp(pids,
					zone,
					symbolic,
					target_id,
					category,
					rec_attributes,
					target_attributes,
					target_header_txt)
{
	var IDX_CATID = 9;
	var IDX_ITEMID = 2;
	var IDX_ITEMNAME = 1;
	var IDX_PRICE = 3;
	var IDX_WASPRICE = 4;
	var IDX_SAVING = 5;
	var IDX_RATING = 7;
	var IDX_PROMOTEXT = 6;
	var IDX_IMAGE = 8;
	if(symbolic != '_NR_')
	{
		var $sprod = $('#tradeupcol');
		if($sprod)
			$sprod.remove();
		
		var $e_zonediv = $(document.createElement('div'));
		$e_zonediv.attr('id', 'tradeupcol');
		
		var $e_h2 = $(document.createElement('h2'));
		$e_h2.text('Other customers went on to buy');
		
		var $zone = $(document.createElement('ul'));
		$zone.attr('id', 'pdp_zone1');
		
		$e_zonediv.append($e_h2);
		$e_zonediv.append($zone);
		$('#helpandadvicecol').after($e_zonediv);
		
		$zone.html('');
		for(var i=0; i<pids.length; i++)
		{
			var $e_li = $(document.createElement('li'));
			
			var $e_div1 = $(document.createElement('div'));
			$e_div1.addClass('tradeupprodimage');
			
			var $e_div2 = $(document.createElement('div'));
			$e_div2.addClass('tradeupproddescription');
			
			var $e_div3 = $(document.createElement('div'));
			$e_div3.addClass('tradeupprodprice');
			
			var $e_a1 =  $(document.createElement('a'));
			$e_a1.attr('href', 'product_storeId_' +
								storeId +
								'_catalogId_' +
								catalogId +
								'_productId_' +
								rec_attributes[i][IDX_ITEMID] +
								'_categoryId_' +
								rec_attributes[i][IDX_CATID] +
								'_langId_-1?cm_vc=IOV4PDPZ1');
			var $e_a2 = $e_a1.clone();
			var $e_a3 = $e_a1.clone();
			$e_a1.attr('title', rec_attributes[i][IDX_ITEMNAME]);
			$e_a2.attr('title', rec_attributes[i][IDX_ITEMNAME]);
								
			var $e_img = $(document.createElement('img'));
			$e_img.attr('src', 'http://washford.scene7.com/is/image/Washford/'+rec_attributes[i][IDX_IMAGE]+'?$prodthumb$');
			$e_img.attr('alt', 'Click to view product details');
			
			$e_a1.append($e_img);
			$e_a2.html(rec_attributes[i][IDX_ITEMNAME]);
			$e_a3.html(currency + rec_attributes[i][IDX_PRICE]);
			$e_div1.append($e_a1);
			$e_div3.append($e_a3);
			$e_div2.append($e_a2);
			$e_div2.append($e_div3);
			$e_li.append($e_div1);
			$e_li.append($e_div2);
			$zone.append($e_li);
		}
	}
}

function PDPz2_zp(pids,
					zone,
					symbolic,
					target_id,
					category,
					rec_attributes,
					target_attributes,
					target_header_txt)
{
	var IDX_CATID = 9;
	var IDX_ITEMID = 2;
	var IDX_ITEMNAME = 1;
	var IDX_PRICE = 3;
	var IDX_WASPRICE = 4;
	var IDX_SAVING = 5;
	var IDX_RATING = 7;
	var IDX_PROMOTEXT = 6;
	var IDX_IMAGE = 8;
	
	if(symbolic != '_NR_')
	{
		var zone = document.getElementById('bottompanelcol2-inner');
		if(!zone)
		{
			var $col = $('#bottompanelcol2');
			var $printpag = $col.children('.printpag');
			$col.html('');
			
			var $tabs = $(document.createElement('ul'));
			$tabs.attr('class', 'tabs');
			var $li = $(document.createElement('li'));
			var $tab1 = $(document.createElement('a'));
			$tab1.attr('id', 'tab1');
			$tab1.attr('class', 'tab active');
			$tab1.attr('tablocator', 'Have you considered tab');
			$tab1.attr('href', '#dtab');
			$tab1.attr('title', 'Have you considered?');
			var $span = $(document.createElement('span'));
			$span.text('Have you considered?');
			$tab1.append($span);
			$li.append($tab1);
			$tabs.append($li);
			$col.append($tabs);
			
			var $clear1 = $(document.createElement('div'));
			$clear1.attr('class', 'clear');
			var $clear2 = $clear1.clone();
			var $border = $(document.createElement('div'));
			$border.attr('id', 'bottompanelcol2-border');
			var $zone = $(document.createElement('div'));
			$zone.attr('id', 'bottompanelcol2-inner');
			$border.append($zone);
			var $clearboth = $(document.createElement('div'));
			$clearboth.attr('class', 'clearBoth');
			$clear1.append($border);
			$clear1.append($clearboth);
			$clear1.append($printpag);
			$col.append($clear1);
			$col.append($clear2);
		}
		else
		{
			var $zone = $(zone);
		}
		
		$zone.html('');
		var catentry_idx = 30; //default to 30 which is unlikely to be used.
		for(var i=0; i<pids.length; i++)
		{
			var $e_div1 = $(document.createElement('div'));
			$e_div1.addClass('relatedproduct');
			
			var $e_div2 = $(document.createElement('div'));
			$e_div2.addClass('relprodimage');
			
			var $e_div3 = $(document.createElement('div'));
			$e_div3.addClass('relproddescandprice');
			
			var $e_div4 = $(document.createElement('div'));
			$e_div4.addClass('relproddesc');
			
			var $e_div5 = $(document.createElement('div'));
			$e_div5.addClass('relprodprice');
			
			var $e_div6 = $(document.createElement('div'));
			$e_div6.addClass('relprodbuy');
			
			var $e_div7 = $(document.createElement('div'));
			$e_div7.addClass('clearBoth');
			
			var $e_a1 = $(document.createElement('a'));
			$e_a1.attr('href', 'HalfordsProductDetailsLiteView?storeId=' +
								storeId +
								'&langId=-1' +
								'&productId=' +
								rec_attributes[i][IDX_ITEMID] +
								'&TB_iframe=true&TB_C_ID=productdetails&TB_C_TITLE=' +
								escape(rec_attributes[i][IDX_ITEMNAME]) +
								'&width=800&cm_vc=IOV4PDPZ2');
			$e_a1.attr('title', 'Link to product description of ' + rec_attributes[i][IDX_ITEMNAME]);
			$e_a1.attr('class', 'thickbox');
			var $e_a2 = $e_a1.clone();
			
			var $e_img = $(document.createElement('img'));
			$e_img.attr('src', 'http://washford.scene7.com/is/image/Washford/'+rec_attributes[i][IDX_IMAGE]+'?$prodthumb$');
			$e_img.attr('alt', 'Click to view product details');
			
			var $e_input1 = $(document.createElement('input'));
			$e_input1.attr('name', 'catEntryId_' + (catentry_idx+i));
			$e_input1.attr('type', 'checkbox');
			$e_input1.attr('value', rec_attributes[i][IDX_ITEMID]);
			
			var $e_input2 = $(document.createElement('input'));
			$e_input2.attr('name', 'quantity_' + (catentry_idx+i));
			$e_input2.attr('type', 'hidden');
			$e_input2.attr('value', 1);
			
			$e_a1.append($e_img);
			$e_a2.html(rec_attributes[i][IDX_ITEMNAME]);
			$e_div2.append($e_a1);
			$e_div4.append($e_a2);
			$e_div5.html(currency + rec_attributes[i][IDX_PRICE]);
			$e_div6.html('Tick to buy ');
			$e_div6.append($e_input1);
			$e_div6.append($e_input2);
			$e_div3.append($e_div4);
			$e_div3.append($e_div5);
			$e_div3.append($e_div6);
			
			$e_div1.append($e_div2);
			$e_div1.append($e_div3);
			$e_div1.append($e_div7);
			
			$zone.append($e_div1);
		}
		
		$('#bottompanelcol2>.tabs>li:first>a>span').html('Customers also bought');
	}
	
	tb_init('a.thickbox, area.thickbox, input.thickbox');
}

$(document).ready(function() {

    $(".content").hide();
    $("#itab1content").show(); 
    $("#dtab1content").show(); 
       
    $("a.tab")
		.click(function() {
	        // toggle tabs  
	        $(this).parent().parent().find(".active").removeClass("active");
	        $(this).addClass("active");   
	           
	        // slide all elements with the class 'content' up   
	        var content = $("#" + $(this).attr("tablocator"));
	        content.parent().find(".content").hide();   
	        content.show();
	
	        if (content.find("li a").length > 3 && $("#" + content.attr("id") + "leftButton").length == 0) {
	          carousel(content, content.attr("id"));
	        }
	    })
	    .each(function() {
			$(this).attr("tablocator", $(this).attr("title"));
			
			var currentTabId = $(this).attr("id");
			var activeTabId = "tab1";
			try {
				if (document.location.href.indexOf("#") != -1) {
					activeTabId = document.location.href.substr(document.location.href.indexOf("#") + 1);
				}
				if (currentTabId == activeTabId) {
					$(this).click();
				}
			} catch (err) {}
	
			var tabTitle = "";
			try {
				tabTitle = $(this).children("span").text();
				$(this).attr("title", "Display " + tabTitle + " tab");
			} catch (err) {
				$(this).attr("title", "Display tab");
			}
		});
    
    // when a non-s7 video link is clicked, search for all active s7 videos and simulate a click on their icon to stop them
    $("div[@id^=itab] a[class!=s7video]").click(imageClick).click(function(){
    	$("a[videoactive=true]").trigger('click');
	});
    
    $("div[id^=itab] a[class=s7video]").each(function(i,el){
   		$(el).click(function(){return showvideo(el);});
		$(el).find("img").attr("src", "http://washford.scene7.com/is/image/Washford/playvideo");
    });
    
    $("#itab1content")
    	.each(function() {
		    if ($(this).find("li a").length > 3)
		    	carousel($(this), "itab1content");
    	});
    
	$('.definingattributes-opt ul')
		.each(function(){
			$(this).parent().append('<select id="facetSelector"> </select>');
			$(this).find('li>*:first-child').each(function(){
				if ($(this).attr('tagName') == 'A') {
					$('#facetSelector').append('<option value="' + $(this).attr('href') + '">' + $(this).find('span').text() + '</option>');
				}
				else {
					$('#facetSelector').append('<option value="" selected="selected">' + $(this).text() + '</option>');
				}
			});
	    })
	    .remove();
    
    $('#facetSelector').change(function() {
		window.location.replace($(this).attr('value'));
		return;
    });
    
    $(".productFullImage").find('img[@src$="$prod$"]').each(function() {
      $(this).attr('jqimg',$(this).attr('src').replace('$prod$','$prodzoom$'));
    });
      
  	$('#mouseOffMessage').hide();

	$(".jqzoom")
	  	.mouseover(function() {
	  		$('#mouseOffMessage').show();
	  		$('#mouseOverMessage').hide();
	  	})
	  	.mouseout(function() {
	  		$('#mouseOffMessage').hide();
	  		$('#mouseOverMessage').show();
	  	})
		.jqueryzoom({
	        xzoom: 458, 
	        yzoom: 300,
	        offset: 60,
	        preload: 0
	  	});
	  	
	$('#viewLarger')
		.each(function() {
			$(this).attr('iTemplate',$(this).attr('href'));
		})
		.click(function() {
			var imageName = $('#fullImage').attr('src');
			imageName = imageName.substring(imageName.lastIndexOf('/') + 1);
			window.open($(this).attr('iTemplate').replace('FULL_IMAGE', imageName.substring(0, imageName.lastIndexOf('?'))),'enlarge','width=550,height=700,resizable=no');
			return false;
		});
  	
	$("#OrderItemAddForm").submit(function(event) {
		var lReturn = $(this).attr('disableSubmit') == 'Y' ? false : true;
		$(this).attr('disableSubmit', 'N');
		return lReturn;
	});

	$('#reserveYourBikeForXmas').click(function() {	
		if (this.checked) {
			tb_show(null, '#TB_inline?height=200&width=350&inlineId=reserveYourBikeForXmasPopup', null);
		}
	});	

	positionTooltip = function(event) {
		var x = event.pageX - 200;
		var y = event.pageY + 20;
		$('div#tooltip').css({"top":y, "left":x});
	}

	showTooltip = function(event) {
		hideTooltip();
		var text = $(this).attr('tiptext');
		
		$('<div id="tooltip">' + text + '</div>').appendTo('body');
		positionTooltip(event);
	}

	hideTooltip = function() {
		$('div#tooltip').remove();
	}

	$(".randctip[@id=available]").tooltip({text: pageDetails.rAndCAvailableMsg});
	$(".randctip[@id=notavailable]").tooltip({text: pageDetails.rAndCNotAvailableMsg});
	
	$(".deliverytip[@id=available]").tooltip({text: pageDetails.hDAvailableMsg});
	$(".deliverytip[@id=notavailable]").tooltip({text: pageDetails.hDNotAvailableMsg});
	
	$("#webexclusivedescription").tooltip({text: pageDetails.webExclusiveMsg});
	
	$('#storeFitOption').click(function() {
		$(this).attr('checked') && $('#mobileFitOption').attr('checked', false);
	});
	
	$('#mobileFitOption').click(function() {
		$(this).attr('checked') && $('#storeFitOption').attr('checked', false);
	});
	
});
