$(document).ready(function() {
	var o = $('#logo');
	if (o.length === 1) {
		o[0].onmouseover = _.logo.over;
		o[0].onmouseout = _.logo.out;
		CacheElements.store(_.logo.data.images.red);
	}
	CacheElements.now();
});

var _ = {
	logo: {
		data: {
			images: {white: '/_/skontobuve.png', red: '/_/skontobuve.red.png'}
		},
		over: function(e) {
			expandEvent(e).o.src = _.logo.data.images.red;
		},
		out: function(e) {
			expandEvent(e).o.src = _.logo.data.images.white;
		}
	}
};

var Spotlights = {
	timeout : 20000,
	lng : false,
	id_to_change : 1,
	not_in : false,
	initTimer : function(lng, not_in) {
		this.lng = lng;
		this.not_in = not_in;
		this.startTimer();
	},
	startTimer : function() {
		setTimeout('Spotlights.change()', this.timeout);
	},
	convertCurrentSpotIds : function() {
		var id;
		var converted = '';
		for (id in this.not_in) {
			converted = converted + this.not_in[id] + '_';
		}
		return converted;
	},
	change: function(id) {
		var not_in = this.convertCurrentSpotIds();
		var ajax_href =  '/ajax/get_spotlight' + (this.lng ? '/' + this.lng : '') + (not_in ? '/' + not_in : '');
		jQuery.ajax({
			url: ajax_href,
			dataType : "json",
			success: function (data) {
				Spotlights.update(data);
			}
		});
	},
	update: function(data) {
		var id = Spotlights.id_to_change;
		if (!data) {
			return;
		}
		if (!$('#spotlight_title_' + id) || !$('#spotlight_text_' + id)) {
			return false;
		}
		var link = '<a href="'+ data.link +'">';
		$('#spotlight_title_' + id).html(link + data.title + '</a>');
		$('#spotlight_text_' + id).html(link + data.text + '</a>');

		var new_id = id + 1;
		if (new_id > 1) {
			new_id = 0;
		}
		this.not_in[id] = data.id;
		this.id_to_change = new_id;
		this.startTimer();
	}
}

var CacheElements = {
	o: null,
	storage: [],
	stored: [],
	store: function(src) {
		var es = this.storage, i = 0, so = es.length;
		for (; i < so; i++) {
			if (es[i] === src) return;
		}
		this.storage[so] = src;
		this.stored[so] = false;
	},
	now: function() {
		if (!this.o) {
			var div = document.createElement('DIV');
			div.style.position = 'absolute';
			div.style.top = '-9999px';
			div.style.left = '-9999px';
			this.o = document.body.appendChild(div);
			div = document.createElement('DIV');
			div.style.position = 'relative';
			div.id = 'elements_image_preloader';
			this.o = this.o.appendChild(div);
		}
		var i = 0, so = this.storage.length, img;
		for (; i < so; i++) {
			if (this.stored[i]) continue;
			img = document.createElement('IMG');
			img.style.position = 'absolute';
			img.style.top = 0;
			img.style.left = 0;
			img.src = this.storage[i];
			this.o.appendChild(img);
			this.stored[i] = true;
		}
	}
}

function expandEvent(e) {
	if (!e) e = event;
	if (!e) return false;
	var o = e.srcElement || e.target;
	if (!o) return false;
	return {e: e, o: o};
}

/*
 * Creates gallery
 * @param string ID of HTML node where to insert code
 * @param array Array with images as strings
 * @param number Which images to show first
 * @return object self
 */
