/* -----------------------------------------------------------------

	In this file:

	1. Define windows
	
		var myWindow = function(){ 
			new MochaUI.Window({
				id: 'mywindow',
				title: 'My Window',
				loadMethod: 'xhr',
				contentURL: 'pages/lipsum.html',
				width: 340,
				height: 150
			});
		}

	2. Build windows on onDomReady
	
		myWindow();
	
	3. Add link events to build future windows
	
		if ($('myWindowLink')){
			$('myWindowLink').addEvent('click', function(e) {
				new Event(e).stop();
				jsonWindows();
			});
		}

		Note: If your link is in the top menu, it opens only a single window, and you would
		like a check mark next to it when it's window is open, format the link name as follows:

		window.id + LinkCheck, e.g., mywindowLinkCheck

		Otherwise it is suggested you just use mywindowLink

	Associated HTML for link event above:

		<a id="myWindowLink" href="pages/lipsum.html">My Window</a>	


	Notes:
		If you need to add link events to links within windows you are creating, do
		it in the onContentLoaded function of the new window.


   ----------------------------------------------------------------- */
// we dont initilise anything so just create the function as empty for now.
initializeWindows = function(){
	

	// create the DDrapes Legend popup
	MochaUI.legendWindow = function(){	
		new MochaUI.Window({
			id: 'legend',
			title: 'Legend Key',
			loadMethod: 'xhr',
			contentURL: 'pages/numaLegend.html',
			width: 250,
			height: 350,
			x: 50,
			y: 100,
			resizable: false,
			maximizable: false,
			minimizable: true,
			contentBgColor: '#f8f8f8'
		});
	}

	if ($('legendLinkCheck')){
		$('legendLinkCheck').addEvent('click', function(e){	
			MochaUI.legendWindow();
			new Event(e).stop();
		});
	}
	

	
	// create the search DDrapes popup
	MochaUI.srchWindow = function(){	
		new MochaUI.Window({
			id: 'drapeSearch',
			title: 'Search DemographicDrapes',
			loadMethod: 'xhr',
			contentURL: 'pages/numaSearch.html',
			width: 550,
			height: 400,
			x: 90,
			y: 140,
			resizable: false,
			maximizable: false,
			minimizable: true,
			useCanvas: true,
			controlsOffset: false,
			contentBgColor: '#f8f8f8'
		});
	}
	
	if ($('srchOpener')){
		$('srchOpener').addEvent('click', function(e){	
			MochaUI.srchWindow();
			new Event(e).stop();
		});
	}


MochaUI.RptWindow = function(){
		var id = "rptAccordian";
		new MochaUI.Window({
			id: id,
			title: 'Area Information',
			width: 300,
			height: 400,
			scrollbars: false,
			resizable: false,
			maximizable: true,	
			contentBgColor: '#f8f8f8',
			content:'',	
			x: 80,
			y: 80,		
			padding: { top: 0, right: 0, bottom: 0, left: 0 },
			onContentLoaded: function(windowEl){
				this.windowEl = windowEl;
				var accordianDelay = function(){
					new Accordion('#' + id + ' h3.accordianToggler', "#" + id + ' div.accordianElement',{
						start: 'all-closed',
						opacity: false,
						alwaysHide: true,
						onActive: function(toggler, element){
								toggler.addClass('open');
						},
						onBackground: function(toggler, element){
								toggler.removeClass('open');
						},							
						onStart: function(toggler, element){
							this.windowEl.accordianResize = function(){
								MochaUI.dynamicResize($(id));
							}
							this.windowEl.accordianTimer = this.windowEl.accordianResize.periodical(10);
						}.bind(this),
						onComplete: function(){
							this.windowEl.accordianTimer = $clear(this.windowEl.accordianTimer);
							MochaUI.dynamicResize($(id)) // once more for good measure
						}.bind(this)
					}, $(id));
				}.bind(this)
				accordianDelay.delay(10, this); // Delay is a fix for IE
							}
		});
	}	


    numaloadReport = function(layer,x,y){
		MochaUI.RptWindow(); 
	 	var rptO = new numaRptObj();
		//var id = 'drapeReport';
		var req = new Request.JSON({url: rptO.getCallURL(layer), onSuccess: function(jobj){
			rptO.getHTMLReportList(jobj,x,y,'rptAccordian');
		}}).get();
		


	 }	


    
	//myTips = new Tips($$('.treeNodeText')); //.treeNodeText is class of all leafs of the tree in treeGen.js
	
	// create the Address assistant popup		
	MochaUI.AddressHelpWin = function(country, geocode, search, selector, selForm, selBox, message, srs, result, searchRequest, noItems)
	{
		new MochaUI.Window({
			id: 'addAssist',
			title: 'Address Assistant',
			loadMethod: 'xhr',
			contentURL: 'pages/numaAddrHelp.html',						
			width: 300,
			height: 350,
			x: 70,
			y: 120,
			resizable: false,
			maximizable: false,
			minimizable: true,
			onContentLoaded: function(){addrAssistant(country, search, selector, selBox, message, srs, result, searchRequest, noItems);},
			contentBgColor: '#f8f8f8'
			});
	};	



	// create opacity slider element
	mySliderDiv = $('opacSlider');
	mySlider = new Slider(mySliderDiv, mySliderDiv.getElement('.knob'), 
	{
		steps: 11, // There are 10 steps
		range: [0,10], // Minimum value is 0
		wheel: false, // can use mouse wheel
		snap: false, // snaps to nearest value in range
		onChange: function(value)
		{
			if(DemoDrape && legendSld != '') {
				newOpacity = (parseFloat(value)/10).toFixed(1);
				DemoDrape.setOpacity(newOpacity);
				legendBuilder("numaLegendDiv", legendSld, null)
			}
		}
	}).set(10);		// this makes sure it ios set to fully opaque first up
	// Set Tips Style
	guiTips = new Tips($$('.numaTips'));
	
	
	MochaUI.notification('Hint: Clicking a poligon area shows more information about that area when drapes are loaded.');
	

}



