﻿// JScript File

//*******************************//
//*    Buttons Functionality    *//
//*******************************//

function minimizeWin(frameID,updateDB)
{
    $("#tableContentWin"+frameID).toggleClass("hidden");
    $("#tableFooterWin"+frameID).toggleClass("hidden");
    var theDiv = $("#win"+frameID);
    var sheet = $("#win"+frameID+"_resizeSheet");
    
    var roundEdgeTable = sheet.prev();
    
    $("#frame"+frameID).attr("src", "");
    
    theDiv.toggleClass("dragWin_minimized");
    
    
    
    sheet.toggleClass("resizeSheet_minimized");
    if (roundEdgeTable.length) {
        roundEdgeTable.toggleClass("WindowRoundEdge_minimized");
//        roundEdgeTable.find(".l").height(30);
//        roundEdgeTable.find(".r").height(30);
    }
    
    if (!theDiv.hasClass("dragWin_minimized") && !sheet.hasClass("resizeSheet_minimized"))
    {
//        if ($.browser.mozilla)
//            reloadFrame(frameID); // FIX: siteshot lose focus on maximize.

        var url = theDiv.find("input:hidden").val();
        
        lpc.Activate('minimize');
        $("#frame" + frameID).one('load', function() { lpc.Disable('minimize'); });
        
        BindFrameUrl(frameID,url);

        var cell = $("#tdContentWin"+frameID);
        var row = $("#trContentWin"+frameID);
        //for ie
        $('#win_vml' + frameID).css('display', 'block');
        theDiv.addClass('WindowRoundEdge');
        SetSize(theDiv,sheet.width()-2,sheet.height()-2);
        FixTable(theDiv.children(":first"),sheet);
        SetFrameSize(cell);
        SetBannersSize(row,sheet);
    }
    else{
        //for ie
        $('#win_vml' + frameID).css('display', 'none');
        theDiv.removeClass('WindowRoundEdge');        
    }
    if (updateDB)
    {
        //isMinimized
        ajaxUpdateCall(
        {
            type        : "minimize",
            id          : frameID,
            isMinimized : sheet.hasClass("resizeSheet_minimized")
        });
    }
    return false;
}
 
function reloadFrame(frameID)
{
    lpc.Activate('reload');
    var iFrame = $("#frame"+frameID);
    iFrame.one('load', function() { lpc.Disable('reload'); });
    iFrame.attr("src", iFrame.attr("src"));
}
function noUserClose(frameID)
{
    //remove window localy, no DB action.
    var theDiv = $("#win" + frameID);
    theDiv.remove();
    
   // --> where did this come from?!?! <-- //
//    ajaxUpdateCall(
//    {
//        type   : "nouser-close",
//        id     : theDiv[0].id.replace("win","")
//    });
}
function LoggedClose(frameID)
{
    //move to recycle bin
    var theDiv = $("#win" + frameID);
    if (theDiv.children(".resizeSheet").length)
        theDiv = theDiv.children(".resizeSheet").removeClass("ui-resizable").parents(".dragWin"); // FIX: resize after tab move
    theDiv = theDiv.removeClass("ui-draggable"); // FIX: drag after tab move
    
    var divH = theDiv.height();
    var divW = theDiv.width();
    var divT = theDiv[0].style.top;
    var divL = theDiv[0].style.left;
    
    var RecycleBinID = $("#hdnRecycle").val();
    var RecycleBin = $("#fragment-"+RecycleBinID);
    
    var containerID = theDiv.parents(".tabContainer").attr("id").replace("fragment-","");
    
    if (containerID != RecycleBinID)
    {   
        theDiv.remove();
        theDiv.appendTo(RecycleBin);
            
        SetPosition(theDiv[0],divT,divL);
        SetSize(theDiv,divW,divH);
        
        initializeDraggables(".dragWin");
        initializeResizable(".resizeSheet");
        
        if (!theDiv.hasClass('dragWin_minimized'))
        {
            try
            {
                minimizeWin(frameID,true);
            }
            catch(e){}
        }
        
        var new_zindex = GetMaxZindex(RecycleBinID) + 1;
        ajaxUpdateCall(
        {
            type      : "user-close",
            id        : frameID,
            tabID     : RecycleBinID,
            frTop     : divT,
            frLeft    : divL,
            zi        : new_zindex
        });
    }
    else
    {
        theDiv.remove();
        ajaxUpdateCall(
        {
            type   : "user-remove",
            id     : frameID
        });
    }
}
 

