var _ = {
	o: {},
	comment: {
		pressedReply: false,
		ini: function() {
			var o = $('#comment')[0];
			if (!o) return;
			_.o['comment'] = o;
			if ($('#hideform')[0].value === '1' && location.hash !== '#form') o.style.display = 'none';
			$('#form')[0].onsubmit = _.comment.check;
			o = $('#addcomment')[0];
			if (o) o.onclick = _.comment.addcomment;
			$('#hide')[0].onclick = _.comment.toggle;
			_.o['comments'] = $('#comments')[0];
			_.o['e'] = $('#e')[0];
			_.o['t'] = $('#t')[0];
			_.o['c'] = o = $('#c')[0];
			o.onkeyup = _.comment.textareaMaxLength;
			var nodes = $('.comm:has(.text)'), i = nodes.length - 1;
			for (; i >= 0; --i) nodes[i].firstChild.onclick = _.comment.reply;
		},
		addcomment: function() {
			if (_.comment.pressedReply) {
				_.o['comments'].parentNode.insertBefore(_.o['comment'].parentNode.removeChild(_.o['comment']), _.o['comments']);
				_.comment.pressedReply = false;
				return false;
			}
			return _.comment.toggle(1);
		},
		reply: function(e) {
			_.comment.pressedReply = true;
			if (!e) e = event;
			var o = e.target || e.srcElement;
			while (o && o.id.charAt(0) !== 'r') o = o.parentNode;
			var id = o.id.substr(1);
			_.o['t'].value = 'RE: '+ $('#t'+ id).text();
			_.o['comment'].style.display =  'none';
			o.parentNode.parentNode.parentNode.insertBefore(_.o['comment'].parentNode.removeChild(_.o['comment']), o.parentNode.parentNode.nextSibling);
			_.o['comment'].style.display =  '';
			location.hash = '';
			location.hash = '#c'+ id;
			_.o['t'].focus();
			return false;
		},
		textareaMaxLength: function() {
			var txt = _.o['c'].value;
			if (txt.length > 600) _.o['c'].value = txt.substr(0, 600);
		},
		toggle: function(no_hash) {
			if (_.o['comment'].style.display === 'none') {
				_.o['comment'].style.display =  '';
				if (no_hash === 'undefined') {
					location.hash = '';
					location.hash = '#form';
				}
				setTimeout(_.comment.scrollHigher, 1);
				_.o['t'].focus();
			} else _.o['comment'].style.display =  'none';
			return false;
		},
		scrollHigher: function() { /* TEMP FIX */
			document.body.scrollTop -= 50;
		},
		check: function() {
			var k = 2, o = _.o['c'], s = jQuery.trim(o.value);
			if (s === '') o.style.borderColor = 'red';
			else {
				o.style.borderColor = '';
				--k;
			}
			o = _.o['e'];
			s = jQuery.trim(o.value);
			if (s === '' || !/^[^\@]+\@[a-z0-9\.\-\_]+\.[a-z]{2,6}$/i.test(s)) o.style.borderColor = 'red';
			else {
				o.style.borderColor = '';
				--k;
			}
			return k === 0 ? true : false;
		}
	}
};

/*
 * 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 (typeof 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.71d868144d360a0223cba6e3f5a0c1dd.png" alt=""></a></td>';
		else html[html.length] = '<td class="l"><img src="/_/left.71d868144d360a0223cba6e3f5a0c1dd.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.05ea07deeda964e2279a048c6f25dbf6.png" alt=""></a></td>';
		else html[html.length] = '<td class="r"><img src="/_/right.05ea07deeda964e2279a048c6f25dbf6.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 this.imageList === 'undefined') return false;
	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;
};

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(){
	_.comment.ini();
})();
