/* -----------------------------------------------------------------

	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.


   ----------------------------------------------------------------- */
function logoutZiro(){
document.location.href = "login.php"
}
var statusWorkspace=false;
function recordWorkspace(){
statusWorkspace=true;
}
function stoprecordWorkspace(){
statusWorkspace=false;
}
function eliminaById(id,sezione){
if(confirm('Sei sicuro? Cancello?')){
		
		var req = new Request({
			method: 'get',
			url: 'editor/eliminaById.php',
			data: { 'idel' :  id , 'sezioneel': sezione}
			}).send();

MochaUI.notification('Eliminazione Completata');

}
}
function eliminaElemento(id,sezione){
if(confirm('Sei sicuro? Cancello?')){
		
		var req = new Request({
			method: 'get',
			url: 'editor/eliminaElemento.php',
			data: { 'idel' :  id , 'sezioneel': sezione}
			//onComplete: function(response) { alert('Response: ' + response); }
			}).send();

MochaUI.notification('Eliminazione Completata');
parent.reloadWindowUrl('editor/archivio_documenti.php?tipo='+sezione,'editor/archivio_documenti.php?tipo='+sezione);
}
}  

var xvalue=20;
var yvalue=20;
function saveWindow(urld, titled ,type){

		var req = new Request({
			method: 'get',
			url: 'editor/workspace.php',
			data: { 'url' : urld, 'title' : titled, 'type' : type, 'function' : 'opened' }
			}).send();

	}
function removeWindow(urld, titled ,type){

		var req = new Request({
			method: 'get',
			url: 'editor/workspace.php',
			data: { 'url' : urld, 'title' : titled, 'type' : type, 'function' : 'remove' }
			}).send();

	}
function removeAllWindow(){

		var req = new Request({
			method: 'get',
			url: 'editor/workspace.php',
			data: { 'function' : 'removeAll' }
			}).send();

	}
function closeWindow(idFrame){

MochaUI.Desktop.close(idFrame);
}	
function reloadWindowUrl(idFrame,url){
	urlFrame=idFrame+"_iframe";
document.getElementById(urlFrame).src="reloading.html";
document.getElementById(urlFrame).src=url;
}
	offsetWindow=0;
	nofollow=true;
