$(document).ready(function(){
    //  If Home
    if ( $('body').hasClass('home')) {
        //  Stage
        $("#stage .images")
        .after('<div class="nav">')
        .cycle({
            fx:         'fade',
            speed:      1000,
            timeout:    4000,
            pager:      '.nav'
        });

        $("#blog-excerpt ul").simplyScroll({
            frameRate: 20,
            speed: 2,
            autoMode:   'loop',
            horizontal: false
        });

        // Reception selector
        todayIs = new Date();
        if (todayIs.getDay() == 0 || todayIs.getDay() == 6) {
            firstTab = 'receptions-detail-privees';
        } else if (todayIs.getHours() < 8 || todayIs.getHours() >= 18) {
            firstTab = 'receptions-detail-privees';
        } else {
            firstTab = 'receptions-detail-entreprises';
        }
        $("#reception-selector").idTabs(firstTab);
        $(".reception-images").cycle({
            fx:     'fade',
            speed:  1000,
            timeout: 2000
        });

    //  Else Flash Banner animation on inside pages
    }  else {
        $("#banniere").flash(
            { src: '/PROD/flash/banniere_header.swf', width: 824, height: 123 },
            { version: 8 })
        ;
    }

    $("#bfm-radio-spot").flash(
        { src: '/PROD/flash/player.swf', width: 320, height: 248, flashvars: { file: '/PROD/fichiers/30_IdBuffet_Mx030609.mp3', image: '/PROD/img/edito/radio-img-2.jpg' } },
        { version: 8 }
    );
    $("#bfm-radio-itw").flash(
        { src: '/PROD/flash/player.swf', width: 320, height: 248, flashvars: { file: '/PROD/fichiers/Thomas_Favrel_sur_BFM.mp3', image: '/PROD/img/edito/radio-img-1.jpg' } },
        { version: 8 }
    );

    //  Anchor Tabs
    anchor = $.url.attr("anchor");
    $("ul.tabs li:eq(1) a").removeClass("selected");
    if (anchor!=null){
        anchor = parseInt(anchor.substr(4));
    } else {
        anchor = 1;
    }

    $("ul.tabs li:eq("+(anchor-1)+") a").addClass("selected");
    $("ul.tabs").idTabs("tabs"+anchor);

    //  Cross Selling tabs
    $(".product-cross-selling").each(function(item){
        $(this).find('li.panier a').bind("click", function(e){
            e.stopPropagation()

            // Fetch product ID from URL
            $(this).attr("href").match(new RegExp(/PID=(\d+)&/g));
            PID = RegExp.$1;

            // Fetch Quantiy from the input text
            QPID = $('#QPID_'+PID).val();

            // If quantity is more than or equal to 1
            if (QPID>0)
            {
                href = $(this).attr("href").replace(/QPID_(\d+)=1&/, "QPID_"+PID+"="+QPID+"&");
                $(this).attr("href", href);

                // Don't move
            } else {
                alert("La quantité saisie doit être au minimum de 1");
            }
        });
    });

    if ($.url.param("Added")=="1")
    {
        $.openDOMWindow({
            loader:0,
            borderSize:0,
            width:245,
            height:100,
            windowPadding:0,
            windowBGColor:'transparent',
            windowBGImage:'/PROD/img/bg-addToCart.png',
            windowSource:'inline',
            windowSourceID:'#inlineAddToCartMessage'
        });
        $('#addToCartMessage').click(function(){$.closeDOMWindow()});
    }
});

function openWindow(url, w, h) {
    var options = "width=" + w + ",height=" + h + ",";
    options += "resizable=yes,scrollbars=yes,status=yes,";
    options += "menubar=no,toolbar=no,location=no,directories=no";
    var newWin = window.open(url, 'newWin', options);
    newWin.focus();
}

