/*******************************************************************************
		Coded by GS on 13.04.2008, True Vision
*******************************************************************************/

function gebi(n) {
	if(!n) return false;
	if(!document.getElementById(n)) return false;
	return document.getElementById(n)
}


function isNumeric(e) {
	var c = e.keyCode ? e.keyCode : e.charCode
	if (c >= 48 && c <= 57 || c == 8 || c == 46 || c == 37 || c == 39) return true;
	return false;
}


function captcha(_this) {
	_this.src = _this.src + Math.random();
}


function mail(name, dom, a, display) {
	var m = 'mailto:';
	a = a ? '.' + a : '.lv';
	document.write('<a href="' + m + name + '@' + dom + a + '">' + (display ? display : name + '@' + dom + a) + '</a>');
}


function file_mask(_this, id) {
	var file = _this.value.split("\\");
	file = file[file.length-1];
	gebi(id).innerHTML = file;
	file = file.split("\.");
	file = file[file.length-1];
	gebi(id).className = 'filename filename-' + file;
}


function flash_height(id, h) {
	var o = gebi(id);
	if (o && h) o.style.height = h + 'px';
}


toggle = {
	init: function(_this, id, hash) {
		this.parent = _this;
		this.id = id;
		this.effect = 'slide';
		this.modal = 1;
		
		try {
			for (var i in hash) this[i] = hash[i];
		} catch(e) {
			alert(e);
		}
	},
		
	hide: function(_this, id, hash) {
		var self = this;
		
		this.init(_this, id, hash);
		
		switch (this.effect) {
			case 'fade':
				$('#' + this.id).fadeOut('slow');
				break;
			case 'slide':
				$('#' + this.id).slideUp('slow');
				break;
			default:
				$('#' + this.id).css({ display:'none' });
		}
		
		$(this.parent).removeClass('toggle-active');
	},
		
	show: function(_this, id, hash) {
		var self = this;
		
		this.init(_this, id, hash);
		
		switch (this.effect) {
			case 'fade':
				$('#' + this.id).fadeIn('slow');
				break;
			case 'slide':
				$('#' + this.id).slideDown('slow');
				break;
			default:
				$('#' + this.id).css({ display:'block' });
		}
		
		$(this.parent).addClass('toggle-active');
	},
	
	execute: function (_this, id, hash) {
		if (!gebi(id)) return false;
		
		var self = this;
		
		this.init(_this, id, hash);
		
		if (gebi(id).style.display == 'none') {
			if (!this.modal) {			
				$(document).click(function(e){
					var target = e.srcElement || e.target;
					if (target != _this && target != _this.childNodes[0]) self.hide(_this, id, hash);
				});
				
				$('#' + id).click(function(e){ e.stopPropagation(); });
			}
			this.show(_this, id, hash);
		} else {
			this.hide(_this, id, hash);
		}
		
		_this.blur();
	},
	
	tabs: function (_this, tabsid, id, url) {
		if (gebi(id) && _this.className.indexOf('toggle-active') == -1) {
			$('#' + tabsid + ' a.toggle-active').removeClass('toggle-active');
			$(_this).addClass('toggle-active');
			page.load(id, url);
		}
		_this.blur();
	}
}


