/*# Metadata ################################### Header v.2
# $URL: http://gt-nas-1/svn/gamebase.frontend/trunk/xsl_gamebase/_browsergames/_xhodon/data/tools_mootools1.2.js $
# $Rev: 5338 $
# $Date: 2011-10-19 16:07:19 +0200 (Mi, 19 Okt 2011) $
# $Author: ru $
### TOC ###################################################
#
### Description ###########################################
#
#########################################################*/

	//document.domain = "gametwist.com";
var theFileName='/data/tools_mootools1.2.js';
var theFile = $$('script[src$='+theFileName+']')[0];

var theIndex = theFile.src.indexOf(theFileName);


var thePath  = theFile.src.substr(0,theIndex);


ie6 = (Browser.Engine.name == "trident" && Browser.Engine.version ==4)?true:false;
formErrors = [];









/*
Cufon.replace('\
h2, \
h3, \
h4, \
#nav a, \
button\
',{
	textShadow: 'black 1px 1px',
	color: '-linear-gradient(0.4=#ffdb70,0.5=#FFCD3A)'
});

Cufon.replace('\
#error h2, \
#error h3, \
#formError h2, \
#formError h3, \
#formOk h2, \
#formOk h3, \
#tip h2, \
#tip h3 \
');

*/


function cleanArray(array) {
	var newArray = []
	array.each(function(element) {
		newArray.push(element);
	});
	return newArray;
}



function doCufon(){
	var tmpElements;
	Cufon.now();
	
	tmpElements = $$('h2,h3,h4,h5,#nav a,#teaserButton').filter(function(element,index,array){
		return ![$('error'),$('formOk'),$('formError'),$('tip'),$('gtBar')].contains(element.getParent('div'));
	});
	
	tmpElements = cleanArray(tmpElements);
	
	Cufon.replace(tmpElements,{
		textShadow: 'black 1px 1px',
		color: '-linear-gradient(0.4=#ffd55c,0.5=#fec006)'
	});

	tmpElements = $$('.formButton').filter(function(element,index,array){
		return ($type(element.getParent('div#register')) == 'element')?false:true;									
	});
	
	tmpElements = cleanArray(tmpElements);
	
	Cufon.replace(tmpElements,{
		textShadow: '#ededed -1px -1px',
		color: '-linear-gradient(0.4=#353535,0.5=#111111)'	  			  
	})
	
	Cufon.replace('#error h2, #error h3, #formError h2, #formError h3, #formOk h2, #formOk h3, #tip h2, #tip h3');
}





function fadeShip(){
	var theShip = $("misc1");
	var blinkShip = new Fx.Style(theShip,"opacity",{
			duration: 1000,
			transition: Fx.Transitions.Bounce.easeInOut
	});

	theShip.setStyles({
		"opacity":"0",
		"display":"block"
	});
		
	blinkShip.start(0,1).chain(function(){
		(function(){
			blinkShip.start(1,0.5).chain(function(){
				blinkShip.start(0.5,1);
			});
		}).periodical(5000);
	});
}


function listTables(){
	$$(".listTable tbody tr, .formTable tbody tr").each(function(tr,index){
		tr.addClass(index % 2 ? "dark":"light");
	});
}


function fixInput(){//braucht nur der ie6
	if(!ie6) return;
	$$("input.ffInput").each(function(input,index){
		var wrapInput = new Element("span",{"class":"wrapInput"});
		wrapInput.wraps(input);
	});
}


function fixedLayout(){
	if($(document.body).hasClass('games') && screen.height > 768)
		$(document.body).addClass("fixed");
}



function showBack(){
	document.getElement("html").setStyle("background-image","url(imgs/back.jpg)");
}

function markFormErrors(){
		formErrors.each(function(el,index){
		var input = (document.getElement('input[id='+el+']'))?document.getElement('input[id='+el+']'):$(el);
		$(input.parentNode.parentNode).addClass("formError");
	});
}




function staticShip(){
	if($(document.body).hasClass("games")){
		var theShip = new Element(ie6?"div":"img",{
					"id":"teaserImg",
					"class":"morphTeaser"
				});
				if(!ie6)
					theShip.set("src",thePath+"/imgs/teaser.png");
				theShip.inject(document.body);
		}
}


