﻿
function clsLayoutCtrl()
{
    this.pageLayout = null;
    this.mainPane = null;
    this.centerPane = null;
    
    this.layoutMode = "";
}

var ctrlLayout = new clsLayoutCtrl();

clsLayoutCtrl.prototype.initLayout = function () 
{
    this.pageLayout = $('body').layout({ 	
		    name:					"PageLayout"
		,	north__spacing_open:			5
		,	north__spacing_closed:			5
		,	south__spacing_open:			5
		,	south__spacing_closed:			5
		,	east__spacing_open:			    0
		,	east__spacing_closed:			0
		,	north__paneSelector:	"#menuPane"
		,	north__togglerLength_open:		30			// HIDE the toggler button
		,	center__paneSelector:	"#mainPane"
		,	east__paneSelector:	    "#tagPane"
		,	south__paneSelector:	    "#divFooter"
		,	east__initClosed:			true
		,   center__onresize:   function(){
		        ctrlLayout.mainPane.resizeAll();
		        document.getElementById('menuPane').style.bacgroungColor = 'black';
		}
		,   east__onopen:   function(){
		    ctrlTag.LoadTags();
		}
		,   north__size:            40
		,   south__size:            50
		,	contentSelector:			".ui-widget-content"
		
	});
	
	this.mainPane = $('#mainPane').layout({
		name:						"mainPane"
		,	spacing_open:			5
		,	spacing_closed:			5
	//,	applyDefaultStyles:         true
    ,   center__paneSelector:	    "#centerPane"
    ,   east__paneSelector:	    "#eastPane"
    ,   west__paneSelector:	    "#westPane"
    ,   west__size:                 110
    ,   east__size:                 160
    ,	center__onresize:		function(){
        ctrlLayout.centerPane.resizeAll();
	}
	,	west__onresize:		function(){ 
        ctrlLayout.resizeWestPane();
	}
	,	east__onresize:		function(){ 
	    ctrlLayout.resizeEastPane();
	}
    ,	contentSelector:			".ui-widget-content"
	});
		
	
	this.centerPane = $('#centerPane').layout({
		name:						"centerPane"
		,	spacing_open:			5
		,	spacing_closed:			5
	//,	applyDefaultStyles:         true
    ,   center__paneSelector:	    "#graph_maptime_Pane"
    ,   south__paneSelector:	        "#divDesc"
    ,   south__size:                 65
    ,	center__onresize:		function(){
        ctrlLayout.resizeCenterPane();
	}
    ,	contentSelector:			".ui-widget-content"
    
	});
	
	this.pageLayout.addPinBtn( "#btnToggleTag", "east" );
    
    $('#divAccBox').accordion({});
    
	this.switchToMapView();
	this.locateTabs();
}

clsLayoutCtrl.prototype.locateTabs = function() 
{
    /*
    var hgtPane = $("#menuPane").height();
    document.getElementById("divTabs").style.top = (hgtPane - 50); 
    //$("#divTabs").css('bottom', hgtPane);
    alert(hgtPane);
    */
}

clsLayoutCtrl.prototype.switchToMapView = function() {
    $('#divDesc').show();
    $('#divTreeGraphsPlots').hide();

    $('#paneGraphTree').hide();
    //$('#divGraph').hide();
    document.getElementById('divGraph').style.visibility = 'hidden';
    document.getElementById('divGraph').style.width = '2px';
    document.getElementById('divGraph').style.height = '1px';

    $('#divMapAndTime').show();
    $('#paneSensSelection').show();
    $('#divSensSelect').show();
    //this.mainPane.open("west");

    this.layoutMode = "map";

    this.mainPane.sizePane("west", 95);
    this.resizeCenterPane();
    this.resizeWestPane();
    this.resizeEastPane();

    ctrlGraph.clickedMapView();

    if (!$('#btnTabMap').hasClass("ui-state-active")) {
        $('#btnTabMap').addClass("ui-state-active");
    }
    if ($('#btnTabGraph').hasClass("ui-state-active")) {
        $('#btnTabGraph').removeClass("ui-state-active");
    }
    $('#btnTabHelp').removeClass("ui-state-active");
    $('#top_btManageData').removeClass("ui-state-active");
}

