///////////////////////////////////////////////////////////////////
//
//					CORE
//					Window Class Version 2.0
//					Bases on Mootools 1.2.1 / CoreExtensionLib
//					Copyright by Christoph Stitz
//
///////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////
//Info
///////////////////////////////////////////////////////////////////
/*

Bugs:

	-im safari wird das onscrollevent auch bei den divscrollen abgefeueret und die
		fenster minimieren sich

	-im netscape klapt das onscroll auch nicht richtig und fenster werden nicht ausgefadet
	
	-im opera überlagern sich die scrollbars beim maximieren auch
		
	- internetexplorer geht der mausfokus der fenster flöten - evtl ist das kein richtiger bug
		kann aber trotzdem etwas verwirren

	- im netscape verschwinden unsere buttons nach dem normalisieren
		evtl hängt das mit dem nicht gesetzten relativen zindex zusammen

	- im internetexplorer hat ein iframe nicht 100% der größe, wenn sein inhalt geändert wird...
		er schrumpft dann zusammen und wird erst mit einem resize des fensters angepasst
			-es sollte aus diesem grund neben dem content noch die option einer url mit
			integriert werden und der iframe als fester bestandteil dieser klasse so eingebunden werden
			- so kann bei einem auftreten des onload events der iframe angepasst werden
		
	- der Trick mit dem Overlayer beim vergrößern oder verschieben der fenster bezogen
		auf iframes scheint beim ie nicht so wirklich zu funktionieren...obwohl ein div
		bei diesen vorgängen über die gesammte seite gelegt wird scheint die maus bei
		framekontakt ihren fokus zu verlieren...
		(evtl lag das an nem style tag fehler der aber schon behpben ist (noch mal testen) )

behobene Bugs:
	
	-im internetexplorer scheinen selectboxen durch divs und damit durch unsere fenster
		im internetexplorer werden nun hinter den content in die divs iframes injiziert
		um somit ein durchscheinen zu verhindern - allerdings lässt es sich nicht verhindern,
		dass die selectboxen durch den rahmen scheinen
		- Danke an Microsoft für diese tolle programmierarbeit -
	
	-beim vergrößern und verkleinern der fenster im ie wird nicht immer
		auch der inhalt mit vergrößert -> repariert indem die höhe
		zusätzlich wärend des resizens manuell angepasst wird - da es scheinbar nicht
		möglich ist die größen danach wieder auf 100% zu setzen muss nun auch
		beim maximieren und normalisieren manuell nachgeholfen werden
		-Der einfachheit halber wurde eine bugfixfunktion geschrieben...
		- Danke dafür mal wieder an Microsoft -

Todo:
	- eventuell die funktion setContent wieder entfernen?
		
*/

///////////////////////////////////////////////////////////////////
//Globals
///////////////////////////////////////////////////////////////////
var CoreWindowArray = new Array();//save all windows
var CoreWindowStartZ = 1001;//start z-index from here

var CoreWindowOverlayer = null;
var CoreWindowFlowBar = null;


///////////////////////////////////////////////////////////////////
//Functions
///////////////////////////////////////////////////////////////////
function CoreWindowToFront(handle)//function to bring window to front
{
	if(handle.zindex!=CoreWindowArray.length)//if window is not on top
	{
		for (var i = 0; i < CoreWindowArray.length; i++)//for each window
		{
			if(CoreWindowArray[i].zindex>handle.zindex)//if index is heigher than the handle index
			{
				CoreWindowArray[i].zindex = CoreWindowArray[i].zindex-1;//put it down one step
				CoreWindowArray[i].main.setStyle('z-index', CoreWindowArray[i].zindex);//update me in dom
			}
		}
		handle.zindex = CoreWindowArray.length+CoreWindowStartZ;//bring me to front now
		handle.main.setStyle('z-index', handle.zindex);//update me in dom
		//alert("Mein z-index ist jetzt "+handle.zindex+"!");
	}
	else//restore my z
	{
	handle.win.setStyle('z-index', handle.zindex);//restore z
	}
}

