﻿var agahMultiView_list=new Array();

// creates new multi view object
function agahMultiView()
{
    this.id='';
}

// loads agah multi view
function agahMultiView_load(viewId)
{
    var view=new agahMultiView();
    agahMultiView_list[viewId]=view;
    
    //
    view.id=viewId;
    view.control= $(document.getElementById(viewId));
    view.header = $(document.getElementById(viewId + "_" + "head"));
    view.body = $(document.getElementById(viewId + "_" + "body"));
    view.selection = $("#" + viewId + "_selection");
         
    //
    view.waiting=false;    
    view.tabs=new Array();
    view.getStatus = function(tab) {
        var c = tab.header.attr('class');
        var suffix = '-header-';
        var p = c.lastIndexOf(suffix);
        c = c.substr(p + suffix.length);
        return c;
    };

    view.getClassPrefix = function(tab) {
        var c = tab.header.attr('class');
        var suffix = '-header-';
        var p = c.lastIndexOf(suffix);
        c = c.substr(0,p+suffix.length-1);
        return c;
    };    
    
    // extracting headers
    var headers = view.header.children('a');
    var bodies = view.body.children('div');
    for (var i=0; i<headers.length; i++)
    {
        var tab = new Object();
        view.tabs[i] = tab;
        tab.header = headers.eq(i);
        tab.body = bodies.eq(i);
        
   }

}


//
function agahMultiView_tabClick(viewId,tabIndex,waitClass,hideMode)
{
    var view = agahMultiView_list[viewId];    
    if (view == null)
        return; // tab not loaded yet.

    var selectedTab = view.tabs[tabIndex];    
    var status=view.getStatus(selectedTab) ;
    if (status == 'selected' || status == 'disabled') {
        return;
    }
    
    // unwaiting all waited views ( note: other tab sets will be reset too.)
    for (id in agahMultiView_list)
    {        
        var tview=agahMultiView_list[id];
                
        if (tview.waiting)
        {
            
            agahMultiView_unwait(tview.id);
        }
    }

    //
    if (hideMode == 'client') {    
        for (var i=0; i<view.tabs.length; i++)
        {
            var tab = view.tabs[i];
            tab.body.hide();
            
            tab.header.attr('class', view.getClassPrefix(tab)+'-unselected');
        }

        selectedTab.body.show();
        selectedTab.header.attr('class', view.getClassPrefix(selectedTab) + '-selected');

        view.selection.val(tabIndex);
            
        return;
    } 
    
    //
    agahMultiView_wait(viewId,tabIndex,waitClass);
    
    
    // doing post back    
    __doPostBack(selectedTab.header.attr('id'),tabIndex);
}

// waits given tab
function agahMultiView_wait(viewId,tabIndex,waitClass)
{
    var view=agahMultiView_list[viewId];

    //
    view.waiting=true;

    // 
    view.control.css('cursor','wait');
    
    // waiting all tabs
    for (var i=0; i< view.tabs.length; i++)
    {
        var tab=view.tabs[i];
        
        //
        tab.oldHeaderCursor=tab.header.css('cursor');
        tab.header.css('cursor','wait');
        
        //
        tab.oldHeaderClass=tab.header.attr('class');
        if (i==tabIndex && waitClass!=null)
            tab.header.attr('class',waitClass);
    }
}

// unwaits given tab
function agahMultiView_unwait(viewId)
{
    var view=agahMultiView_list[viewId];
    
    //
    view.waiting=false;
    view.control.css('cursor', null);
    
    // waiting all tabs
    for (var i=0; i< view.tabs.length; i++)
    {
        var tab=view.tabs[i];
        
        //
        tab.header.css('cursor', tab.oldHeaderCursor);
        tab.header.attr('class',tab.oldHeaderClass);
    }
}





