/*

name			spcWebApp UI & Internals
author			Mark de Jong
copyright		(c) Mark de Jong
website			http://www.spellcoder.nl
version			december 2009

*/

var windowidcount = 0;

var sections	= Array();



function productC()
{
	var self = this;
	addEvent(window, 'resize', function() { self.onresize(); });
}

productC.prototype.onresize = function()
{
	//console.group('Sending resize event to windows');

	for(var tel=0; tel<this.windows.length; tel++)
	{
		//console.log(this.windows[tel].name);

		if (this.windows[tel].config && this.windows[tel].config.onviewportresize)
			this.windows[tel].config.onviewportresize();
	}
	
	//console.groupEnd();
}

productC.prototype.openWindow = function(settings)
{
	var self = this;
	
	windowidcount++;
	//console.log('Opening window with name "'+settings.name+'"');

	if (!settings.zindex)
	{
		var highestz = this.getHighestWindowZindex();
		settings.zindex = highestz >= 100 ? highestz + 1 : /*minwindowzindex*/100;
	}
	
	settings.id = windowidcount;
	settings.onbeforeclose = function()
				 			{
				 				self.deregisterWindow(this.id,true);
				 			}

	var winobj = { id:		windowidcount
				 , name:	settings.name
				 , obj:		new spcWindow(settings)
				 };

	this.windows.push(winobj);

	return winobj.obj;
}

productC.prototype.deregisterWindow = function(id,dontclose)
{
	//console.log('Deregisteren window #'+id);
	
	for(var tel=0; tel<this.windows.length; tel++)
	{
		if (this.windows[tel].id == id)
		{
			// clean up event handlers
			this.windows[tel].onresize = null;

			if (!dontclose)
				this.windows[tel].obj.close();
			
			// remove the window info object from the array
			this.windows.splice(tel,1);

			return;
		}
	}
	
	console.error('Tried to deregister unknown window '+name);
}

productC.prototype.closeWindow = function(name)
{
	this.deregisterWindow(name,false);
}

productC.prototype.getWindowByName = function(name)
{
	for(var tel=0; tel<this.windows.length; tel++)
		if (this.windows[tel].name == name)
			return this.windows[tel].obj;

	return null;
}

productC.prototype.getHighestWindowZindex = function()
{
	var highestz = 0;
	for(var tel=0; tel<this.windows.length; tel++)
	{
		var wndobj = this.windows[tel].obj;

		if (wndobj.config.zindex > highestz)
			highestz = wndobj.config.zindex;
	}
	//console.log('highest z index is '+highestz);
	return highestz;
}

productC.prototype.setWindowToFront = function(wnd)
{
	var highestz = this.getHighestWindowZindex();

	// ignore if the window is already on top
	if (wnd.config.zindex == highestz)
		return;

	var newzindex = highestz >= 100 ? highestz + 1 : /*minwindowzindex*/100;
	wnd.config.zindex = newzindex;
	
	if (wnd.window)
		wnd.window.style.zIndex = newzindex; // FIXME: dont mess with window itself here
	else if (wnd.isclosing)
	{
		// if this happens, we are probably called toFront during onclose()
	}
	else
	{
		console.group();
		console.log('Failed to move window to front. No window DOM existant anymore.');
		console.log(wnd);
		console.groupEnd();
	}
}



function protoSection(name)
{
	this.name		= name;
	this.frames		= new Array();
	this.root		= true;

//	methods
	this.addFrame	= section_addFrame;
}
function section_addFrame(frmName) {
	this.frames[this.frames.length] = frmName;
}

function newSection(name) {
	return sections[name] = new protoSection(name);
}



productC.prototype.switchFrame = function(frameName)
{
	var objRef = document.getElementById(frameName);

	if (typeof objRef == 'undefined') {
		console.error('unknown frameName '+frameName);
		return;
	}

	if (objRef.style.display != 'block')
		objRef.style.display = 'block';
	else
		objRef.style.display = 'none';
}

productC.prototype.switchSection = function(sectionName)
{
	//console.warn('Switching to section: '+sectionName);
	
	if (window.mygmap)
		window.mygmap.removeAllAccidents();

	if (sections[sectionName] == undefined)
	{
		console.error('The requested section does not exist.');
		return;
	}

	var prevSec = sections[ product.currentSection ];
	var curSec  = sections[ sectionName ];

	if (sectionName == product.currentSection)
		return;

	if (this.onBeforeSectionChange)
		this.onBeforeSectionChange( product.currentSection , sectionName );

	
	var framesV = [];

	if (product.currentSection) {
		if (prevSec.onBeforeLeave)
			prevSec.onBeforeLeave();

		//	Make a list all frames. True means they need to be hidden.
		var seclistlen = prevSec.frames.length;

		for(var tel=0 ; tel < seclistlen ; tel++)
			framesV[ prevSec.frames[tel] ] = true;

		if (prevSec.onLeave)
			prevSec.onLeave();
	}

	//	enable layers of new section
	var seclistlen = curSec.frames.length;
	for(var tel=0 ; tel < seclistlen ; tel++)
	{
		if (framesV[ curSec.frames[tel] ])
		{
			// no need to disable this frame later, since we need it}
			framesV[ curSec.frames[tel] ] = false;
		}
		else
			setDisplay( curSec.frames[tel] , 'block' );
	}
	
	//	disable all layers we don't need anymore
	for(frameName in framesV) {
		if (framesV[frameName])
			setDisplay( frameName , 'none' );
	}
	
	if (curSec.onSelect)
		curSec.onSelect();

	product.currentSection = sectionName;

	if (this.onSectionChange)
		this.onSectionChange( product.currentSection , sectionName );
}

function setDisplay(objID,dsetting)
{
	// check if the layer exists
	if (!document.getElementById( objID )) {
		console.error('Element with id "' + objID + '" doesn\'t exist.');
		return;
	}

	document.getElementById( objID ).style.display = dsetting;
}
