// JavaScript Document
var _markedObjects = {
	cartBarOpen: false,
	toggleCartBar: function() {
		if(this.cartBarOpen == false) {
			$('#cartouter').animate({'width': '300px'}, '800');
			this.cartBarOpen = true;
		}
		else {
			$('#cartouter').animate({'width': '50px'}, '800');
			this.cartBarOpen = false;
		}
	},
	refreshCartContent: function() {
		//console.log("refreshe Cart");
		cleanedArray = new Array();
		
		items = $('.cartfrmObjekt');
		items.each(function(){
			//console.log($(this).attr("checked") + ": " + $(this).val());
			if($(this).attr("checked") == "checked") {
				for(i=0; i<$(window).data("markedObjects").length;i++) {
					//console.log($(window).data("markedObjects")[i][1] + "==" + $(this).val());
					if($(window).data("markedObjects")[i][1] == $(this).val()) {
						cleanedArray.push($(window).data("markedObjects")[i]);
					}
				}
			}
		});

		$(window).data("markedObjects", cleanedArray);
		this.saveToCookie();
		this.getInnerHTML();
		$('#cartinner').html("");
		this.loadDetailsForCartItems();
	},
	init : function() {
		if($(window).data("markedObjects")) {
			//console.log("init: Variable vorhanden");
		}
		else {
			////console.log("init: Variable NICHT vorhanden")
			$(window).data("markedObjects", new Array());
		}

		mySideBar = $('<div id="cartinner" style="position: absolute; top: 60px; left: 20px; height: 400px; overflow: auto;"></div>')
					.appendTo($('<div id="cartouter"><a href="javascript:void(0);" onclick="_markedObjects.toggleCartBar();"><img src="images/transparent.gif" style="width: 50px; height: 60px; border: 0;" alt="Informationstext einblenden" title="Informationstext einblenden" /></a></div>')
					//.hide()
					.appendTo('body'));
		
		$('#cartouter').html("<a href=\"javascript:void(0);\" onclick=\"_markedObjects.toggleCartBar();\"><img src=\"images/buttons/cart_dark.png\" style=\"width: 50px; height: 64px; border: 0;\" alt=\"Informationstext einblenden\" title=\"Informationstext einblenden\" /></a><div style=\"position: absolute; top: 10px; left: 60px; height: 50px; width: 230px; color: #a5b9c3;\">Über unseren Warenkorb können Sie Ihre Objekte auswählen und weitere Informationen anfordern.</div><div id=\"cartButtonBar\" style=\"position: absolute; top: 480px; left: 80px; width: 230px;\"><a href=\"javascript:void(0);\" onclick=\"_markedObjects.refreshCartContent();\"><img src=\"images/buttons/refresh.png\" style=\"width: 40px; height: 40px; border: 0; margin-left: 50px;\" alt=\"Liste aktualisieren\" title=\"Liste aktualisieren\"></a> <a href=\"6_kontakt.php\"><img src=\"images/buttons/kontakt.png\" style=\"width: 40px; height: 40px; border: 0; margin-left: 20px;\" alt=\"Kontakt\" title=\"Kontakt\"></a></div><div id=\"cartinner\" style=\"position: absolute; top: 60px; left: 60px; width: 230px; height: 400px; overflow: auto;\"></div>");


		$(window).scroll(function() {
			var scrollTop = $(this).scrollTop();
			$("#cartouter").animate({top: (scrollTop) + 60 + "px"}, 100);
		});

		var sidebar = $("#cartouter");
			//sidebar.css("display","block");
			sidebar.css("position","absolute");
			//sidebar.css("top", (($(window).height() - sidebar.outerHeight()) / 2) + $(window).scrollTop() + "px");
			sidebar.css("top", $(window).scrollTop() + 60 + "px");
			sidebar.css("right", "0px");
			sidebar.css("z-Index", 1);
			/*sidebar.css("margin-left", "-50px");*/
			//sidebar.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");

			$("#cartouter").each(function() { 
				var slideOut = $(this);  
					//slideOut.bind("mouseenter",function(){
					//	//slideOut.animate({"left": "+=80px"}, "slow");
					//	slideOut.animate({"width": "300px"}, "slow");
					//});   
					//slideOut.bind("mouseleave",function(){
					//	//slideOut.animate({"left": "-=80px"}, "fast");
					//	slideOut.animate({"width": "50px"}, "fast");
					//});
			});
			
		var blinker = $('<div id="blinker" style="width: 50px; height: 64px; position: absolute; display:none; z-index: 9999;"><img src=\"images/buttons/cart_light.png\"></div>')
					.appendTo('body');
					blinker.css("top", sidebar.css("top"));
					blinker.css("right", sidebar.css("right"));	
		
		////console.log("Init: Lese Cookie");	
		this.getFromCookie();
		////console.log("Init: erzeuge HTML");
		this.getInnerHTML();
		this.loadDetailsForCartItems();

		////console.log("Init: Sidebar");
		this.showSideBar();		
		

	},
	initCartOnly : function() {
		if($(window).data("markedObjects")) {
			////console.log("init: Variable vorhanden");
		}
		else {
			////console.log("init: Variable NICHT vorhanden")
			$(window).data("markedObjects", new Array());
		}
		this.getFromCookie();
	},
	showSideBar : function() {
		if($(window).data("markedObjects").length > 0) {
			$("#cartouter").show();
		} else {
			$("#cartouter").hide();
		}
	},
	addObject : function(obid, openimmoid) {
		////console.log("Funktionsaufruf zum hinzufügen");
		tempArray = $(window).data("markedObjects");
		alreadyIn = false;
		
		for(k=0;k<tempArray.length;k++) {
			if(tempArray[k][0] == obid) {
				alreadyIn = true;
			}
		}
		
		if(alreadyIn == false) {
			tempArray.push(new Array(obid, openimmoid));
			$(window).data("markedObjects", tempArray);
			//console.log("hinzugefügt: " + $(window).data("markedObjects").length + "=" +  $(window).data("markedObjects")[ $(window).data("markedObjects").length-1][0])
			this.getInnerHTML();
			this.showSideBar();
			//$("#blinker").effect("pulsate", { times:3 }, 2000);
		}
		
	},
	deleteObject : function(obid, openimmoid) {
		cleanedArray = new Array();
		for(i=0; i<$(window).data("markedObjects").length;i++) {
			if($(window).data("markedObjects")[i][0] != obid) {
				cleanedArray.push($(window).data("markedObjects")[i]);
			}
		}
		
		$(window).data("markedObjects", cleanedArray);
		this.getInnerHTML();
	},
	isInCart : function(obid, openimmoid) {
		////console.log("Funktionsaufruf zum hinzufügen");
		tempArray = $(window).data("markedObjects");
		
		for(k=0;k<tempArray.length;k++) {
			if(tempArray[k][0] == obid) {
				return true;
			}
		}
		
		return false;
		
		
	},	
	saveToCookie : function() {
		//$.cookie('the_cookie', 'the_value');
		if($(window).data("markedObjects").length > 0) {
			cookieElements= "";
			for(i=0; i<$(window).data("markedObjects").length;i++) {
				////console.log("zum speichern: " + i + "=" + $(window).data("markedObjects")[i][0] + "|" + $(window).data("markedObjects")[i][1]);
				cookieElements += $(window).data("markedObjects")[i][0] + "|" + $(window).data("markedObjects")[i][1];
				if(i < $(window).data("markedObjects").length - 1) {
					cookieElements += "#";
				}
			}
			////console.log("save Cookie: " + cookieElements);
			$.cookie('the_cookie', cookieElements);
		}
		else {
			this.deleteCookieInfo();
		}
	},
	getFromCookie : function() {
		//$.cookie('the_cookie', 'the_value');
		cookieElements = $.cookie('the_cookie');
		////console.log("get Cookie: " + cookieElements + ":" + typeof(cookieElements));
		if(typeof(cookieElements) != "undefined" && cookieElements != null) {
			cookieElementsArray = cookieElements.split("#");
			temparray = new Array();
			for(i=0; i<cookieElementsArray.length;i++) {
				//this.addObject((cookieElementsArray[i].split("|")[0]), (cookieElementsArray[i].split("|")[1]));
				////console.log("get Cookie: " + i + " = " + cookieElementsArray[i]);
				temparray.push(new Array(cookieElementsArray[i].split("|")[0], cookieElementsArray[i].split("|")[1]));
			}
		}
		
		if(typeof(temparray) != "undefined" && temparray != null) {
			for(k=0; k<temparray.length;k++) {
				////console.log(k + "vorher");
				this.addObject(temparray[k][0], temparray[k][1]);
				////console.log(k + "danach");
			}
		}

		this.saveToCookie();
	},
	deleteCookieInfo : function() {
		$.cookie('the_cookie', null);
	},
	deleteAll : function() {
		$(window).data("markedObjects") = null;
		this.deleteCookieInfo();
	},
	getInnerHTML : function() {
		str = "";
		myObjects = $(window).data("markedObjects");
		for(i = 0; i < myObjects.length; i++) {
			//str += "<div id=\"cartitem_" + myObjects[i][1] + "\">" + myObjects[i][0] + " [ <a href=\"javascript:void(0);\" onclick=\"_markedObjects.deleteObject('" + myObjects[i][0] + "');\">entfernen</a> ]</div>";
		}
		
		//return str;
		//$('#cartinner').html(str);
	},
	loadDetailsForCartItems : function() {
		myObjects = $(window).data("markedObjects");
		for(i = 0; i < myObjects.length; i++) {		
			$.ajax({
				url: "getobjectdata.ajax.php",
				type: "GET",
				data: {id: myObjects[i][1]},
				//context: document.body,
				success: function(response){
					json = jQuery.parseJSON(response);
					//console.log("Lade: " + json.openimmoid[0]);
					tempDiv = $("<div id='cartitem_" + json.openimmoid[0] + "' class='cartitem'></div>").appendTo($("#cartinner"));
					//$('#cartitem_' + json.openimmoid[0]).html(json.obid[0] + ", " + json.textTitle[0]);
					//tempDiv.html("<div style='width: 50px; float: left;'><input type='checkbox' name='frm_Objekte[]' id='frm_Objekte[]' value='" + json.openimmoid[0] + "' style='width: 20px;' checked='checked'></div><div style='width: 610px; float: right;'><a href='objekt_detailansicht.php?object=" + json.openimmoid[0] + "' id='" + json.openimmoid[0] + "' title=\"" + json.textTitle[0] + "\" class=\"cartItemLink\"><strong>" + json.obid[0] + "</strong>,<br /> " + json.textTitle[0] + "</a></div>");
					tempDiv.html("<div style='width: 30px; float: left;'><input type='checkbox' name='cartfrm_Objekte[]' id='cartfrm_Objekte[]' value='" + json.openimmoid[0] + "' style='width: 20px;' checked='checked' class='cartfrmObjekt'></div><div style='width: 150px; float: right;'><a href='objekt_detailansicht.php?object=" + json.openimmoid[0] + "' id='" + json.openimmoid[0] + "' title=\"" + json.textTitle[0] + "\" class=\"cartItemLink\"><strong>" + json.obid[0] + "</strong>,<br /> " + json.textTitle[0] + "</a></div><br style='clear: both;'>");
					////console.log("attache EINZELNEN reOpendialog");
					$mylink = tempDiv.find('#' + json.openimmoid[0]);
					////console.log($mylink);
					attachreOpenDialog($mylink);
					//attachreOpenDialogs();
				}
			});
		}

	},
	loadDetailsForSingleCartItem : function(obid, openimmoid) {
		$.ajax({
			url: "getobjectdata.ajax.php",
			type: "GET",
			data: {id: openimmoid},
			//context: document.body,
			success: function(response){
				json = jQuery.parseJSON(response);
					////console.log(json.openimmoid[0]);
					tempDiv = $("<div id='cartitem_" + json.openimmoid[0] + "' class='cartitem'></div>").appendTo($("#cartinner"));
					//$('#cartitem_' + json.openimmoid[0]).html(json.obid[0] + ", " + json.textTitle[0]);
					//tempDiv.html("<a href='objekt_detailansicht.php?object=" + json.openimmoid[0] + "' title=\"" + json.textTitle[0] + "\"><strong>" + json.obid[0] + "</strong>,<br /> " + json.textTitle[0] + "</a>");
					tempDiv.html("<div style='width: 30px; float: left;'><input type='checkbox' name='cartfrm_Objekte[]' id='cartfrm_Objekte[]' value='" + json.openimmoid[0] + "' style='width: 20px;' checked='checked' class='cartfrmObjekt'></div><div style='width: 150px; float: right;'><a href='objekt_detailansicht.php?object=" + json.openimmoid[0] + "' id='" + json.openimmoid[0] + "' title=\"" + json.textTitle[0] + "\" class=\"cartItemLink\"><strong>" + json.obid[0] + "</strong>,<br /> " + json.textTitle[0] + "</a></div><br style='clear: both;'>");
					//attachreOpenDialog($("#reopen_" + json.openimmoid[0]));
					////console.log("attache EINZELNEN reOpendialog");
					$mylink = tempDiv.find('#' + json.openimmoid[0]);
					////console.log($mylink);
					attachreOpenDialog($mylink);
			}
		});
	}	
	
}

//_markedObjects.init();

