/////////////////////////////////////////////////////////////////////
//Core Context Library
/////////////////////////////////////////////////////////////////////				

/////////////////////////////////////////////////////////////////////
//Info
/////////////////////////////////////////////////////////////////////
/*
	Version: 1.2 
	Last changed: Jan 2009
	bases on mootools lib (1.2)
	Copyright by Christoph Stitz

	- Hauptkontextmenu benötigt eine bindung an "body" und an "document"!
	- funktioniert nicht über Frames!
	- Hauptmenu fals vorhanden zuerst im dom registrieren und dann untermenus!

	- das Scrollevent wird beim ie vom body abgefeuert...bei allen anderen browsern vom
		document
*/

/////////////////////////////////////////////////////////////////////
//Bugs
/////////////////////////////////////////////////////////////////////
/*	
	-causes an error in InternetExplorer (Version 7.0.5730.13 or just 7)  on function e.stop()
		e.stop wird verwändet um das browser contextmenu zu deaktivieren
		fehler tritt nicht auf, wenn keine zusätzliche eventanbindung an document corliegt...
	
	-fehler bei der body eventanbindung im ie6 / allerding wird diese vom ie7 benötigt ? 
		evtl. an window anbinden?
	
*/
/////////////////////////////////////////////////////////////////////
//Behobene Bugs
/////////////////////////////////////////////////////////////////////
/*	
	-beim internetexplorer 6.0 funktioniert das menu nur, wenn der background-layer eine farbe hat
		???????? oder relativ ist
		-es muss eine hintergrundfarbe gesetzt werden und der div muss transparent gemacht werden
		Dank der guten Microsoft Programmmierarbeit...

*/
/////////////////////////////////////////////////////////////////////
//Todo
/////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////
//Globals
/////////////////////////////////////////////////////////////////////
var CoreContextArray = new Array();
var CoreContextBGLayer = null;
var CoreContextZStart = 2001;

/////////////////////////////////////////////////////////////////////
//Functions
/////////////////////////////////////////////////////////////////////
function CoreContextBGLayer_center()
{
	CoreContextBGLayer.setStyle('height',getViewSizeY());
	CoreContextBGLayer.setStyle('width',getViewSizeX());
	CoreContextBGLayer.setStyle('top',$(document.body).getScroll().y);
	CoreContextBGLayer.setStyle('left',$(document.body).getScroll().x);
}

function CoreContextCloseOthers(context)//funktion um alle menus zu schließen bis auf eines
{
	for (var i = 0; i < CoreContextArray.length; i++)//for each window
	{
		if(CoreContextArray[i]!=context)
		{
		CoreContextArray[i].hide();
		}
	} 
}

function CoreContextGetActive()
{
	for (var i = 0; i < CoreContextArray.length; i++)//for each window
	{
		if(CoreContextArray[i].active==true)
		{
		return CoreContextArray[i].id;//return menu id
		}
	}
	return false;//return false
}

window.addEvent('domready', function() {
	

	//deactivate browser context on document
    document.addEvent('contextmenu',function(e) {  
    e.stop(); 
	}); 
	
/*
Backgroundlayer ist momentan deaktiviert
	//create background layerbackground-color:#00ff00;
	CoreContextBGLayer = new Element ( 'div' , { 
	'style' : 'overflow:hidden;position:absolute;z-index:0;top:0;left:0;width:200px;height:200px;'
	}) ;
	CoreContextBGLayer.injectInside($(document.body));
	CoreContextBGLayer_center();
*/	
	/*<<<<<<<<<<Internet Explorer Fix>>>>>>>>>>>>*/
	/*if(navigator.appName=="Microsoft Internet Explorer")
	{
		CoreContextBGLayer.setStyle('background-color','#0000ff');
		CoreContextBGLayer.setStyle('filter','alpha(opacity=0)');
	}*/
	/*<<<<<<<<<<Internet Explorer Fix>>>>>>>>>>>>*/
	

	//close all menus on bodyclick
	document.addEvent('click',function(e) {  
		for (var i = 0; i < CoreContextArray.length; i++)//for each window
		{
		CoreContextArray[i].hide();	
		} 
	});

	//create event to resize and move background layer dynamicaly to window size
	//internet explorer
	
/*
	document.body.addEvent( 'scroll' , function()
	{
		CoreContextBGLayer_center();
	});
	//other browsers
	
	document.addEvent( 'scroll' , function()
	{
		CoreContextBGLayer_center();
	});
*/
});  

function CoreContext()
{	
	//copy to array
	CoreContextArray.push(this);
	
	//options
	this.id = CoreContextArray.length;//use Context count as id
	this.width = 225;
	this.css = "default";
	this.name = "";
	
	this.active = false;//intern var
	
	this.main = null;
	
	var that = this; // copy for subclasses

	this.create = function()
	{
		//create div
		this.main = new Element ( 'div' , { 
		'id'    : this.name,
		'class' :this.css+'_main',
		'style' : 'display:none;overflow:hidden;position:absolute;z-index:'+CoreContextZStart+';top:0;left:0;width:'+this.width+'px;height:auto;'
		}) ;
		this.main.injectInside($(document.body));
	};

	this.show = function(e)
	{
		CoreContextCloseOthers(this);
		
		this.main.setStyle('display','block');
		
		if(CoreContextGetActive()==false||CoreContextGetActive()==this.id)//if no context is active
		{
			//calculate position
			if(e.page.x>(getViewSizeX()/2)+$(document.body).getScroll().x)
			{var xpos = e.page.x-parseInt(that.width);}
			else
			{
			var xpos = e.page.x;
			}
			if(e.page.y>(getViewSizeY()/2)+$(document.body).getScroll().y)
			{var ypos = e.page.y-parseInt(that.main.getSize().y);}
			else
			{
			var ypos = e.page.y;
			}
			//set style position
			that.main.setStyle('left',xpos);
			that.main.setStyle('top',ypos);
			if(this.active==false)//if iam disabled
			{	
				this.main.fade(1);
				that.active = true;
			}

		}//check active

	};//function

	this.hide = function()
	{
		//this.main.setStyle('display','none');
		
		if(this.active==true)
		{
			this.main.fade(0);
			that.active = false;
			
		}
	};

	this.add = function(imgp,html,eval_func)
	{
		var line = new Element ( 'div' , {
		'class':this.css+'_line',
		'style' : 'position:relative;width:100%;height:25px;'
		}) ;
		line.injectInside(this.main);
		
		var img = new Element ( 'div' , {
		'class':this.css+'_imgdiv',
		'style' : 'position:absolute;top:0px;left:0px;overflow:hidden;height:100%;25px;'
		}) ;
		img.injectInside(line);		
		img.set('html','<img src="'+imgp+'" height="24" width="24">');

		var desc = new Element ( 'div' , {
		'class':this.css+'_descdiv',
		'style' : 'position:absolute;top:0px;right:0px;height:100%;width:85%;'
		}) ;
		desc.injectInside(line);
		desc.set('html',html);
			
		line.addEvent('click', function() {eval(eval_func);});		//ADD CLICK EVENT
		
		var myEffect = new Fx.Morph(desc, {duration: 500, transition: Fx.Transitions.Sine.easeOut});
		line.addEvent('mouseenter', function()
		{
			myEffect.cancel();
			myEffect.start('.'+that.css+'_descover');
		});
		line.addEvent('mouseleave', function()
		{
			myEffect.cancel();
			myEffect.start('.'+that.css+'_descdiv');
		});
	
	};
	
	this.bind = function(elem)
	{
		elem.addEvent( 'contextmenu', function(e) 
		{
		   that.show(e);
		});
	};

	
}


