﻿/**
* $Date$
* $Author$
* $Revision$
**/

KL.TellusProduct = function(obj) {

	this._title = obj.name;
	this._media = getMedia();
	this._text = getText("sv");
	this._contact = obj.contactList.contact;
	this._category = { id: obj.categoryList.category.id, name: obj.categoryList.category.name };
	this._adress = obj.address;

	function getMedia() {

		var media = new Array();

		if (obj.mediaList == undefined)
			return null;

		if (obj.mediaList.media.length) {
			$.each(obj.mediaList.media, function(i, item) {
				media.push(new KL.TellusProductMedia(this));
			});

		} else
			media.push(new KL.TellusProductMedia(obj.mediaList.media));

		return media;

	}

	function getImageBySortIndex(i) {
		var m = getMedia();

		if (m == null)
			return false;

		var img = $.grep(m, function(n, i) {
			return (n.sequence == i);
		});

		if (m.length > 1 && img.length > 0)
			return img[0];
		else
			return m[0];

	}

	function getText(lang) {
		if (obj.textList == undefined)
			return null;

		if (obj.textList.text.length) {
			var text = $.grep(obj.textList.text, function(n, i) {
				return (n.lang == lang);
			});

			if (text[0])
				return text[0].value

			else
				return obj.textList.text[0].value;
		} else
			return obj.textList.text.value;


	}

	/**
	* Public methods
	*/
	return {
		Title: this._title,
		Media: this._media,
		Text: this._text,
		Website: this._website,
		Contact: this._contact,
		Category: this._category,
		Address: this._adress,
		getText: function(lang) { return getText(lang); },
		getImageBySortIndex: function(i) { return getImageBySortIndex(i); }
	}
}

KL.TellusProductMedia = function(obj)
{
	this.copyright = obj.copyright.name;
	this.mime = obj.mime;
	this.large = obj.large.URI;
	this.small = obj.small.URI;
	this.thumbnail = obj.thumbnail.URI;
	this.sequence = parseInt(obj.sequence);

	return this;
}