function Gallery(id, imageList, class_name, current) {
	if (typeof id !== 'string') {
		alert('Internal error. First parameter in Gallery() should be a string.');
		return false;
	}

	if (typeof imageList !== 'object' || !(imageList instanceof Array)) {
		alert('Internal error. Second parameter in Gallery() should be an array.');
		return false;
	}

	var type = typeof current;
	if (type !== 'undefined') {
		if (type !== 'number') {
			alert('Internal error. Third parameter in Gallery() should be a number.');
			return false;
		}
		if (current > imageList.length - 1) {
			alert('Internal error. Third parameter in Gallery() is greater than total amount of images in rotation.');
			return false;
		}
	} else current = 0;

	if (imageList.length === 0) return false;

	var o = $('#'+ id);
	if (o.length !== 1) {
		alert('Internal error. Could not get node with id "'+ id +'".');
		return false;
	}

	this.o = o[0];
	this.id = id;
	this.imageList = imageList;
	this.current = current;
	this.cached = false;
	if (class_name == 'undefined') {
		class_name = 'gallery_image';
	}
	this.class_name = class_name;
	this.html();

	return this;
}
Gallery.prototype.html = function(current) {
	var type = typeof current;

	if (type !== 'undefined') {
		if (type !== 'number') {
			alert('Internal error. First parameter in Gallery.html() should be a number.');
			return false;
		}
		if (current > this.imageList.length - 1) {
			alert('Internal error. First parameter in Gallery.html() is greater than total amount of images in rotation.');
			return false;
		}
		this.current = current;
	}

	var html = [], so = this.imageList.length, i = 0;

	html[html.length] = '<div class="'+ this.class_name +'"><img src="'+ this.imageList[this.current] +'" alt=""></div><div class="gallery">';
	if (so > 1) {
		html[html.length] = '<table class="pages"><tr>';

		var from = 0, till = so;
		if (till - from > 10) {
			if (this.current > 7) from = this.current - 7;
			if (till - from > 9) till = from + 9;
			if (till == so) from = till - 9;
		}

		if (this.current > 0) html[html.length] = '<td class="l"><a href="" onclick="'+ this.id +'.html('+ (this.current - 1) +');return false"><img src="/_/left.png" alt=""></a></td>';
		else html[html.length] = '<td class="l"><img src="/_/left.png" alt=""></td>';
		for (i = from; i < till; ++i) {
			html[html.length] = '<td class="m" style="width:14px;text-align:center"><a href=""'+ (this.current == i ? ' class="selected"' : '') +' onclick="'+ this.id +'.html('+ i +');return false">'+ (i + 1) +'</a></td>';
		}

		if (this.current < so - 1) html[html.length] = '<td class="r"><a href="" onclick="'+ this.id +'.html('+ (this.current + 1) +');return false"><img src="/_/right.png" alt=""></a></td>';
		else html[html.length] = '<td class="r"><img src="/_/right.png" alt=""></td>';

		html[html.length] = '<//tr></table>';
	}
	html[html.length] = '</div>';

	this.o.innerHTML = html.join('');
	if (this.current > 0 && !this.cached) this.cache(this.current);
	return true;
}
Gallery.prototype.cache = function(skip) {
	if (typeof skip === 'undefined') skip = 0;
	var so = this.imageList.length, i = 1;
	for (; i < so; i++) {
		if (i !== skip) CacheElements.store(this.imageList[i]);
	}
	CacheElements.now();
	this.cached = true;
	return true;
}

/* animation for objects */
var TextAnimation = {
	animationSpeed : 200,
	default_box_size : false,
	max_box_size : 132,
	timer_over : {},
	timer_out : {},
	timer_timeout : 50,
	slideOut: function (box_id, text_id) {
		var animation_obj = $("#" + box_id),
			text_height = $("#" + text_id).innerHeight();
		if (text_height == this.default_box_size) return false;
		if (this.default_box_size === false) this.default_box_size = animation_obj.height();
		if (text_height < this.default_box_size) return false;
		else if (text_height > this.max_box_size) text_height = this.max_box_size;
		animation_obj.animate({height: text_height +"px"}, {duration : this.animationSpeed, queue : true});
	},
	slideIn: function (box_id, text_id) {
		var animation_obj = $("#" + box_id),
			text_height = $("#" + text_id).innerHeight();
		if (!this.default_box_size) this.default_box_size = animation_obj.height();
		if (text_height == this.default_box_size) return false;
		animation_obj.animate({height: this.default_box_size +"px"}, {duration: this.animationSpeed});
	},
	over: function (box_id, text_id, bg_id) {
		if (this.timer_over[box_id] !== undefined) return false;
		if (this.timer_out[box_id] !== undefined) {
			clearTimeout(this.timer_out[box_id]);
			delete this.timer_out[box_id];
		}
		$("#" + bg_id).find('div').toggleClass('text_box_hover');
		this.timer_over[box_id] = setTimeout('TextAnimation.slideOut("'+ box_id +'","'+ text_id +'")', this.timer_timeout);
	},
	out: function(box_id, text_id, bg_id) {
		if (this.timer_out[box_id] !== undefined) return false;
		if (this.timer_over[box_id] !== undefined) {
			clearTimeout(this.timer_over[box_id]);
			delete this.timer_over[box_id];
		}
		$("#" + bg_id).find('div').toggleClass('text_box_hover');
		this.timer_out[box_id] = setTimeout('TextAnimation.slideIn("'+ box_id +'","'+ text_id +'")', this.timer_timeout);
	}
}
