﻿jQuery(function (e, i) { function j() { var a = e("script:first"), b = a.css("color"), c = false; if (/^rgba/.test(b)) c = true; else try { c = b != a.css("color", "rgba(0, 0, 0, 0.5)").css("color"); a.css("color", b) } catch (d) { } return c } function k(a, b, c) { var d = []; a.c && d.push("inset"); typeof b.left != "undefined" && d.push(parseInt(a.left + c * (b.left - a.left), 10) + "px " + parseInt(a.top + c * (b.top - a.top), 10) + "px"); typeof b.blur != "undefined" && d.push(parseInt(a.blur + c * (b.blur - a.blur), 10) + "px"); typeof b.a != "undefined" && d.push(parseInt(a.a + c * (b.a - a.a), 10) + "px"); if (typeof b.color != "undefined") { var g = "rgb" + (e.support.rgba ? "a" : "") + "(" + parseInt(a.color[0] + c * (b.color[0] - a.color[0]), 10) + "," + parseInt(a.color[1] + c * (b.color[1] - a.color[1]), 10) + "," + parseInt(a.color[2] + c * (b.color[2] - a.color[2]), 10); if (e.support.rgba) g += "," + parseFloat(a.color[3] + c * (b.color[3] - a.color[3])); g += ")"; d.push(g) } return d.join(" ") } function h(a) {var b, c, d = {}; if (b = /#([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})/.exec(a)) c = [parseInt(b[1], 16), parseInt(b[2], 16), parseInt(b[3],16), 1]; else if (b = /#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])/.exec(a)) c = [parseInt(b[1], 16) * 17, parseInt(b[2], 16) * 17, parseInt(b[3], 16) * 17, 1]; else if (b = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(a)) c = [parseInt(b[1], 10), parseInt(b[2], 10), parseInt(b[3], 10), 1]; else if (b = /rgba\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9\.]*)\s*\)/.exec(a)) c = [parseInt(b[1], 10), parseInt(b[2], 10), parseInt(b[3], 10), parseFloat(b[4])]; d = (b = /(-?[0-9]+)(?:px)?\s+(-?[0-9]+)(?:px)?(?:\s+(-?[0-9]+)(?:px)?)?(?:\s+(-?[0-9]+)(?:px)?)?/.exec(a)) ? { left: parseInt(b[1], 10), top: parseInt(b[2], 10), blur: b[3] ? parseInt(b[3], 10) : 0, a: b[4] ? parseInt(b[4], 10) : 0} : { left: 0, top: 0, blur: 0, a: 0 }; d.c = /inset/.test(a); d.color = c; return d} e.extend(true, e, { support: { rgba: j()} }); var f; e.each(["boxShadow", "MozBoxShadow", "WebkitBoxShadow"], function (a, b) { a = e("html").css(b); if (typeof a == "string" && a != "") { f = b; return false } }); if (f) e.fx.step.boxShadow = function (a) {if (!a.init) {a.b = h(e(a.elem).get(0).style[f] || e(a.elem).css(f)); a.end = e.extend({}, a.b, h(a.end)); if (a.b.color == i) a.b.color = a.end.color || [0, 0, 0]; a.init = true } a.elem.style[f] = k(a.b, a.end, a.pos)}}); function MessageBox(message) {var item = $("<div />"); item.html(message); item.hide(); item.appendTo("body"); $("#dialog:ui-dialog").dialog("destroy"); item.dialog({ modal: true, buttons: { Ok: function () { $(this).dialog("close"); } } }); return item; }

$(function () {
    $.GoTo = function (path, redirect) { window.location.href = $.Path + path + (redirect ? "?ReturnUrl=" + encodeURI(window.location.href) : ""); }
    $.fn.radius = function (value) {
        $(this).each(function () {
            this.style.borderRadius = value;
            this.style.MozBorderRadius = value;
            this.style.WebkitBorderRadius = value;
        });
    }
    $.fn.popup = function () {
        return $(this).each(function () {
            return $(this).overlay({
                top: 160,
                mask: { color: '#fff', loadSpeed: 200, opacity: 0.5 },
                closeOnClick: true, oneInstance: true
            });
        });
    }
    var offsetMethod = $.fn.offset;
    $.fn.offset = function () {
        var offset = offsetMethod.call(this),
            bottom = parseInt($(window).height()) - offset.top + this.outerHeight(),
            right = parseInt($(window).width()) - (offset.left + this.outerWidth()),
            a = arguments;
        return (a.length) ? this.animate({ top: a[0].top || a[0], left: a[0].left || a[1] },
                                         (a[0].top ? a[1] : a[2]) || 1) : $.extend(offset, { bottom: bottom, right: right });
    };

    $.tools.validator.fn("[equals]", "Value not equal with the $1 field", function (input) {
        var name = input.attr("equals"),
		 field = this.getInputs().filter("[name=" + name + "]");
        return input.val() == field.val() ? true : [name];
    });
    $.tools.validator.fn("[minlength]", function (input, value) {
        var min = input.attr("minlength");
        return value.length >= min ? true : {
            en: "Please provide at least " + min + " character" + (min > 1 ? "s" : ""),
            fi: "Kentän minimipituus on " + min + " merkkiä"
        };
    });

    $("form.AcceptNewFriends button").click(function () {
        var me = $(this); var form = me.parent();
        form.find("button").prop("disabled", true);
        me.addClass("isLoading");
        $.post(form.attr("action"), "IsAjax=true&" + me.attr("name") + "=" + me.attr("value"), function (data) {
            var spn = form.parents(".IconExpandMenu").find("> a > span"); var num = parseInt(spn.text()) - 1;
            if (num < 1) spn.remove();
            else spn.text(num);
            form.remove();
        });
        return false;
    });


    $("a.AddToWishlist").click(function () {
        if (!$.IsAuth) { $.GoTo('Account/LogOn', true); return; }
        $(this).addClass("AddedToList");
        $.post($.Path + "Library/AddToWishlist", 'id=' + $(this).attr('id'), function () { });
        return false;
    });

    $.tools.tabs.addEffect("CustomMenuSlide", function (i, done) {
        var panels = this.getPanes(); var panel = panels.eq(i);
        panels.not(panel).animate({ width: "0px" }, "slow", "easeInOutExpo", function () { $(this).hide() });
        panel.css("width", "0px").show().animate({ width: "200px" }, "slow", "easeInOutExpo", function () {
            done.call();
        });
    });

    var CheckSearchBox = function () {
        setTimeout(function () {
            var IsActive = false;
            $(".OutSide *").each(function () {
                if (this.IsActive) IsActive = true;
            });

            if (IsActive) {
                if (!$.InSearchBox) {
                    $(".OutSide, .OutSide-body, .OutSide #Search").animate({ width: "+=150px" }, "normal", "easeInOutExpo", function () {
                        if ($.SearchBox_Height) return;
                        $.SearchBox_Height = true;
                        $(".OutSide, .OutSide-body, .OutSide #SearchOption").animate({ height: "+=20px" }, "normal", "easeInOutExpo");
                        $(".OutSide-Over").animate({ height: "+=20px", top: "-=20px" }, "normal", "easeInOutExpo", function () { $.SearchBox_Height = false; });
                    });
                    $.InSearchBox = true;
                }
            }
            else {
                if ($.InSearchBox) {
                    $(".OutSide, .OutSide-body, .OutSide #SearchOption").animate({ height: "-=20px" }, "normal", "easeInOutExpo");
                    $(".OutSide-Over").animate({ height: "-=20px", top: "+=20px" }, "normal", "easeInOutExpo", function () {
                        if ($.SearchBox_Height) return;
                        $.SearchBox_Height = true;
                        $(".OutSide, .OutSide-body, .OutSide #Search").animate({ width: "-=150px" }, "normal", "easeInOutExpo", function () { $.SearchBox_Height = false; });
                    });
                    $.InSearchBox = false;
                }
            }
        }, 400);
    }

    $(".OutSide *").focus(function () {
        this.IsActive = true; CheckSearchBox();
    }).blur(function () {
        this.IsActive = false; CheckSearchBox();
    });

    $(".menu a").click(function () {
        $(".OverLayout").fadeTo(0, 0.8);
        $("#MenuHolder").html("");
        var item = $(this);
        var div = $("#" + item.attr("ref"));
        var header = $(".header");

        if (!this.IsMenuOpen) {
            if ($.IsMenuOpen) {
                $(".menu a").each(function () { this.IsMenuOpen = false; });
                var acc = div.clone(); acc.hide().appendTo("#MenuHolder"); acc.show("slow");
            } else {
                $.IsMenuOpen = true;
                $(".MenuSlider").animate({ top: '+=175px' }, "slow", 'easeOutBounce');
                header.animate({ height: '175px' }, "slow", 'easeOutBounce', function () {
                    var acc = div.clone(); acc.hide().appendTo("#MenuHolder"); acc.show("slow");
                });
            }
            this.IsMenuOpen = true;
        } else {
            this.IsMenuOpen = false;
            $.IsMenuOpen = false;
            $(".MenuSlider").animate({ top: '-=175px' }, "slow", 'easeOutBounce');
            header.animate({ height: '0px' }, "slow", 'easeOutBounce');
        }
    });
});
