﻿var currentUrl = window.location.toString();

// usage: log('inside coolFunc',this,arguments);
// http://paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function () {
    log.history = log.history || [];   // store logs to an array for reference
    log.history.push(arguments);
    if (this.console) {
        console.log(Array.prototype.slice.call(arguments));
    }
};


// COOKIE FUNCTIONS
function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else {
        var expires = "";
    }
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

var catCookie = readCookie("nobleArtCatCookie");

function checkRootCategoriesToShow() {
    // alert(catCookie);
    if (catCookie != null) {
        if (catCookie == "Profilklader") {
            $(".ProductTreeRoot1").show();
        }
        else if (catCookie == "Presentreklam") {
            $(".ProductTreeRoot2").show();
        }
        else if (catCookie == "Arbetsklader") {
            $(".ProductTreeRoot3").show();
        }
        else if (catCookie == "SportFritid") {
            $(".ProductTreeRoot4").show();
        }
    }
}
checkRootCategoriesToShow();


// FANCYBOX LINK CONTENT
function initializeFancybox() {
    $("iframe.LightboxContent").each(function () {
        $(this).load(function () {
            if (!$(this).hasClass("UrlChanged")) {
                var thisSrc = $(this).attr("src");
                $(this).attr("src", thisSrc + "?pageUrl=" + escape(currentUrl));
                $(this).addClass("UrlChanged");
            }
        });
    });

    $(".Lightbox").fancybox({
        'padding': 20
    });
    $(".LightboxContent").wrap("<div style='display:none'></div>");

}

// CHECK IF TOP CATEGORY IS CLOSED (IMMEDIATELY)
if ($(".LeftArea").length && !$(".ProductCategoryLevel2").length && catCookie != null && currentUrl.match(/-c-/i) != null) {
    window.location.reload();
}


// REDIRECT TO SUBCATEGORY (IMMEDIATELY)
function checkRedirect() {
    var hash = location.hash;
    var redirect = hash.replace("#_redirect_", "");
    if (hash.match("_redirect_")) {
        location.replace(location.protocol + "//" + location.hostname + redirect);
        //log(location.protocol + "//" + location.hostname + "/" + redirect);
    }
}
checkRedirect();

// SCRIPTS TO BE RUN AT STARTUP BELOW -----
$(document).ready(function () {
    initializeFancybox();
});