clsLayoutCtrl.prototype.switchToGraphView = function() {
    //this.mainPane.close("west");
    $('#divDesc').show();
    document.getElementById('divGraph').style.visibility = 'visible';
    document.getElementById('divGraph').style.width = '100%';
    document.getElementById('divGraph').style.height = '100%';

    $('#divMapAndTime').hide();
    $('#paneSensSelection').hide();

    $('#paneGraphTree').show();
    $('#divTreeGraphsPlots').show();

    this.layoutMode = "graph";
    this.mainPane.sizePane("west", 95);

    this.resizeWestPane();
    this.resizeEastPane();

    ctrlGraph.clickedGraphView();

    if (!$('#btnTabGraph').hasClass("ui-state-active")) {
        $('#btnTabGraph').addClass("ui-state-active");
    }
   
    $('#btnTabMap').removeClass("ui-state-active");
    $('#btnTabHelp').removeClass("ui-state-active");
    $('#top_btManageData').removeClass("ui-state-active");
}

clsLayoutCtrl.prototype.resizeEastPane = function() {
    // locates the help description in the right place
    resizeHelp();
    // etc ...
    if (this.layoutMode == "map") {
        $('#divTreeGraphsPlots').hide();

        var width_ = $('#eastPane').width();
        var height_ = $('#eastPane').height();
        height_ -= 100;

        $("#treeSensorTypes_env").html("");
        $("#treeSensorTypes_box").html("");


        if (dataManager.dataMode == "data") {
            $("#divSensorTypes_env_title").html("Environmental Sensors");
            $("#divSensorTypes_box_title").html("Internal Sensors");
            if (ctrlUserdata.login)
                ctrlTag.UserLogin();
            else
                ctrlTag.UserLogout();
            $("#divAccDetail_title").show();
            $("#divAccDetail").show();

            $('#divSensorTypes_env').height(height_ * 0.3);
            $('#divSensorTypes_box').height(height_ * 0.25);
            $('#divAccQuick').height(30);
            $('#divAccQuick').width(width_ - 2);
            $('#btnQuickCSV').show();

            $('#btnQuickGraph').height(16);
            $('#btnQuickCSV').height(16);
            $('#btnQuickGraph').css("font-size", "12px");
            $('#btnQuickGraph').width((width_ / 2) - 40);
            $('#btnQuickCSV').width((width_ / 2) - 40);
            $('#divAccDetail').height(height_ * 0.35);
            $('#sltPlot').height((height_ * 0.35) - 50);
            width_ -= 20;
            $('#divCreate').width(width_ * 0.3);
            $('#divDelete').width(width_ * 0.3);
            $('#divEdit').width(width_ * 0.25);
            $('#btnURL').width(width_ * 0.8);
        }
        else {
            $("#divSensorTypes_env_title").html("LogType");
            $("#divSensorTypes_box_title").html("Tables");
            $("#divLocation_title").html("Box");
            $("#divAccDetail_title").hide();
            $("#divAccDetail").hide();

            $('#divSensorTypes_env').height(height_ * 0.5);
            $('#divSensorTypes_box').height(height_ * 0.4);
            $('#divAccQuick').height(60);
            $('#divAccQuick').width(width_ - 2);
            $('#btnQuickCSV').hide();
            $('#btnQuickGraph').css("font-size", "17px");
            $('#btnQuickGraph').height(30);
            $('#btnQuickGraph').width(width_ - 50);

            //$('#divAccDetail').height(height_ * 0.35);
            //$('#sltPlot').height((height_ * 0.35) - 50);
            //width_ -= 20;
            //$('#divCreate').width(width_ * 0.3);
            //$('#divDelete').width(width_ * 0.3);
            //$('#divEdit').width(width_ * 0.25);
            //$('#btnURL').width(width_ * 0.8);
        }
    }
    else if (this.layoutMode == "graph") {
        $('#divSensSelect').hide();

        width_ = $('#eastPane').width();
        height_ = $('#eastPane').height();
        if (ctrlSmallMap.toggle == true) {
            height_ -= 300;
        }
        else {
            height_ -= 100;
        }
        $('#divTreePlots').height(height_);
        width_ -= 80;

        if (ctrlUserdata.login == true) {
            $('#aDraw').width(width_ * 0.32);
            $('#aErase').width(width_ * 0.32);
            $('#aGetCSV').show();
            $('#aGetCSV').width(width_ * 0.32);
        }
        else {
            $('#aDraw').width(width_ * 0.5);
            $('#aErase').width(width_ * 0.5);
            $('#aGetCSV').hide();
        }
        //ctrlSmallMap.render_Map();
    }
}

