/* Slidebox items */
var animInterrupted=false;
var selectedSlidebox=0;
var slideboxes =["home","benefits","storelocator","offers","testimonials"];


function getURL(idx) {

    
    
    switch (idx) {
        case 0:
            document.location = "about-us"; 
            break;
        case 1:
            //document.location = "leightons-privilege-club";
            break;
        case 2:
            document.location = "store-locator";
            break;
        case 3:
            document.location = "our-services/promotions";
            break;
        case 4:
            document.location = "testimonials";
            break;
        default:
            break;
    }
}


/* Before we showed the selected slidebox hide all */
function __hideAllSildeBoxes(exceptMe) {
    
        
    if (dontchangeslidebox) return false;

    //for (var item in slideboxes) { $("#" + slideboxes[item]).hide(); }
    //$("#wrapper>div:hidden:eq(0)").show(); //.animate({ position: "absolute", top: "305px" }, 1, function() { $(this).hide(); });
    //$("#wrapper").animate({left:"500px"});
    //$("#" + slideboxes[exceptMe]).animate({left : "-" + parseFloat(exceptMe*1000)}); //.css({ top: "-305px" }).show().animate({ top: "0" });
    var dd = 0; var dd_e = 0;


    if ($.browser.msie) {
        if ($.browser.version == '6.0')
            dd_e = 20;
    }
    
    
    if (exceptMe == 3) { dd = 2942; }
    else if (exceptMe == 2) { dd = 1969; }
    else if (exceptMe == 1) { dd = 985; }
    else if (exceptMe == 4) { dd = 3911; }
    
    else { dd = parseFloat(exceptMe * 1000); }

    window.status = exceptMe + ", " + dd;

    $("#wrapper").animate({ left: "-" + parseInt( dd + dd_e) });
    
    //$("#" + slideboxes[exceptMe]).show();
}


var inter = null;
function animAfterDelay() {
    __hideAllSildeBoxes(idxExt);
    //$("#logo").append("a-" + inter + "-");
    clearInterval(inter);
    inter = null;
}

var dontchangeslidebox = false;

function onGetHtmlMessageCallback(message) {
    alert(message);
}

/* Dom ready let the party begins! */
$(document).ready(function() {


    //    var m_url = document.location.toString();
    //    var r_url = "";
    //    if (m_url.indexOf("#") > -1) {
    //        r_url = m_url.split("#")[1];
    //        if (r_url != "") document.location = r_url;
    //    }


    $("a[href^='#ddss']").click(function(e) {
        //if (r_url == "") { return false; }
        //$("#center").prepend($("div#loader"));
        $("span#loader").show();

        var t_url = $(this).attr("href");
        var url = t_url.split("#")[1];
        if (url === "") { $("div#loader").hide(); return false; }

        $("li").removeClass("selected");
        //e.preventDefault();

        $(this).parent().addClass("selected");

        $.ajax({
            url: url
                , cache: 'false'
                , error: function() { $("div#loader").hide(); alert("3rror occur3d!"); }
                , success: function(html) {
                    var h = $("#center", html).html();
                    //alert(h);
                    $("#center").hide(1, function() { $(this).html(h); }).slideDown(10, function() { $("span#loader").hide(); });
                }
        })
        return true;
        //e.preventDefault();
    });

    //MessageWebService.HelloWorld("",onGetHtmlMessageCallback);

    //    $("a[rel='facebox']").click(function() {
    //        $.ajax({
    //            url: "/umbraco/login.aspx",
    //            cache: false,
    //            success: function(html) {
    //                $.facebox($("#Panel1", html).html());
    //            }
    //        });
    //        return false;
    //    });



    /* text size inc dec rollovers */
    $("#inc").mouseover(function() { $(this).attr("src", "/images/rolloverPlus.gif"); });
    $("#inc").mouseout(function() { $(this).attr("src", "/images/plus.gif"); });

    $("#dec").mouseover(function() { $(this).attr("src", "/images/rolloverMinus.gif");});
    $("#dec").mouseout(function() { $(this).attr("src", "/images/minus.gif"); });


    //$("a[href='#']").click(function() { alert("This page is under construction!"); });


    var currentlyAnimatedSlideboxId = -1;

    $("#slidecontainer>.slidebox").mouseover(function(key) {
        $("#wrapper>div").show();
        animInterrupted = true;
        dontchangeslidebox = false;

        //if (currentlyAnimatedSlideboxId == $("#slidecontainer>.slidebox").index($(this))) return false;
        //currentlyAnimatedSlideboxId = $("#slidecontainer>.slidebox").index($(this));
        //alert(currentlyAnimatedSlideboxId);


        //$(".slidebox>.hidden").hide();
        var greybox = $(".hidden", this); //.fadeTo(1, 0.7).show();
        $("p.hide", this).show();

        //$("p.hide",this).highlightFade();               
        var idx = $("#slidecontainer>.slidebox").index($(this));
        selectedSlidebox = idx;
        //idxExt=idx

        //if (inter == null) inter = setInterval(animAfterDelay, 2000);
        $(this).mouseout(function() { dontchangeslidebox = true; $("#footer").stop(); return false; })

        $("#footer").fadeTo(400, 1, function() {

            if (!dontchangeslidebox) {
                $(".slidebox>.hidden").hide();

                if ($.browser.msie) {
                    if ($.browser.version == '6.0') { }
                    else { greybox.fadeTo(1, 0.7).show(); }
                } else { greybox.fadeTo(1, 0.7).show(); }

                __hideAllSildeBoxes(idx);
            }
        });
    });


    $("#slidecontainer>.slidebox").click(function() {
        var idx = $("#slidecontainer>.slidebox").index($(this));
        getURL(idx);
    });



    if ($.browser.msie) {
        if ($.browser.version != '6.0')
            _animate();
    } else { _animate(); }


    //var inter = setInterval(_animate,timeout);

    /* font size change functions */

    var fontSize = 12; //parseInt($("p").css("font-size"));
    if ($.cookie(COOKIE_NAME)) {
        fontSize = parseInt($.cookie(COOKIE_NAME));
        $("p").css("font-size", fontSize);
        $("div").css("font-size", fontSize);
    }

    $("img[id='inc']").click(function() {
        fontSize += 1;
        if (fontSize > 14) { fontSize = 14 };
        $("p").css("font-size", fontSize);
        $("div").css("font-size", fontSize);
        __saveCookie(fontSize);
    });

    $("img[id='dec']").click(function() {
        fontSize -= 1;
        if (fontSize < 11) fontSize = 11;
        $("p").css("font-size", fontSize);
        $("div").css("font-size", fontSize);
        __saveCookie(fontSize);
    });

    /* check the content of the page. if there is a picture puc a span underneath set value its alt element value. */
    $(".copy img").not(".testimonial-image").each(function() {

        var val = $(this).attr("alt");
        $(this).after("<br /><i>" + val + "</i>");

        try { // home page fix
            var color = $("#center h2").css("background-color");
            $(this).css("border-bottom", "solid 5px " + color);
        } catch (e) { }

    });
});


