var requestInProgress = false;
var arrowbox_over = false;
var seatSelected = '';
var debugmode = false;
var debugtxt = '';

function show_trailer() {
	if($('fimage')) Element.hide($('fimage'));
	if($('ftrailer')) Element.show($('ftrailer'));
	$('toggletext').innerHTML = 'Trailer ausblenden';
	return false;
}

function hide_trailer() {
	if($('fimage')) Element.show($('fimage'));
	if($('ftrailer')) Element.hide($('ftrailer'));
	$('toggletext').innerHTML = 'Trailer ansehen';
	return false;
}

function toggle_trailer() {
	if(Element.visible($('fimage')))
	{
		show_trailer();
	} else {
		hide_trailer();
	}
	return false;
}

function checkProgress(){
	if(requestInProgress)
	{
		return false;
	} else {
		requestInProgress = true;
		return true;
	}
	
}

Array.prototype.removeByValue = function(val)
{
	outArr = new Array();
	for(var i = 0; i < this.length; i++)
	{
		if(this[i] != val) outArr.push(this[i]);
	}
	return outArr;
}


function setRes(p){
	selectionData[seatSelected] = p;
	setSeatDisplay(true);
	updateOrderbox();
	arrowbox_over = false;
	closeResOptions();
	// alert('jSONtest: ' + Object.toJSON(selectionData));
}

function unsetRes(){
	selectionData[seatSelected] = undefined;
	setSeatDisplay(false);
	updateOrderbox();
	arrowbox_over = false;
	closeResOptions();
}

function setSeatDisplay(is_reserved){
	if(is_reserved)
	{
		if($(seatSelected).hasClassName('c'))
		{
			// selectedItems = selectedItems.removeByValue(seatSelected);
		    // $(seatSelected).removeClassName('c');
		} else {			
			selectedItems.push(seatSelected);
			$(seatSelected).addClassName('c');
		}
	} else {
		if($(seatSelected).hasClassName('c'))
		{
			selectedItems = selectedItems.removeByValue(seatSelected);
		    $(seatSelected).removeClassName('c');
		} else {			

		}
	}	
	var newtext = selectedItems.length + ' ' + (selectedItems.length==1?'Karte':'Karten');
	$('countertext').innerHTML = newtext;
	$('sel').value = Object.toJSON(selectionData);
}

function showResOptions(r,s,c){
	if(c!='b')
	{
		seatSelected = r + '_' + s;
		showResOptions.arrowHeight = 11;
		showResOptions.xoffset = $(seatSelected).cumulativeOffset();
		showResOptions.left = (showResOptions.xoffset[0] + $(seatSelected).getWidth() -142 );
		showResOptions.top = (showResOptions.xoffset[1] + $(seatSelected).getHeight()/2 - 146 - showResOptions.arrowHeight);
		showResOptions.boxHeight = $('arrowbox').getHeight();
		showResOptions.leftPx = showResOptions.left + 'px';
		$(seatSelected).setStyle({ zIndex: 30 });
		
		if((showResOptions.top+showResOptions.boxHeight)>550)
		{
			showResOptions.boxDir = 'up';
			showResOptions.top = showResOptions.top - showResOptions.boxHeight + 3*showResOptions.arrowHeight;
			showResOptions.topPx = showResOptions.top + 'px';
			showResOptions.arrowTop = showResOptions.boxHeight - 2*showResOptions.arrowHeight;
			showResOptions.arrowTopPx = showResOptions.arrowTop + 'px';
			$('arrowhead').setStyle({ top: showResOptions.arrowTopPx });
		} else {
			showResOptions.boxDir = 'down';
			showResOptions.topPx = showResOptions.top + 'px';
			showResOptions.arrowTop = showResOptions.arrowHeight;
			showResOptions.arrowTopPx = showResOptions.arrowTop + 'px';
			$('arrowhead').setStyle({ top: showResOptions.arrowTopPx });
		}

		$('arrowbox').setStyle({ left: showResOptions.leftPx, top: showResOptions.topPx });
		$('arrowbox_head').innerHTML = 'Reihe '+ r + ', Platz ' +s;
		// show arrowbox
		Element.show($('arrowbox'));
		if($('shadow')) Element.show($('shadow'));
		arrowbox_open = true;
	}
	return false;
}

function hideResOptions(){
	window.setTimeout("closeResOptions();",50);
}

function closeResOptions(){
	if(!arrowbox_over)
	{
		if($('arrowbox')) Element.hide($('arrowbox'));
		if($('shadow')) window.setTimeout("Element.hide($('shadow'));",50);
	}
}

function existsSeat(theSeat){
	return $(theSeat)?true:false;
}

function isFreeSeat(theSeat){
	if (!existsSeat(theSeat)) return false;
	return ($(theSeat).hasClassName('o') && !$(theSeat).hasClassName('c'));
}

function isMySeat(theSeat){
	if (!existsSeat(theSeat)) return false;
	return ($(theSeat).hasClassName('c'));
}

