﻿$(document).ready(function () {
    $('.topMenuFullItemArea').mouseenter(function () {
        $(this).find('.topMenuSubMenuArea').slideDown(200);
    }).mouseleave(function () {
        $(this).find('.topMenuSubMenuArea').slideUp(100);
    });
    $('.articleBoxControl').live('mouseenter', function () {
        $(this).addClass('articleBox_Hover');
        $(this).find(".articleBoxIconLinks").stop(true, true).fadeIn(400);
    }).live('mouseleave', function () {
        $(this).removeClass('articleBox_Hover');
        $(this).find('.articleBoxIconLinks').stop(true, true).fadeOut(200);
    });
    $('.blogBoxControl').live('mouseenter', function () {
        $(this).addClass('articleBox_Hover');
    }).live('mouseleave', function () {
        $(this).removeClass('articleBox_Hover');
    });
    $('#cartControl').live('mouseenter', cart_mouseEnter).live('mouseleave', cart_mouseLeave);
    $('.checkoutCartQuantityBox').change(function () {
        $(this).parent().find('.checkoutCartHiddenTextButton').click();
    });
    $('.deliveryText').live('click', function () {
        $(this).parent().find('.deliveryButton').click();
    });
    $('.paymentText').live('click', function () {
        $(this).parent().find('.paymentButton').click();
    });
    $('.acceptText').click(function () {
        $(this).parent().find('.acceptButton').click();
    });
    $('.conditionOpenLink1').click(function () {
        $('#registerConditions1').slideDown(1000);
    });
    $('.conditionCloseLink1').click(function () {
        $('#registerConditions1').slideUp(700);
    });
    $('.conditionOpenLink2').click(function () {
        $('#registerConditions2').slideDown(1000);
    });
    $('.conditionCloseLink2').click(function () {
        $('#registerConditions2').slideUp(700);
    });
    $('.buyConditionOpenLink1').click(function () {
        $('#buyConditions1').slideDown(1000);
    });
    $('.buyConditionOpenLink2').click(function () {
        $('#buyConditions2').slideDown(1000);
    });
    $('.buyConditionCloseLink1').click(function () {
        $('#buyConditions1').slideUp(700, function () { HideStep1Conditions(); });
    });
    $('.buyConditionCloseLink2').click(function () {
        $('#buyConditions2').slideUp(700, function () { HideStep1Conditions(); });
    });
    $('.allOrdersSummaryRow').mouseenter(function () {
        $(this).addClass('allOrdersSummaryRow_Hover');
    }).mouseleave(function () {
        $(this).removeClass('allOrdersSummaryRow_Hover');
    });
    $('.imgInformation').live('mouseenter', function () {
        var div = $('#divInfoPlate');
        div.stop(true, true);
        var position = $(this).position();
        var offset = $(this).offset();
        div.css("top", offset.top + 10).css("left", position.left + 50);
        $('#spanPaymentHeader').html($(this).find('.infoHeader').val());
        $('#spanPaymentText').html($(this).find('.infoText').val());
        div.show(400);
    }).live('mouseleave', function () {
        $('#divInfoPlate').stop(true, true).hide(200);
    });
    $('.expandLink').mouseenter(function () {
        $(this).parent().find('.expandDIV').stop(true, true).slideDown('fast');
    });
    $(".expandLink").parent().mouseleave(function () {
        $(this).find('.expandDIV').stop(true, true).slideUp();
    });
    $('.slideshowButtonLeft').mouseenter(function () {
        var div = $(this).parent().find(".slideShowSliderArea");
        if (div.position().left < 0) {
            $(this).attr('src', '/img/spots/slideshow-button-left-hover.png');
        }
    }).mouseleave(function () {
        $(this).attr('src', '/img/spots/slideshow-button-left.png');
    }).click(function () {
        var div = $(this).parent().find(".slideShowSliderArea");
        if (div.position().left < 0) {
            var sliderWidth = $(this).attr('sliderwidth');
            div.animate({ left: '+=' + sliderWidth + 'px' }, 400);
        }
    });
    $('.slideshowButtonRight').mouseenter(function () {
        var sliderObj = $(this).parent().find(".slideShowSliderArea");
        var sliderWidth = $(this).attr('sliderwidth');
        if ((sliderObj.position().left + sliderObj.width()) > sliderWidth) {
            $(this).attr('src', '/img/spots/slideshow-button-right-hover.png');
        }
    }).mouseleave(function () {
        $(this).attr('src', '/img/spots/slideshow-button-right.png');
    }).click(function () {
        var sliderObj = $(this).parent().find(".slideShowSliderArea");
        var sliderWidth = $(this).attr('sliderwidth');
        if ((sliderObj.position().left + sliderObj.width()) > sliderWidth) {
            sliderObj.animate({ left: '-=' + sliderWidth + 'px' }, 400);
        }
    });

    $('.smallSlideshowButtonLeft').mouseenter(function () {
        var div = $(this).parent().find(".slideShowSliderArea");
        if (div.position().left < 0) {
            $(this).attr('src', '/img/articles/small-slideshow-button-left-hover.png');
        }
    }).mouseleave(function () {
        $(this).attr('src', '/img/articles/small-slideshow-button-left.png');
    }).click(function () {
        var div = $(this).parent().find(".slideShowSliderArea");
        if (div.position().left < 0) {
            var sliderWidth = $(this).attr('sliderwidth');
            div.animate({ left: '+=' + sliderWidth + 'px' }, 400);
        }
    });
    $('.smallSlideshowButtonRight').mouseenter(function () {
        var sliderObj = $(this).parent().find(".slideShowSliderArea");
        var sliderWidth = $(this).attr('sliderwidth');
        if ((sliderObj.position().left + sliderObj.width()) > sliderWidth) {
            $(this).attr('src', '/img/articles/small-slideshow-button-right-hover.png');
        }
    }).mouseleave(function () {
        $(this).attr('src', '/img/articles/small-slideshow-button-right.png');
    }).click(function () {
        var sliderObj = $(this).parent().find(".slideShowSliderArea");
        var sliderWidth = $(this).attr('sliderwidth');
        if ((sliderObj.position().left + sliderObj.width()) > sliderWidth) {
            sliderObj.animate({ left: '-=' + sliderWidth + 'px' }, 400);
        }
    });
    SetSystemMessageToLogin();
});