function reselectWin(frameID)
{
    var frame = $("#frame"+frameID);
    var winUrl = frame.attr("srcmemo");
    var indexS = winUrl.indexOf("loadUrl=");
    var indexE = winUrl.indexOf("&selectionOffsetX");
    var url = winUrl.substring(indexS+8,indexE);
    leaveSite=true;
window.location = phpSelection + "?FrameID="+frameID+"&loadUrl=" +url;
}

//refresh Iframe
function BindFrameUrl(frameID,url)
{
    var theDiv = $('#win' + frameID); // why this line is needed?
    var frame = $("#frame" + frameID);
    //frame.one('load', function() { HandleFrameOnLoad(); });
    frame.attr("src",url);
}

//******************************//
//*      Z-index settings      *//
//******************************//

function SetZindexServer(frameID,zi)
{
    $('#win'+frameID).css("z-index", zi);
}
function SetZindexUpdateMax(win,tabid)
{
    var newZi = GetMaxZindex(tabid)+1;
    win.css("z-index", newZi);
    
    //update maxZindex
    for (i in max_zindex)
    {
        if (max_zindex[i].split(':')[0] == tabid)
        {
            max_zindex[i] =tabid + ':' + newZi;
        }
    }
    //TODO: think of a way and stage to normlize the zindex in the DB.

    ajaxUpdateCall(
        {
            type : "zindex-changed",
            id   : win[0].id.replace("win",""),
            zi   : newZi,
            tabID : tabid
        });
}
function GetMaxZindex(tabid)
{
    var ret = 0;
    for (i in max_zindex)
    {
        if (max_zindex[i].split(':')[0] == tabid)
        {
            ret = max_zindex[i].split(':')[1];
            break;
        }
    }
    return ret*1;// return ret*1 in order to return int.
}



//******************************//
//*     Window Option Menu     *//
//******************************//

var openMenuWin = null;
var menuTimeout = null;

function OpenMenu(frameID)
{
    var win = document.getElementById("MoreMenuWin" + frameID);
    var OptionsBtn = document.getElementById("tdOptions" + frameID);
    
    if(win.style.display == "none")
    {
        CloseMenu();
        win.style.display = "block";
        openMenuWin = win;
        handleFrameMenuTimeout();
    }
}

function handleFrameMenuTimeout()
{
    if (menuTimeout!=null) clearTimeout(menuTimeout);
    menuTimeout = setTimeout("CloseMenu();",3000);
}

function CloseMenu()
{
    if(openMenuWin!=null) openMenuWin.style.display = "none";
}

function OpenCloseMenu(frameID)
{
    var win = document.getElementById("MoreMenuWin" + frameID);
    var OptionsBtn = document.getElementById("tdOptions" + frameID);
    if(win.style.display == "none")
    {
        OpenMenu(frameID);
    }
    else
    {
        win.style.display = "none";
    }
}


//////////////////////
// icons show/hide
/////////////////////

var ICONSopenMenuFrameID = null;
var ICONSmenuTimeout = null;

function ICONSOpenMenu(frameID)
{
        document.getElementById('toolBarHidden'+frameID).style.display='inline';
        document.getElementById('toolBarVisible'+frameID).style.display='none';
       
        if(frameID!=ICONSopenMenuFrameID)
        {
            ICONSCloseMenu();
            ICONSopenMenuFrameID=frameID;
        }
    
        ICONShandleFrameMenuTimeout();

}

function ICONShandleFrameMenuTimeout()
{
    if (ICONSmenuTimeout!=null) clearTimeout(ICONSmenuTimeout);
    ICONSmenuTimeout = setTimeout("ICONSCloseMenu();",3000);
}

function ICONSCloseMenu()
{
    if(ICONSopenMenuFrameID!=null)
    {
        document.getElementById('toolBarVisible'+ICONSopenMenuFrameID).style.display='inline';
        document.getElementById('toolBarHidden'+ICONSopenMenuFrameID).style.display='none';
    } 
}


