

var energyIndex = 1;
var periodicalEnergy;
var energyRefresh = 30;
var energyFiles = 1082;
var omnilocationMode = 'omni';

initializeWindows = function()
{
	MochaUI.refreshOmni = function()
	{
		devicesToTrack = new Array();
		initializeGEViews(omnilocationMode)
	}
	MochaUI.addAdminEvent = function(elementAddEventFor, formToGet, actionToSend, linkToExecute)
	{
		$(elementAddEventFor).addEvent('click', function(e){
		    new Event(e).stop();
		    var addAdminRequest = new Request.JSON({
			    url: actionToSend,
			    onSuccess: function(myResponse)
			    {
			          if(myResponse.success)
			          {
			        	    $(linkToExecute).fireEvent('click');
			        	    closeWindow('addAdminWindow');
			          }
			          else
			          {
			              $('errorMsg').set('text', myResponse.error);
			          }
			    },
			    onFailure: function(){
			    	$('errorMsg').set('text', 'The Request could not be completed. Please try again later.');
			    }
			});
			addAdminRequest.send($(formToGet).toQueryString());
		});
	}
	MochaUI.getOmniVersion = function()
	{
		var geVersion = 'N/A';
		try
		{
			geVersion = getDocFrame('gePanel_iframe').getEarthVersion();
		}
		catch(err)
		{
			geVersion = 'N/A';
		}

		MochaUI.aboutWindow(geVersion);
	}

	MochaUI.showLiveTracking = function()
	{
		getDocFrame('gePanel_iframe').showLiveTracking();
		//getDocFrame('gePanel_iframe').getMarkers();
		MochaUI.reloadAssetPanel();
	}

	MochaUI.reloadAssetPanel = function()
	{
		MochaUI.updateContent({'loadMethod': 'xhr', 'element':  $('assetPanel'), 'childElement': 'dynamicAssetList', 'url':'./controller?action=showDynamicRecentAssets&seedTime=' + $time()});
	}

	MochaUI.aboutWindow = function(geVersion){
        new MochaUI.Window({
            id: 'aboutWindow',
            title: 'OmniLocation',
            loadMethod: 'xhr',
            contentURL: './controller?action=showIndex&page=aboutWindow',
            type: 'modal',
            width: 350,
            height: 165,
            //contentBgColor: '#e5e5e5 url(images/logo2.gif) left 3px no-repeat',
            //padding: { top: 43, right: 12, bottom: 10, left: 12 },
            scrollbars:  false,
            closable: true,
            onContentLoaded: function(){
                if($('gePluginVersion')){      $('gePluginVersion').set('text', geVersion);  }
                if($('geRequirementsLink'))
                {
                	$('geRequirementsLink').addEvent('click', function(e){
            			new Event(e).stop();
	                	/*new MochaUI.Window({
				            id: 'geRequirementsWindow',
				            title: 'GE Requirements',
				            loadMethod: 'iframe',
				            contentURL: 'http://earth.google.com/support/bin/answer.py?answer=20701&topic=15099',
				            scrollbars:  true,
				            closable: true
				        });
				        */

				        MochaUI.closeWindow($('aboutWindow'));
				     });
                }
            }
        });
    }

    MochaUI.loginWindow = function(sessionExpired){
    	if(!sessionExpired){	sessionExpired = false; }
        new MochaUI.Window({
            id: 'login',
            title: 'Login',
            loadMethod: 'xhr',
            contentURL: './controller?action=showIndex&page=loginModal',
            type: 'modal',
            modalOverlayClose: false,
            width: 350,
            height: 195,
            //contentBgColor: '#e5e5e5 url(images/logo2.gif) left 3px no-repeat',
            //padding: { top: 43, right: 12, bottom: 10, left: 12 },
            scrollbars:  true,
            closable: false,
            onContentLoaded: function(){
            	if(sessionExpired)
            	{
            		errorMsg.set('html', 'Session has expired.<br> Please Login Again.');
            	}
            }
        });
    }

    /*
    * Confirmation Message Window.
    * Allows to create/show a Modal window that will Ask The User Yes/No.
    * Trigger the CallBack Function.
    * Receives:
    *          loadOptions(Array):
    *								confirmationMessage: Message To show
    *								width
    *								height
    *								title:	defaults to empty
    *								loadMethod: defaults to xhr
    *
    */
    MochaUI.showConfirmation = function(loadOptions, yesCallback, noCallback){
    	if(!loadOptions){	loadOptions = {'width': 200, 'height': 100, 'title': '', 'loadMethod': 'xhr'};	}
        if(!loadOptions.loadMethod){	loadOptions.loadMethod = 'xhr';		}
        if(!loadOptions.title){	loadOptions.title = '';		}
        if(!loadOptions.width){	loadOptions.width = 200;		}
        if(!loadOptions.height){	loadOptions.height = 100;		}
        var seedTime = new Date();
    	new MochaUI.Window({
            id: 'confirmationWindow',
            title: loadOptions.title,
            loadMethod: loadOptions.loadMethod,
            contentURL: './controller?action=showIndex&page=confirmationWindow',
            type: 'modal2',
            width: loadOptions.width,
            height: loadOptions.height,
            //contentBgColor: '#e5e5e5 url(images/logo2.gif) left 3px no-repeat',
            //padding: { top: 43, right: 12, bottom: 10, left: 12 },
            scrollbars:  true,
            closable: false,
            onContentLoaded: function(){
            	if($('confirmationMessage')){
            		$('confirmationMessage').set('text', loadOptions.confimartionMessage);
            	}
                if($('confirmYesButton')){
                      $('confirmYesButton').addEvent('click',function(e){
                        MochaUI.closeWindow($('confirmationWindow'));
                        eval(yesCallback)(e);
                	   });
                }
                if($('confirmNoButton')){
                      $('confirmNoButton').addEvent('click',function(e){
                        MochaUI.closeWindow($('confirmationWindow'));
                        eval(noCallback)(e);
                		});
             	}
            }
        });
    }

    if ($('loginLink')){
        $('loginLink').addEvent('click', function(e){
            new Event(e).stop();
            MochaUI.loginWindow(false);
        });
    }
    MochaUI.logout = function()
    {
    	var logoutOptions = {'confimartionMessage': 'Are you sure You want to Log Out'};
    	MochaUI.showConfirmation(loadOptions, confirmYes, confirmNo);
    }

    /*
    //check to see if the admin module was on first. If so. disable it.
    	//Enable GePanel.
    	//Enable Asset Panel
    	//Enable all other panels.
    */
    MochaUI.showAdmin = function()
    {
        if(viewAdmin)
        {
            $('gePanel_header').setStyle('display', 'none');
	        MochaUI.updateContent({'element':  $('gePanel'),'loadMethod': 'iframe', 'url':'./controller?action=showPage&page=gePanel'});

	        showPanel('overMapPanel');
	        showPanel('assetMenuPanel');
	        viewAdmin = false;
	     }
    }
    MochaUI.collapseTaskPanel = function()
    {
    	if(showTaskPanel)
		{
			$('mainTaskPanel_collapseToggle').fireEvent('click');
			showTaskPanel = false;
		}
    }
    MochaUI.asetPanelContent = function()
    {
    	viewAdmin = true;
    	if (omnilocationMode == 'omni' || omnilocationMode == 'ec09')
    	{
    		MochaUI.updateContent({'element':  $('assetPanel'), 'url':'./controller?action=showPage&page=assetPanel'});
    		MochaUI.showAdmin();

    		if (omnilocationMode == 'ec09')
    		{
    			hidePanel('assetMenuPanel');
    		}
    	}
    	else if (omnilocationMode == 'rmgbox')
    	{
    		// toglePanel('leftBottomPanel');
    		hidePanel('assetMenuPanel');
    		hidePanel('overMapPanel');
    		MochaUI.updateContent({'element':  $('assetPanel'), 'url':'./controller?action=showPage&page=assetFinderForm'});
    	}
    }

    MochaUI.overMapWindow = function(top)
    {
    	if(viewAdmin){	return; }
        var height = MochaUI.findOverMapTopHeight('gePanel');
        //alert('height: ' + height);
        if(!top)
        {
            top = MochaUI.findTopPos('gePanel') - (height + 10);
        }
        else
        {
            top = top - (height + 10);
        }
        if(height < 200)
        {
            height = 200;
            top = MochaUI.findTopPos('gePanel') - (height + 10);
        }
        //alert('top: ' + top);

        //alert(elDimensions.height);
        //alert(elDimensions);
        new MochaUI.Window({
            id: 'overMap',
            title: 'Overview',
            loadMethod: 'iframe',
            contentURL: './controller?action=showOverMap',
            width: 200,
            height: height,
            //contentBgColor: '#e5e5e5 url(images/logo2.gif) left 3px no-repeat',
            //padding: { top: 43, right: 12, bottom: 10, left: 12 },
            scrollbars:  false,
            closable: false,
            minimizable: true,
            maximizable: false,
            footerHeight: 0,
            headerHeight: 0,
            x: 1,
            //y: (elDimensions.height - 100),
            y: top,
            container: 'gePanel',
            restrict: true,
            collapsible: false,
            minimizable: false,
            maximizable: false,
            closable: false,
            draggable: true,
            resizable: true,
            useCanvas: true
        });
    }
    if ($('overMapLink'))
    {
        $('overMapLink').addEvent('click', function(e){
            new Event(e).stop();
            //$('overMapPanel_collapseToggle').fireEvent('click');
            toglePanel('overMapPanel');
        });
    }

    if ($('flyToLink'))
    {
        $('flyToLink').addEvent('click', function(e){
            new Event(e).stop();
            MochaUI.flyToWindow();
        });
    }

    if ($('adminLink'))
    {
        $('adminLink').addEvent('click', function(e){
            new Event(e).stop();
            if(viewAdmin){  return; }
            //show list on asset panel
            MochaUI.updateContent({'element':  $('assetPanel'), 'url':'./controller?action=showPage&page=adminOptions'});
			if($('gePanel_iframe')){		$('gePanel_iframe').src = './controller?action=showIndex&page=noLogin';		}
			MochaUI.updateContent({'element':  $('gePanel'), 'loadMethod':'xhr', 'url':'./controller?action=showIndex&page=noLogin'});

            hidePanel('assetMenuPanel');
            hidePanel('overMapPanel');
            viewAdmin = true;
        });
    }

    MochaUI.flyToWindow = function()
    {
        new MochaUI.Window({
            id: 'searchLocationWindow',
            title: 'Where to?',
            loadMethod: 'xhr',
            contentURL: './controller?action=showPage&page=flyToForm',
            width: 230,
            height: 260,
            x: 20,
            y: 100,
            scrollbars:  true,
            closable: true,
            minimizable: false,
            maximizable: false,
            collapsible: false,
            draggable: true,
            resizable: true,
            onContentLoaded: function(){
            	if($('addressLine1'))
            	{
            		$('addressLine1').focus();
				}
            }
        });
    }

    MochaUI.assetFinderWindow = function()
    {
        new MochaUI.Window
        ({
            id: 'assetFinderWindow',
            title: 'Asset Finder',
            loadMethod: 'xhr',
            contentURL: './controller?action=showPage&page=assetFinderForm',
            width: 230,
            height: 320,
            x: 20,
            y: 100,
            scrollbars:  true,
            closable: true,
            minimizable: false,
            maximizable: false,
            collapsible: false,
            draggable: true,
            resizable: true,
            onContentLoaded: function()
            {
            	if($('radius'))
            	{
            		$('radius').focus();
				}
            }
        });
    }

    MochaUI.energy = function()
    {
    //1082 files
    //30 seconds
		if(energyIndex <= energyFiles)
    	{
			getDocFrame('gePanel_iframe').energy(energyIndex);
		}
		else
		{
			energyIndex = 1;
			getDocFrame('gePanel_iframe').energy(energyIndex);
		}
		energyIndex++;
    }

    if ($('energyStartLink')){
	    $('energyStartLink').addEvent('click', function() {
	    	//check if clicking on link again. to stop
	    	if(energyIndex == 1)
	    	{
				MochaUI.energy();
				periodicalEnergy = MochaUI.energy.periodical((energyRefresh * 1000));
				$('energyStartLink').set('html', 'Energy Stop');
			}
			else
			{
				$clear(periodicalEnergy);
				$('energyStartLink').set('html', 'Energy Start');
				energyIndex = 1;
			}
		});
	}

	if ($('presetsLink')) {
	    $('presetsLink').addEvent('click', function() {
	    	MochaUI.presetOptionsWindow($time());
		});
	}

	if ($('picturesLink')) {
	    $('picturesLink').addEvent('click', function() {
	    	MochaUI.picturesWindow($time());
		});
	}

	MochaUI.picturesWindow = function(seedTime)
    {
        new MochaUI.Window({
            id: 'picturesWindow',
            title: 'Pictures',
            loadMethod: 'iframe',
            contentURL: './controller?action=loadAssetPicturesDesktop',
            width: 210,
            height: 225,
            scrollbars:  true,
            closable: true,
            minimizable: false,
            maximizable: false,
            x: 10,
            container: 'desktop',
            restrict: true,
            collapsible: false,
            draggable: true,
            resizable: true,
            useCanvas: true
        });
    }

	MochaUI.presetOptionsWindow = function(seedTime)
    {
        new MochaUI.Window({
            id: 'presetOptionsWindow',
            title: 'Presets',
            loadMethod: 'xhr',
            contentURL: './controller?action=showPage&page=presetOptions',
            width: 210,
            height: 225,
            scrollbars:  true,
            closable: true,
            minimizable: false,
            maximizable: false,
            x: 10,
            container: 'assetPanel',
            restrict: true,
            collapsible: false,
            draggable: true,
            resizable: true,
            useCanvas: true,
            onClose: function () {
        		stopIntercept();
    		}
        });
    }

    MochaUI.traceOptionsWindow = function(imei, riderId, deviceId)
    {
        new MochaUI.Window({
            id: 'TraceOptions',
            title: 'Trace Options',
            loadMethod: 'xhr',
            contentURL: './controller?action=traceSelectByAsset&imei=' + imei + '&deviceId=' + deviceId + '&riderId=' + riderId,
            width: 200,
            height: 200,
            //contentBgColor: '#e5e5e5 url(images/logo2.gif) left 3px no-repeat',
            //padding: { top: 43, right: 12, bottom: 10, left: 12 },
            scrollbars:  true,
            closable: true,
            minimizable: false,
            maximizable: false,
            x: 10,
            container: 'assetPanel',
            restrict: true,
            collapsible: false,
            draggable: true,
            resizable: true,
            onContentLoaded: function(){
            	if($('startDate'))
            	{
              		myCal1 = new Calendar({ startDate: 'd/m/y' }, { direction: -1, tweak: {x: -150, y: 20} });
              	}
            },
            useCanvas: true
        });
    }

	MochaUI.placemarkPopupWindow = function(inFeatureName, inFeatureDescription, inGeControls, inDirections,
		inPostalBoxStyleName, inStartStyleName, inFinishStyleName, inFinishWithPopupStyleName, inTargetStyleName)
	{
		var balloonWidth = 550;
		var balloonHeight = 200;
		var headerStartColor = [250,250,250];
  		var headerStopColor = [229,229,229];

		if (omnilocationMode == "rmgbox")
		{
	    	balloonWidth = 300;
			balloonHeight = 240;
			headerStartColor = [255,255,255];
			headerStopColor = [51,102,255];
		}

	    new parent.MochaUI.Window({
            id: 'placemarkPopup',
            title: inFeatureName,
            loadMethod: 'html',
            content: inFeatureDescription,
            width: balloonWidth,
            height: balloonHeight,
            maximizable: false,
            scrollbars:  true,
            closable: true,
            contentBgColor: '#fff',
            headerStartColor: headerStartColor,
            headerStopColor: headerStopColor,
            x: 1,
            y: 10,
            onClose: function()
	        {
				if (omnilocationMode == "rmgbox")
				{
		        	if ((inDirections.getDisplayed()) && (!inDirections.getIconsDisplayed()))
		        	{
						if (inDirections.getFinishName() != null)
						{
							inGeControls.placemarkHash.get(inDirections.getFinishName()).setStyleUrl('#' + inPostalBoxStyleName);
							if (inDirections.getPrevFinishName() != null)
							{
								inGeControls.placemarkHash.get(inDirections.getPrevFinishName()).setStyleUrl('#' + inPostalBoxStyleName);
							}
						}
		        		inGeControls.placemarkHash.get('Start Location').setStyleUrl('#' + inStartStyleName);
						inGeControls.placemarkHash.get(inDirections.getFinishName()).setStyleUrl('#' + inFinishStyleName);
						inDirections.setIconsDisplayed(true);
		        	}
		        	else
		        	{
		        		if (inFeatureName == inDirections.getFinishName())
						{
							inGeControls.placemarkHash.get(inFeatureName).setStyleUrl('#' + inFinishStyleName);
						}
						else
						{
							inGeControls.placemarkHash.get(getDocFrame('gePanel_iframe').lastPostBoxSelected).setStyleUrl('#' + inPostalBoxStyleName);
						}
		        	}
		        }
	        }
        });
	}

	MochaUI.directionsOutputWindow = function(inDirections)
	{
		var currentTime = new Date();

		var contentUrlString
			= './controller?seed='
			+ currentTime.getTime()
			+ '&action=loadDirectionsOutput'
			+ '&startAddress=' + inDirections.getStartAddress()
			+ '&finishAddress=' + inDirections.getFinishAddress();

	    new parent.MochaUI.Window({
            id: 'directionsOutput',
            title: 'Directions',
            loadMethod: 'iframe',
            contentURL: contentUrlString,
            type: 'modal',
            width: 800,
            height: 600,
            maximizable: false,
            scrollbars:  true,
            closable: true,
            contentBgColor: '#fff',
            x: 1,
            y: 10,
            onClose: function ()
            {
    		}
        });
	}

    MochaUI.interceptOptionsWindow = function(imei, seedTime)
    {
        new MochaUI.Window({
            id: 'interceptOptionsWindow',
            title: 'Intercept Options',
            loadMethod: 'xhr',
            contentURL: './controller?action=showDynamicRecentAssets&method=intercept&imeiToIntercept=' + imei + '&seedTime=' + seedTime,
            width: 210,
            height: 225,
            scrollbars:  true,
            closable: true,
            minimizable: false,
            maximizable: false,
            x: 10,
            container: 'assetPanel',
            restrict: true,
            collapsible: false,
            draggable: true,
            resizable: true,
            useCanvas: true,
            onClose: function () {
        		stopIntercept();
    		}
        });
    }

    MochaUI.intercept = function(gpsImei, targetImei)
    {
    	var editRequest = new Request({
	    	url: './controller?action=interceptTargetByImei&assetToStart=' + gpsImei + '&assetToIntercept=' + targetImei + '&seedTime=' + $time(),
		    onSuccess: function(registerResponse)
		    {
				var myResponse = JSON.decode(registerResponse);
				if(myResponse.success)
				{
					getDocFrame('gePanel_iframe').showInterceptByImei(myResponse);
					MochaUI.updateContent({'element':  $('interceptOptionsWindow'), 'loadMethod': 'html', 'content': updateInterceptInfo(myResponse)});
					startImei = gpsImei;
					interceptImei = targetImei;
					bIntercept = true;
					interceptRefresh = myResponse.refreshPeriod * 1000;
					refreshMe();
				}
				else
				{
					MochaUI.notification('No Current Data Available');
				}
		    },
		    onFailure: function(){
		    	MochaUI.notification('No Current Data Available');
		    }
		});

		editRequest.send();
    }

    function updateInterceptInfo(jsonObj)
	{
		var interceptLat = jsonObj.intercept.latitude.toFixed(2);
		var interceptLon = jsonObj.intercept.longitude.toFixed(2);
		var timeToIntercept = jsonObj.intercept.timeToIntercept.toFixed(2);
		var interceptCourse = jsonObj.intercept.interceptCourse.toFixed(2);
		var turnBearing = jsonObj.intercept.turnBearing.toFixed(2);
		var html = '<br /><span class="oltext">';
		html = html + '<b>' + jsonObj.username.device.name + '&nbsp;Intercepting&nbsp;' + jsonObj.assetToIntercept.device.name + '</b><br></br>';
		if (jsonObj.intercept.solutionFound == false)
		{
			html = html + '<font color="#FF0000"><b>No Solution Found!</b></font><br></br>';
		}
		else
		{
			//html = html + '<b>Latitude:</b></br>' + interceptLat + '<br></br>';
			//html = html + '<b>Longitude:</b></br>' + interceptLon + '<br></br>';
			html = html + '<b>Time To Intercept:</b></br>' + timeToIntercept + '&nbsp;Hours<br></br>';
			html = html + '<b>Intercept Course:</b></br>' + interceptCourse + '<br></br>';
			html = html + '<b>Turn Bearing:</b></br>' + turnBearing + '<br></br>';
		}
		html = html + '</span>';

		return html;

	}//END updateInterceptInfo

	var stopIntercept = function()
	{
		bIntercept = false;
		clearTimeout(interceptRefresh);
		getDocFrame('gePanel_iframe').hideIntercept();
		getDocFrame('gePanel_iframe').resetRefreshCounter();
		getDocFrame('gePanel_iframe').showLiveTracking();
		MochaUI.reloadAssetPanel();
	}

    MochaUI.editAdminWindow = function(action, elId, loadOptions)
    {
        if(!loadOptions){	loadOptions = {'width': 200, 'height': 100, 'title': 'Edit', 'loadMethod': 'xhr'};	}
        if(!loadOptions.loadMethod){	loadOptions.loadMethod = 'xhr';		}
        var seedTime = new Date();
        new MochaUI.Window({
            id: 'editAdminWindow',
            title: loadOptions.title,
            loadMethod: loadOptions.loadMethod,
            contentURL: './controller?action=' + action + '&id=' + elId + '&seed=' + seedTime.getTime(),
            width: loadOptions.width,
            height: loadOptions.height,
            //contentBgColor: '#e5e5e5 url(images/logo2.gif) left 3px no-repeat',
            //padding: { top: 43, right: 12, bottom: 10, left: 12 },
            //footerHeight: 0,
            //headerHeight: 0,
            //x: 1,
            //y: (elDimensions.height - 100),
            //y: 30,
            container: 'desktop',
            restrict: true,
            scrollbars:  true,
            collapsible: true,
            minimizable: false,
            maximizable: false,
            closable: true,
            draggable: true,
            resizable: true,
            useCanvas: true,
            restrict: true
        });
    }

    MochaUI.addAdminWindow = function(action, loadOptions)
    {
        if(!loadOptions){	loadOptions = {'width': 200, 'height': 100, 'title': 'New', 'loadMethod': 'xhr'};	}
        if(!loadOptions.loadMethod){	loadOptions.loadMethod = 'xhr';		}
        var seedTime = new Date();
        new MochaUI.Window({
            id: 'addAdminWindow',
            title: loadOptions.title,
            loadMethod: loadOptions.loadMethod,
            contentURL: './controller?action=' + action + '&seed=' + seedTime.getTime(),
            width: loadOptions.width,
            height: loadOptions.height,
            //contentBgColor: '#e5e5e5 url(images/logo2.gif) left 3px no-repeat',
            //padding: { top: 43, right: 12, bottom: 10, left: 12 },
            //footerHeight: 0,
            //headerHeight: 0,
            //x: 1,
            //y: (elDimensions.height - 100),
            //y: 30,
            container: 'desktop',
            restrict: true,
            scrollbars:  true,
            collapsible: false,
            minimizable: false,
            maximizable: false,
            closable: true,
            draggable: true,
            resizable: true,
            useCanvas: true,
            restrict: true
        });
    }

    MochaUI.closeAddGeofence = function()
	{
		viewAdmin = true;
        var currentTime = new Date();
        showLists('./controller?action=listGeoFences&seed=' + currentTime.getTime(), './controller?action=showIndex&page=geofenceMenu');
        MochaUI.closeAdminOptionsWindows();
	}

	MochaUI.closeAdminOptionsWindows = function()
	{
		if($('addAdminWindow'))
        {
			MochaUI.closeWindow($('addAdminWindow'));
		}
		if($('editAdminWindow'))
		{
			MochaUI.closeWindow($('editAdminWindow'));
		}
	}

    if(newLogin)
    {
        (function(){
        	MochaUI.collapseTaskPanel();
			$('rightPanel_collapseToggle').fireEvent('click');
			//$('sideColumn2_handle').setStyle('cursor', 'default');
			$('sideColumn2_handle').removeEvents('dblclick');
        	MochaUI.loginWindow(false); }).delay(1000);
     }
     else
    {
        setUseName(username, adminAccess);
    }
}//END initializeWindows