function SetSystemMessageToLogin() {
    $('#loginPopupSystemMessage').html($('#pSystemMessage').html());
}

function changeStoreMapView() {
    $('#contentdiv').fadeToggle();
}
function toggleCart() {
    $('#cartSummaryTable').slideToggle();
}
function ShowLoginWindow() {
    var loginWin = document.getElementById('loginControl');
    loginWin.style.display = 'block';
    try { SetLoginBoxFocus(); } catch (e) { }
}
function CloseLoginWindow() {
    var loginDarkWin = document.getElementById('loginControl');
    loginDarkWin.style.display = 'none';
   }

function ShowRegisterWindow() {
   	var registerWin = document.getElementById('registerControl');
   	registerWin.style.display = 'block';
}
function ShowForgottenPasswordWindow() {
    var registerWin = document.getElementById('forgottenPasswordControl');
    registerWin.style.display = 'block';
}
function ShowOrderCatalogueWindow() {
	var registerWin = document.getElementById('orderCatalogueControl');
	registerWin.style.display = 'block';
}
function CloseRegisterWindow() {
    var registerDarkWin = document.getElementById('registerControl');
    registerDarkWin.style.display = 'none';
}
function CloseForgottenPasswordWindow() {
    var registerDarkWin = document.getElementById('forgottenPasswordControl');
    registerDarkWin.style.display = 'none';
}
function CloseOrderCatalogueWindow() {
    var registerDarkWin = document.getElementById('orderCatalogueControl');
    registerDarkWin.style.display = 'none';
}
function ShowUpdateCustomerWindow() {
    var registerWin = document.getElementById('updateUserControl');
    registerWin.style.display = 'block';
}
function ShowPasswordChangeWindow() {
    var registerWin = document.getElementById('passwordChangeControl');
    registerWin.style.display = 'block';
   }
function ShowStep1Conditions() {
    var conditionWin = document.getElementById('conditionControl');
    conditionWin.style.display = 'block';
}
function HideStep1Conditions() {
	var conditionWin = document.getElementById('conditionControl');	
	conditionWin.style.display = 'none';
}

function printSelection(node) {
	var content = node.innerHTML
	var pwin = window.open('', 'print_content', 'width=400,height=600');

	pwin.document.open();
	pwin.document.write('<html><body onload="window.print()">' + content + '</body></html>');
	pwin.document.close();
	//setTimeout(function () { pwin.close(); }, 1000);
}

function cart_mouseEnter() {
    var div = $('#cartMovableArea');
    div.stop();
    div.animate({
        bottom: (-div.find(".cartHiddenItemArea").height()) + 'px'
    }, 200);
}
function cart_mouseLeave() {
    var div = $('#cartMovableArea');
    div.stop();
    div.animate({
        bottom: '0px'
    }, 500, 'easeOutQuart');
}

function showAlert(message) {
    alert(message);
}
function showAttributes(index) {
    $('.articleSearchFilterAttributeValuesArea').hide();
    var attributeDiv = $('#articleSearchFilterAttribute_' + index);
    var valueDiv = $('#articleSearchFilterAttributeValuesArea_' + index);
    valueDiv.css("top", attributeDiv.position().top + attributeDiv.height() - 15).css("left", attributeDiv.position().left + (attributeDiv.width() / 2) - (valueDiv.width() / 2));
    valueDiv.slideDown(300);
}
function hideAttributes(index) {
    $('#articleSearchFilterAttributeValuesArea_' + index).hide();
}

