﻿var Utils = {
    IE6: function() {
        var image = null; var imagesrc = "";
        var images = $("img.png24");
        for (var k = 0; k < images.length; k++) {
            image = images.eq(k);
            imagesrc = image.attr("src");
            if (imagesrc.indexOf(".png") > 0) {
                image.attr("src", imagesrc.replace("/images/", "/images-ie6/"));
            }
        }
    }
};

var Keys = {
    enter: 13,
    escape: 27
}
var Offset = {
    y: function() {
        return (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
    }
};

var Forms = {
    clearInput: function(obj, initValue) {
        if (obj.value == initValue)
            obj.value = '';
    },
    clearMagicLabel: function() {
        if (arguments.length > 1) {
            if ($("#" + arguments[1]).val() != "") {
                var obj = $('#' + arguments[0]);
                if (obj) {
                    obj.text('');
                }
            } else {
                var timeout = setTimeout('Forms.clearMagicLabel("' + arguments[0] + '","' + arguments[1] + '")', 100);
            }
        } else {
            var obj = $('#' + arguments[0]);
            if (obj) {
                obj.text('');
            }
        }
    },
    registerEnterKey: function(objId, targetId) {
        var $obj = $('#' + objId);
        var $target = $('#' + targetId);
        $obj.keypress(function(e) {
            if (e.which == Keys.enter) {
                $target.click();
            }
        });
    },
    registerPostBack: function(objId, targetId) {
        var $obj = $('#' + objId);
        var $target = $('#' + targetId);
        $obj.keypress(function(e) {
            if (e.which == Keys.enter) {
                __doPostBack(targetId, '');
            }
        });
    }
};

var Culture = {
    inited: false,
    format: "xx-CA",
    culture: "fr-CA",
    oppositeCulture: "en-CA",
    queryKey: "culture",
    init: function() {
        if (arguments.length >= 1) {
            this.culture = this.format.replace("xx", arguments[0]);
            this.oppositeCulture = arguments[0].indexOf("fr") >= 0 ? this.format.replace("xx", "en") : this.format.replace("xx", "fr");
        } else {
            var url = document.location.toString();
            if (url.indexOf("?") > 0 && url.indexOf(this.queryKey + "=") > 0) {
                var q = url.substr(url.indexOf(this.queryKey + "=") + 1, this.format.length);
                this.culture = q;
                this.oppositeCulture = this.culture.indexOf("en") > 0 ? this.culture.replace("en", "fr") : this.queryCulture.replace("fr", "en");
            }
        }
        this.inited = true;
    },
    switchBackground: function() {
        if (!this.inited)
            this.init();
        var arObj = new Array();
        arObj = $(".cultured");
        var newSrc, oldSrc = "";
        var obj;
        for (var k = 0; k < arObj.length; k++) {
            obj = $(arObj[k]);
            oldSrc = obj.css("background-image");
            newSrc = oldSrc.replace(this.oppositeCulture, this.culture);
            obj.css("background-image", newSrc);
        }
    },
    switchSrc: function() {
        if (!this.inited)
            this.init();
        var obj = arguments[0];
        var originalSrc = obj.src;
        if (originalSrc.indexOf("/fr-CA/") > 0) {
            obj.src = originalSrc.replace("/fr-CA/", "/en-CA/");
        } else if (originalSrc.indexOf("/en-CA/") > 0) {
            obj.src = originalSrc.replace("/en-CA/", "/fr-CA/");
        }
    }
};

var Background = {
    random: function(argv) {
        var min = (typeof (argv.min) != "undefined") ? argv.min : 1;
        var max = (typeof (argv.max) != "undefined") ? argv.max : 6;
        var random = Math.floor((Math.random() * max) + min);
        if (random < 10)
            random = "0" + random;
        $("body").css("background-image", $("body").css("background-image").replace("01", random));
    }
};

var Reward = {
    init: function() {
        var obj = null;
        var arObj = $(".reward");
        for (var k = 0; k < arObj.length; k++) {
            obj = $(arObj[k]);
            if (obj.attr("rel")) {
                obj.css("position", "relative");
                obj.append("<span class='tt_reward'>" + obj.attr("rel") + "</span>");
                obj.hover(function() { $(this).find("span").fadeIn("fast"); }, function() { $(this).find("span").fadeOut("fast"); });
            }
        }
    }
};

var Popup = {
    manager: {},
    culture: "fr", oculture: "en",
    width: 512, height: 400,
    onload: false,
    scroll: false,
    manipulation: true,
    backgroundColor: "#fff",
    // argv : {id:'',width:'',height:'',culture:'{en|fr}',onload:{true|false},scroll:{true|false},manipulation:{true|false},backgroundColor:'{#ffffff}'}
    show: function(argv) {
        if (typeof (argv.onload) != "undefined")
            this.onload = argv.onload;
        if (this.manager[argv.id] != true) {
            if (this.onload) {
                argv.onload = false;
                $(document).ready(function() { Popup.show(argv); });
            } else {
                this.setup(argv);
                this.init(argv);
            }
        }
        if ($("#" + argv.id).css("display") == "none") {
            var $visiblePopup = $(".popup:visible");
            for (var k = 0; k < $visiblePopup.length; k++)
                Popup.close({ id: ($($visiblePopup[k]).attr("id")) });
            var $contour = $("#" + argv.id + "_contour");

            if (jQuery.browser.msie && jQuery.browser.version == "6.0") {
                window.scrollTo(0);
                $("#" + argv.id + "_bg").css({ "height": document.documentElement.clientHeight + 80 + "px", "top": "-" + this.position() });
            }
            $contour.css("top", this.position());
            /*if (jQuery.browser.safari) { $("object").css("visibility", "hidden"); }*/
            $("#" + argv.id + "_bg").fadeIn("fast", function() {
                $("#" + argv.id).fadeIn("fast", null);
                $("#" + argv.id + "_border").fadeIn("normal", function() {
                    if (jQuery.browser.msie && jQuery.browser.version == "6.0") { $("select").hide(); }
                });
            });
        } else {
            $("#" + argv.id + "_border").fadeOut("fast", function() {
                $("#" + argv.id).fadeOut("fast", null);
                $("#" + argv.id + "_bg").fadeOut("fast", function() {
                    if (jQuery.browser.msie && jQuery.browser.version == "6.0") { $("select").show(); }
                    /*if (jQuery.browser.safari) { /*$("object").css("visibility", "visible"); }*/
                });
            });
        }
    },
    close: function(argv) {
        var id = argv.id;
        $("#" + id + "_border").fadeOut("fast", function() {
            $("#" + id).fadeOut("fast", null);
            $("#" + id + "_bg").fadeOut("fast", function() { if (jQuery.browser.msie && jQuery.browser.version == "6.0") { $("select").show(); } /*$("object").css("visibility", "visible");*/ });
        });
    },
    init: function(argv) {
        var $popup = $("#" + argv.id);
        if (this.manipulation == true) {
            $popup.remove();
            $("body").append($popup);
        }
        if (this.scroll == true) {
            var $title = $popup.find("h3").eq(0);
            var $content = $popup.find(".content").eq(0);
            var titleHeight = parseInt($content.css("padding-top").replace("px", ""));
            titleHeight += parseInt($content.css("padding-bottom").replace("px", ""));
            titleHeight += parseInt($title.css("height").replace("px", ""));
            titleHeight += parseInt($title.css("padding-bottom").replace("px", ""));
            titleHeight += parseInt($title.css("padding-top").replace("px", ""));
            var contentWidth = this.width;
            $content.css({ "width": contentWidth + "px", "height": (this.height - titleHeight) + "px", "overflow": "scroll" });
        }
        $popup.css({ "background": this.backgroundColor });
        $popup.width(this.width + "px");
        $popup.height(this.height + "px");
        $popup.append("<img id='" + $popup.attr("id") + "_close' class='close' src='/Resources/images/common/popup/popup_close.png' alt='" + (this.culture == "fr" ? "Fermer" : "Close") + "' />");
        $("#" + $popup.attr("id") + "_close").click(function() { Popup.show({ id: '' + $popup.attr("id") + '' }); });
        $popup.wrap('<div id="' + $popup.attr("id") + '_contour" class="popup_contour"></div>');
        var $contour = $("#" + $popup.attr("id") + "_contour");
        $contour.css({ "margin-left": (($popup.width() / 2) * -1) + "px" });
        $contour.append('<div id="' + $popup.attr("id") + '_border" class="popup_border"></div>');
        var $border = $("#" + $popup.attr("id") + "_border");
        $border.css({ 'top': "-6px", 'left': "-6px", 'opacity': "0.55", 'filter': "alpha(opacity=55)" });
        $border.width($popup.width() + 12); $border.height($popup.height() + 12);
        $border.after('<div id="' + $popup.attr("id") + '_bg" class="popup_bg"></div>');
        var $bg = $("#" + $popup.attr("id") + "_bg");
        $bg.css({ 'opacity': "0.55", 'filter': "alpha(opacity=55)" });
        if (jQuery.browser.msie && jQuery.browser.version == "6.0") {
            /*left: ((document.documentElement.clientWidth / 2 - $popup.width() / 2) * -1) + "px", */
            $bg.css({ top: "-" + this.position(), position: "absolute", left: ((document.documentElement.clientWidth / 2 - $popup.width() / 2) * -1) + "px", width: document.documentElement.clientWidth + "px", height: document.documentElement.clientHeight + 80 + "px" });
        }
        $contour.css("top", this.position());
        $(document).keydown(function(event) { if (event.keyCode == Keys.escape) { Popup.close({ id: $popup.attr("id") }); }; });
        $bg.click(function() { Popup.close({ id: $popup.attr("id") }); });
        this.manager[argv.id] = true;
    },
    position: function() {
        return (Offset.y() + 80 + "px");
    },
    setup: function(argv) {
        if (typeof (argv.culture) != "undefined")
            this.culture = argv.culture;
        if (this.culture == "en")
            this.oculture = "fr";
        if (typeof (argv.width) != "undefined")
            this.width = argv.width;
        if (typeof (argv.height) != "undefined")
            this.height = argv.height;
        if (typeof (argv.manipulation) != "undefined")
            this.manipulation = argv.manipulation;
        if (typeof (argv.scroll) != "undefined")
            this.scroll = argv.scroll;
        if (typeof (argv.backgroundColor) != "undefined")
            this.backgroundColor = argv.backgroundColor;
        else
            this.backgroundColor = "#fff";
    }
};