/*
 * Find the "top" possition of a given element (id)
 */
MochaUI.findTopPos = function(elID)
{
    var elDimensions = $(elID).getStyles('height', 'width');
    var topPos = elDimensions.height.substring(0, elDimensions.height.length-2);
    var top = topPos;
    return top;
}//END MochaUI.findTopPos

/*
 * Finds the height of the overMap Window
 * Set to 10% of container Panel
 */
MochaUI.findOverMapTopHeight = function(elID)
{
    var elDimensions = $(elID).getStyles('height', 'width');
    var height = elDimensions.height.substring(0, elDimensions.height.length-2);
    var height = height * 0.1;
    return height;
}//END MochaUI.findOverMapTopHeight

MochaUI.resizeTaskPanel = function()
{
	var elDimensions = $('mainTaskPanel').getStyles('height', 'width');
	var panelHeight = elDimensions.height.substring(0, elDimensions.height.length-2);
	var panelWidth = elDimensions.width.substring(0, elDimensions.width.length-2);
	$('mainTaskPanel_pad').setStyles({'height': panelHeight + 'px', 'width': panelWidth + 'px'});
	var panelWidthThird = panelWidth * 0.33;
	$('taskPanel').setStyles({'height': panelHeight + 'px', 'width': panelWidthThird + 'px'});
	$('completedWork').setStyles({'height': panelHeight + 'px', 'width': panelWidthThird + 'px'});
	$('work').setStyles({'height': panelHeight + 'px', 'width': panelWidthThird + 'px'});
}//END resizeTaskPanel

