/*

THESE ARE THE GENERIC TOAST SCRIPTS 

*/


/* TABS */

$(function() {
 $tabs = $("#tabs").tabs();
});


$(function() {
  $("#tabsBedlinen").tabs();
});

$(document).ready(function() {
$ctabs = $('#ctabs').tabs();
$cprodtabs = $('.cprodtabs').tabs(); 
});



// TOOLTIP
$(document).ready(function() {
$('a.basic').cluetip();
  $('a.title').cluetip({splitTitle: '|'});
});

//unrelated to clueTip -- just for the demo page...

//unrelated to clueTip -- just for the demo page

// STYLE STORIES TOOLTIPS

$(document).ready(function() {
    try {
        $('.imageFadeNone').eztip('area', { contentAttrs: ['title'], opacity: .9, xOffset: 10, yOffset: 10 });
    }
    catch (ex) { }
});


// FAKE CHECKBOX

$(document).ready(function(){
  // check for what is/isn't already checked and match it on the fake ones
  $("input:checkbox").each( function() {
    (this.checked) ? $("#fake"+this.id).addClass('fakechecked') : $("#fake"+this.id).removeClass('fakechecked');
  });
  // function to 'check' the fake ones and their matching checkboxes
  $(".fakecheck").click(function(){
    ($(this).hasClass('fakechecked')) ? $(this).removeClass('fakechecked') : $(this).addClass('fakechecked');
    $(this.hash).trigger("click");
    return false;
  });
});

$(function(){
  //all hover and click logic for buttons
  $(".fg-button:not(.ui-state-disabled)")
  .hover(
    function(){ 
      $(this).addClass("ui-state-hover"); 
    },
    function(){ 
      $(this).removeClass("ui-state-hover"); 
    }
  )
  .mousedown(function(){
      $(this).parents('.fg-buttonset-single:first').find(".fg-button.ui-state-active").removeClass("ui-state-active");
      if( $(this).is('.ui-state-active.fg-button-toggleable, .fg-buttonset-multi .ui-state-active') ){ $(this).removeClass("ui-state-active"); }
      else { $(this).addClass("ui-state-active"); }  
  })
  .mouseup(function(){
    if(! $(this).is('.fg-button-toggleable, .fg-buttonset-single .fg-button,  .fg-buttonset-multi .fg-button') ){
      $(this).removeClass("ui-state-active");
    }
  });
});


// accordion menu

ddaccordion.init({
  headerclass: "expandable", //Shared CSS class name of headers group that are expandable
  contentclass: "categoryitems", //Shared CSS class name of contents group
  revealtype: "click", //Reveal content when user clicks or onmouseover the header? Valid value: "click", "clickgo", or "mouseover"
  mouseoverdelay: 200, //if revealtype="mouseover", set delay in milliseconds before header expands onMouseover
  collapseprev: true, //Collapse previous content (so only one open at any time)? true/false 
  defaultexpanded: [], //index of content(s) open by default [index1, index2, etc]. [] denotes no content
  onemustopen: false, //Specify whether at least one header should be open always (so never all headers closed)
  animatedefault: false, //Should contents open by default be animated into view?
  persiststate: false, //persist state of opened contents within browser session?
  toggleclass: ["", "openheader"], //Two CSS classes to be applied to the header when it's collapsed and expanded, respectively ["class1", "class2"]
  togglehtml: ["prefix", "", ""], //Additional HTML added to the header when it's collapsed and expanded, respectively  ["position", "html1", "html2"] (see docs)
  animatespeed: "fast", //speed of animation: integer in milliseconds (ie: 200), or keywords "fast", "normal", or "slow"
  oninit:function(headers, expandedindices){ //custom code to run when headers have initalized
    //do nothing
  },
  onopenclose:function(header, index, state, isuseractivated){ //custom code to run whenever a header is opened or closed
    //do nothing
  }
})



// Switch zoom images
function productZoomClickHandler(link)
{
    var productId = $(link).attr('productid') != null ? $(link).attr('productid') + ' - ' : '';
    _gaq.push(['_trackEvent', 'prod_img', 'click_zoom', productId + 'large product images viewed with zoom']);
}
  
function switchImg(imgName, mediaserver) {
  $('.MYCLASS').unbind();
  if (mediaserver == undefined) {
      mediaserver = '';
  }
  $('a.MYCLASS').attr('href', mediaserver + "/stormsites/toast/images/products/zoom/" + imgName);
  $('.MYCLASS img').attr('src', mediaserver + "/stormsites/toast/images/products/large/" + imgName);
  
  var options = {
      zoomWidth: 355,
      zoomHeight: 476,
        xOffset: 21,
        yOffset: -1,
        position: "right",
    showEffect: "fadein",
    preloadPosition: "center",
    showEffect: 'fadein',
     hideEffect: 'fadeout',
     fadeinSpeed: 'fast',
     fadeoutSpeed: 'fast',
    showPreload: true,
     title: false,
    preloadText : 'Loading...',
    preloadPosition : 'center',
    onClick: productZoomClickHandler
  }; 
$(".MYCLASS").jqzoom(options);
}


$(document).ready(function(){

      //$(".MYCLASS").attr ('href', 'javascript:;');
});