gallery = {
	tmr: 0,
	count: 0,
	id: '',

	init: function(g, count) {
		gallery.count = count;
		gallery.id = g;
		
		var iw = 116;
		var scrollable = gebi(g + '_scrollable');
		var wrap = gebi(g + '_scrollable_wrap');
		var next = $('#' + g + '_next');
		var prev = $('#' + g + '_prev');
		
		scrollable.scrollLeft = 0;
		scrollable.style.width = '116px';
		wrap.style.width = (iw * count) + 'px';
		
		var offset = Math.floor((gebi(g).offsetWidth - 16) / iw);
		
		scrollable.style.width = (iw * (offset > count ? count : offset)) + 'px';
		
		if (offset > count || (scrollable.scrollLeft >= (scrollable.scrollWidth - scrollable.offsetWidth))) next.addClass('deactive');
		else next.removeClass('deactive');
		
		prev.addClass('deactive');
		
		$('#' + g + ' div.gallery-active').each(function(){
			if (this.offsetLeft) gallery.scroll(g, 1, this.offsetLeft);
		});

		prev.mouseover(function(e) {
				clearTimeout(gallery.tmr);
				gallery.scroll(g, -1);
			})
			.mouseout(function(e) {
				clearTimeout(gallery.tmr);
			});
		
		next.mouseover(function(e) {
				clearTimeout(gallery.tmr);
				gallery.scroll(g, 1);
			})
			.mouseout(function(e) {
				clearTimeout(gallery.tmr);
			});
	},

	scroll: function(g, d, cur) {
		var scrollable = gebi(g + '_scrollable');
		var next = $('#' + g + '_next');
		var prev = $('#' + g + '_prev');
		
		if (!cur) scrollable.scrollLeft += 10 * d;
		else scrollable.scrollLeft = cur;
		
		if (scrollable.scrollLeft > 0) prev.removeClass('deactive');
		else prev.addClass('deactive');
		
		if (scrollable.scrollLeft < (scrollable.scrollWidth - scrollable.offsetWidth)) next.removeClass('deactive');
		else next.addClass('deactive');
		
		if (!cur) gallery.tmr = setTimeout('gallery.scroll("' + g + '", ' + d + ')', 100);
	},
	
	load: function(_this, g, id, url) {
		if (_this.parentNode.className.indexOf('gallery-active') == -1) {	
			var container = $('#' + id);
			
			container.addClass('gallery-preloader');
			$('#' + id + ' table').animate({ opacity:'hide' });
			
			$('#' + g + ' div.gallery-item').removeClass('gallery-active');
			$(_this.parentNode).addClass('gallery-active');
		
			$.ajax({
				type: 'get',
				url: url,
				success: function(html) {
					container
						.html(html)
						.removeClass('gallery-preloader');
					$('#' + id + ' table').animate({ opacity:'show' });
				}
			});
			
			if (_this.rel) $('#print_image').attr('href', './?' + _this.rel + '&image');
		}
			
		_this.blur(); 
	}
};


page = {
	offset: function (id) {
		var h = gebi(id).offsetHeight + 'px';
	
		$('#' + id + ' div.preloader div.preoverlay').css({ height:h });
		$('#' + id + ' div.preloader div.pretimer').css({ height:h });
	},
	
	overlay: function(id) {
		var self = this;
		$('#' + id).prepend('<div class="preloader"><div class="preoverlay"></div><div class="pretimer"></div></div>');
		
		this.offset(id);
		
		$(window).resize(function() {
			self.offset(id);
		});
	},
	
	load: function (id, url, _this, data) {
		this.overlay(id);

		if (_this) {
			var options = {
				type: 'post',
				url: url,
				data: data ? data : false,
				success: function(html) {
					$('#' + id).html(html);
				}
			};
			$(_this).ajaxSubmit(options);
		} else {
			$.ajax({
				type: 'get',
				url: url,
				data: data ? data : false,
				success: function(html) {
					$('#' + id).html(html);
				}
			});
		}
	},
	
	scroll: function(id) {
		var offset = $('#' + id).offset();
		window.scroll(offset.left, offset.top);
	}
};


modal = {
	add: function(id, url, hash, data) {
		this.scroll = 0;
		this.modal = 0;
		this.view = null;
		
		try {
			for (var i in hash) this[i] = hash[i];
		} catch(e) {
			alert(e);
		}
		
		if (!this.scroll) window.scroll(0, 0);
		
		$('body').append('<div id="' + id + '" class="modal"><table class="overlay' + (this.scroll ? ' overlay-scrollable' : '') + '"><tr><td id="' + id + '_overlay" class="overlay overlay-preloader"><table id="' + id + '_container" class="modal' + (this.view ? '-' + this.view : '') + '"><tr><td class="modal-11 png"><div></div></td><td class="modal-12 pngscale"><div></div></td><td class="modal-13 png"><div></div></td></tr><tr><td class="modal-21 pngscale"><div></div></td><td class="modal-22"><div class="modal-close"><a class="png" href="#close" onclick="modal.remove(\'' + id + '\'); this.blur; return false;"></a></div><div id="' + id + '_content"></div></td><td class="modal-23 pngscale"><div></div></td></tr><tr><td class="modal-31 png"><div></div></td><td class="modal-32 pngscale"><div></div></td><td class="modal-33 png"><div></div></td></tr></table></td></tr></table><div class="overlay"></div><iframe class="overlay"></iframe></div>');
		
		if (!this.modal) {
			$('#' + id).click(function(){
				modal.remove(id);
			});	
			
			$('#' + id + '_container').click(function(e){
				e.stopPropagation();
			});
		}
		
		$.ajax({
			type: 'get',
			url: url,
			data: data ? data : false,
			success: function(html) {
				$('#' + id + '_content').html(html);
				$('#' + id + '_overlay').removeClass('overlay-preloader');
				$('#' + id + '_container').css({ visibility:'visible' });
			}
		});
	},

	remove: function (id) {
		$('#' + id).css({ display:'none' })
			.remove();
	}
};


