﻿(function(d) { d.each(["backgroundColor", "borderBottomColor", "borderLeftColor", "borderRightColor", "borderTopColor", "color", "outlineColor"], function(f, e) { d.fx.step[e] = function(g) { if (!g.colorInit) { g.start = c(g.elem, e); g.end = b(g.end); g.colorInit = true } g.elem.style[e] = "rgb(" + [Math.max(Math.min(parseInt((g.pos * (g.end[0] - g.start[0])) + g.start[0]), 255), 0), Math.max(Math.min(parseInt((g.pos * (g.end[1] - g.start[1])) + g.start[1]), 255), 0), Math.max(Math.min(parseInt((g.pos * (g.end[2] - g.start[2])) + g.start[2]), 255), 0)].join(",") + ")" } }); function b(f) { var e; if (f && f.constructor == Array && f.length == 3) { return f } if (e = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(f)) { return [parseInt(e[1]), parseInt(e[2]), parseInt(e[3])] } if (e = /rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(f)) { return [parseFloat(e[1]) * 2.55, parseFloat(e[2]) * 2.55, parseFloat(e[3]) * 2.55] } if (e = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(f)) { return [parseInt(e[1], 16), parseInt(e[2], 16), parseInt(e[3], 16)] } if (e = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(f)) { return [parseInt(e[1] + e[1], 16), parseInt(e[2] + e[2], 16), parseInt(e[3] + e[3], 16)] } if (e = /rgba\(0, 0, 0, 0\)/.exec(f)) { return a.transparent } return a[d.trim(f).toLowerCase()] } function c(g, e) { var f; do { f = d.curCSS(g, e); if (f != "" && f != "transparent" || d.nodeName(g, "body")) { break } e = "backgroundColor" } while (g = g.parentNode); return b(f) } var a = { aqua: [0, 255, 255], azure: [240, 255, 255], beige: [245, 245, 220], black: [0, 0, 0], blue: [0, 0, 255], brown: [165, 42, 42], cyan: [0, 255, 255], darkblue: [0, 0, 139], darkcyan: [0, 139, 139], darkgrey: [169, 169, 169], darkgreen: [0, 100, 0], darkkhaki: [189, 183, 107], darkmagenta: [139, 0, 139], darkolivegreen: [85, 107, 47], darkorange: [255, 140, 0], darkorchid: [153, 50, 204], darkred: [139, 0, 0], darksalmon: [233, 150, 122], darkviolet: [148, 0, 211], fuchsia: [255, 0, 255], gold: [255, 215, 0], green: [0, 128, 0], indigo: [75, 0, 130], khaki: [240, 230, 140], lightblue: [173, 216, 230], lightcyan: [224, 255, 255], lightgreen: [144, 238, 144], lightgrey: [211, 211, 211], lightpink: [255, 182, 193], lightyellow: [255, 255, 224], lime: [0, 255, 0], magenta: [255, 0, 255], maroon: [128, 0, 0], navy: [0, 0, 128], olive: [128, 128, 0], orange: [255, 165, 0], pink: [255, 192, 203], purple: [128, 0, 128], violet: [128, 0, 128], red: [255, 0, 0], silver: [192, 192, 192], white: [255, 255, 255], yellow: [255, 255, 0], transparent: [255, 255, 255]} })(jQuery);

