﻿function logoutFromWebsite() {
    var $div = $("body");
    var redirectPath = applicationPath + 'login.aspx?action=logout';
    var $frame = $("<iframe id='ifrSiteLogout' name='ifrSiteLogout' width='0px' height='0px'  style='display:none' ></iframe>");
    $frame.appendTo($div);

    var $form = $("<form method='GET' id='fSiteLogout' action='http://www.edpnet.be/conf/logout' target='ifrSiteLogout'></form>");
    $form.appendTo($div);
    $form.submit();

    $('#ifrSiteLogout').load(function () {
        document.location.href = redirectPath;
    });
    //if site didn't load into frame in 3 seconds
    setTimeout(function () { document.location.href = redirectPath; }, 3000);
}
var initCloseMessages = function () {
    $(".msg .msg-close").unbind("hover");
    $(".msg .msg-close").unbind("click");
    $(".msg .msg-close").css({ opacity: 0.4 });
    $(".msg .msg-close").hover(function () {
        $(this).css({ opacity: 1 });
    }, function () {
        $(this).css({ opacity: 0.4 });
    });
    $(".msg .msg-close").click(function () {
        $(this).parent(0).fadeOut();
    });
};
var msie6 = $.browser.msie && $.browser.version < 7;
var msie7 = $.browser.msie && $.browser.version > 6 && $.browser.version < 8;
var msie9 = $.browser.msie && $.browser.version > 8;

