/* Moved from page.js - Roz 20/02/2008 */

/* show button in a td#closebuttonimage for popup window when js enabled - Jamesb 8/2/08 */

var genclosewindowtdimg = {

      init: function() {

            var element = document.getElementById("closebuttonimage");

            if (!element) return;

             var closeButton = '<a href="#" onclick="window.close()">'
 				 + '<img alt="Click here to close this window" src="/wcsstore/homebase/en_GB/images/p1/b_close_window_popup.gif" border="0"/>'
 				 + '</a>';

            element.innerHTML = closeButton;

      }

}

/* New PDP tabs - D. Gafford 10/07/08. Changes made to remove hard-coded tab widths and to make tabs more reusable and easier to maintain */

// Setup namespace
if(!homebase) var homebase = {};
if (!homebase.product) homebase.product = {};

// begin: homebase.product.details
if (!homebase.product.details) homebase.product.details = {};
homebase.product.details.Tabs = {
    init : function() {
    	$("li.bundleaction").show(); 
        // exit if no producttabs divs are on page
        if ($(".producttabs").length == 0) return;
        // add class to flag that js is enabled
        $(".producttabs").addClass("jstabs");
        // create new object for each instance of .producttabs
        $(".producttabs").each(function(i){
            this.TabSet = new homebase.product.details.Tabs.TabSet(this);
        });            
        
    }, // end init
    
    // TabSet class
    TabSet: function(tabset) {
    
       init();
       
        function init() {
            // by default add active class to tab1
            $("h4#tab1", tabset).addClass("active");
            // move all h4s and place before tab content (allowing floated/fluid tabs rather than absolute pos/explicit width)
            $(tabset).prepend($("h4.tabTitle", tabset));
            
            // stuff thats done to every tab h4
            $("h4.tabTitle", tabset).each(function(i){
                //wrap text in h4 with a div
                var htmlStr = $(this).html();
                // empty h4
                $(this).empty();
                // fill h4 with a div and the value of htmlStr
                $(this).append("<div>" +htmlStr+ "</div>");
                // Add additional class to JS tabs for further css targeting
                $(this).addClass("jstabstyle");
            });
            
            addEvents();
            
            // Show Special Offers Tab
            // first check that the tab exists
            if ($("#tab5").length == 0) return; 
	        // jump to anchor and show tab
	        $("#showspecialoffers").click( 
                function() { 
                    hideAllTabs(); 
                    showTab("tab5"); 
                }
            );

            
        } //end init
        
        function addEvents() {
            $("h4.tabTitle", tabset).click( function() {
            
				removeEvents();
                hideAllTabs();
                showTab($(this)[0].id);
                    
                if( $(this)[0].id == "tabrev" ) 
					fireOmnitureReviewsEvent();
			} );
        }
        
        function showTab(tabId) {
            $("h4#"+tabId, tabset).addClass("active");
            $("."+tabId, tabset).show(); 
            addEvents();
            $("h4#"+tabId, tabset).unbind();
        }
        
        function hideAllTabs() {
            $(".tabs", tabset).hide();
            $("h4.tabTitle", tabset).removeClass("active");
        }
        
        function removeEvents() {
            $("h4.tabTitle", tabset).unbind();


        }

        function fireOmnitureReviewsEvent() {   
			var linkName = "Reviews";
			s.linkTrackVars = "products,eVar28,eVar30,eVar31"; 
			s.eVar28 = linkName;
			s.eVar30 = homebase.product.details.Tabs.omnitureReviews.totalReviewsCount;
			s.eVar31 = homebase.product.details.Tabs.omnitureReviews.avgRating;	

			var reviewTabObj = $("h4#tabrev")[0];
		
//alert( "s.eVar30=" + s.eVar30 + " s.eVar31=" + s.eVar31 + " - "+reviewTabObj.id+" - "+ linkName);
			s.tl( reviewTabObj, 'o', linkName );	

			
        }
        
        
    },// End TabSet class
    
    omnitureReviews: {
	    totalReviewsCount:  0,
    	avgRating: 0
    }
    
    

}// end: homebase.product.details


var HB = HB || {};
HB.ReadReviewLink = new (function(){
	$(document).ready(init);
		
	var container = null, link = null, targetTabArea = null, targetTab = null;
		
	function init() {	
		container = $("#ratingpanel-bot")[0] || null;
		if(!container) return;
	
		link = $(container).find("a.readmoreAnchor");
		if(!link) return;
		
		targetTabArea = $("div.producttabs")[0] || null;
		if(!targetTabArea) return;
		
		targetTab = $("div.producttabs h4#tabrev")[0] || null;
		if(!targetTab) return;
		
		for (var i = 0; i < link.size(); i++) {
			$(link[i]).bind("click", function() {
				$(targetTab).click();
			});
		}
	}
});	


$(document).ready(function() {
	// changed conditional to correctly match jquery array
	if ($("#BVFrame")[0]) {
		// hide review tab by default if JS enabled. Conditional later shows this if there are reviews available
			$("#tabrev").hide();
		// hide iframe for js enabled users
			var frame = $("#BVFrame");
			frame.addClass("jsHide");
		// replace format value in src path to ensure correct visual formatting within iframe
			var frameSrcValue = frame.attr("src");
			frame.attr("src", frameSrcValue.replace(/noscript/i, "embedded"));			
		}
	
	
	// initalise PDP tabs
	homebase.product.details.Tabs.init();
});
		
 

