﻿jQuery(function($) {
    $("input[value='disabled']").each(function() {
        $(this).parent().parent().hide();
    });

    $("[name='HideThisSection'] :checkbox:not(:checked)").map(function() {
        $(this).parent().parent().nextAll("div").each(function() {
            $(this).find("input[id$='_ecstate']").each(function() {
                $(this).parent().parent().hide();
                $(this).val("disabled");
            });
        });
    });

    $("[name='HideThisSection'] input").click(function() {
        var checked = $(this).attr("checked");
        $(this).parent().parent().nextAll("div").each(function() {
            $(this).find("input[id$='_ecstate']").each(function() {
                if (!checked) {
                    $(this).parent().parent().hide();
                    $(this).val("disabled");
                } else {
                    $(this).parent().parent().show();
                    $(this).val("");
                }
            });
        });
    });


    $("input").keypress(function(e) {
        var isFirstmatch = true;
        if (e.keyCode == 13) {
            $(this).parents().each(function() {
                if (isFirstmatch) {
                    $(this).find("input[type='submit'],input[type='image']").map(function() {
                        isFirstmatch = false;
                        $(this).trigger('click');
                        return false;
                    });
                }
            });
            return false;
        }
    });

    $j(".modalinfo").click(function() {
        var maxWidth = 800;
        var maxHeight = 800;

        if ($j("#modalinfo-overlay").html() == null) {
            $j(this).after("<div id='modalinfo-overlay'></div><div id='modalinfo-content'></div>");
        }

        var w = ($j(window).width() < maxWidth) ? $j(window).width() * 0.7 : maxWidth;
        var h = ($j(window).height() < maxHeight) ? $j(window).height() * 0.7 : maxHeight;
        var lPos = (($j(document).width() - w) / 2); // +$(document).scrollLeft();
        var tPos = (($j(window).height() - h) / 2) + $j(document).scrollTop();

        $j("#modalinfo-overlay").css({ 'position': 'absolute', 'top': '0px', 'left': '0px', 'z-index': '0', 'background': '#000' });
        $j("#modalinfo-overlay").css({ width: $j(document).width() + 'px', height: $j(document).height() + 'px', opacity: 0.7 }).show(); //.fadeIn();

        $j("#modalinfo-content").css({ position: 'absolute', top: '200px', left: '200px', display: 'none', width: '600px', 'z-index': '1', background: '#fff', padding: '10px', overflow: 'auto' });
        $j("#modalinfo-content").css({ 'left': lPos + 'px', 'width': w + 'px', 'top': tPos + 'px', 'height': h + 'px', 'background': '#fff' }).fadeIn();

        var href = $j(this).attr("href").replace(/\s/g, '%20');
        $j("#modalinfo-content").load(href + ' .content');

        $j("#modalinfo-overlay").click(function() {
            $j(this).fadeOut();
            $j("#modalinfo-content").fadeOut();
        });

        $j(window).resize(function() {
            $j("#modalinfo-overlay:visible").map(function() {
                $j(".modalinfo").trigger("click");
            });
        });
        return false;
    });

    /* Product menu */

    var container = $(".product-menu-cols");

    $("div.product-menu-col4 dt", container).each(function() {

        $(this)
          .click(function() { productMenuSetActive($(this)); })
          .mouseover(function() { $(this).css("cursor", "pointer"); });
    });

    /* qTip tooltip */
    $('a.contentLink').each(function() {

        $(this).qtip
        (
            {
                content: $(this).find('.hiddenContent:first').html(),
                position: {
                    corner: {
                        target: 'rightMiddle',
                        tooltip: 'leftTop'
                    }
                },
                show: 'mouseover',
                hide: 'mouseout',
                style: {
                    width: 300,
                    height: 150,
                    padding: 5,
                    border: {
                        width: 1,
                        color: '#305d82'
                    },
                    tip: 'leftTop'
                }
            }
        );
    });

    $("#sokeknapp").click(function() {
        location.href = "/Hovedmeny/Funksjoner/Sok.aspx?s=" + $("#sokeboks").attr("value");
        return false;
    });
});

