﻿	//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 = [];




window.addEvent('domready',function(){
	var teaserButton = $('teaserButton');

	if(false && teaserButton) {
		teaserButton.removeProperty('onClick');
		request = new Request.HTML({
			method: 'get',
			url: '/display.asp?page=play',
			onFailure: function (xhr) {
				//window.location = '/login.asp';
			},
			onSuccess: function (tree, elems, html, js) {
				var elem = $('mb_tournament_register');
				if (elem) {
					elem.set('html', html);
				} else {
					elem = new Element('div', {
						id: 'mb_tournament_register',
						html: html,
						styles: {
							display: 'none'
						}
					});
					$$('body').grab(elem);
				}
								
				Mediabox.open('#mb_tournament_register', '', '500 200');
			}.bind(this)
		});
		
		teaserButton.addEvent('click',function(e) {
			e.preventDefault();
			request.send();
		});
	}
});





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 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 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 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);

/* ist nur für Finalblow
window.addEvent('load',function(){
	if(!$(document.body).hasClass('small')){
		$$('html')[0].setStyle('background-image','url('+thePath+'/imgs/back_teaser.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);