clsLayoutCtrl.prototype.resizeEastMap = function() {
    $('#divSensSelect').hide();
        
    var width_ = $('#eastPane').width();
    var height_ = $('#eastPane').height();
    
    if(ctrlSmallMap.toggle == true)
    {
        height_ -= 300;
    }
    else
    {
        height_ -= 100;
        //ctrlSmallMap.render_Map();
    }
    $('#divTreePlots').height(height_);
    width_ -= 80;
    
    if(ctrlUserdata.login == true)
    {
        $('#aDraw').width(width_ * 0.32);
        $('#aErase').width(width_ * 0.32);
        $('#aGetCSV').show();
        $('#aGetCSV').width(width_ * 0.32);
    }
    else
    {
        $('#aDraw').width(width_ * 0.5);
        $('#aErase').width(width_ * 0.5);
        $('#aGetCSV').hide();
    }  
}

clsLayoutCtrl.prototype.resizeWestPane = function() {
	// locates the help information in place
	resizeHelp();
	// etc...
	if (this.layoutMode == "map") {
		var scrollvisible = document.getElementById('divLocations').scrollHeight > $('#divLocations').height() ? true : false;
		var width_ = $('#westPane').width();
		if (scrollvisible) {
			if ((width_ - 17) % 26 != 0) {
				this.mainPane.sizePane("west", 26 * Math.round($('#westPane').width() / 26) + 27);
				return;
			}
		} else if ((width_) % 26 != 0) {
			this.mainPane.sizePane("west", 26 * Math.round($('#westPane').width() / 26));
			return;
		}
		var height_ = $('#westPane').height();
		height_ -= 65;
		//$('#paneSensSelection').width(width);
		document.getElementById('divLocations').style.width = (width_ - 1) + 'px';
		//$('#divLocations').width((width_ - 2);
		$('#headSltSites').width(width_ - 10);
		$('#divSites').width(width_);
		$('#divSites').height(height_ * 0.3);
		$('#headSltLocations').width(width_ - 10);
		$('#divLocations').width(width_);
		$('#divLocations').height(height_ * 0.7);
	}
	else if (this.layoutMode == "graph") {
		var width_ = $('#westPane').width();
		var height_ = $('#westPane').height();
		height_ -= 60;
		$('#divTreeGraphs').width(width_);
		$('#divTreeGraphs').height(height_);
	}
}

clsLayoutCtrl.prototype.resizeCenterPane = function() 
{
    if(this.layoutMode == "map")
    {
        //$('#divGraph').hide();
        var width_ = $('#centerPane').width() - 3;
        var height_ = $('#centerPane').height();
        
        height_ -= $('#divDesc').height();
        
        $('#graph_maptime_Pane').height(height_);
        $('#graph_maptime_Pane').width(width_);
        $('#divDesc').width(width_- 5);
        
        ctrlMap.Resize();
        ctrlTime.Resize();
    }
    else if(this.layoutMode == "graph")
    {
        ctrlGraph.Resize();
        $('#divMapAndTime').hide();
    }
    
}

clsLayoutCtrl.prototype.resizeAll = function() 
{
    if(this.layoutMode == "map")
    {
        //$('#divGraph').hide();
        $('#divTreeGraphsPlots').hide();
        $('#divSensSelect').show();
    }
    else if(this.layoutMode == "graph")
    {
        $('#divMapAndTime').hide();
        $('#divSensSelect').hide();
    }
}

clsLayoutCtrl.prototype.BeforeDIalog = function() 
{
    if(this.layoutMode == "map")
    {
        ctrlTime.SetWindowless();
    }
    else if(this.layoutMode == "graph")
    {
        ctrlGraph.HideAllGraphs();
    }
}

clsLayoutCtrl.prototype.AfterDIalog = function() 
{
    if(this.layoutMode == "map")
    {
        ctrlTime.Resize();
    }
    else if(this.layoutMode == "graph")
    {
	    ctrlGraph.ShowAllGraphs();
    }
}