function neighbourLeft(theSeat){
	neighbourLeft.rs = theSeat.split('_');
	neighbourLeft.r = neighbourLeft.rs[0];
	neighbourLeft.rs[1]--;
	neighbourLeft.s = neighbourLeft.rs[1];
	neighbourLeft.seat = neighbourLeft.r + '_' + neighbourLeft.s;
	if(existsSeat(neighbourLeft.seat)) return neighbourLeft.seat;
	else return false;
}

function neighbourRight(theSeat){
	neighbourRight.rs = theSeat.split('_');
	neighbourRight.r = neighbourRight.rs[0];
	neighbourRight.rs[1]++;
	neighbourRight.s = neighbourRight.rs[1];
	neighbourRight.seat = neighbourRight.r + '_' + neighbourRight.s;
	if(existsSeat(neighbourRight.seat)) return neighbourRight.seat;
	else return false;
}

function checkSeating(){
	if(!selectedItems.length) return true;
	checkSeating.seatingIsAllowed = true;
	checkSeating.leftCloseUp = 1;
	checkSeating.rightCloseUp = 1;
	debugtxt = '';
	
	for(var i = 0; i < selectedItems.length; i++)
	{
		checkSeating.leftNeighboursFree = 0;
		checkSeating.rightNeighboursFree = 0;
		checkSeating.nl = neighbourLeft(selectedItems[i]);
		checkSeating.nr = neighbourRight(selectedItems[i]);
		if(checkSeating.nl)
		{
			if(isFreeSeat(checkSeating.nl))
			{
				checkSeating.leftCloseUp = 0;
				checkSeating.leftNeighboursFree++;
				checkSeating.nnl = neighbourLeft(checkSeating.nl);
				if(checkSeating.nnl)
				{
					if(isFreeSeat(checkSeating.nnl))
					{
						checkSeating.leftNeighboursFree++;
					}
				} else {
					checkSeating.leftNeighboursFree = 2;
				}
			}
		} else {
			checkSeating.leftNeighboursFree = 2;
		}
		if(checkSeating.nr)
		{
			if(isFreeSeat(checkSeating.nr))
			{
				checkSeating.rightCloseUp = 0;
				checkSeating.rightNeighboursFree++;
				checkSeating.nnr = neighbourRight(checkSeating.nr);
				if(checkSeating.nnr)
				{
					if(isFreeSeat(checkSeating.nnr))
					{
						checkSeating.rightNeighboursFree++;
					}
				} else {
					checkSeating.rightNeighboursFree = 2;
				}
			}
		} else {
			checkSeating.rightNeighboursFree = 2;
		}
		
		if(checkSeating.leftNeighboursFree == 1 && isMySeat(checkSeating.nr)) checkSeating.seatingIsAllowed = false;
		if(checkSeating.rightNeighboursFree == 1 && isMySeat(checkSeating.nl)) checkSeating.seatingIsAllowed = false;
		if(checkSeating.leftNeighboursFree == 1 && checkSeating.rightNeighboursFree == 1) checkSeating.seatingIsAllowed = false;
		if(checkSeating.leftNeighboursFree == 1 && checkSeating.rightNeighboursFree == 2) checkSeating.seatingIsAllowed = false;
		if(checkSeating.leftNeighboursFree == 2 && checkSeating.rightNeighboursFree == 1) checkSeating.seatingIsAllowed = false;
		
		if(debugmode) debugtxt += '<br />' + checkSeating.leftNeighboursFree + ',' + checkSeating.rightNeighboursFree;
	}
	if(checkSeating.leftCloseUp || checkSeating.rightCloseUp) checkSeating.seatingIsAllowed = true;
	return checkSeating.seatingIsAllowed;
}


function updateOrderbox(){
	var seatingIsAllowed = checkSeating();
	updateOrderbox.newtext = '<b>Aktuell ausgewählt:</b><br />';
	if(!seatingIsAllowed) updateOrderbox.newtext += '<span style="color:red; font-style: italic;line-height: 1.2em;">Bitte lassen Sie keine einzelnen Plätze frei. Die Auswahl kann erst bestätigt werden, wenn Sie entsprechend aufschließen.</span><br />';
	if(!selectedItems.length) updateOrderbox.newtext = '<b>Keine Karten ausgewählt.</b><br />';
	for(var i = 0; i < selectedItems.length; i++)
	{
		updateOrderbox.seat = selectedItems[i];
		updateOrderbox.rs = selectedItems[i].split('_');
		updateOrderbox.newtext += 'Reihe ' + updateOrderbox.rs[0] + ', Platz ' + updateOrderbox.rs[1] + ' ' + priceNames[selectionData[updateOrderbox.seat]] + '<br />';
	}

	$('kartenliste').innerHTML = updateOrderbox.newtext;
	Element.show($('kartenliste'));

	if(seatingIsAllowed){
		Element.show($('submitarea'));
	} else {
		Element.hide($('submitarea'));
	}
	if(debugmode)
	{
		$('debuginfo').innerHTML = debugtxt;
		Element.show($('debuginfo'));
	}
}
