﻿// JScript File

var GlobalConsts = 
{
    WaitTillLoadingNonCurrentTabs : 5000,
    updateDB : true
};
var topBorder;

var dragged_element;
var dragged_element_top;
var dragged_element_left;
var dragged_element_width;
var dragged_element_height;

var tabs;
var isTabLoaded = false;
var init_tab_selected;

var max_tab_id;

var tab_status_arr = new Array();
var max_zindex = new Array();

/*****************************/
/*  Event Manager Variables  */
/*****************************/

var _isDropEvent = false;


var lpc = new LeavePageControl(); // single instance of the control


/*****************************/
/*    $(document).ready()    */
/*****************************/

$(document).ready(function()
{
    lpc.Instantiate();
    lpc.AddSkipID('lb');
	lpc.AddSkipID('btn');
	lpc.AddSkipID('a');
	lpc.AddSkipID('lnk');
	lpc.AddSkipID('ib');
	lpc.AddSkipID('tb');
	lpc.AddSkipID('td');
	
    tab_status_arr = $("#hdnTabStatus").val().split(';');
    max_zindex = $("#hdnMaxZindex").val().split(';');
    
    //the current tab comes from the server
    var tab_to_select = currentTab;
    
    // TODO: when adding a tab, we can change the template to suite our design need
    // in the Jquery/ui.tas.js line 56!!
    $(".TabSection").tabs(
    {
        selected : 0,
        fx: { height: 'toggle', opacity: 'toggle', duration: 'fast' }
    }).bind('add.ui-tabs', 
        function(e, ui)
        {
            $(ui.panel).addClass('tabContainer');
            $(this).children(":last").addClass('dropZone');
            initializeDroppables('.dropZone');
        }).bind('select.ui-tabs',onTabSelected).tabs("select","#fragment-"+tab_to_select);
    
	
	
    tabs = $('#Tabs > ul').tabs();
    init_tab_selected = $(tabs).data('selected.ui-tabs');
    
    topBorder= $(".tabContainer").eq(init_tab_selected).offset().top + 10;
    
    
    initializeSortableTabs("#Tabs > ul");
    
    initializeDraggables(".dragWin");

    initializeDroppables('.dropZone');
    
    initializeResizable(".resizeSheet");
    
    
    $(".btnMinimize").click(function() {
       return false; 
    });
    
    $("#disableBG").height(GetWindowHeight() - 3);
    $("#disableBG").width(GetWindowWidth() - 3);
    
    $(window).resize(function()
    {
        $("#disableBG").height(GetWindowHeight() - 3);
        $("#disableBG").width(GetWindowWidth() - 3);
    });
    
    $("#lbLogin").click(function(){
        return false;
    });
    
    $("#addTab").click(function(){
        return false;
    });
    
    
    //lpc.Activate();
    
	//if there are frames in the tab - loads them first.
    if(tab_status_arr.length > 0 && tab_status_arr[0].length > 0)
    {
        var loaded = false;
        //setTimeout(function(){ if(!loaded) {loadNonCurrentTabs();loaded=true;} } ,GlobalConsts.WaitTillLoadingNonCurrentTabs );
        $("#fragment-"+currentTab+" iframe").one("load", function() {
            var winID = $(this).parents(".dragWin").attr("id").replace("win","");
            setFrameReady(winID);
            if (!loaded && checkTabsReady()) {
                //loadNonCurrentTabs();
                loaded = true;
                //lpc.Disable();
            }
        });
		if (!isTabLoaded) {
			loadTabByID(currentTab);
		}
		isTabLoaded = false;
    }
    else
    {
        //lpc.Disable();
        //loadNonCurrentTabs();
    }
    
    /*******************/
    /* Image Preloader */
    /*******************/
    
    //alert(ArrSiteImages.length);
    var x = new ImagePreloader(ArrSiteImages, function(aImages, nLoaded) {
       //alert('there were ' + aImages.length + ' images, and ' + nLoaded + ' loaded'); 
    });
    
});