(function($) {
    VVSInt = {};

    VVSInt.Login = {
    storeURL: 'https://webshop.ahlsell.com/vivp/Login.do?SCM=A209381B421948E218F12984A1239487E12439789123789FF23FFAACCEASd981241CCAE209841FAAECAE986712873161ADEFCEAF2498167249867239478ADE&actionType=authenticate&failureURL=/vivp/index.jsp&language=sv&country=SE&mobileView=false&removeSessionsForUser=true',
        submitAction: null,

        init: function() {
            this.checkValue($("#store-login input.login-text"));
            this.setBehavior();
        },

        setBehavior: function() {
            var instance = this;
            $("#store-login input.login-text").bind('blur', function() {
                instance.checkValue(this);
            });
            $("#store-login input.login-text").bind('focusin', function() {
                $(this).addClass('focus').css({ 'background-position': '0 -69px' });
                $("#store-info").removeClass("hidden");
            }).bind('focusout', function() {
                $(this).removeClass('focus').removeAttr('style'); //.css({ 'background-position': '' });
                $("#store-info").addClass("hidden");
            }); ;
        },

        checkValue: function(element) {
            if ($(element).val().length > 0)
                $(element).addClass("value").css({ 'background-position': '0 -46px' });
            else
                $(element).removeClass("value").removeAttr('style'); //.css({ 'background-position': '' });
        },

        submitLogin: function() {
            var myWindow;
            var tScreenResolution = "";

            if (screen.availWidth > 1200 && screen.availHeight > 850) {
                tScreenResolution = "1280x1024";

            } else {
                tScreenResolution = "1024x768";

            }
            myWindow = window.open(this.storeURL + '&userName=' + $("#store-login .login-name").val() + '&password=' + $("#store-login .login-psw").val() + '&screenResolution=' + tScreenResolution + '&webshopid=102', 'mywindow', this.winPrm());
            $("#store-login .login-psw").val('');
            document.forms[0].action = this.submitAction;
            myWindow.focus();
        },

        loginUser: function(check, str) {
            var doLogin = false;
            if (!check) {
                doLogin = true;
            }
            if (doLogin && $("#store-login .login-name").val().length != 0 && $("#store-login .login-psw").val().length != 0) {
                this.submitAction = document.forms[0].action;
                document.forms[0].action = 'javascript:VVSInt.Login.submitLogin();';
            }
            return true;
        },


        winPrm: function() {
            var ret = 'toolbar=no,titlebar=no,location=no,menubar=no,status=no,left=0,top=0,resizable=yes,screenX=0,screenY=0,scrollbars=yes';
            var tScreenResolution = "";
            var popupWidth;
            var popupHeight;

            if (screen.availWidth > 1200 && screen.availHeight > 850) {
                tScreenResolution = "1280x1024";
                popupWidth = "1270";
                popupHeight = "946";
            } else {
                tScreenResolution = "1024x768";
                popupWidth = "1014";
                popupHeight = "730";
            }
            ret += ',width=' + popupWidth + ',height=' + popupHeight;
            return ret;
        }
    };

    VVSInt.Products = {
        modalID: null,
        closeAction: 0,

        init: function() {
            $("#product-list ul li input").change(function() {
                //alert($(this).is(":checked") + " - " + $(this).attr('value'));
                var id = $(this).attr("value");
                var checked = $(this).is(":checked");

                VVSInt.Products.updateSelection(id, checked, '1',
                    function(msg, status) {
                        $("#cart").attr('class', 'show').find('#cart-info').html('Din beställning (' + msg.d.orderCount + ')');
                    });
            });

            $("#cart, .order-button").click(function(e) {
                e.preventDefault();
                VVSInt.Products.loadCart();
                return false;
            });


            $("#product-list ul li a.box").colorbox({
                speed: 100,
                initialWidth: 400,
                initialHeight: 100,
                slideshow: false,
                current: '',
                opacity: 0.45,
                onLoad: function() { modalID = $(this).prev().attr("value"); },
                onComplete: VVSInt.Products.registerCartEvents,
                onCleanup: VVSInt.Products.updateTable
            });
        },

        submitOrder: function() {
            if (VVSInt.Products.closeAction == 1)
                $("#ctl00_MainContentHolder_OrderSubmitButton").click();
        },

        loadCart: function() {
            Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(VVSInt.Products.showCart);
            __doPostBack('ctl00_MainContentHolder_Panel1', '');
        },

        registerCartEvents: function() {
            $("#product-info #order-button").click(function(e) {
                //alert($(this).is(":checked") + " - " + $(this).attr('value'));
                var ids = '';
                var checked = '';
                var amounts = '';
                $("#product-info ul li input:checkbox").each(function() {
                    ids += $(this).attr("value") + ';';
                    checked += $(this).is(":checked") + ';';
                    amounts += $(this).parent().find("input:text").attr("value") + ';';
                });
                ids = ids.substr(0, ids.length - 1);
                checked = checked.substr(0, checked.length - 1);
                amounts = amounts.substr(0, amounts.length - 1);
                VVSInt.Products.updateSelection(ids, checked, amounts,
                    function(msg, status) {
                        $("#cart").attr('class', 'show').find('#cart-info').html('Din beställning (' + msg.d.orderCount + ')');
                    });
                $.colorbox.close();
            });
        },

        showCart: function() {
            $.colorbox({
                speed: 250,
                initialWidth: 400,
                initialHeight: 100,
                width: 600,
                inline: true,
                opacity: 0.45,
                href: "#order-form",
                onClosed: VVSInt.Products.submitOrder
            });
            $("#order-form .remove a").click(function(e) {
                var elem = this;
                var id = $(this).attr('rel');
                $(this).parent().parent().find('td').animate({ backgroundColor: '#cccccc' }, 150, function() {
                    VVSInt.Products.updateSelection(id, 'false', '0',
                        function(msg, status) {
                            $(elem).parent().parent().hide();
                            VVSInt.Products.updateTableSelection(msg.d.remove);
                            $("#cart").attr('class', 'show').find('#cart-info').html('Din beställning (' + msg.d.orderCount + ')');
                            if ($("#order-form table tr:visible").length > 1)
                                $.colorbox.resize();
                            else
                                $.colorbox.close();
                        })
                });
            });

            $("#colorbox #submit-order").click(function(evt) {
                var valid = true;
                $("#form-fields input:text").each(function(e) {
                    if ($(this).val() == '') {
                        valid = false;
                        $(this).parent().addClass('invalid');
                    }
                    else
                        $(this).parent().removeClass('invalid');
                });
                if (!valid) {
                    evt.preventDefault();
                    return false;
                }
                evt.preventDefault();
                var ids = '';
                var checked = '';
                var amounts = '';
                $("#order-form table tr.order-item").each(function(e) {
                    ids += $(this).find("input:hidden").attr("value") + ';';
                    checked += true + ';';
                    amounts += $(this).find(".amount input:text").attr("value") + ';';
                });
                ids = ids.substr(0, ids.length - 1);
                checked = checked.substr(0, checked.length - 1);
                amounts = amounts.substr(0, amounts.length - 1);
                VVSInt.Products.updateSelection(ids, checked, amounts,
                    function(msg, status) {
                        VVSInt.Products.closeAction = 1;
                        $.colorbox.close();
                    });
                return false;
            });
        },

        updateTable: function() {
            var checked = $("#product-info ul li input:checked");
            $("#product-list ul li input[value='" + modalID + "']").attr('checked', checked.length > 0);
        },

        updateSelection: function(ids, checked, amounts, successCallback) {
            jQuery.ajax({
                type: "POST",
                url: "/Templates/Public/WebServices/AjaxService.asmx/RegisterAmountSelection",
                data: "{'pageIds':'" + ids + "','isChecked':'" + checked + "','amounts':'" + amounts + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                processData: false,
                cache: false,
                success: successCallback,
                error: function(xhr, msg, e) {
                    alert(msg + ', ' + e); //Error Callback
                }
            });
        },

        updateTableSelection: function(id) {
            if (id != null)
                $("#product-list ul li input[value='" + id + "']").attr('checked', '');
        }
    };

    VVSInt.Scrollable = {
        init: function() {
            $("#slider-image .scrollable").scrollable({ circular: true, speed: 800 }).autoscroll(5000).navigator();
            $("#contact .scrollable").scrollable({ circular: false, speed: 800 }).autoscroll(5000);
        }
    };

    $(document).ready(function() {
        VVSInt.Login.init();
        VVSInt.Products.init();
        VVSInt.Scrollable.init();
    });
})(jQuery);