function showShip(){
	 theShip = new Asset.image(thePath+"/imgs/teaser.png",{
		onload: function(){
		
			var theElement = new Element(ie6?"div":"img",{
				"id":"ship"
			});
			if(!ie6)
				theElement.set("src",thePath+"/imgs/teaser.png");
			theElement.inject(document.body);
			
			var moveShip = new Fx.Morph(theElement,{
				onComplete:function(){
					theElement.addClass("morphTeaser");
					theElement.removeProperty("style");
				//	showContent();
				}
			});
			
			
			
			moveShip.start('.morphTeaser');
			
			
		}
	});
}





function hideContent(){
	//$$("#header","#wrapper").setStyle("opacity","0");
	$("header").setStyle("top","-162px");
	$("wrapper").setStyle("margin-top",$("wrapper").getStyle("margin-top").toInt() + document.body.getSize().y);
	//showContent();
	
}
function showContent(){
	var moveHeader = new Fx.Morph($("header"),{
	//	"property":"top",
		transition: Fx.Transitions.Expo.easeOut,
		duration: "long"
		
	});
	var moveWrapper = new Fx.Morph($("wrapper"),{
		//"property":"margin-top",
		transition: Fx.Transitions.Expo.easeOut,
		duration: "long"
		
	});
	//$("header").tween("top","0");
	//$("wrapper").tween("margin-top","400");
	moveHeader.start({
		"top":[-162,0]/*,
		"opacity":[.5,1]*/
	}).chain(
		function(){
			moveWrapper.start({
			"margin-top":[$("wrapper").getStyle("margin-top").toInt() + document.body.getSize().y,400]/*,
			"opacity":[.5,1]*/
			});
		}/*,
		showShip()*/
	)
	

}




function initLanguages(){
	if($(document.body).hasClass("small") || !$('languages')) return;
	var languages = $('languages');
	languages.inject('header');
	var height = languages.getElement('li').getStyle('height').toInt();
	var minHeight = languages.getSize().y;
	var maxHeight = (languages.getElements('li').length)* height + minHeight +13;
	//alert(maxHeight)
	var morphLang = new Fx.Tween(languages,{link:'cancel',duration: 'normal',transition: Fx.Transitions.Circ.easeOut});
	var delay;
	
	languages.addEvent("mouseenter",function(evt){
		$clear(delay);
		morphLang.start('height',maxHeight)
		});
		
	languages.addEvent("mouseleave",function(evt){
		delay = (function(){morphLang.start('height',minHeight)}).delay(1000);
		});	
	
	/*var minHeight = languages.getElement('li').getSize().y;
	var maxHeight = (languages.getElements('li').length+1) * minHeight +5;
//	var morphLang = new Fx.Tween(languages,{link:'cancel',transition:Fx.Transitions.Circ.easeOut});
	var morphLang = new Fx.Tween(languages,{link:'cancel',transition:Fx.Transitions.Circ.easeOut});
	var delay;
	languages.addEvent("mouseenter",function(evt){
		$clear(delay);
		morphLang.start('height',maxHeight);
	});
	languages.addEvent("mouseleave",function(evt){
		delay = (function(){morphLang.start('height',minHeight)}).delay(1000);

	});*/		
	
 }





function setTabIndex(){//firefox hat probleme beim durchtabben - angepasste tabindexes für die ganze seite.
	
	if(!$('main') || $(document.body).hasClass('small')||$(document.body).hasClass('games_applet')) return; 
	
	var topObjects = [$$('#header h1 a')[0],$$('#lNick')[0],$$('#lPass')[0],$$('#lAutologin')[0],$$('#qLogin button')[0]].combine($$('#status a'));
	//var teaserObjects = $('teaser')?$$('#teaser').getElements('').filter(function(element,index,array){return ['a','button'].contains(element.get('tag'));}):null;
	//wegen ie
	var teaserObjects = $('teaser')?$$($A(document.body.getElementById('teaser').getElementsByTagName('*')).filter(function(element,index,array){
			return ['a','button'].contains(element.nodeName.toLowerCase());
	})):null;
		
	var navObjects = $$('#nav a');
	
	//da der ie probleme macht wird das so geschrieben
	var mainObjects = $$($A(document.body.getElementById('main').getElementsByTagName('*')).filter(function(element,index,array){
		return ['a','input','textarea','select','button'].contains(element.nodeName.toLowerCase());
	}));	
	var langObjects = $$('#languages a');
	var footerObjects = $$('#footer a');
	var gtBarObjects = $$('#gtBar a');



	
	var theObjects = [];
	theObjects.combine(gtBarObjects);
	theObjects.combine(topObjects);
	theObjects.combine(navObjects);
	theObjects.combine(mainObjects);
	theObjects.combine(footerObjects);
	theObjects.combine(langObjects);
	
	theObjects = theObjects.clean();

	theObjects.each(function(element,index,array){
		element.set('tabindex',index+1);
	});

}