function CoreWindowNormalizeAll()
{
	for (var i = 0; i < CoreWindowArray.length; i++)
	{
		if(CoreWindowArray[i].maximized==true)
		{
			CoreWindowArray[i].normalize();
		}
	}
}

function CoreWindowIEBugFix_1(that)
{
	//<<<<<<<<internet explorer fix>>>>>>>>>>>
	if(navigator.appName=="Microsoft Internet Explorer")
	{
		that.border2.setStyle('height',that.main.getSize().y);
		that.border4.setStyle('height',that.main.getSize().y);
		that.content.setStyle('height',that.main.getSize().y);
	}
	//<<<<<<<<internet explorer fix>>>>>>>>>>>
}

document.addEvent( 'domready' , function()
{

	//create FlowBar
	CoreWindowFlowBar = new Element ( 'div' , { 
	'style' : 'position:absolute;z-index:9000;top:0px;right:50px;width:100px;height:0px;'
	});
	CoreWindowFlowBar.injectInside($(document.body));
	CoreWindowFlowBar.fade(0);//internet explorer fix
	
	//create CoreWindowOverlayer
	CoreWindowOverlayer = new Element ( 'div' , { 
	'style' : 'overflow:hidden;position:absolute;z-index:9999;top:0px;left:0px;height:0px;'
	});
	CoreWindowOverlayer.injectInside($(document.body));
	
	
	//scroll event
	window.addEvent( 'scroll' , function(){CoreWindowNormalizeAll()});
	document.addEvent( 'scroll' , function(){CoreWindowNormalizeAll()});
	
});

