// JavaScript Document
function ImmobilienCollection() {
	this.collection = [];
	this.filteredCollection = [];
	
	this.addItem = function(givenItem){
		this.collection.push(givenItem);
	}
	
	this.filters = [];
	this.addFilter = function(attribute, operator, value) {
		this.filters.push(new Array(attribute, operator, value));
	}
	
	this.specialfilters = [];
	this.addSpecialFilter = function($attribute, $operator, $value) {
		if($operator == "or") {
			if(this.specialfilters["or"] === undefined) {
				this.specialfilters["or"] = new Array();
			}
			if(this.specialfilters["or"][$attribute] === undefined) {
				this.specialfilters["or"][$attribute] = new Array();
			}
			this.specialfilters["or"][$attribute].push($value);
			//echo "//console.log('this -> specialfilters[\"or\"][" . $attribute . "] = " . $value . "');\r";
		}
	}	
	
	this.resetFilters = function() {
		this.filters = [];
	}
	
	this.removeFilter = function(attribute) {
		var newFilters = [];
		for(var i = 0; i < this.filters.length; i++) {
			if(this.filters[i][0] != attribute) {
				newFilters.push(this.filters[i]);
			}
		}
		this.filters = newFilters;
		
		newFilters = [];
		for($key in this.specialfilters) {
			if($key != attribute) {
				newFilters.push(this.specialfilters[$key]);
			}
		}
		this.specialfilters = newFilters;		
	}
	
	this.filterCollection = function() {
		var hasPassed = true;
		var currentFilter = null;
		var currentImmo = null;
		this.filteredCollection = this.collection;
		var newfilteredCollection = [];
		for(var i = 0; i < this.filteredCollection.length; i++) {
			currentImmo = this.filteredCollection[i];
			hasPassed = true;
			for(var k = 0; k < this.filters.length; k++) {
				currentFilter = this.filters[k];
				if(currentFilter[1] == "<" && hasPassed == true) {
					if(currentImmo.data[currentFilter[0]] < currentFilter[2]) {
						hasPassed = true;
					}
					else {
						hasPassed = false;
					}
				}
				
				if(currentFilter[1] == ">" && hasPassed == true) {
					if(currentImmo.data[currentFilter[0]] > currentFilter[2]) {
						hasPassed = true;
					}
					else {
						hasPassed = false;
					}
				}

				if(currentFilter[1] == "=" && hasPassed == true) {
					if(currentImmo.data[currentFilter[0]] == currentFilter[2]) {
						hasPassed = true;
					}
					else {
						hasPassed = false;
					}
				}

				if(currentFilter[1] == "<=" && hasPassed == true) {
					if(currentImmo.data[currentFilter[0]] <= currentFilter[2]) {
						hasPassed = true;
					}
					else {
						hasPassed = false;
					}
				}

				if(currentFilter[1] == ">=" && hasPassed == true) {
					if(currentImmo.data[currentFilter[0]] >= currentFilter[2]) {
						hasPassed = true;
					}
					else {
						hasPassed = false;
					}
				}
								
			}


			for($key in this.specialfilters) {
				$value = this.specialfilters[$key];
				if($key == "or") {
					for($subkey in this.specialfilters["or"]) {
						$subvalues = this.specialfilters["or"][$subkey];
						$currentFilter = this.specialfilters["or"][$subkey];
						//console.log($subkey);
						//console.log("Prüfe: " + currentImmo.data[$subkey] + ":" + this.specialfilters["or"][$subkey]);
						//console.log(jQuery.inArray(currentImmo.data[$subkey], this.specialfilters["or"][$subkey]));
						if((jQuery.inArray(currentImmo.data[$subkey], this.specialfilters["or"][$subkey]) >= 0) && hasPassed == true ) {
							hasPassed = true;
						}
						else {
							hasPassed = false;
						}
					}
				}
			}

			
			if(hasPassed == true) {
				newfilteredCollection.push(currentImmo);
			}
		}
		
		this.filteredCollection = newfilteredCollection;
	}
	
	this.getOrte = function() {
		$Orte = new Array();
		for(var i = 0; i < this.collection.length; i++) {
			if(this.collection[i].data.ort in $Orte) {
				//do nothing
				
			}
			else {
				$Orte[this.collection[i].data.ort] = new Array();
			}		
		}
		
		for(var i = 0; i < this.filteredCollection.length; i++) {
			$Orte[this.filteredCollection[i].data.ort].push(this.filteredCollection[i].data.openimmoid);
		}
		
		return $Orte;		
	}
	
	this.getMaxPreis = function() {
		var maxPreis = 0;
		for(var i = 0; i < this.filteredCollection.length; i++) {
			if(maxPreis < this.filteredCollection[i].data.Preis) {
				maxPreis = this.filteredCollection[i].data.Preis;
			}
		}		
		return maxPreis;
	}
	
	this.getImmoById = function(number) {
		for(var i = 0; i < this.collection.length; i++) {
			if(this.collection[i].id == number) {
				return this.collection[i];
			}
		}
	}
	
	this.buildView = function(mode) {
		var viewStr = "";
		if(mode == "detail") {
			viewStr += "<div id=\"tabs\" class=\"tabs-bottom\">";
			viewStr += "<ul>";
			
			numberoftabs = Math.ceil(this.filteredCollection.length / 6);
			for(var i = 0; i < numberoftabs; i++) {
				viewStr += "<li><a href=\"#tabs-" + i + "\">" + (i+1) + "</a></li>";
			}
			viewStr += "</ul>";
			
			var currentCount = -1;
			for(var i = 0; i < numberoftabs; i++) {
				viewStr += "<div id=\"tabs-" + i + "\">";
				for(var k = 0; k < 6; k++) {
					currentCount++;
					if(currentCount < this.filteredCollection.length) {
						
						viewStr += "<div class=\"innerCont\" style=\"margin-right: 0px; width: 750px; height: 160px;\">";
						viewStr += "<div style=\"margin: 0px; padding: 5px; background-color: #eeefef;\">";
						viewStr += "<a id=\"" + this.filteredCollection[currentCount].data.openimmoid + "\" href=\"objekt_detailansicht.php?object=" + this.filteredCollection[currentCount].data.openimmoid + "\" class=\"moreLink\" title=\"" + this.filteredCollection[currentCount].data.name + "\"><img src=\"image.php?imageFile=" + this.filteredCollection[currentCount].data.Bilder[0] + "\" style=\"width: 230px; height: 151px; border: 0; margin: 0; padding: 0; float: left; margin-right: 20px;\" /></a>";
						viewStr += "<p style=\"font-weight: bold; margin-top: 5px; display: block; height: 30px; overflow: hidden;\">" + this.filteredCollection[currentCount].data.name + "</p>";
						
						//console.log(AllObjects);
						currentObject = AllObjects.getObjectByOpenimmoid(this.filteredCollection[currentCount].data.openimmoid);
						viewStr += "<p style=\"display: block; height: 45px; overflow: hidden; margin-top: 5px;\">" + currentObject.textBeschreibung[0] + "</p>";
						
						viewStr += "<p style=\"margin-top: 5px;\">Ort: " + this.filteredCollection[currentCount].data.ort + "</p>";
						viewStr += "<p>Preis: " + number_format(this.filteredCollection[currentCount].data.Preis, 2, ",", ".") + " Euro</p>";
						viewStr += "<p style=\"text-align: right;\"><a id=\"" + this.filteredCollection[currentCount].data.openimmoid + "\" href=\"objekt_detailansicht.php?object=" + this.filteredCollection[currentCount].data.openimmoid + "\" class=\"moreLink\" title=\"" + this.filteredCollection[currentCount].data.name + "\">Details</a></p>";
						viewStr += "</div>";
						viewStr += "</div>";
						
						/*
						<div class="innerCont" style="margin-right: 0px; width: 750px;">
						<div style="margin: 0px; padding: 5px; background-color: #fff; color: #333; font-size: 11px;">
						<img style="width: 230px; height: 151px; border: 0; margin: 0; padding: 0; float: left; margin-right: 20px;" src="image_mitarbeiter.php?imageFile=images/mitarbeiter/pk_big.jpg">
			
						<p style="font-weight: bold; margin-top: 5px; display: block; height: 30px;">Philipp Krentz<br />
						  Geschäftsinhaber
						</p>
						<p style="margin-top: 5px;">Telefon: +49 (331) 270 72 70</p>
						<p>E-Mail Adresse: <a href="mailto:philipp@krentz.de">philipp@krentz.de</a></p>
						<p style="text-align: right;"></p>
						</div>
						</div>
						*/
					}
				}
				viewStr += "<div style=\"clear: both;\"></div>";
				viewStr += "</div>";
			}
			viewStr += "</div>";			
			
			/*for(var i = 0; i < this.filteredCollection.length; i++) {
				viewStr += "<div class=\"innerCont\" style=\"margin-right: 0px; width: 750px;\">";
				viewStr += "<div style=\"padding: 5px;\">";
				viewStr += "<img src=\"image.php?imageFile=" + this.filteredCollection[i].data.Bilder[0] + "\" style=\"width: 230px; height: 151px; border: 0; margin: 0; padding: 0;\" />";
				viewStr += "<p style=\"font-weight: bold;\">" + this.filteredCollection[i].data.name + "</p>";
				viewStr += "<p>Potsdam</p>";
				viewStr += "<p>229.000 Euro</p>";
				viewStr += "</div>";
				viewStr += "</div>";
			}*/
		}
		else if (mode == "galerie")  {
			viewStr += "<div id=\"tabs\" class=\"tabs-bottom\">";
			viewStr += "<ul>";
			
			numberoftabs = Math.ceil(this.filteredCollection.length / 9);
			for(var i = 0; i < numberoftabs; i++) {
				viewStr += "<li><a href=\"#tabs-" + i + "\">" + (i+1) + "</a></li>";
			}
			viewStr += "</ul>";
			
			var currentCount = -1;
			for(var i = 0; i < numberoftabs; i++) {
				viewStr += "<div id=\"tabs-" + i + "\">";
				for(var k = 0; k < 9; k++) {
					currentCount++;
					if(currentCount < this.filteredCollection.length) {
						viewStr += "<div class=\"innerCont\" style=\"margin-right: 0px;\">";
						viewStr += "<div style=\"margin: 0px; padding: 5px; background-color: #eeefef;\">";
						viewStr += "<a id=\"" + this.filteredCollection[currentCount].data.openimmoid + "\" href=\"objekt_detailansicht.php?object=" + this.filteredCollection[currentCount].data.openimmoid + "\" class=\"moreLink\" title=\"" + this.filteredCollection[currentCount].data.name + "\"><img src=\"image.php?imageFile=" + this.filteredCollection[currentCount].data.Bilder[0] + "\" style=\"width: 230px; height: 151px; border: 0; margin: 0; padding: 0;\" /></a>";
						viewStr += "<p style=\"font-weight: bold; margin-top: 5px; display: block; height: 30px; overflow: hidden;\">" + this.filteredCollection[currentCount].data.name + "</p>";
						viewStr += "<p style=\"margin-top: 5px;\">Ort: " + this.filteredCollection[currentCount].data.ort + "</p>";
						viewStr += "<p>Preis: " + number_format(this.filteredCollection[currentCount].data.Preis, 2, ",", ".") + " Euro</p>";
						viewStr += "<p style=\"text-align: right;\"><a id=\"" + this.filteredCollection[currentCount].data.openimmoid + "\" href=\"objekt_detailansicht.php?object=" + this.filteredCollection[currentCount].data.openimmoid + "\" class=\"moreLink\" title=\"" + this.filteredCollection[currentCount].data.name + "\">Details</a></p>";
						viewStr += "</div>";
						viewStr += "</div>";
					}
				}
				viewStr += "<div style=\"clear: both;\"></div>";
				viewStr += "</div>";
			}
			viewStr += "</div>";
		}
		else if (mode == "index")  {
			viewStr += "<ul style=\"min-width: 100%;\">";
			for(var i = 0; i < this.filteredCollection.length; i++) {
				viewStr += "<li>";
				viewStr += "<div class=\"indexCarouselCont\">";
				viewStr += "<div style=\"margin: 5px; padding: 10px; background-color: #fff; height: 100%;\">";
				//viewStr += "<img src=\"image.php?imageFile=" + this.filteredCollection[i].data.Bilder[0] + "\" style=\"width: 230px; height: 151px; border: 0; margin: 0; padding: 0;\" />";
				viewStr += "<a id=\"" + this.filteredCollection[i].data.openimmoid + "\" href=\"objekt_detailansicht.php?object=" + this.filteredCollection[i].data.openimmoid + "\" class=\"moreLink\" title=\"" + this.filteredCollection[i].data.name + "\"><img src=\"image.php?imageFile=" + this.filteredCollection[i].data.Bilder[0] + "\" style=\"width: 230px; height: 151px; border: 0; margin: 0; padding: 0;\" /></a>";
				viewStr += "<p style=\"font-weight: bold; margin-top: 5px; display: block; height: 30px; overflow: hidden;\">" + this.filteredCollection[i].data.name + "</p>";
				viewStr += "<p style=\"margin-top: 5px;\">Ort: " + this.filteredCollection[i].data.ort + "</p>";
				viewStr += "<p>Preis: " + number_format(this.filteredCollection[i].data.Preis, 2, ",", ".") + " Euro</p>";
				viewStr += "<p style=\"text-align: right;\"><a id=\"" + this.filteredCollection[i].data.openimmoid + "\" href=\"objekt_detailansicht.php?object=" + this.filteredCollection[i].data.openimmoid + "\" class=\"moreLink\" title=\"" + this.filteredCollection[i].data.name + "\">Details</a></p>";
				viewStr += "</div>";
				viewStr += "</div>";
				viewStr += "</li>";
			}
			viewStr += "</ul>";
			
			
			/*viewStr += "<div id=\"tabs\" class=\"tabs-bottom\">";
			viewStr += "<ul>";
			
			numberoftabs = Math.ceil(this.filteredCollection.length / 3);
			for(var i = 0; i < numberoftabs; i++) {
				viewStr += "<li><a href=\"#tabs-" + i + "\">#" + (i+1) + "</a></li>";
			}
			viewStr += "</ul>";
			
			var currentCount = -1;
			for(var i = 0; i < numberoftabs; i++) {
				viewStr += "<div id=\"tabs-" + i + "\">";
				for(var k = 0; k < 3; k++) {
					currentCount++;
					if(currentCount < this.filteredCollection.length) {
						viewStr += "<div class=\"innerCont\" style=\"margin-right: 0px;\">";
						viewStr += "<div style=\"margin: 5px; background-color: #fff;\">";
						viewStr += "<img src=\"image.php?imageFile=" + this.filteredCollection[currentCount].data.Bilder[0] + "\" style=\"width: 230px; height: 151px; border: 0; margin: 0; padding: 0;\" />";
						viewStr += "<p style=\"font-weight: bold; margin-top: 5px; display: block; height: 30px;\">" + this.filteredCollection[currentCount].data.name + "</p>";
						viewStr += "<p style=\"margin-top: 5px;\">Ort: " + this.filteredCollection[currentCount].data.ort + "</p>";
						viewStr += "<p>Preis: 229.000 Euro</p>";
						viewStr += "<p style=\"text-align: right;\"><a id=\"" + this.filteredCollection[currentCount].data.openimmoid + "\" href=\"objekt_detailansicht.php?object=" + this.filteredCollection[currentCount].data.openimmoid + "\" class=\"moreLink\" title=\"" + this.filteredCollection[currentCount].data.name + "\">Details</a></p>";
						viewStr += "</div>";
						viewStr += "</div>";
					}
				}
				viewStr += "<div style=\"clear: both;\"></div>";
				viewStr += "</div>";
			}
			viewStr += "</div>";*/			
			
		}



		
		return viewStr;
	}
}