//START-Functions for updating stock symbol in product listing

function ApplicationLoadHandler() {
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    if (!prm.get_isInAsyncPostBack()) {
        Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoaded);
    }
    Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(beginRequest);
}

var postbackElement;
var articles;
function beginRequest(sender, args) {
    postbackElement = args.get_postBackElement();
}

function pageLoaded(sender, args) {
    if ((typeof (postbackElement) == "undefined") || (postbackElement.id.indexOf("lnbPageLinkButton") > -1)) {
        GetArticlesOnPage();
        UpdateStockData(articles, 'context');
    }
}

function GetArticlesOnPage() {
    articles = '';
    var box = $('.articleBoxControl');
    for (var i = 0; i < box.length; i++) {
        var articleNumber = box[i].id.split('_')[1];
        var boxId = box[i].id;
        if (articles.length == 0) {
            articles = articleNumber;
        } else {
            articles = articles + ';' + articleNumber;
        }
    }
}

function ServerCallSucceeded(result, context) {
    var list = result.split(';')
    for (var i = 0; i < list.length; i++) {
        var articleDiv = document.getElementById('articleBox_' + list[i].split('@')[0]);
        var images = articleDiv.getElementsByTagName('img');
        for (var r = 0; r < images.length; r++) {
            if (images[r].id.indexOf('imgStockSymbol') > -1) {
                images[r].src = list[i].split('@')[1];
                images[r].title = GetStockText(list[i].split('@')[1]);
            }
        }
    }
}

function GetStockText(img) {
    if (img.search('InStock') != -1) {
        return InStock;
    }
    if (img.search('LowOnStock') != -1) {
        return LowOnStock;
    }
    if (img.search('SoldOut') != -1) {
        return SoldOut;
    }
    return '';
}

function ServerCallFailed(result, context) {
}
//END-Functions for updating stock symbol in product listing

//START-Functions for Checkout Step1
function showInfoText(button2) {
    var header = document.getElementById('spanPaymentHeader');
    var text = document.getElementById('spanPaymentText');
    var butn = document.getElementById(button2.id);
    var thePayType = butn.getAttribute('paymType');
    header.innerHTML = eval('H' + thePayType + '_header');
    text.innerHTML = eval('T' + thePayType + '_text');
}

function showChosenOptionText() {
    document.getElementById('spanPaymentHeader').innerHTML = defaultPaymentHeader;
    document.getElementById('spanPaymentText').innerHTML = defaultPaymentText;
}

function SetDefaultText(thePType) {
    defaultPaymentHeader = eval('H' + thePType + '_header');
    defaultPaymentText = eval('T' + thePType + '_text');
    showChosenOptionText();
}
function initTexts() {
    showChosenOptionText();
}
//END-Functions for Checkout Step1


function showBuyAction(control) {
    try {
        //$(control).parent().parent().parent().parent().find('.articleBoxLabelAddToCart').fadeIn(400);
        //$(control).parent().find(".articleBoxIconLinksAddToCart").val("Lägger i varukorgen");
        moveImageToCart(control, '.articleBoxImage', 140, 140, 500);
        $(control).fadeOut(400, function () {
            $(control).fadeIn(400);
        });
    } catch (e) { }
}

function moveImageToCart(control, imageClass, width, height, duration) {
    var articleImage = $(control).parent().parent().find(imageClass);
    var offset = $(articleImage).offset();
    var newId = $(control).attr('id') + "_m";

    //Duplicate article image
    $('body').append($('<img>', {
        src: $(articleImage).attr("src"),
        width: width,
        height: height,
        id: newId
    }));
            
    $("#" + newId).addClass('articleMoveImageToCart');
    $("#" + newId).css("top", offset.top);
    $("#" + newId).css("left", offset.left);
    $("#" + newId).css("opacity", 1);

    var speed = offset.top / 250;
    //Move image to cart
    var offsetCart = $(".cartVisibleTopArea").offset();
    $("#" + newId).animate({ "top": offsetCart.top + "px", "left": offsetCart.left + 60 + "px", opacity: 0 }, duration * speed, function () {
        $("#" + newId).remove();
    });    
}

function flashCartUpdate() {

    $('.cartSummaryTotalRight').css('color', '#ff0000');
        $('.cartSummaryTotalRight').fadeOut(200, function () {
            $('.cartSummaryTotalRight').fadeIn(200);
            $('.cartSummaryTotalRight').css('color', '#000000');
        });
        try { } catch (e) { }
}

function LoadStars() {
    $("#stars-wrapper2").stars({
        inputType: "select",
        cancelShow: false,
        callback: function (ui, type, value) {
            $('#Article_Grade').val($('#stars-wrapper2').data('stars').options.value);
        }
    });
}