$(document).ready(function(){
  var options = {
      zoomWidth: 355,
      zoomHeight: 476,
        xOffset: 21,
        yOffset: -1,
        position: "right",
    showEffect: "fadein",
    preloadPosition: "center",
    showEffect: 'fadein',
     hideEffect: 'fadeout',
     fadeinSpeed: 'fast',
     fadeoutSpeed: 'fast',
    showPreload: true,
     title: false,
    preloadText : 'Loading...',
    preloadPosition : 'center'
  }; 
$(".MYCLASS").jqzoom(options);

}); 

// HANDLE THE ZOOM



// hover images preloader

jQuery.preloadImages = function() {   
     for(var i = 0; i<arguments.length; i++)   
    {
          jQuery("<img>").attr("src", arguments[i]);   
    }
}

// stop elements jumping about by hiding them until the page loads

$(window).load(function () {
  // run code
  $('.b').show(); 
  $('.a').show();
  $('#productDetailTextContainer').show();

});

/*$(document).ready(function() {
//When page loads...
 
    $(".tab_content").hide(); //Hide all content
    $("ul.tabs li:first").addClass("active").show(); //Activate first tab
    $(".tab_content:first").show(); //Show first tab content

    //On Click Event
    $("ul.tabs li").click(function() {

        $("ul.tabs li").removeClass("active"); //Remove any "active" class
        $(this).addClass("active"); //Add "active" class to selected tab
        $(".tab_content").hide(); //Hide all tab content

        var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
        $(activeTab).fadeIn(0); //Fade in the active ID content
        return false;
    });

});*/

// Product Images Hover

$(document).ready(function(){
  $("img.a").not('.cancelhover').hover(
    function() {
      $(this).animate({"opacity": "0"}, "fast");
    },
    function() {
      $(this).animate({"opacity": "1"}, "fast");
    });
 
});


// MAIN SLIDESHOW

$(document).ready(function() {
    $('.slideshow').cycle({
    
    fx:     'fade',
    //fx: 'scrollLeft',
       speed:    1000, 
      timeout:  4000,  
      pause:   1 
    
  });
});

// MENU
// initialise plugins
jQuery(function(){
    jQuery('ul.sf-menu').superfish();
});
    
    
// CYCLE
$(function() {
    $('#slideshow').cycle({
        fx:     'fade',
        speed:  'slow',
        timeout: 0,
        pager:  '#nav',
        pagerEvent: 'click',
        pagerAnchorBuilder: function(idx, slide) {
            // return sel string for existing anchor
            return '#slideNav li:eq(' + (idx) + ') a';
        }
    });
});


$(function() {
    $('#shopMapContainer').cycle({
        fx:     'fade',
        speed:  'fast',
        timeout: 0,
        pager:  '#productsList',
    pagerEvent: 'click',
        pagerAnchorBuilder: function(idx, slide) {
            // return sel string for existing anchor
            return '#productsList li:eq(' + (idx) + ') a';
        }
    });
});

function submitForm ()
{
  document.searchForm.submit() ;
}


// PRETTY PHOTO CUSTOMISATION

// CATEGORY BANNER LINKS
$(document).ready(function() {
 $('.ssCategoryImage a[title="PJs for Men"]').click(function(){return false;});
  $('.ssCategoryImage a[title="PJs for Men"]').css( 'cursor', 'default' );   
});   

$(document).ready(function() {
 $('.ssCategoryImage a[title="PJs for Women"]').click(function(){return false;});
  $('.ssCategoryImage a[title="PJs for Women"]').css( 'cursor', 'default' );   
}); 


// Google analystics tracking click handlers.
$(document).ready(function () {
    var button = $('#btnAddAllToBasket');

    if (button != null && button[0] != null) {
        button.click(function (event) {
            _gaq.push(['_trackEvent', 'wishlist', 'wlist_to_basket', 'all products added from wishlist to basket']);
        });
    }

    $('.moveToWishlist').each(function () {
        $(this).click(function (event) {
            var productId = $(this).attr('productid') != null ? $(this).attr('productid') + ' - ' : '';
            _gaq.push(['_trackEvent', 'basket', 'basket_to_wlist', productId + 'product moved from basket to wishlist']);
        });
    });

    $('.basketAlsoLikeLink').each(function () {
        $(this).click(function (event) {
            var productId = $(this).attr('productid') != null ? $(this).attr('productid') + ' - ' : '';
            _gaq.push(['_trackEvent', 'basket', 'you_may_like', productId + 'recommended product added to basket']);
        });
    });

    $('#viewfullsize').each(function () {
        $(this).click(function (event) {
            var productId = $(this).attr('productid') != null ? $(this).attr('productid') + ' - ' : '';
            _gaq.push(['_trackEvent', 'prod_img', 'view_fullsize', productId + 'product images viewed at fullsize']);
        });
    });

    $('.relatedProductLink').each(function () {
        $(this).click(function (event) {
            var productId = $(this).attr('productid') != null ? $(this).attr('productid') + ' - ' : '';
            _gaq.push(['_trackEvent', 'related_item', 'view_related', productId + 'related item viewed']);
        });
    });
    
});
