$(document).ready(function() {

    // if the browser window is less than these values then add a scroll bar so the flash doesn't scale any further
    function checkSizes() {
        // width
        var windowWidth = $(window).width();
        if (windowWidth < 967) {
            $("body").css("width", "967px");
        } else {
            $("body").css("width", "100%");
        }
        // height
        var windowHeight = $(window).height();
        if (windowHeight < 620) {
            $("body").css("height", "620px");
        } else {
            $("body").css("height", "100%");
        }
    }

    // check if flash exists before running resize function 
    if ($("object#flashContent").length > 0) {
        $(window).resize(function() {
            checkSizes();
        });
        checkSizes();
    }

    $("select[name=ctl00$main$sampleAlternateContent$country]").change(function() {
        var countries = ["AL", "BB", "BS", "KY", "DO", "GR", "HK", "IE", "JM", "MO", "PA", "AE", "IT"].join();
		if (countries.indexOf($(this).val()) > -1){
			$("input[name=__EVENTTARGET]").val("ctl00$main$sampleAlternateContent$country");
			form = $("body form");
			form.submit();
		}
    });

    //* homepage *//	
    //AccDev: Code edited
    var language=getCookie('boss_language_descriptor');
    if(language == "de" || language == "es" || language == "ru" || language == "fr" || language == "it")
    {
    var image1 = $('<img />').attr({ src: '/bossorange/Resources/images/homeIMG1.jpg', id: 'homeIMG1' }).load(function() { $('div.home').append(this) });
    var image2 = $('<img />').attr({ src: '/bossorange/Resources/images/homeIMG2.jpg', id: 'homeIMG2' }).load(function() { $('div.home').append(this) });
    var image3 = $('<img />').attr({ src: '/bossorange/Resources/images/homeIMG3.jpg', id: 'homeIMG3' }).load(function() { $('div.home').append(this) });
    

    $("img#homeIMG1").insertAfter("img#homeBottle");
    $("img#homeIMG2").insertAfter("img#homeIMG1");
    $("img#homeIMG3").insertAfter("img#homeIMG2");


    var timer = 1;
    counter = setInterval(homeFade, 2000);
    }

    function homeFade()
    {
        $("div.home img").eq(timer).fadeIn(1000, function() {
            $(this).fadeOut(1000);
            timer++;
        });
        if (timer > 3) 
        {
            timer = 1;
        }
    }
    

    //* range page *//

    //set the range colour
    $("ul#productRange li").css({ color: "#d65b0b" });

    //on mouseover
    $("map#rangeImgMap area").mouseover(function() {

        //find this elements class
        className = $(this).attr('class');
        timeover = setTimeout(over, 1500);

        function over() {
            //fade the range list to 40%
            $("ul#productRange li").animate({ opacity: "0.4" }, 500);
            //if the range matches this elements li class then fade the image up to 100%
            $("ul#productRange li." + className).animate({ opacity: "1" }, 500);
        }

    });

    $("map#rangeImgMap area").mouseout(function() {
        //on mouseout, clear the timer									
        clearInterval(timeover);
    });

    $("img#rangeImg").mouseout(function() {
        //on mouseout of the entire image, return the list opacity to 100%
        $("ul#productRange li").animate({ opacity: "1" }, 500);
    });


    //* advertising page *//	

    var idleTime = 0;
    var idleTimer = setInterval(timerIncrement, 2000);

    var images = 0;
    var imageTimer = setInterval(fades, 2000);

    $(document).mousemove(function() {
        idleTime = 0;
    });

    function timerIncrement() {
        idleTime++;
        //console.log("time = " +idleTime);

        if (idleTime == 5) {
            images = 0;
            idleTime = 0;
            imageTimer = setInterval(fades, 2000);

        }

    }

    function fades() {
        //console.log("images = " + images);
        $("div.advertisingHome div.advertInfo").next().eq(images).fadeIn(1000);
        $("div.advertisingHome div.advertInfo").next().eq(images).fadeOut(1000);
        images++;
        if (images > 3) {
            clearInterval(imageTimer);
            return false;
        }
    }


    //set the div color and the pointer hand

    $("div.advertisingHome div.advertInfo, div.advertisingHome div.advertInfo a").css({
        cursor: "pointer",
        color: "#f0c08e"
    });

    $("div.advertisingHome div.advertInfo").hover(function() {
        $(this).css({ color: "#d65b0b" });
        $(this).find("a").css({ color: "#d65b0b" });

        $(this).next().fadeIn(1000);

    }, function() {

        //re-set the colour
        $(this).css({ color: "#f0c08e" });
        $(this).find("a").css({ color: "#f0c08e" });
        $(this).next().fadeOut(1000);
    });



    //* scent page *//	

    //check if element exists
    if ($("dl#scent").length > 0) {

        $("dl#scent dt a").click(function() {
            return false;
        });

        $("dl#scent dd").css({ display: "none" });

        $($("dl#scent dt")).each(function(index) {

            //hide the fragrance note images
            $("dl#scent#scent dt a").attr({ href: "#" });
            $("div.images img#notes_" + index).css({ display: "none" });

            $($("div.images img")).each(function(index) {
                $(this).attr({ id: "notes_" + index });
            });


            // on hover of the a tag element
            $(this).hover(function() {

                //re-set the fragrance images to display none for bug issues 
                $("div.images img#notes_" + index).css({ display: "none" });
                $("dl#scent#scent dt a").attr({ title: "" });

                //set the zIndex with an increment to fade above the previous image	  
                $("div.images img#notes_" + index).css({ zIndex: "99" + index });
                $("div.images img#notes_" + index).fadeIn(1000);

                //show the description element
                $(this).next().show();

            }, function() {

                //on rollOut fadeout the image
                $("div.images img#notes_" + index).fadeOut(1000);
                $("div.images img#notes_" + index).css({ zIndex: "" });

                //hide the description element
                $("dl#scent dd").hide();
            });
        });
    };

});


function setSound(value)
{
    setCookie('bo_sound', value, 30);   
}

function getSound()
{
    var sound = getCookie('bo_sound');
    if (sound == "") {
        return "true";
    }
    return sound;
}


function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) + "; path=/" + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}


function getCookie(c_name) {
    if (document.cookie.length>0) {
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1)
        {
            c_start=c_start + c_name.length+1;
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start,c_end));
        }
    }
    return "";
}