win = {
	popup: function(url, name, hash) {
		this.width = 800;
		this.height = 600;
		
		try {
			for (var i in hash) this[i] = hash[i];
		} catch(e) {
			alert(e);
		}
	
		var left = (screen.width) ? (screen.width - this.width) / 2 : 0;
		var top  = (screen.height) ? (screen.height - this.height) / 3 : 0;

		settings = 'width=' + this.width + ', height=' + this.height + ', top=' + top + ', left=' + left + ', scrollbars=yes';
		w = window.open(url, name, settings);
		w.focus();
	}
};


txt = {
	size: function(_this, size) {
		function sizesend(size) {
			$.ajax({
				type: 'get',
				url: '.?text-size',
				data: { size:size }
			});
		}
		
		var cur = $('body').css('fontSize');
		cur = Number(cur.replace('px', ''));
		
		if (size > 1) {
			$('body').css({ fontSize:size + 'px' });
			$(_this.parentNode.parentNode).css({ backgroundPosition:'0 ' + (-(size - 10) * 14) + 'px' });
			
			sizesend(size);
		} else if (cur && ((size < 0 && cur > 10) || (size > 0 && cur < 16))) {
			size = cur + size;
			$('body').css({ fontSize:size + 'px' });
			$(_this.parentNode).css({ backgroundPosition:'0 ' + (-(size - 10) * 14) + 'px' });
			
			sizesend(size);
		}
		
		_this.blur();
	}
};


/*-----------------------------  http://www.kryogenix.org/code/browser/searchhi/ --------------------------------*/
function highlightWord(node,word) {
	// Iterate into this nodes childNodes
	if (node.hasChildNodes) {
		var hi_cn;
		for (hi_cn=0;hi_cn<node.childNodes.length;hi_cn++) {
			highlightWord(node.childNodes[hi_cn],word);
		}
	}
	
	// And do this node itself
	if (node.nodeType == 3) { // text node
		tempNodeVal = node.nodeValue.toLowerCase();
		tempWordVal = word.toLowerCase();
		if (tempNodeVal.indexOf(tempWordVal) != -1) {
			pn = node.parentNode;
			// check if we're inside a "nosearchhi" zone
			checkn = pn;
			while (checkn.nodeType != 9 && 
			checkn.nodeName.toLowerCase() != 'body') { 
			// 9 = top of doc
				if (checkn.className.match(/\bnosearchhi\b/)) { return; }
				checkn = checkn.parentNode;
			}
			if (pn.className != "searchword") {
				// word has not already been highlighted!
				nv = node.nodeValue;
				ni = tempNodeVal.indexOf(tempWordVal);
				// Create a load of replacement nodes
				before = document.createTextNode(nv.substr(0,ni));
				docWordVal = nv.substr(ni,word.length);
				after = document.createTextNode(nv.substr(ni+word.length));
				hiwordtext = document.createTextNode(docWordVal);
				hiword = document.createElement("var");
				hiword.className = "searchword";
				hiword.appendChild(hiwordtext);
				pn.insertBefore(before,node);
				pn.insertBefore(hiword,node);
				pn.insertBefore(after,node);
				pn.removeChild(node);
			}
		}
	}
}

function highlightSentence( node, str ) {
	var words = str.split(/\s+/);
	for (var i = 0; i < words.length; i++) {
		highlightWord( node, words[i] );
	}
}
