﻿$(document).ready(function(){
    InitBadges();
    InitRangeMenu();
    InitPromos();
});

function InitBadges()
{
    $('p#rotaryBadges img').ImageSwap();
}

function InitRangeMenu()
{
    //Set background images on all containers
    $('div#rangeHeroCollection div.rangeHeroContainer').each(function(){
        var $this = $(this);
        var sku = $this.find('div.rangeHero div.heroProduct a').text();
        sku = sku.substring(0, sku.indexOf(','));
        var bg = "url('/resources/images/catalogue/product-variant/" + sku.replace(/\//g, '-') + "/extralarge/hero.jpg')";
        $this.css('background-image', bg);        
    });
    
    //Remove the default bg image from the page
    $('form').css('background-image', 'none');
    
    //Set up the cycle
    $('div#rangeHeroCollection').cycle({ 
        fx: 'fade',
        easing: 'easeInCubic',
        delay: -3000,
        speed: 1000,
        timeout: 8000,
        sync: 1
    });
    
    //Set up the range nav to switch on mouseover and cancle the cycle
    var listItems = $('div#rangeNavigation ul.vertical > li');
    listItems.each(function(){
        var $this = $(this);
        var classes = $(this).attr('class').split(' ');
        var rangeId = classes[classes.length - 1];
        
        $this.data('RangeId', rangeId);
        $this.mouseover(function(){
            $('div#rangeHeroCollection').cycle('pause');
            
            var rangeId = $(this).data('RangeId');
            var rangeHero = $('div#rangeHeroCollection div.' + rangeId + 'Container');
            
            $('div#rangeHeroCollection div.rangeHeroContainer').hide();
            rangeHero.css('opacity', 1);
            rangeHero.css('display', 'block');
            rangeHero.show();
        });
    });
}

function InitPromos()
{
    $('div#content div.promo.isFlash').each(function(){
        //Get the name of the image - the swf should be named exactly the same
        var src = $(this).find('img').attr('src');
        var path = src.substring(0, src.lastIndexOf('.')).replace('/images/', '/flash/') + '.swf';
        
        //Set up the swfObject
        var flashvars = {};
	    var params = {};
	    params.menu = "false";
	    var attributes = {};
	    swfobject.embedSWF(path, $(this).find('div.promoHolder').attr('id'), "364", "66", "9.0.0", false, flashvars, params, attributes);
    });
}
