/**
 * @author shevchenquot
 */

var LoveAdverts = function() {
	var self = this;
	this.div = new Array;
	this.img = new Array;
	this.titles = new Array;
	this.fadeInterval = 2000;
	this.interval = 1500;
	this.timer;
	this.current = 0;
	this.depth;
	
	this.init = function() {
		self.div = ['#land_col', '#house_col', '#business_col', '#investment_col', '#farm_col'];
		for (var i = 0; i < self.div.length; i++) {
			self.img[i] = $(self.div[i] + ' img').get();
			$.map(self.img[i], function (elt) { return $(elt).attr('src'); });
			self.titles[i] = $.map($(self.div[i] + ' span').get(), function (elt) { return $(elt).text(); });
			if (self.img[i].length < 1) {
				self.img[i] = new Array(self.img[i]);
			}
			for (var j = 0; j < self.img[i].length; j++) {
				$(self.div[i] + ' img').eq(j).css('z-index', (self.img[i].length - j) + 100);
			}
			$(self.div[i] + ' > .title_ad').text(self.titles[i][0]);
			
			$(self.div[i] + ' img').mouseover(function() {
				$(this).css('cursor', 'pointer');
			})
			.click(function() {
				document.location.href = 'annonce-' + $(this).attr('id').substr(1) + '.htm';
			});
		}
		self.depth = 100;
	}
	
	this.start = function() {
		if (self.img[0].length > 0) {
			self.timer = setTimeout(self.update, self.interval);
		}
	}
	
	this.update = function() {
		for (var i = 0; i < self.div.length; i++) {
			$(self.div[i] + ' img').eq(self.current).fadeOut(self.fadeInterval);
		}
		$(self.div[3] + ' img').eq(self.current).fadeOut(self.fadeInterval, function() {
			self.depth--;
			if (self.depth < 0) {
				for (var j = 0; j < self.img[i].length; j++) {
					$(self.div[i] + ' img').eq(j).css('z-index', (self.img[i].length - j) + 100);
				}
				self.depth = 100;
			}
			for (var i = 0; i < self.div.length; i++) {
				$(self.div[i] + ' img').eq(self.current).css('z-index', self.depth);
				$(self.div[i] + ' img').eq(self.current).show();
			}
			if (self.current < self.img[3].length - 1) {
				self.current++;
			}
			else {
				self.current = 0;
			}
			for (var i = 0; i < self.div.length; i++) {
				$(self.div[i] + ' > .title_ad').text(self.titles[i][self.current]);
			}
			self.timer = setTimeout(self.update, self.interval);
		});
	}
}