function reflection(){
	$$('.theScreenshot').reflect();
}


function gameWindowLinks(){
$$('a.room').addEvent('click',function(event){
	event.preventDefault();
	openTheGameWindow(this.getProperty('href'));

});

}


function profileLinks(){
	$$('a.profile').addEvent('click',function(event){
			event.preventDefault();
			openWindow(this.getProperty('href'),'_blank','scrollbars=yes',480,450);
	});
}


function openerLinks(){
		$$('a.opener').addEvent('click',function(event){
				event.preventDefault();
				loadOpenerNew(this);
		});
}



function openTheGameWindow(url){
	var theGameSize = gameSize(1000,800);//spielgrösse errechnen
		var theURI = new URI(url);//spielgrösse an die url hängen
	theURI.setData({
		'width': theGameSize.width,
		'height': theGameSize.height
	}, true);
	
	if(top.theGameWindow && !top.theGameWindow.closed)
		top.theGameWindow.focus();
	else
		top.theGameWindow = openWindow(theURI.toString(),'_blank',theGameSize.width,theGameSize.height);//fenster öffnen
}





function gameSize(width,height){
	var ratio, sWidth, sHeight, sRatio, nWidth, nHeight, nRatio;
	//spiel grösse
	/*
	width = 1000;
	height = 800;
	*/
	ratio = width/height;
	
	//verfügbare grösse
	sWidth = screen.availWidth - 50;
	sHeight = screen.availHeight - 50;
	sRatio = sWidth/sHeight;
	
	//default werte
	nRatio = sRatio;//seitenverhältniss des schirms
	nWidth = width;//breite das spiels
	nHeight = height;//höhe des spiels
		
	 
	if(sWidth < width || sHeight < height){//wenn spiel zu gross
		nRatio = (sRatio > ratio) ? Math.pow(sRatio,-1) : sRatio;//seitenverhältniss anpassen
		nWidth = Math.round(width*nRatio);//neue breite
		nHeight = Math.round(height*nRatio);//neue höhe
	}	
	
	return {
//		'width': nWidth,
//		'height': nHeight
		'width': width,
		'height': height
	}
}






function basefolderSet(folder) {
	try {
		if(basefolderGet()) {
			basefolderGet() = folder;
			return true;
		}
		else
			return false;
	} catch(e) {
		if(self.baseFolder) {
			self.baseFolder = folder;
			return true;
		}
		else
			return false;
	}
}




function basefolderGet() {
	try {
			return basefolderGet();
	} catch(e) {
			return self.baseFolder;
	}
}



/*************** loading *********************/
//window.addEvent("domready",hideContent)


window.addEvent('loaded',gameWindowLinks);
window.addEvent('loaded',profileLinks);

window.addEvent('load',function(){
	if(!$(document.body).hasClass("small")){
		$$("html")[0].setStyle("background-image","url("+thePath+"/imgs/back.jpg)");
	}
});


window.addEvent("loaded",function(){
	if(Browser.Engine.presto)
		document.body.addClass("opera");
		doCufon();
		if($(document.body).hasClass("small")){
			$$('html')[0].setStyle("min-width","0");
		}
		else {
			$('status').inject('header');
			//$('header').getElement('h2').inject('headerWrapper');
		}	
	}
);


window.addEvent("loaded",listTables);

window.addEvent('loaded',openerLinks);

window.addEvent('loaded',markFormErrors);

window.addEvent('loaded',initLanguages);
window.addEvent('domready',setTabIndex);


//window.addEvent("loaded",fixedLayout);

//window.addEvent("loaded",staticShip);

//window.addEvent('loaded',reflection);
















//window.addEvent("domready",fixInput);




//window.addEvent("domready",formValidator);

//window.addEvent("load",showBack);
//window.addEvent("load",showShip);


//window.addEvent("load",fadeShip);
//window.addEvent("load",showStars);