$(document).ready(function () {
    /*** DATATABLE ***/
    $(".datatable td").each(function () {
        if ($(this).parent(0).attr("href")) {
            $(this).css("cursor", "pointer");
        }
    });
    $(".datatable td").hover(function (e) {
        if ($(this).parent(0).attr("href")) {
            $(this).parent(0).find("td").addClass("hover");
        }
    }, function (e) {
        if ($(this).parent(0).attr("href")) {
            $(this).parent(0).find("td").removeClass("hover");
        }
    });
    $(".datatable td").click(function (e) {
        if (!$(e.target).is("a") && !$(e.target).is("img")) {
            if ($(this).parent(0).attr("href")) {
                window.location.href = $(this).parent(0).attr("href");
            }
        }
    });
//    /** FILTER **/
//    $(".filter-button").click(function () {
//        if ($(this).parent(0).hasClass("filter-closed")) {
//            $(this).parent(0).removeClass("filter-closed");
//            $(".filter-content", $(this).parent(0)).css("display", "block");
//        } else {
//            $(this).parent(0).addClass("filter-closed");
//            $(".filter-content", $(this).parent(0)).css("display", "none");
//        }
//        initFilterButton();
//    });
//    initFilterButton();
//    function initFilterButton() {
//        if ($(".filter-button").parent(0).hasClass("filter-closed")) {
//            $(".filter-button").html("Show Filter");
//        } else {
//            $(".filter-button").html("Hide Filter");
//        }
//    }

//    Cufon.replace('h1,h2,h3,h5,h6,#content h4');
//    if ($.browser.msie && $.browser.version > 6) {
//    } else {
//        Cufon.replace('#footer h4');
//    }
//    Cufon.replace('.customfont');

    if ($.browser.msie && $.browser.version < 7) {
        $("#content,#footer .footer-content").pngFix();
        $("#header #logo").pngFix();
        $("#header #myedpnet").pngFix();
    }
    if ($.browser.msie && $.browser.version < 9) {
        $(".btn-black").corner("5px");
        $(".btn-order").corner("20px");
    }
    if ($.browser.msie && $.browser.version < 9) {
        $(".my-search-field").corner("5px");
        //$(".icon-button").corner("5px");
    }
    if ($.browser.msie && $.browser.version < 8) {
        $(".footer-content").children().each(function () {
            if ($(this).css("float") != "right") {
                $(this).css("float", "left");
            }
        });
    };

    // PUSH DOWN FOOTER 
    $("#content").css("paddingBottom", $("#footer").height() + 50);

    // COL-LAYOUT IE6 
    /** GET ALL COL .last for IE6 */
    if ($.browser.msie && $.browser.version < 6) {
        var numbers = array("one", "two", "three", "four", "five", "six", "seven", "eight", "nine");
        $(".col").each(function () {
            if ($(this).hasClass("last")) {
                var classname = $(this).attr("class");
                var classes = classname.split(" ");
                var replaced = false;
                for (a = 0; a < classes.length; a++) {
                    for (i = 0; i < numbers.length; i++) {
                        if (classes[a].indexOf(numbers[i]) !== -1) {
                            classes[a] = classes[a] + "-last";
                            replaced = true;
                        }
                    }
                }
                if (replaced == true) {
                    $(this).attr("class", classes.join(" "));
                }
            }
        });
    }

    var setFontSize = function (itemSelect, itemDeselect, nsize) {
        if (!itemSelect.hasClass("selected")) {
            var rel = itemSelect.attr("rel");
            var src = itemSelect.attr("src");
            itemSelect.attr("src", rel);
            itemSelect.attr("rel", src);
            itemSelect.addClass("selected");
            if (nsize == "small") {
                $('#content').css("fontSize", "12px");
            }
            if (nsize == "big") {
                $('#content').css("fontSize", "13px");
            }
            $.cookie("fontsize", nsize, { expires: 365 });
        }
        if (itemDeselect.hasClass("selected")) {
            var rel = itemDeselect.attr("rel");
            var src = itemDeselect.attr("src");
            itemDeselect.attr("src", rel);
            itemDeselect.attr("rel", src);
            itemDeselect.removeClass("selected");
        }
    }
    /** GET && SET FONT SIZE **/
    if ($.cookie("fontsize")) {
        if ($.cookie("fontsize") == "small") {
            setFontSize($("#smallfont"), $("#bigfont"), "small");
        } else {
            setFontSize($("#bigfont"), $("#smallfont"), "big");
        }
    } else {
        setFontSize($("#smallfont"), $("#bigfont"), "small");
        $.cookie("fontsize", "small", { expires: 365 });
    }
    $("#smallfont").click(function () {
        setFontSize($(this), $("#bigfont"), "small");
    });
    $("#bigfont").click(function () {
        setFontSize($(this), $("#smallfont"), "big");
    });


    /**
    * FADING URGENT MESSAGES
    */
    if ($(".info-messages em").length > 0) {

        var currentMsgIndex = 0;
        var maxMsgIndex = $(".info-messages em").length;
        var msgInterval = false;
        var setMsgInterval = function () {
            if (msgInterval)
                clearMsgInterval();
            msgInterval = setInterval(function () { nextMessage(); }, 5000);
        };
        var clearMsgInterval = function () {
            if (msgInterval)
                clearInterval(msgInterval);
        };
        $(".info-messages em").each(function () {
            var height = $(this).height() + ($(this).css("marginTop").replace("px", "") * 1) + ($(this).css("marginBottom").replace("px", "") * 1);
            $(this).data("info-height", height);
            $(this).css("display", "none");
            $(this).css("visibility", "visible");
        });
        var nextMessage = function () {
            var nextMsgIndex = currentMsgIndex + 1;
            if (nextMsgIndex > maxMsgIndex) {
                nextMsgIndex = 1;
            }
            var current = next = null;
            var count = 1;
            $(".info-messages em").each(function () {
                if (count == currentMsgIndex) {
                    current = $(this);
                }
                if (count == nextMsgIndex) {
                    next = $(this);
                }
                count++;
            });
            if (msie6) {
                if (current)
                    current.css("display", "none");
                $(this).parent(0).animate({ height: next.data("info-height") });
                setTimeout(function () { next.css("display", "block"); }, 450);
            } else {
                if (current)
                    current.fadeOut(500);
                next.parent(0).animate({ height: next.data("info-height") });
                setTimeout(function () { next.fadeIn(500); }, 450);
            }
            currentMsgIndex = nextMsgIndex;
        }
        $(".info-messages").hover(function () {
            clearMsgInterval();
        }, function () {
            setMsgInterval();
        });
        nextMessage();
        setMsgInterval();
    }



    /** ONLINE SUPPORT **/
    if ($(".open-chat-button").length > 0) {
        
        var url = escape(location.toString());
        var request_url = "http://www.edpnet.be/phplive/request.php?l=edpnet&x=1&deptid=" + department + "&pagex=" + url;
        $(".open-chat-button").click(function () {
            window.open(request_url, '_blank', 'width=980,height=700,toolbar=no,location=no,directories=no,status=no,menubar=no,copyhistory=no,resizable=yes,scrollbars=yes,top=5, left=5');
        });
        //        $(".open-chat-button").attr("href", request_url);
        //        $(".open-chat-button").colorbox({ width: 500, height: 560, iframe: true });
        //        $(".colorbox-link").colorbox();
    }


    initCloseMessages();
});
$(window).load(function () {
    var initInfoBoxShades = function () {
        var colors = new Array("green", "red", "yellow", "pink", "white", "purple", "dpurple");
        if (!msie6) {
            $(".column-right .infobox").each(function () {
                if ($(this).hasClass("noshade"))
                    return;
                /** get all classes **/
                var extraclass = "";
                var extramin = 0;
                var container = $(this);
                var bwidth = 190;
                if ($(this).css("paddingLeft").replace("px", "") == "0") {
                    bwidth = 210;
                }
                for (i = 0; i < colors.length; i++) {
                    if ($(this).attr("class").indexOf(colors[i]) !== -1) {
                        extraclass = colors[i] + "-shade";
                    }
                }
                $("img", $(this)).each(function () {
                    if (($(this).css("float") == "left" || $(this).css("float") == "right") && $("p", container).length > 0) {
                        if (msie6 || msie7 || msie9) {
                            extramin = 5;
                        }
                    }
                });
                if ((msie6 || msie7 || msie9) && $("h3 .cufon,h4 .cufon", container).length > 0) {
                    extramin = extramin + 10;
                }
                $(this).wrap("<div class='infobox-wrapper " + extraclass + "'></div>");

                $(this).parent(0).css({
                    position: "relative",
                    height: $(this).outerHeight() - extramin,
                    width: 210
                });
                $(this).css({
                    position: "relative",
                    left: 1,
                    top: 1,
                    width: bwidth
                });
            });
            $(".column-right .infobox-footer").each(function () {
                if ($(this).hasClass("noshade"))
                    return;
                var extraclass = "";
                var bwidth = 190;

                if ($(this).css("paddingLeft").replace("px", "") == "0") {
                    bwidth = 210;
                }
                if ($(this).attr("class").indexOf(colors[i]) !== -1) {
                    extraclass = colors[i] + "-shade";
                }
                $(this).wrap("<div class='infobox-footer-wrapper " + extraclass + "'></div>");
                $(this).parent(0).css({
                    position: "relative",
                    height: $(this).outerHeight(),
                    width: 210,
                    backgroundColor: "#839c78"
                });
                $(this).css({
                    position: "relative",
                    left: 1,
                    top: 1,
                    width: bwidth
                });
            });
        }
        $(".column-right .infobox .middle").each(function () {
            var height = $(this).parent(0).height();
            var mgleft = 10;
            if ($.browser.msie && $.browser.version < 8) {
                height = height - 20;
                mgleft = 10;
            }
            var sheight = $(this).height();
            var newtop = Math.floor((height - sheight) / 2);

            var div = $("<div></div>").css({
                height: height,
                position: "relative"
            });
            $(this).wrap(div);
            if (newtop < 0)
                newtop = 10;
            $(this).css({
                top: newtop,
                position: "absolute",
                margin: "0",
                left: mgleft
            });
        });
    }
    Cufon.DOM.ready(function () {
        initInfoBoxShades();
    });
});
/**
* COOKIE PLUGIN
*/
jQuery.cookie = function (name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