// Initialize MochaUI when the DOM is ready (domready)
window.addEvent('domready', function(){
	MochaUI.Desktop = new MochaUI.Desktop();
	MochaUI.Dock = new MochaUI.Dock();

	/* 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: 'sideColumn1',
		initclosed: true,
		placement: 'left',
		resizeLimit: 500
		});
*/	 

	new MochaUI.Column({
		id: 'mainColumn',
		placement: 'main',	
		width: null
		
	});


	new MochaUI.Column({
		id: 'sideColumn2',
		placement: 'right',	
		width: 300
	});
	
	// first panel main column 
	new MochaUI.Panel({
		id: 'mapGuiPanel',
		title: 'Map Panel',
		loadMethod: 'xhr',
		contentURL: 'pages/numaMapArea.html',
		column: 'mainColumn'
	});
	$('mapGuiPanel').setStyle('overflow', 'visible');
	

	
	new MochaUI.Panel({
		id: 'guiUtilityPanel',
		title: 'Utility Panel',
		loadMethod: 'xhr',
		contentURL: 'pages/numaUtilTabs.cfm',
		column: 'sideColumn2',
		tabsURL: 'pages/panel-tabs.html',
		addClass: 'padding0'
	});

	
	/*new MochaUI.Panel({
		id: 'drapeReport',
		title: 'Info Panel',
		column: 'sideColumn2',
		contentBgColor: '#fff',
		content:'',
		scrollbars: 1
	});*/	
	


	new MochaUI.Panel({
		id: 'guiAdPanel',
		title: 'Ad Panel',
		loadMethod: 'iframe',
		contentURL: 'pages/googAdPanel.cfm',
		column: 'sideColumn2',
		height: 270,
		scrollbars: 0
	});
	



	
	MochaUI.Desktop.desktop.setStyles({
		'background': '#fff',
		'visibility': 'visible'
	});

	initGlobals();
	MochaUI.Modal = new MochaUI.Modal();
	initializeWindows();	
});


// This runs when a person leaves your page.
window.addEvent('unload', function(){
	if (MochaUI) MochaUI.garbageCleanUp();
});
