
/*
Class Portfolio
***************************************************************************************************/

function Portfolio() {
	
	this.owner = "Mecky van den Brink";
	
	this.language = "nl"; // Set the initial language here
	this.defaultLanguage = "nl"; // Set the initial language here
	this.languages = new Array("nl", "en");
	
	this.items = new Array();
		
	// Indexed properties
	this.index = new Object();
	this.index.labels = new Array("type", "material", "year", "forSale");
	
	
	for (var i = 0; i < this.index.labels.length; i++) {
		this.index[this.index.labels[i]] = new Array();
	}
	
	this.sayHello = function() {
		var msg = "Hello, I am the portfolio of " + this.owner + "\n";
		msg += "this portfolio consists of " + this.items.length + " items";
		alert(msg);
	};
}

/**************************************************************************************************/




/*
Class PortfolioItem
***************************************************************************************************/

function PortfolioItem() {

	this.id = -1;

	this.name = "";
	this.title = "";
	this.description = "";
	
	this.year = "";
	this.type = "";
	this.material = "";
	
	this.forSale = false;
	
	this.languages = new Object();
	for (var i = 0; i < portfolio.languages.length; i++) {
		this.languages[portfolio.languages[i]] = new Object();
	}
	
	this.thumbnailUrl = "";
	this.fullSizeUrl = "";
	
	this.thumbnail = null;
}
/**************************************************************************************************/




/*
Class Repository

Class handling the data loading and access for the portfolio

***************************************************************************************************/


function Repository(portfolio) {

	this.portfolio = portfolio;
	
	this.siteRoot = "/~eelko/mecky10/";
	
	this.contentUrl = "content";
	
	this.url = this.contentUrl + "/portfolio.xml";
	
	this.itemsUrl = this.contentUrl + "/items";	
	
	this.editorUrl = "editor";
	
	
	this.languages = new Array("nl", "en");

	this.xmlDocument = null;
	
	this.lastId = 0;
	
	this.separator = ",";
	
	
	// Load the XML data
	this.loadData = function(url) {
	
		url = (url) ? url : this.url;
		
		this.request = new Ajax.Request(repository.randomiseUrl(url), 
				{method: "get", onSuccess: repository.storeXml});
	};
	
	
	// Save the AJAX response as an XML document
	this.storeXml = function(transport) {
		if (transport.responseXML) {
			if (transport.responseXML.documentElement) {
				repository.xmlDocument = transport.responseXML.documentElement;
			}
		}
		
		controller.dataLoaded();
		
	};
	
	this.randomiseUrl = function(url) {

		return url + ((url.indexOf("?") > -1) ? "&" : "?") + Math.random();
		
	};
	
	
	this.setImageUrls = function(item) {
	
		item.fullSizeUrl = this.itemsUrl + "/" + item.name + "/image-large.jpg";
		item.thumbnailUrl = this.itemsUrl + "/" + item.name + "/image-small.jpg";	
	};
	
	
	

	// Error message in case something goes wrong loading the data
	this.ajaxFailure = function(transport) {
		controller.debug("Error " + transport.status + " -- " + transport.statusText);
	};

	
	// Set the data in the portfolio from the XML document
	this.deSerialize = function() {
	
		if(!this.xmlDocument) {
			controller.debug("Error: XML data did not load!");
		}
		
		var xmlItems = this.xmlDocument.getElementsByTagName("item");
		
		this.portfolio.items = new Array();
		
		
		for (var i = 0; i < xmlItems.length; i++) {

			var xmlItem = xmlItems[i];
			var item = new PortfolioItem();
			
			item.id = xmlItem.getAttribute("id");
			
			if (parseInt(item.id) > this.lastId) {
				this.lastId = parseInt(item.id);
			}
			
			item.name = this.getValueFromXml(xmlItem, 
					"name", true, false);
			
			item.title = this.getValueFromXml(xmlItem, 
					"title", true, this.portfolio.language);
			item.description = this.getValueFromXml(xmlItem, 
					"description", true, this.portfolio.language);
			
			item.type = this.getValueFromXml(xmlItem, 
					"type", true, false);
			item.year = this.getValueFromXml(xmlItem, 
					"years", true, false);
			item.year = (item.year.length >= 4) ? 
					item.year.substring(0, 4) : item.year;
			item.material = this.getValueFromXml(xmlItem, 
					"materials", true, false);
			
			item.forSale = this.getValueFromXml(xmlItem, 
					"for-sale", true, false);
					
			
			/*
			item.thumbnailWidth = 
					parseInt(this.getValueFromXml(xmlItem, "thumbnail-width", true, false));

			item.thumbnailHeight = 
					parseInt(this.getValueFromXml(xmlItem, "thumbnail-height", true, false));
					
			*/
					
					
			this.setTranslatableProperty(item, "title", xmlItem, "title");
			this.setTranslatableProperty(item, "description", xmlItem, "description");


			// Index the values that are used in the filters			
			for (var j = 0; j < this.portfolio.index.labels.length; j++) {		
				var label = this.portfolio.index.labels[j];
				this.addToIndex(label, item[label]);
			}

			this.setImageUrls(item);
			
			this.portfolio.items[this.portfolio.items.length] = item;
		}
		
		
		controller.dataDeserialized();

	};
	
	
	// Get one property from an XML node, strip whitespace
	this.getValueFromXml = function(xmlItem, tagName, singleValue, language) {
		
		if (language) {
			tagName = tagName + "-" + language;
		}
		
		var element = xmlItem.getElementsByTagName(tagName)[0];
		var value = "";
		if (element) {
			if (element.firstChild) {
				value = element.firstChild.nodeValue.replace(/^\s+|\s+$/, "");
				if (!singleValue) {
					value = value.split(this.separator);
					for (var i = 0; i < value.length; i++) {			
						value[i] = value[i].replace(/^\s+|\s+$/, "");
					}
				}
			}
		}
		return value;
	};
	
	
	// Add a value to the an index identified by "label"
	this.addToIndex = function(label, value) {
	
		if (value.length == 0) {
			return false;
		}

		var values = value.split(",");

		for (var i = 0; i < values.length; i++) {
			
			var value = values[i].replace(/^\s+|\s+$/, "");
			
			var unique = true;
			for (var j = 0; j < this.portfolio.index[label].length; j++)  {
				if (this.portfolio.index[label][j] == value) {
					unique = false;
					break;
				}
			}
			if (unique == true) {
				var index = this.portfolio.index[label];
				index[index.length] = value;
			}
		}
	};
	
	
	this.setTranslatableProperty = function(item, propertyName, xmlItem, tagName) {
	
		for (var i = 0; i < this.portfolio.languages.length; i++) {
		
			var language = this.portfolio.languages[i];
			
			item.languages[language][propertyName] = 
					this.getValueFromXml(xmlItem, tagName, true, language);
		}
	};

	
	// Change the repository language. Options: nl or en
	this.loadLanguage = function(languageCode) {
		
		if (languageCode != this.portfolio.language) {
		
			this.portfolio.language = languageCode;
			
			for (var i = 0; i < this.portfolio.items.length; i++) {
			
				var item = this.portfolio.items[i];
				
				item.title = item.languages[languageCode].title;
				item.description = item.languages[languageCode].description;

			}
		}
	};
}

/**************************************************************************************************/