// Initialize MochaUI when the DOM is ready
window.addEvent('load', function(){
    MochaUI.Desktop = new MochaUI.Desktop();
    /* - No Dock To save empty space
     MochaUI.Dock = new MochaUI.Dock({
        dockPosition: 'bottom'
    });
    */

    /* Create Columns

    If you are not using panels then these columns are not required.
    If you do use panels, the main column is required. The side columns are optional.
    Create your columns from left to right. Then create your panels from top to bottom,
    left to right. New Panels are inserted at the bottom of their column.

    */
    new MochaUI.Column({
        id: 'sideColumn2',
        placement: 'left',
        width: 200,
        resizeLimit: [100, 200],
		onResize: function(){
			$('rightPanel_collapseToggle').fireEvent('click');
		},
		onExpand: function(){
			$('rightPanel_collapseToggle').fireEvent('click');
		}
    });

    new MochaUI.Column({
        id: 'mainColumn',
        placement: 'main',
        width: null
    });
    new MochaUI.Column({
        id: 'rightColumn',
        placement: 'right',
        width: 25,
        resizeLimit: [25, 25]
    });

    // Add panels to first side column


    // Add panels to main column
    new MochaUI.Panel({
        id: 'gePanel',
        title: '',
        loadMethod: 'xhr',
        //contentURL: './controller?action=showIndex&page=noLogin-frame',
        contentURL: './controller?action=showIndex&page=noLogin',
        column: 'mainColumn',
        panelBackground: '#000000',
        addClass: 'mochaIframe',
        scrollbars: true,
        padding: { top: 0, right: 0, bottom: 0, left: 0 }
    });

    new MochaUI.Panel({
        id: 'mainTaskPanel',
        title: '',
        loadMethod: 'xhr',
        contentURL: './controller?action=showIndex&page=noLogin',
        column: 'mainColumn',
        height: 200,
        panelBackground: '#fff',
        onExpand:function(){
            showTaskPanel = true;
        },
        onCollapse: function(){
        	showTaskPanel = false;
        },
        scrollbars:  false,
        header: false,
		padding: { top: 0, right: 0, bottom: 0, left: 0 }
    });


    // Add panels to second side column
    new MochaUI.Panel({
        id: 'leftTopPanel',
        title: "Assets",
        loadMethod: 'xhr',
        contentURL: './controller?action=showIndex&page=noLogin',
        column: 'sideColumn2',
        scrollbars: true,
        panelBackground: '#fff',
		height: 300,
        onContentLoaded: function(){
            new MochaUI.Column({
				container: 'leftTopPanel',
				id: 'leftTopPanel_mainColumn',
				placement: 'main',
				width: null
			});
			//add panels
			new MochaUI.Panel({
		        id: 'assetPanel',
		        title: "Assets",
		        loadMethod: 'xhr',
		        contentURL: './controller?action=showIndex&page=noLogin',
		        column: 'leftTopPanel_mainColumn',
		        scrollbars: true,
		        panelBackground: '#000000',
				height: 270,
				header: false,
				padding: { top: 0, right: 0, bottom: 0, left: 0 },
				addClass: 'mochaIframe',
		        onContentLoaded: function(){
		        	if (omnilocationMode == 'ec09')
		        	{
		        		if($('dynamicAssetList'))
			        	{
			        		$('dynamicAssetList').setStyle('display', 'block');
			        		MochaUI.updateContent({'loadMethod': 'xhr', 'element':  $('assetPanel'), 'childElement': 'dynamicAssetList', 'url':'./controller?action=showDynamicRecentAssets&seedTime=' + $time()});
			        	}
			        	if($('staticAssetList'))
			        	{
					   		$('staticAssetList').setStyle('display', 'block');
					   		MochaUI.updateContent({'loadMethod': 'xhr', 'element':  $('assetPanel'), 'childElement': 'staticAssetList', 'url': './controller?action=showIndex&page=showEc09StaticAssets' });
					   	}
		        	}
		        	else
		        	{
			        	if($('dynamicAssetList'))
			        	{
			        		$('dynamicAssetList').setStyle('display', 'block');
			        		MochaUI.updateContent({'loadMethod': 'xhr', 'element':  $('assetPanel'), 'childElement': 'dynamicAssetList', 'url':'./controller?action=showDynamicUserLoginAssets&seedTime=' + $time()});
			        	}
				   		if($('staticAssetList'))
			        	{
					   		$('staticAssetList').setStyle('display', 'block');
					   		MochaUI.updateContent({'loadMethod': 'xhr', 'element':  $('assetPanel'), 'childElement': 'staticAssetList', 'url': './controller?action=showIndex&page=showStaticAssets' });
					   	}
					}

		        }
		    });
		    new MochaUI.Panel({
		        id: 'assetMenuPanel',
		        title: "",
		        loadMethod: 'xhr',
		        contentURL: './controller?action=showIndex&page=noLogin',
		        column: 'leftTopPanel_mainColumn',
		        scrollbars: false,
		        panelBackground: '#fff',
		        height: 30,
		        header: false,
		        padding: { top: 0, right: 0, bottom: 0, left: 10 }
		    });
        }
    });

    new MochaUI.Panel({
        id: 'overMapPanel',
        title: 'OverMap',
        loadMethod: 'iframe',
        contentURL: './controller?action=showIndex&page=noLogin-frame',
        column: 'sideColumn2',
        scrollbars: true,
        panelBackground: '#fff',
        height: 200,
        header: false,
		padding: { top: 0, right: 0, bottom: 0, left: 0 },
        onContentLoaded: function(){
            //getDocFrame('gePanel_iframe').
        }
    });

    //Task Panels - Inside mainTaskPanel
    new MochaUI.Column({
		container: 'mainTaskPanel',
		id: 'mainTaskPanel_leftColumn',
		placement: 'left',
		width: 270,
		resizeLimit: [200, 600]
	});

	new MochaUI.Column({
		container: 'mainTaskPanel',
		id: 'mainTaskPanel_mainColumn',
		placement: 'main',
		width: null,
		resizeLimit: [200, 600]
	});
	new MochaUI.Column({
		container: 'mainTaskPanel',
		id: 'mainTaskPanel_rightColumn',
		placement: 'right',
		width: 270,
		resizeLimit: [200, 600]
	});

	new MochaUI.Panel({
		header: false,
		id: 'taskPanel',
		loadMethod: 'xhr',
		contentURL: './controller?action=showIndex&page=omni-bottom',
		column: 'mainTaskPanel_leftColumn',
		panelBackground: '#fff'
	});

	new MochaUI.Panel({
		header: false,
		id: 'planedWorkPanel',
		loadMethod: 'xhr',
		contentURL: './controller?action=showIndex&page=noLogin',
		column: 'mainTaskPanel_mainColumn',
		panelBackground: '#fff'
	});

	new MochaUI.Panel({
		header: false,
		id: 'completedWorkPanel',
		loadMethod: 'xhr',
		contentURL: './controller?action=showIndex&page=noLogin',
		column: 'mainTaskPanel_rightColumn',
		panelBackground: '#fff'
	});

	new MochaUI.Panel({
		header: false,
		id: 'rightPanel',
		loadMethod: 'xhr',
		contentURL: './controller?action=showIndex&page=noLogin',
		column: 'rightColumn',
		panelBackground: '#fff',
		scrollbars: false,
		height: 300,
		padding: { top: 0, right: 0, bottom: 0, left: 0 }
	});

    MochaUI.Modal = new MochaUI.Modal();

    MochaUI.Desktop.desktop.setStyles({
        'background': '#000000',
        'visibility': 'visible'
    });

    initializeWindows();
	//$('rightColumn').setStyle('display', 'none');
	//hidePanel('rightPanel');

});

// This runs when a person leaves your page.
window.addEvent('unload', function(){
    if (MochaUI) MochaUI.garbageCleanUp();
});


function getDocFrame(frameName)
{
    var iframe = document.getElementById(frameName);
    return iframe.contentWindow;
}//END getDocFrame

function confirmYes()
{

}
function confirmNo()
{

}

function refreshIntercept()
{
	MochaUI.intercept(startImei, interceptImei);
}

function refreshMe()
{
	if (bIntercept)
	{
		return interceptRefresh = setTimeout('refreshIntercept()', interceptRefresh);
	}
 }