function modalDialog(urld, titled ,type){
	if(statusWorkspace)
saveWindow(urld, titled ,type);
R=255;
G=255;
B=255;
	if(type=="leftSmall"){
	wi=300;	
	he=300;
	xvalue=8;
	yvalue=160;
	}else if(type=="small"){
	wi=450;	
	he=200;
	}else if(type=="left"){
	wi=350;	
	he=300;
	xvalue=8;
	yvalue=100;
	}else if(type=="center"){
	wi=670;	
	he=450;
	xvalue=360;
	yvalue=100;
	}else if(type=="centerSmall"){
	wi=600;	
	he=250;
	xvalue=360;
	yvalue=100;
	}else if(type=="centerBottom"){
	wi=670;	
	he=150;
	xvalue=360;
	yvalue=100;
	}else if(type=="centerTop"){
	wi=670;	
	he=200;
	xvalue=360;
	yvalue=100;
	}else if(type=="immagini"){
	wi=670;	
	he=170;
	xvalue=360;
	yvalue=100;
	}else if(type=="testo"){
	wi=670;	
	he=100;
	xvalue=360;
	yvalue=100;
	}else if(type=="insiemi"){
	wi=670;	
	he=150;
	xvalue=360;
	yvalue=100;
	}else if(type=="centerTopSmall"){
	wi=450;	
	he=100;
	xvalue=360;
	yvalue=100;
	}else if(type=="statistica"){
	wi=1000;	
	he=500;
	xvalue=0;
	yvalue=100;
	}else{
	wi=670;	
	he=400;
	xvalue=360;
	yvalue=100;
	}
	if(offsetWindow>200){
	offsetWindow=0;
	}else{
	//xvalue=xvalue+offsetWindow;
	//yvalue=yvalue+offsetWindow;	
		offsetWindow=offsetWindow+20;
	}
	
	loadMetodS='iframe';
	if(type=="ext"){
	wi=1000;	
	he=420;
	xvalue=2;
	yvalue=70;
	toolbars='editor/nav/default.php?url='+urld;
	}else if(type=="extLink"){
	toolbars='editor/nav/default.php';
	}else{
	toolbars=urld+'&bar=true';
	}
	var windowstemp = new MochaUI.Window({
			id: urld,
			title: titled,
			loadMethod: loadMetodS,
			contentURL: urld,
			toolbarURL: toolbars,
			width: wi,
			height: he,
			x:xvalue,
			y:yvalue,
			headerHeight: 25,
			toolbar: true,
			footerHeight:20,
			headerStartColor: [R,G,B],
			maximizeBgColor: [255,198,0],
			closeBgColor: [255,84,0],
			minimizeBgColor: [0,198,255],
			contentBgColor: '#ffffff',
			//toolbarPosition:'bottom',
			onBlur: function(windowEl){
				//if(nofollow)
				//MochaUI.Dock.minimizeWindow(windowEl);
				//MochaUI.toggleWindowVisibility(windowEl);
				//MochaUI.closeWindow(windowEl);
				
			},
			restrict: true
		});
	
	if(type=="immagini"){
MochaUI.Desktop.maximizeWindow(windowstemp);
	}
}
initializeWindows = function(){



	MochaUI.parametricsWindow = function(){	
		new MochaUI.Window({
			id: 'parametrics',
			title: 'Benvenuto',
			loadMethod: 'xhr',
			contentURL: 'benvenuto.php',
			width: 305,
			height: 160,
			x: 230,
			y: 180,
			padding: { top: 12, right: 12, bottom: 10, left: 12 },
			resizable: false,
			maximizable: false,
			contentBgColor: '#fff'
		});
	}
	if ($('parametricsLinkCheck')){
		$('parametricsLinkCheck').addEvent('click', function(e){
			new Event(e).stop();
			MochaUI.parametricsWindow();
		});
	}

	MochaUI.clockWindow = function(){	
		new MochaUI.Window({
			id: 'clock',
			title: 'Orologio',
			addClass: 'transparent',
			loadMethod: 'xhr',
			contentURL: 'layoutFramework/plugins/coolclock/index.html?t=' + new Date().getTime(),
			onContentLoaded: function(){
				if ( !MochaUI.clockScript == true ){
					new Request({
						url: 'layoutFramework/plugins/coolclock/scripts/coolclock.js?t=' + new Date().getTime(),
						method: 'get',
						onSuccess: function() {
							if (Browser.Engine.trident) {	
								myClockInit = function(){
									CoolClock.findAndCreateClocks();
								};
								window.addEvent('domready', function(){
									myClockInit.delay(10); // Delay is for IE
								});
								MochaUI.clockScript = true;
							}
							else {
								CoolClock.findAndCreateClocks();
							}
						}.bind(this)
					}).send();
				}
				else {
					if (Browser.Engine.trident) {
						myClockInit = function(){
							CoolClock.findAndCreateClocks();
						};
						window.addEvent('domready', function(){
							myClockInit.delay(10); // Delay is for IE
						});
						MochaUI.clockScript = true;
					}
					else {
						CoolClock.findAndCreateClocks();
					}
				}
			},
			shape: 'gauge',
			headerHeight: 30,
			width: 160,
			height: 160,
			x: 20,
			y: 60,
			padding: { top: 0, right: 0, bottom: 0, left: 0 },
			bodyBgColor: [250,250,250]
		});	
	}
	if ($('clockLinkCheck')){
		$('clockLinkCheck').addEvent('click', function(e){
			new Event(e).stop();
			MochaUI.clockWindow();
		});
	}

	// Examples > Tests
	MochaUI.eventsWindow = function(){	
		new MochaUI.Window({
			id: 'windowevents',
			title: 'Window Events',
			loadMethod: 'xhr',
			contentURL: 'pages/events.html',
			onContentLoaded: function(windowEl){
				MochaUI.notification('Window content was loaded.');
			},
			onCloseComplete: function(){
				MochaUI.notification('The window is closed.');
			},
			onMinimize: function(windowEl){
				MochaUI.notification('Window was minimized.');
			},
			onMaximize: function(windowEl){
				MochaUI.notification('Window was maximized.');
			},
			onRestore: function(windowEl){
				MochaUI.notification('Window was restored.');
			},
			onResize: function(windowEl){
				MochaUI.notification('Window was resized.');
			},
			onFocus: function(windowEl){
				MochaUI.notification('Window was focused.');
			},
			onBlur: function(windowEl){
				MochaUI.notification('Window lost focus.');
			},
			width: 340,
			height: 250
		});
	}	
	if ($('windoweventsLinkCheck')){
		$('windoweventsLinkCheck').addEvent('click', function(e){
			new Event(e).stop();
			MochaUI.eventsWindow();
		});
	}


	if ($('noCanvasLinkCheck')){
		$('noCanvasLinkCheck').addEvent('click', function(e){
			new Event(e).stop();
			MochaUI.noCanvasWindow();
		});
	}

	// View
	if ($('sidebarLinkCheck')){
		$('sidebarLinkCheck').addEvent('click', function(e){
			new Event(e).stop();
			MochaUI.Desktop.sidebarToggle();
		});
	}

	if ($('cascadeLink')){
		$('cascadeLink').addEvent('click', function(e){
			new Event(e).stop();
			MochaUI.arrangeCascade();
		});
	}

	if ($('tileLink')){
		$('tileLink').addEvent('click', function(e){
			new Event(e).stop();
			MochaUI.arrangeTile();
		});
	}

	if ($('closeLink')){
		$('closeLink').addEvent('click', function(e){
			new Event(e).stop();
			MochaUI.closeAll();
		});
	}

	if ($('minimizeLink')){
		$('minimizeLink').addEvent('click', function(e){
			new Event(e).stop();
			MochaUI.minimizeAll();
		});
	}

	// Todo: Add menu check mark functionality for workspaces.

	// Workspaces

	if ($('saveWorkspaceLink')){
		$('saveWorkspaceLink').addEvent('click', function(e){
			new Event(e).stop();
			MochaUI.saveWorkspace();
		});
	}
	
	if ($('loadWorkspaceLink')){
		$('loadWorkspaceLink').addEvent('click', function(e){
			new Event(e).stop();
			MochaUI.loadWorkspace();
		});
	}

	// Deactivate menu header links
	$$('a.returnFalse').each(function(el){
		el.addEvent('click', function(e){
			new Event(e).stop();
		});
	});

	// Build windows onDomReady
	MochaUI.parametricsWindow();
	if (!Browser.Engine.trident) {
		//MochaUI.clockWindow();
	}
	else {
		//MochaUI.clockWindow.delay(500);	
	}
	
}

// Initialize MochaUI when the DOM is ready
window.addEvent('domready', function(){
	MochaUI.Desktop = new MochaUI.Desktop();
	MochaUI.Dock = new MochaUI.Dock({
		dockPosition: 'top'
	});
	MochaUI.Modal = new MochaUI.Modal();
	
	MochaUI.Desktop.desktop.setStyles({
		'background': '#fff',
		'visibility': 'visible',
		'width':'100%'
	});
	
	initializeWindows();
	inizializzaAnteprima();
});

// This runs when a person leaves your page.
window.addEvent('unload', function(){
	if (MochaUI) MochaUI.garbageCleanUp();
});
