﻿$(document).ready(
function()
{
    var timeout = getTimeoutSeconds();
    if (timeout != "" && timeout != null)
    {
        registerTimeout(timeout);
    }
});

/************ Webparts *************/

function wptoggle(obj) {
    if (obj == null) return false;

    var $webpart = $(obj).parent().parent().parent().children(".wpbody");
    $webpart.toggle();
}

/***********************************/

function searchQuery()
{
    var q = $("#q").val();
    href = "/" + getCurrentCompany() + "/search?q=" + encodeURI(q);
    //window.open(href);
    location.href = href;
    return false;
}

function onQKeyPress(e)
{
    var keynum
    var keychar

    if (window.event) // IE
    {
        keynum = e.keyCode
    }
    else if (e.which) // Netscape/Firefox/Opera
    {
        keynum = e.which
    }
    if (keynum == 13) return searchQuery();
}

////////////// Messaging ////////////

function showMessage(obj) {
    var msgId = $(obj).parent().children("td:first").children("input").attr("msgid");
    //alert(msgId);
    window.open("/" + getCurrentCompany() + "/messagebox/message/" + msgId, "msgView");
}

function deselectallmsg() {
    $("form :checkbox").each(function() {
        //alert("1");
        $(this).attr('checked', false);
    });
}

function selectallmsg() {
    $("form :checkbox").each(function() {
        //alert("1");
        $(this).attr('checked', true);
    });
}

function closeMessageWindow() {
    //window.opener.location.reload();
    if (window.opener != null) {
        var btn = window.opener.document.getElementById("m_ph3wp1_Inbox_btnRefresh");
        if (btn != null) {
            btn.click();
        }
    }
    window.close();
}

/////////////////////////////////////

//////// TIMEOUT ///////////////

function registerTimeout(seconds)
{
    if (seconds > 0)
    {
        //alert("setting timeout for " + seconds + " seconds");
        //setTimeout(showSessionEndPanel, seconds * 1000);
        initTimer();
    }
}
function getTimeoutSeconds()
{
    return $("#m_hdnTimeout").val();
}

var secondsToClose = 20;
function showSessionEndPanel()
{
    var handle = setInterval(closingTimer, 1000);
    var params = { expose: { color: '#333', loadSpeed: 200, opacity: 0.9 },
        closeOnClick: false,
        api: true,
        onClose: function() { initTimer();  clearInterval(handle); }
    };
    var overlayApi = $("#pnlSessionTimeout").overlay(params);
    overlayApi.load();             
    
    $("#btnCloseSesstionTime").click(function()
    {        
        overlayApi.close();
    });
}

function initTimer()
{
    secondsToClose = 20;
    setTimeout(showSessionEndPanel, getTimeoutSeconds() * 1000);
    var span = $("#secondstoclose");
    span.html(secondsToClose + " שניות");
}

function closingTimer()
{
    var span = $("#secondstoclose");
    if (secondsToClose > 1)
    {
        secondsToClose--; 
        span.html(secondsToClose + " שניות");
    }
    else
    {
        if(secondsToClose == 1)
        {
            secondsToClose--;
            span.html("שניה אחת");
        }
        else
        {
            window.location = "/" + getCurrentCompany() + "/login";
        }
    }
    
}


///////////////////////////////
