﻿function loadAll() {
    if ($("#contentTable").length > 0) {
        if ($("#contentTable").find("#contentTableRight").html().replace(" ", "") == "") {
            $("#contentTable").find("#contentTableRight").hide();
            $("#contentTable").find("#contentTableSpace").hide();
        }
        else {
            $("#contentTable").find("#contentTableRight").show();
            $("#contentTable").find("#contentTableSpace").show();
        }
    }

    $.preLoadImages('files/images/menu_item_bg.png', 'files/images/menu_item_left.png', 'files/images/menu_item_right.png');
    $.preLoadImages('files/images/submenu_bg.png', 'files/images/submenu_top.png', 'files/images/submenu_bottom.png');
    $.preLoadImages('files/images/submenu_item_bg.png', 'files/images/submenu_item_hover.png');

    $("#imgslider img:first").show();
    if ($("#imgslider img").length > 1) {
        setInterval("imgSlider()", 6000);
    }

    $("#menu li").hover(function() {
        if ($(this).find(".submenu a").length > 0) {
            $(this).find(".submenu").show();
        }
    }, function() {
        $(this).find('.submenu').hide();
    });

    $("#menu li .submenu .submenuitem").hover(function() {
        if ($(this).find(".ssubmenu a").length > 0) {
            $(this).find(".ssubmenu").show();
        }
    }, function() {
        $(this).find('.ssubmenu').hide();
    });

    $("#login").hide();
    $(".mentor").show();
    $(".mentor").click(function() {
        $("#login").css("width", $(".mentor").outerWidth())
        .css("height", $(".mentor").outerHeight());

        if ($(".mentor").text().replace(/(^[\s\xA0]+|[\s\xA0]+$)/g, '') != "Mentor") {
            $("#login").find(".newlogin").hide();
            $("#login").find(".logout").show();

            $(".mentor").hide();
            $("#login").show();

            $("#login").animate({ width: "120px", height: "60px" }, { queue: false, duration: 1000 });
        }
        else {
            $("#login").find(".logout").hide();
            $("#login").find(".newlogin").show();

            $(".mentor").hide();
            $("#login").show();

            $("#login").animate({ width: "271px", height: "236px" }, { queue: false, duration: 2000 });
        }




    });
    $("#login").find(".close").click(function() {

        $("#login").animate({ width: $(".mentor").outerWidth(), height: $(".mentor").outerHeight() }, { queue: false, duration: 2000, complete: function() {
            $("#login").hide();
            $(".mentor").show();
        }
        });

    });


    //$(".forum").find("#addnew").hide();

    $("#imgAddNewPost").click(function() {
        $(".forum").find("#addnew").css("width", "0px")
        .css("height", "0px");
        $(".forum").find("#addnew").show();
        $(".forum").find("#addnew").animate({ width: "350px", height: "400px" }, { queue: false, duration: 2000 });

    });
    $(".forum").find(".close").click(function() {
        $(".forum").find("#addnew").animate({ width: "0px", height: "0px" }, { queue: false, duration: 2000, complete: function() {
            $(".forum").find("#addnew").hide();
        }
        });
    });
    $(".forum").find("#btnClear").click(function() {
        $(".forum").find(".forumtxt").val("");
    });

    //$(".comments").find("#addnew").hide();
    $("#imgAddNewComment").click(function() {
        $(".comments").find("#addnew").css("width", "0px")
        .css("height", "0px");
        $(".comments").find("#addnew").show();
        $(".comments").find("#addnew").animate({ width: "375px", height: "370px" }, { queue: false, duration: 2000 });

    });
    $(".comments").find(".close").click(function() {
        $(".comments").find("#addnew").animate({ width: "0px", height: "0px" }, { queue: false, duration: 2000, complete: function() {
            $(".comments").find("#addnew").hide();
        }
        });
    });
    $(".comments").find("#btnClear").click(function() {
        $(".comments").find(".forumtxt").val("");
    });

    $(".calendar").find("#addnew").hide();
    $(".calendar").find("#btnClear").click(function() {
        $(".calendar").find(".forumtxt").val("");
        $(".calendar").find(".starttime").val("");
        $(".calendar").find(".endtime").val("");
    });
    $(".calendar").find(".close").click(function() {
        $(".calendar").find("#addnew").animate({ width: "0px", height: "0px" }, { queue: false, duration: 2000, complete: function() {
            $(".calendar").find("#addnew").hide();
        }
        });
    });

    $('.searchtxt').blur(function() {
        if ($(this).val() == "") {
            $(this).val("Sök");
        }
    }).focus(function() {
        if ($(this).val() == "Sök") {
            $(this).val("");
        }
    });

    $(".boxMiddle").find(".btnsearch").click(function() {
        if ($('.searchtxt').val() == "Sök") {
            $('.searchtxt').val("");
            $('.searchtxt').focus();
            return false;
        }
        else
            return true;
    });

    $("#login").find(".lblusername").hide();
    $("#login").find(".lblpassword").hide();
    $("#btnLogin").click(function() {
        $("#login").find(".lblusername").hide();
        $("#login").find(".lblpassword").hide();

        var error = false;
        if ($("#login").find(".username").val() == "") {
            $("#login").find(".username").focus();
            $("#login").find(".lblusername").show();
            error = true;
        }

        if ($("#login").find(".password").val() == "") {
            if (!error)
                $("#login").find(".password").focus();

            $("#login").find(".lblpassword").show();
            error = true;
        }

        if (error)
            return;

        $.ajax({
            type: "POST",
            url: "invidzonen.ashx?method=adminLogin&username=" + $("#login").find(".username").val() + "&password=" + $("#login").find(".password").val(),
            dataType: "xml",
            success: function(xml) {
                if ($(xml).find('Return').attr("success") == "1") {
                    $(".mentor").text($(xml).find('Return').text());
                    $("#login").find(".close").trigger('click');
                    window.location.reload();
                }
                else {
                    alert('Ett fel uppstod');
                }
            }
        });
    });

    $("#btnLogout").click(function() {
        $.ajax({
            type: "POST",
            url: "invidzonen.ashx?method=logout",
            dataType: "xml",
            success: function(xml) {
                if ($(xml).find('Return').attr("success") == "1") {
                    $("#login").find(".close").trigger('click');
                    window.location.reload();
                }
                else {
                    alert('Ett fel uppstod');
                }
            }
        });
    });
    resizeContent();

    $(window).resize(function() {
        resizeContent();
    });

}
function newEvent(start) {
    var date = new Date(start);
    $(".calendar").find(".starttime").val(formatDate(date));
    $(".calendar").find(".endtime").val(formatDate(addTime(date, 30)));
    $(".calendar").find("#addnew").css("width", "0px")
        .css("height", "0px");
    $(".calendar").find("#addnew").show();
    $(".calendar").find("#addnew").animate({ width: "370px", height: "390px" }, { queue: false, duration: 2000 });

}
function formatDate(date) {
    var formattedDate = date.getFullYear()
    + '-' + pad(date.getMonth() + 1, 2)
    + '-' + pad(date.getDate(), 2)
    + ' ' + pad(date.getHours(), 2)
    + ':' + pad(date.getMinutes(), 2);
    return formattedDate;

}
function pad(number, length) {
    var str = '' + number;
    while (str.length < length) {
        str = '0' + str;
    }
    return str;
}
function addTime(date, minutes) {
    var rDate = new Date(date);
    rDate.setMinutes(date.getMinutes() + minutes);
    return rDate;
}

function imgSlider() {

    var active = $("#imgslider img:visible");
    var next = active.next();
    if (next.length == 0) {
        next = $("#imgslider img:first");
    }

    active.fadeOut(1000);
    next.fadeIn(1000);

}

function resizeContent() {
    var conHeight = $("#container").height();
    var winHeight = $(window).height();
    var height = Math.max(conHeight, winHeight);

    if (height > conHeight) {
        $("#container").css("height", height);
    }
}
function popup(url, height, width) {
    window.open(url, '', 'height=' + height + 'location=0,menubar=0,resizable=1,scrollbars=1,status=0,toolbar=0, width=' + width);
}
function refreshParent() {
    window.opener.location.href = window.opener.location.href;
    window.close();
    if (window.opener.progressWindow) {
        window.opener.progressWindow.close()
    }

}