var id = 0;
var timeout = 6000;

function _animate() {


    if (animInterrupted) { $("#wrapper>div").show(); return true; }

    $("#" + slideboxes[id])
                 .fadeIn(timeout / 6)
                 .animate({ opacity: 1.0 }, timeout / 1)
                 .fadeOut(timeout / 6, function() {


                     //alert(selectedSlidebox);
                     if (animInterrupted) { if (selectedSlidebox == 0) { $("#" + slideboxes[id]).fadeIn(timeout / 6); } $("#wrapper>div").show(); return false; }
                     $(".slidebox>.hidden").hide();
                     $("#slidecontainer>.slidebox:eq(1)>.hidden").fadeTo(1, 0.7).show();
                     $("#slidecontainer>.slidebox:eq(1)>p.hide").show();

                     $("#" + slideboxes[id + 1])
                             .fadeIn(timeout / 6) //, function() { this.style.removeAttribute('filter'); }
                             .animate({ opacity: 1.0 }, timeout / 1)
                             .fadeOut(timeout / 6, function() {

                                if (animInterrupted) { $("#wrapper>div").show(); return; }
                                 $(".slidebox>.hidden").hide();
                                 $("#slidecontainer>.slidebox:eq(2)>.hidden").fadeTo(1, 0.7).show();
                                 $("#slidecontainer>.slidebox:eq(2)>p.hide").show();

                                 $("#" + slideboxes[id + 2])
                                         .fadeIn(timeout / 6)
                                         .animate({ opacity: 1.0 }, timeout / 1)
                                         .fadeOut(timeout / 6, function() {

                                             if (animInterrupted) { $("#wrapper>div").show(); return; }
                                             $(".slidebox>.hidden").hide();
                                             $("#slidecontainer>.slidebox:eq(3)>.hidden").fadeTo(1, 0.7).show();
                                             $("#slidecontainer>.slidebox:eq(3)>p.hide").show();

                                             $("#" + slideboxes[id + 3])
                                               .fadeIn(timeout / 6)
                                               .animate({ opacity: 1.0 }, timeout / 1)
                                               .fadeOut(timeout / 6, function() {

                                                   if (animInterrupted) { $("#wrapper>div").show(); return; }
                                                   $(".slidebox>.hidden").hide();
                                                   $("#slidecontainer>.slidebox:eq(4)>.hidden").fadeTo(1, 0.7).show();
                                                   $("#slidecontainer>.slidebox:eq(4)>p.hide").show();

                                                   $("#" + slideboxes[id + 4])
                                                       .fadeIn(timeout / 6)
                                                       .animate({ opacity: 1.0 }, timeout / 1)
                                                       .fadeOut(timeout / 6, function() {
                                                           //$("#slidecontainer>.slidebox:eq(0)").trigger("mouseover");
                                                           $(".slidebox>.hidden").hide();
                                                           $("#slidecontainer>.slidebox:eq(0)>.hidden").fadeTo(1, 0.7).show();
                                                           $("#slidecontainer>.slidebox:eq(0)>p.hide").show();
                                                           //alert("here");
                                                           $("#home").show();
                                                       });
                                               });
                                         });
                             });
                 });

}

var COOKIE_NAME = 'leightons_cookie';
var ADDITIONAL_COOKIE_NAME = 'leightons_additional';

function __saveCookie(fs){
    var date = new Date();
    date.setTime(date.getTime() + (3 * 24 * 60 * 60 * 1000));
    $.cookie(COOKIE_NAME, fs, { path: '/', expires: date });
}











//window.dhtmlHistory.create();
//var yourListener = function(newLocation, historyData) {
//    alert("ss");
//}

//window.onload = function() {
//        dhtmlHistory.initialize();
//        dhtmlHistory.addListener(yourListener);
//};
