var OnLoadFunctionsArray = [], PortletStates = [];

$(document).ready(function() { Init(); });
window.onbeforeunload = function() { SaveMenuStates(); };

window.onload = function()
{
    for (l = 0; l < OnLoadFunctionsArray.length; l++)
        try { OnLoadFunctionsArray[l]() } catch (ex) { alert("ONLOAD ERROR: " + ex) }
}

function InitDataTableHover()
{
    $("table.DataTable tr").hover(
        function() { $(this).addClass("HoverRow"); },
        function() { $(this).removeClass("HoverRow"); }
    );
}

function Init()
{
    $("div.PortletBody").after("<div class=\"PortletFooter\"></div>");

    $("div.PortletFooter").corner("bl br 14px");
    $("div.PortletHeader").corner("14px tl tr");
    $("div.Navigation").corner("14px");

    $("div.Collapsable").hover(
        function() { $(this).addClass("PortletHeaderHover"); },
        function() { $(this).removeClass("PortletHeaderHover"); }
    );

    $("div.NavItem").hover(
        function() { $(this).addClass("Hover"); },
        function() { $(this).removeClass("Hover"); }
    );
    
    $("div.OptionButtons input").hover(
        function() { $(this).addClass("Hover"); },
        function() { $(this).removeClass("Hover"); }
    );

    $("div.Collapsable").click(function()
    {
        var PanelBody = $(this).next("div.PortletBody");

        if (PanelBody.is(":hidden"))
        {
            PanelBody.slideDown("slow");
            SetMenuState($(this).text(), true);
        }
        else
        {
            PanelBody.slideUp("slow");
            SetMenuState($(this).text(), false);
        }

    });

    var PortletStatesSerialized = GetCookie("PortletStates");

    $("div.Collapsable").next("div.PortletBody").hide();

    if (PortletStatesSerialized != null)
    {
        var PortletStatesSplit = PortletStatesSerialized.split("|");

        for (pair in PortletStatesSplit)
        {
            var PortletPair = PortletStatesSplit[pair].split(":");
            PortletStates[PortletPair[0]] = PortletPair[1];

            if (PortletPair[0])
            {
                if (PortletPair[1] == "true")
                    $("div.PortletHeader:contains('" + PortletPair[0] + "')").next().show();
                else
                    $("div.PortletHeader:contains('" + PortletPair[0] + "')").next().hide();
            }
        }
    }
}

function ForceOpenPortlet(text)
{
    $("div.PortletHeader:contains('" + text + "')").next().show();
}

function AddLoadEvent(func)
{
    OnLoadFunctionsArray.push(func);
}

function SetMenuState(id, opened)
{
    PortletStates[id] = opened;
}

function SaveMenuStates()
{
    var PortletPairs = [];

    for (var item in PortletStates)
        PortletPairs.push(item + ":" + PortletStates[item]);
    
    SetCookie("PortletStates", PortletPairs.join("|"));
}

var popUpWin=0;
function pop_up (URLStr, left, top, width, height)
{
  	if(popUpWin)
	{
		if(!popUpWin.closed) popUpWin.close();
  	}
	popUpWin = window.open(URLStr, 'popUpWin', 'toolbar=0,location=no,directories=0,statusbar=0,menubar=no,scrollbars=yes,resizable=0,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}

function ConfirmDelete ()
{
	return confirm("Are you sure you want to delete this?\n\nOnce removed, this item is NOT recoverable!");
}

function redirect (url)
{
	document.location.href = url;
}

function CheckPageFileName(obj)
{
    obj.value = obj.value.replace(/ /g, "");
}

function FadeInNotification(id)
{
    $("#" + id).stop().fadeIn("slow").fadeTo("fast", 0.5).fadeTo("slow", 0.8).fadeTo(10000, 0.8).fadeOut("slow");
}

function FadeOutNotification(id)
{
    $("#" + id).stop().fadeOut("slow");
}

function PreviewPopup(url)
{
    window.open(url, "Preview", "", false);
}

function ScrollTo(selector, callback)
{
    AddLoadEvent(function()
    {
        var targetOffset = $(selector).offset().top;
        $('html,body').animate({ scrollTop: targetOffset }, 800, callback);
    });
}