function productMenuSetActive(obj) {
    var aObj = $(".product-menu-col4-active");
    $(aObj).each(function() {
        changeClass($(this), 'product-menu-col4-active', 'product-menu-col4');
    });

    var parent = $(obj).parent().parent();
    changeClass(parent, 'product-menu-col4', 'product-menu-col4-active');
}

function changeClass(obj, class1, class2) {
    if ($(obj).hasClass(class1)) {
        remove = class1;
        add = class2;
    } else {
        remove = class2;
        add = class1;
    }
    $(obj).removeClass(remove);
    $(obj).addClass(add);
};

function SwapSection(divout, divin) {
    jQuery('#section_' + divin).show();
    jQuery('#section_' + divout).hide();
}

function loadnext(divout, divin) {
    jQuery("." + divin).show();
    jQuery("." + divout).hide();
    jQuery('#buttons').insertBefore('.scfSubmitButtonBorder');
    SwapSection(divout, divin);
}

function LoadRendering(rendering, placeholder) {
    var id = jQuery("#scID").attr("content");

    jQuery.ajax({
        type: "POST",
        url: "/layouts/ecommerce/Ajax.aspx/LoadRendering",
        data: "{rendering:'" + rendering + "', id : '" + id + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            // Replace the div's content with the page method's return.
            placeholder.html(msg.d);
        }
    });
}

function LoadSublayout(sublayout, placeholder, callback) {
    var id = jQuery("#scID").attr("content");

    jQuery.ajax({
        type: "POST",
        url: "/layouts/ecommerce/Ajax.aspx/LoadSublayout",
        data: "{sublayout:'" + sublayout + "', id : '" + id + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: (callback) ? callback : function(msg) {
            placeholder.html(msg.d);
        }
    });
}


function SetCurrentTab(tab) {
    jQuery.ajax({
        type: "POST",
        url: "/layouts/ecommerce/Ajax.aspx/SetCurrentTab",
        data: "{tab:'" + tab + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    });
}


function AddReview(title,text,rate) {
    var id = jQuery("#scID").attr("content");
    jQuery.ajax({
        type: "POST",
        url: "/layouts/ecommerce/Ajax.aspx/AddReview",
        data: "{title:'" + title + "', text :'" + text + "', rate:'" + rate + "', id : '" + id + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    });
}


function AddToBasket(productId, quantity, uom) {
    jQuery.ajax({
        type: "POST",
        url: "/layouts/ecommerce/Ajax.aspx/AddToBasket",
        data: "{'productId':'" + productId + "', 'quantity':'" + quantity + "', 'uom' :'" + uom + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {

            // Replace the div's content with the page method's return.
            LoadSublayout("Ecommerce/UserControls/SmallBasket", null, function(msg) {
                jQuery("#small_basket_container").animate({ opacity: 0.3 }, 200).replaceWith(msg.d);
                jQuery("#small_basket_container").css({ opacity: 0.3 }).animate({ opacity: 1 }, 600);
            });
            window.location.replace(window.location);

        }
        , error: function(msg) {
            window.location.replace(window.location);
        }

    }); 
}



function DeleteFromBasket(productId) {
    //alert(productId);
    jQuery.ajax({
        type: "POST",
        url: "/layouts/Ecommerce/Ajax.aspx/DeleteFromBasket",
        data: "{productId:'" + productId  + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            // Replace the div's content with the page method's return.
            LoadSublayout("Ecommerce/UserControls/SmallBasket", null, function(msg) {
                jQuery("#small_basket_container").animate({ opacity: 0.3 }, 200).replaceWith(msg.d);
                jQuery("#small_basket_container").css({ opacity: 0.3 }).animate({ opacity: 1 }, 600);
            });
            window.location.replace(window.location);
        }
    });
}

function UpdateBasket(productId, quantity, uom) {
    jQuery.ajax({
        type: "POST",
        url: "/layouts/ecommerce/Ajax.aspx/UpdateBasket",
        data: "{productId:'" + productId + "' quantity:'" + quantity + "', uom :'" + uom + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    });
}