///////////////////////////////////////////////////////////////////
//Class
///////////////////////////////////////////////////////////////////
function CoreWindow()
{
	
	//copy to array
	CoreWindowArray.push(this);
	
	//private
	var that = this; //copy handel for lower classes	
	
	//public
	
	this.maximized = false;
	
	//options
	this.id = CoreWindowArray.length;//use window count as id
	this.height = 200;
	this.width = 200;
	this.top = 0;
	this.left = 0;
	this.zindex = this.id + CoreWindowStartZ;//z = id + Zstart
	this.dragable = true;
	this.resizeable = true;
	this.html = "";
	this.css = "default";
	this.overflow = "auto";
	this.title = "";
	
	this.create = function()
	{
	
		//create window div
		this.main = new Element('div',{'id':'core_window_'+this.id,'class':this.css+'_bg','style':'position:absolute;z-index:'+this.zindex+';top:'+Number(this.top+25)+'px;left:'+Number(this.left+25)+'px;width:'+Number(this.width-50)+'px;height:'+Number(this.height-50)+'px;'});
		this.main.injectInside($(document.body));
		this.main.setStyle('opacity','0');
		
		//<<<<<<<<internet explorer fix>>>>>>>>>>>
		if(navigator.appName=="Microsoft Internet Explorer")
		{
			this.main.set('html',"<iframe height='100%' width='100%'>");
		}
		//<<<<<<<<internet explorer fix>>>>>>>>>>>
		
		//bring to front on click
		this.main.addEvent( 'click' , function()
		{
			CoreWindowToFront(that);
		});
		
		//create border
		this.border1 = new Element ( 'div' , {'class':this.css+'_border1','style':'overflow:hidden;position:absolute;top:-25px;left:0px;height:25px;width:100%;'}) ;
		this.border1.injectInside(this.main);
		this.border2 = new Element ( 'div' , {'class':this.css+'_border2','style':'overflow:hidden;position:absolute;top:0px;right:-25px;height:100%;width:25px;'}) ;
		this.border2.injectInside(this.main);
		this.border3 = new Element ( 'div' , {'class':this.css+'_border3','style':'overflow:hidden;position:absolute;bottom:-25px;left:0px;height:25px;width:100%;'}) ;
		this.border3.injectInside(this.main);
		this.border4 = new Element ( 'div' , {'class':this.css+'_border4','style':'overflow:hidden;position:absolute;top:0px;left:-25px;height:100%;width:25px;'}) ;
		this.border4.injectInside(this.main);
		
		//create edges
		this.edge1 = new Element ( 'div' , {'class':this.css+'_edge1','style':'overflow:hidden;position:absolute;top:-25px;left:-25px;height:25px;width:25px;'}) ;
		this.edge1.injectInside(this.main);	
		this.edge2 = new Element ( 'div' , {'class':this.css+'_edge2','style':'overflow:hidden;position:absolute;top:-25px;right:-25px;height:25px;width:25px;'}) ;
		this.edge2.injectInside(this.main);	
		this.edge3 = new Element ( 'div' , {'class':this.css+'_edge3','style':'overflow:hidden;position:absolute;bottom:-25px;right:-25px;height:25px;width:25px;'}) ;
		this.edge3.injectInside(this.main);	
		this.edge4 = new Element ( 'div' , {'class':this.css+'_edge4','style':'overflow:hidden;position:absolute;bottom:-25px;left:-25px;height:25px;width:25px;'}) ;
		this.edge4.injectInside(this.main);	
		
		//create title
		this.border1.set('html',this.title);
		
		//create content
		this.content = new Element ( 'div' , {'class':this.css+'_content','style':'overflow:'+this.overflow+';position:absolute;top:0px;left:0px;height:100%;width:100%;'}) ;
		this.content.injectInside(this.main);
		this.setContent(this.html);
		
		//create close
		this.close = new Element ( 'div' , {'class':this.css+'_close','style':'border:0px solid #000000;overflow:hidden;position:absolute;top:-25px;right:0px;height:25px;width:25px;'}) ;
		this.close.injectInside(this.main);
		this.close.addEvent( 'click' , function(){ that.hide(); });
		
		//create maximize
		this.max = new Element ( 'div' , {'class':this.css+'_max','style':'border:0px solid #000000;overflow:hidden;position:absolute;top:-25px;right:25px;height:25px;width:25px;'}) ;
		this.max.injectInside(this.main);
		this.max.addEvent( 'click' , function(){ that.toggle_maximize(); });
		
		//make dragable
		new Drag(this.main, {
		snap: 0,
		handle: this.border1,
		onStart: function()
		{
			that.main.setStyle('z-index', '8888');//bring to front
			that.show_overlayer();
		},
		onComplete: function()
		{	
			CoreWindowToFront(that);
			that.update_options();
			that.hide_overlayer();
			if(that.top<0)//set it back if out of screen
			{
				that.main.setStyle('top',25);
				that.update_options();
			}
			if(that.left<0)//set it back if out of screen
			{
				that.main.setStyle('left',25);
				that.update_options();
			}
		}
		});
		
		//make resizeable
		this.main.makeResizable({
		handle: this.edge3,
		limit: {x: [100, null], y: [100, null]},
		snap: 0,
		onStart: function()
		{
			CoreWindowToFront(that);
			that.show_overlayer();
		},
		onDrag: function()
		{
			CoreWindowIEBugFix_1(that);
		},
		onComplete: function()
		{
			that.update_options();
			that.hide_overlayer();
		}
		});
		
	};//create
	
	this.show_flowbar = function()
	{
		this.close.setStyle('top',0);
		this.max.setStyle('top',0);
		this.close.injectInside(CoreWindowFlowBar);
		this.max.injectInside(CoreWindowFlowBar);
		CoreWindowFlowBar.setProperty('class',this.css+'_bg');
		CoreWindowFlowBar.setStyle('top',$(document.body).getScroll().y);
		CoreWindowFlowBar.setStyle('height',25);
		CoreWindowFlowBar.fade(1);
		
	};
	
	this.hide_flowbar = function()
	{
		this.close.setStyle('top',-25);
		this.max.setStyle('top',-25);
		CoreWindowFlowBar.fade(0);
	};
	
	this.show_overlayer = function()
	{
		CoreWindowOverlayer.setStyle('height',$(document.body).getScroll().y+getViewSizeY());
		CoreWindowOverlayer.setStyle('width',$(document.body).getScroll().x+getViewSizeX());
	};
	
	this.hide_overlayer = function()
	{
		CoreWindowOverlayer.setStyle('height',0);
		CoreWindowOverlayer.setStyle('width',0);
	};
	

	this.update_options = function()
	{
		this.height = parseInt($('core_window_'+this.id).getStyle('height'));
		this.width = parseInt($('core_window_'+this.id).getStyle('width'));
		this.top = parseInt($('core_window_'+this.id).getStyle('top'));
		this.left = parseInt($('core_window_'+this.id).getStyle('left'));
	};
	
	
	this.destroy = function()
	{

		//destroy children dom
		this.main.destroy();		
		
		//remove from array
		CoreWindowArray.array_value_delete(this.id);
		
		//destroy class data
		that = 0;
		return 0;
	
	};//destroy
	
	this.show  = function()
	{
	
	this.main.fade(1);

	};//show
	
	this.hide = function()
	{
	this.hide_flowbar();
	this.main.fade(0);
		
	};//hide

	this.hide_border = function()
	{

		this.border1.setStyle('display', 'none');
		this.border2.setStyle('display', 'none');
		this.border3.setStyle('display', 'none');
		this.border4.setStyle('display', 'none');
		this.edge1.setStyle('display', 'none');
		this.edge2.setStyle('display', 'none');
		this.edge3.setStyle('display', 'none');
		this.edge4.setStyle('display', 'none');

	};
	
	this.show_border = function()
	{
		this.border1.setStyle('display', 'block');
		this.border2.setStyle('display', 'block');
		this.border3.setStyle('display', 'block');
		this.border4.setStyle('display', 'block');
		this.edge1.setStyle('display', 'block');
		this.edge2.setStyle('display', 'block');
		this.edge3.setStyle('display', 'block');
		this.edge4.setStyle('display', 'block');
	};
	
	this.maximize = function()
	{
		this.main.setStyle('z-index', '8888');
		this.hide_border();//hide border
		this.maximized = true;//tel prog that a win is maximized
		this.show_flowbar();
		
		var myEffect = new Fx.Morph(this.main, {duration: 'short', transition: Fx.Transitions.Sine.easeOut});
		myEffect.start({
			'height': getViewSizeY()+'px', //use own function because of netscape
			'width': getViewSizeX()+'px', //use own function because of netscape
			'top': $(document.body).getScroll().y, 
			'left': $(document.body).getScroll().x  
		});
		//onComplete will not work here but:
		myEffect.addEvent('onComplete', function()
		{
			CoreWindowIEBugFix_1(that);
		});

	};
	
	
	this.normalize = function()
	{
		var myEffect = new Fx.Morph(this.main, {duration: 'short', transition: Fx.Transitions.Sine.easeOut});
		myEffect.start({
		'height': that.height-50, 
		'width': that.width-50,
		'top': that.top+25, 
		'left': that.left+25
		});
		myEffect.addEvent('onComplete', function()
		{
		that.main.setStyle('z-index', that.zindex);//restore z
		that.show_border();
		that.maximized = false;
		that.close.injectInside(that.main);
		that.max.injectInside(that.main);
		CoreWindowIEBugFix_1(that);
		});
		that.hide_flowbar();
	};
	
	this.toggle_maximize = function()
	{
		if(this.maximized==true)
		{
		this.normalize();
		}
		else
		{
		this.maximize();
		}
	};
	
	this.center = function()
	{
		this.main.setStyle('top',$(document.body).getScroll().y + (getViewSizeY()/2) - (this.height)/2)+25;
		this.main.setStyle('left',$(document.body).getScroll().x + (getViewSizeX()/2) - (this.width)/2)+25;
		this.update_options();

	};
	
	this.setContent = function(html)
	{	
		this.content.set('html',html);
	};
	
	this.setUrl = function(url)
	{	
		this.content.setStyle('overflow','hidden');//prevent firefox from drawing scrollbars
		this.content.set('html',"<iframe frameborder='0' src='"+url+"' height='100%' width='100%'>");
	};
	
}

