var contentmaskCoords = new Object();
var searching = false;
var pseudobuttonMorph, pseudobuttonTween,textfieldMorph,coords,myScroller;

function setStartCoordinates()
{
	playfieldTop = $('PlayField').getCoordinates().top;
	playfieldLeft = $('PlayField').getCoordinates().left;
	contentmaskCoords = $(TextMask).getStyles('top', 'left', 'height', 'width');
	contentmaskCoords.top = contentmaskCoords.top.toInt();
	contentmaskCoords.left = contentmaskCoords.left.toInt();
	contentmaskCoords.height = contentmaskCoords.height.toInt();
	contentmaskCoords.width = contentmaskCoords.width.toInt();
	contentmaskCoords = checkTextmaskMaxMin(contentmaskCoords);
	coords = $(TextMask).getCoordinates();
	coords = checkTextmaskMaxMin(coords);
}

function displayTextfield()
{
	$(TextMask).setStyles( {'top':(contentmaskCoords.top+buttonHeight), 'left':contentmaskCoords.left,  'height': contentmaskCoords.height, 'overflow': 'hidden'});
	$('PseudoButton').setStyles({'top':(contentmaskCoords.top+playfieldTop), 'left':(contentmaskCoords.left+playfieldLeft), 'width':contentmaskCoords.width});
	
	if($(TextContainer).innerHTML.clean().length == 0 || pageId == '1')
	{
		$('PseudoButton').setStyle('visibility', 'hidden');
		$(TextMask).setStyle('visibility', 'hidden');
	}
	else
	{
		$('PseudoButton').setStyle('visibility', 'visible');
	}
}

function setNewData()
{
	pageId = this.no;
	setOpacity(pageId);
	removeBrackets();
	setBrackets(this.level);
	clearSpecialActions()
	hideScrolling();
	$(TextMask).setStyle( 'visibility', 'hidden');
	$('PseudoButton').setStyle('visibility', 'hidden');
	
	if(pageId != startPage)
	{
		var TM = 'TextMask'+pageId;
		var contentCont = new Element('div', { 'id': TM, 'styles':{ 'position':'absolute' } }).injectInside($('Page'));
		coords = $(TM).getCoordinates();
		coords = checkTextmaskMaxMin(coords);
		contentCont.dispose();
	}
	else
	{
		coords.top = contentmaskCoords.top;
		coords.left = contentmaskCoords.left;
		coords.height = contentmaskCoords.height;
		coords.width = contentmaskCoords.width;
		coords = checkTextmaskMaxMin(coords);
	}
	
	var elCoords =this.getCoordinates();
	buttonHeight = elCoords.height;
	this.setStyle('background-position', -(200-elCoords.width)+'px 0px');
	$('PseudoButton').empty();
	$('PseudoButton').setStyles({ 'visibility':'hidden', 'top':elCoords.top, 'left':elCoords.left, 'height':elCoords.height, 'width':elCoords.width });
	
	setTitle(this.levelCode);
}

function getText()
{
	Request.send('p='+pageId);
	this.setStyle('background-position', '0px 0px')
	if(pageId != '1')
	{
		$('PseudoButton').setStyle('visibility', 'visible');
		setPath();
		pseudobuttonMorph.start({ left:(coords.left+playfieldLeft)+'px', top:(coords.top+playfieldTop)+'px'});
	}
}

function showText(html)
{
	$(TextContainer).set('text', '');
	$(TextContainer).adopt(html);
	
	$(TextMask).setStyles({'top':(coords.top+buttonHeight+1), 'left':coords.left, 'width':coords.width, 'height':coords.height });
	adjustScrolling();
	$(TextMask).setStyles({ 'width':0, 'height':0 });
	if(searching)
	{
		
		$(TextMask).setStyle( 'visibility', 'visible');
		textfieldMorph.start({ width: coords.width+'px', height: coords.height+'px' });
		setTimeout("showScrolling()", textfieldOpeningTime);
		setTimeout("checkForSpecialAction()", textfieldOpeningTime*2);
		searching = false;
	}
	else
	{
		if(pageId != '1')
		{
			setTimeout("openText()", shootingTime);
			setTimeout("checkForSpecialAction()", textfieldOpeningTime*2);
		}
	}
}

function initScroller()
{
	myScroller = new ScrollControl($(TextContainer), {'htmlElementPrefix': '', 'controlOffset': 0}, $(TextMask),$('Scroller'),$('ScrollTrack'), $('ScrollKnob'), $('ScrollKnobCenter'), $('ScrollUpBtn'), $('ScrollDownBtn'))
	showScrolling();
}

function adjustScrolling()
{
	myScroller.adjustScroller();
}

function showScrolling()
{
	myScroller.showScroller();
}
function hideScrolling()
{

	myScroller.hideScroller();
}

function openText()
{
	$(TextMask).setStyle( 'visibility', 'visible');
	pseudobuttonTween.start( 'width', coords.width );
	setTimeout('showCompletePath()', textfieldOpeningTime);
	textfieldMorph.start({ width: coords.width+'px', height: coords.height+'px' });
	setTimeout("showScrolling()", textfieldOpeningTime);
}
function setPath()
{
	var firstOne = false;
	
	for(var i=(linkTexts.length-1); i>=0; i--)
	{
		var path;
		if(linkTexts[i].length)
		{
			if(!firstOne)
			{
				path = new Element('span', {
									'html': linkTexts[i],
									'class': 'clicked'}).adopt();
				$('PseudoButton').adopt(path);
				if(i == 1)
					path.setStyle('width', 170);
				firstOne = true;
			}
			else
			{
				path = new Element('span', {
									'html': linkTexts[i],
									'styles': {
									'visibility': 'hidden'}}).adopt();
				if(i == 1)
					path.setStyle('width', 170);
				$('PseudoButton').adopt(path);
			}
		}
								
	}
}
function showCompletePath()
{
	var pathEl = $('PseudoButton').getElements('SPAN');
	for(var i=0; i<pathEl.length; i++)
		pathEl[i].setStyle('visibility', 'visible');
}

function checkTextmaskMaxMin(obj)
{
	if((obj.top + obj.height) > textmaskMaxHeight || obj.top < textmaskMinTop)
		obj.top = textmaskMinTop;
	if((obj.top + obj.height) > textmaskMaxHeight)
		obj.height = textmaskMaxHeight;
	if((obj.left + obj.width) > textmaskMaxWidth || obj.left < textmaskMinLeft)
		obj.left = textmaskMinLeft;
	if((obj.left + obj.width) > textmaskMaxWidth)
		obj.width = textmaskMaxWidth;
	return obj;
}
// TODO: check if there is a possibility to change the method
var Request = new Request.HTML({
	url: 'getText.php',
	method: 'get',
	onSuccess: function(html){
		showText(html);
	},
	onFailure: function(){
		showText('<p><strong>Es gab einen Fehler</strong></p><p>Versuchen Sie bitte, die Seite zu einem späteren Zeitpunkt noch einmal aufzurufen.</p><p>Vielen Dank!</p>');
		textInside = true;
	}
});
