/**
 * virtual football field
 * based on jQuery 1.2
 * 
 * (c) 2009 hitcom new media gmbh
 */

$(document).ready(init);

var ClickedElements = new Array();

var toPay = 0;

function init() {

	/**+++++++++++++++++++++++++++**/
	$(".bought").click(function() {
		
		var fieldId = $(this).attr("id");
		
		var field = fieldId.split("-");

		/**+++++++++++++++++++++++++++**/
		$("#boughtFieldContent").load("index.php5?m=getFieldInformation&field=" + fieldId, false, function() {
			
			$("#boughtFieldInfo").fadeIn(600);
			
		});

		/**+++++++++++++++++++++++++++**/
		$("#boughtFieldClose").click(function() { 
			
			$("#boughtFieldInfo").fadeOut(600);
			
		});
		
	});
	
	/**+++++++++++++++++++++++++++**/
	$("area").click(function() {
		
		/** close (if open) the detail info window **/
		$("#boughtFieldInfo").fadeOut(600);
		
		var status = $(this).attr("class");
		
		var fieldInfo = $(this).attr("id");
		
		var field = fieldInfo.split("-");
		
		/**+++++++++++++++++++++++++++**/
		/*$(".reserved").click(function() {
			
			var doBreak = false;
			
			var fieldInfo = $(this).attr("id");
			
			var field = fieldInfo.split("-");
			
			var id = field[1] + "-" + field[2] + "-" + field[3];
			
			for(var i=0;i<=ClickedElements.length;i++) {
				
				if (ClickedElements[i] == id) {
					
					$("#field-" + id).remove();
					
					ClickedElements.splice(i, 1);
					
					$("#fieldCount").html(ClickedElements.length + " Feld" + ((ClickedElements.length == 1) ? "" : "er") + "</span>");
					
					toPay = parseFloat(toPay) - parseFloat( $(this).attr("price") );
					
					$("#amountCount").html(toPay + " &euro;");
					
					var doBreak = true;
					
				}
				
				if (doBreak == true) break;
				
			}
			
		});*/
				
		
		if (in_array($(this).attr("id"), ClickedElements)) {
			
			$("#infoBox").html("Das Feld '" + field[0] + "-" + field[1] + "' wurde bereits f&uuml;r sie vorgemerkt.");
			
		} else {
						
			ClickedElements[ClickedElements.length] = $(this).attr("id");
			
			toPay = parseFloat(toPay) + parseFloat( $(this).attr("price") );
			
			$("#infoBox").html("Das Feld '" + field[0] + "-" + field[1] + "' wurde f&uuml;r Sie vorgemerkt.<br /><br />Gesamt: <span id='fieldCount'>" + ClickedElements.length + " Feld" + ((ClickedElements.length == 1) ? "" : "er") + "</span><br />Betrag: <span id='amountCount'>" + toPay + " &euro;</span>"); 
			
			$("#howItWorks").html("<a href='javascript:;' id='finish'>Weiter zur Bestellung</a>").css("visibility","visible");

			/**+++++++++++++++++++++++++++**/
			$("#reservedInfo").html("Das Feld wurde f&uuml;r Sie vorgemerkt.").fadeIn(300, function() {
				
				/**+++++++++++++++++++++++++++**/
				$(".reserved").click(function() {
					
					var doBreak = false;
					
					var fieldInfo = $(this).attr("id");
					
					var field = fieldInfo.split("-");
					
					var id = field[1] + "-" + field[2] + "-" + field[3];
					
					for(var i=0;i<=ClickedElements.length;i++) {
						
						if (ClickedElements[i] == id) {
							
							$("#field-" + id).remove();
							
							ClickedElements.splice(i, 1);
							
							$("#fieldCount").html(ClickedElements.length + " Feld" + ((ClickedElements.length == 1) ? "" : "er") + "</span>");
							
							toPay = parseFloat(toPay) - parseFloat( $(this).attr("price") );
							
							$("#amountCount").html(toPay + " &euro;");
							
							var doBreak = true;
							
						}
						
						if (doBreak == true) break;
						
					}
					
				});
				

				
				$(this).fadeOut(2600);
				
			});
			
			newTop = ( 3 + ( field[0] * 10 ) - 10 );
			
			newLeft = ( 3 + ( field[1] * 10 ) - 10 );
			
			$("#footballField").append("<div id='field-" + field[0] + "-" + field[1] + "-" + field[2] + "' class='reserved' price='" + $(this).attr("price") + "' style='cursor:pointer;top:" + newTop + "px;left:" + newLeft + "px;'></div>");
			
		}
		

		/**+++++++++++++++++++++++++++**/
		$("#finish").click(function() {
			
			document.location.href = "https://sslsites.de/kunstrasen-seedorf.de/index.php5?c=order&e=" + ClickedElements;
			
		});
		
	});
	
	
	
}


/**
 * equivalent to phps function in_array()
 */
function in_array(item, array) {
	
	for(p=0;p<array.length;p++) if (item == array[p]) return p;
	
	return false;
	
}
