function Photobar() {
	
	//Class properties
	var actionUrl;
	var mediaPath;
	var ajaxServicePageId;
	var handlerId;
	var reoType;
	var searchSize;
	var activeSearchPosition;
	var containerCount;
	var startPosition;
	var showSpecialOffers;
	var noPicturePath;
	
	//Internal attributes
	var responseVerify = "abcd123";
	// array(positionIndex, array(reo_url, picture))
	var cache = new Array();
	
	//Class methods
	this.getXmlHttp = getXmlHttp;
	this.getDataFromServer = getDataFromServer;
	this.processResponse = processResponse;
	
	this.showPictures = showPictures;
	this.showPicture = showPicture;
	this.moveLeft = moveLeft;
	this.moveRight = moveRight;
	this.onShowPictures = function(){};
	//this.showHideScrollButtons = showHideScrollButtons;
	
	//Create and return XmlHttp object
	function getXmlHttp() {		
		if (typeof XMLHttpRequest != "undefined") {
			return new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			var aVersions = ["MSXML2.XMLHttp.5.0",
			                 "MSXML2.XMLHttp.4.0",
			                 "MSXML2.XMLHttp.3.0",
			                 "MSXML2.XMLHttp",
			                 "Microsoft.XMLHttp"];
			
			for (var i = 0; i < aVersions.length; i++) {
				try {
					var oXmlHttp = new ActiveXObject(aVersions[i]);
					return oXmlHttp;
				} catch (oError) {
					//alert(oError);
				}
			}
		}
		throw new Error("XMLHttp object could be created.");
	}
	
	function getDataFromServer(positionIndex, photoContainerId) {
		var sUrl = this.actionUrl
		         + "&pageId=" + this.ajaxServicePageId
		         + "&verify_code=" + responseVerify
		         + "&handler_id=" + this.handlerId
		         + "&reo_type=" + this.reoType
		         + "&position_index=" + positionIndex
		         + "&show_special_offers=" + this.showSpecialOffers;
		
		var oXmlHttp = getXmlHttp();
		
		oXmlHttp.open("GET", sUrl, true);
		
		var me = this;	//Use it to be able to access the class methods
		
		oXmlHttp.onreadystatechange = function() {
			if (oXmlHttp.readyState == 4) {
				if (oXmlHttp.responseText != "") {
					var response = oXmlHttp.responseText;
					if (response.substring(0, 7) == responseVerify) {
						if (response.substr(7) != "") {
							me.processResponse(response.substr(7), positionIndex, photoContainerId);
						}
					}
				}
			}
		}
		
		oXmlHttp.send(null);
	}
	
	function processResponse(response, positionIndex, photoContainerId) {
		var parser = new DOMParser();
		var doc = parser.parseFromString(response, "text/xml");
		
		var reoUrl = getValueFromDocument(doc, "reo_url");
		var mediaSource = getValueFromDocument(doc, "media_source");
		var price = getValueFromDocument(doc, "price");
		var mediaAlt = getValueFromDocument(doc, "media_alt");
		
		if (mediaSource == null) {
			mediaSource = this.noPicturePath;
		}
		var picture = new Image();
		picture.src=mediaSource;
		
		var cacheItem = {"reo_url":reoUrl, "picture":picture, "price":price, "alt":mediaAlt};
		cache[positionIndex] = cacheItem;
		
		this.showPicture(positionIndex, photoContainerId, cacheItem);
	}
	
	function getValueFromDocument(doc, tagName) {
		var elements = doc.getElementsByTagName(tagName);
		if (elements.length > 0) {
			return elements[0].childNodes[0].nodeValue;
		}
		return null;
	}
	
	function showPicture(positionIndex, photoContainerId, cacheItem) {
		var reoUrl = cacheItem["reo_url"];
		var picture = cacheItem["picture"];
		var price = cacheItem["price"];
		var alt = cacheItem["alt"];
		
		var imageHtml = "";
		imageHtml += "<a href='" + reoUrl + "'>";
		imageHtml += "<img src='" + picture.src + "' alt='" + alt + "' title='" + alt + "'>";
		imageHtml += "</a>";
		
		var photoDiv = findChildDiv(photoContainerId, "photo");
		photoDiv.innerHTML = imageHtml;
		
		if (positionIndex == this.activeSearchPosition) {
			photoDiv.style.border = "3px solid #fbaa00";
			photoDiv.style.margin = "0px";
		} else {
			photoDiv.style.border = "0px";
			photoDiv.style.margin = "3px";
		}
		
		var priceDiv = findChildDiv(photoContainerId, "price");
		if (priceDiv != null) {
			if (price != null) {
				priceDiv.innerHTML = price;
			} else {
				priceDiv.innerHTML = "";
			}
			
			if (positionIndex == this.activeSearchPosition) {
				priceDiv.style.color = "#fbaa00";
			} else {
				priceDiv.style.color = "#000000";
			}
		
		}
	}
	
	function showPictures() {
		this.onShowPictures();
		for (var i=0; i<this.containerCount; i++) {
			var positionIndex = this.startPosition + i;
			var photoContainerId = "photo_" + (i + 1);
			
			while (positionIndex < 0) {
				positionIndex += this.searchSize;
			}
			while (positionIndex >= this.searchSize) {
				positionIndex -= this.searchSize;
			}
			
			if (cache[positionIndex] != null) {
				this.showPicture(positionIndex, photoContainerId, cache[positionIndex]);
			} else {
				this.getDataFromServer(positionIndex, photoContainerId);
			}
		}
	}
	
	function moveLeft() {
		this.startPosition--;
		this.showPictures();
	}
	
	function moveRight() {
		this.startPosition++;
		this.showPictures();
	}
	
	// helper method
	function findChildDiv(parentId, childId) {
		var children = document.getElementById(parentId).getElementsByTagName("div");
		for (var i=0; i<children.length; i++) {
			var child = children[i];
			if (child.id == childId) {
				return child;
			}
		}
		return null;
	}
	
}

