function Virsus () {
	this.container = $('#place');

	this.data = Array();

	this.current = null;
	this.cindex = -1;

	this.load = function () {
		var self = this;
		
		$.ajax({
			async : false,
			url : '/data.php?type=1',
			dataType: "xml",
			complete : function (xml) {
				$(xml.responseXML).find("product").each(function() {
					self.data.push({
						'cost' : $(this).find("price").text(),
						'id' : $(this).find("url").text().split('=').pop(),
						'link' : $(this).find("url").text()
					});
				});
			}
		});
		
		$.ajax({
			async : false,
			url : '/data.php?type=2',
			dataType: "xml",
			complete : function (xml) {
				$(xml.responseXML).find("product").each(function() {
					self.data.push({
						'cost' : $(this).find("price").text(),
						'id' : $(this).find("url").text().split('=').pop(),
						'link' : $(this).find("url").text()
					});
				});
			}
		});
		
		$.ajax({
			async : false,
			url : '/data.php?type=3',
			dataType: "xml",
			complete : function (xml) {
				$(xml.responseXML).find("product").each(function() {
					self.data.push({
						'cost' : $(this).find("price").text(),
						'id' : $(this).find("url").text().split('=').pop(),
						'link' : $(this).find("url").text()
					});
				});
			}
		});
	};

	this.change = function () {
		var self = this;
		
		var i;
		do {
			i = Math.floor(Math.random() * this.data.length);
		} while (i == this.cindex);
		
		this.cindex = i;
		
		this.generate(this.data[i]);
	};

	this.fadeIn = function (el) {
		if ($.browser.msie && parseInt(jQuery.browser.version) <= 7) 
			el.animate({
				left : "150px"
			}, 500, jQuery.easing.InSine);
		else
			el.animate({
				opacity : 1,
				left : "150px"
			}, 500, jQuery.easing.InSine);
	};

	this.fadeOut = function (el) {
		el.animate({
				opacity : 0,
				left : "30px"
			}, 
			{
				duration: 400,
				ease: jQuery.easing.InSine,
				complete: function () {
					$(this).remove();
				}
			});
	};

	this.generate = function (data) {
		var self = this;
		var el = $("<a target='_blank' href='" + data.link + "' />");
		el.addClass('cell');
		var img = $('<img/>')
		img.error(function () {
			self.change();
		});
		img.load(function () {
			el.append(img);
			el.append(
				"<div class='cost'>" + data.cost + " Lt</div>"
			);
			if (!$.browser.msie || parseInt(jQuery.browser.version) != 8)
				el.fadeOut(0);
			
			if (self.current)
				self.fadeOut(self.current);
				
			self.current = el;
				
			self.container.append(self.current);
			self.fadeIn(self.current);
		});
		img.attr('src', '/static/images/produktai/' + data.id + '.png');
	};

	this.load();
	this.change();
}

function str_replace(from, to, text) {
	return text.split(from).join(to);
}
function formatTitle(title) {
	title = str_replace('"Aras" ', '', title);
	return capitaliseFirstLetter(str_replace('"ARAS" ', '', title));
}

function capitaliseFirstLetter(string) {
    return string.charAt(0).toUpperCase() + string.slice(1);
}

var _virsus;

$(function () {
	_virsus = new Virsus();
});
