/* ******************************************************************************
		Coded by GS on 25.02.2009, True Vision
****************************************************************************** */

function gebi(n) {
	if(!n) return false;
	if(!document.getElementById(n)) return false;
	return document.getElementById(n)
}

function mail(id, name, dom, a, display) {
	var m = 'mailto:';
	a = a ? '.' + a : '.lv';
	var html = '<a href="' + m + name +'@'+ dom + a +'">' + (display ? display : name + '@' + dom + a)+'</a>';
	
	if (id && gebi(id)) {
		$('#' + id).html(html);
	} else {
		document.write(html);
	}
}

function captcha(_this) {
	_this.src = _this.src + Math.random();
}


dose = {
	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);
		});
	},
	
	content: 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);
				}
			});
		}
	}
};


modal = {
	add: function(id, url, hash, data) {
		this.scroll = 0;
		this.modal = 0;
		this.iframe = 0;
		this.width = 800;
		this.height = 600;
		
		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"><tr><td class="modal-11"><div></div></td><td class="modal-12"><div></div></td><td class="modal-13"><div></div></td></tr><tr><td class="modal-21"><div></div></td><td class="modal-22"><div class="modal-content"><div class="modal-close"><a class="png" href="#close" onclick="modal.remove(\'' + id + '\'); this.blur; return false;"></a></div><div id="' + id + '_content"></div></div></td><td class="modal-23"><div></div></td></tr><tr><td class="modal-31"><div></div></td><td class="modal-32"><div></div></td><td class="modal-33"><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();
			});
		}
		
		//alert(1);
		if (this.iframe) {
			$('#' + id + '_content').html('<iframe src="' + url + '" frameborder="0" style="width:' + this.width + 'px; height:' + this.height + 'px;"></iframe>');
			$('#' + id + '_overlay').removeClass('overlay-preloader');
		} else {
			$.ajax({
				type: 'get',
				url: url,
				data: data ? data : false,
				success: function(html) {
					$('#' + id + '_content').html(html);
					$('#' + id + '_overlay').removeClass('overlay-preloader');
				}
			});
		}
	},

	remove: function (id) {
		$('#' + id).css({ display:'none' })
			.remove();
	}
};


gallery = {
	tmr: 0,
	count: 0,
	id: '',

	init: function(g, count) {
		gallery.count = count;
		gallery.id = g;
		
		var iw = 114;
		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 = '114px';
		wrap.style.width = (iw * count) + 'px';
		
		var offset = Math.floor((gebi(g).offsetWidth - 88) / 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);
	},
		
	show: function(_this, g, id) {
		if (_this.parentNode.className.indexOf('gallery-active') == -1) {
		
			var type = /\.jpg|\.jpeg|\.png|\.gif/g;
			type = _this.href.toLowerCase().match(type);
				
			if(type == '.jpg' || type == '.jpeg' || type == '.png' || type == '.gif') {
			
				var container = $('#' + id);
				var img = $('#' + id + ' img:first');
				
				$('#' + g + ' div.gallery-item').removeClass('gallery-active');
				$(_this.parentNode).addClass('gallery-active');
				
				container.addClass('gallery-preloader');
				
				preloader = new Image();
				preloader.onload = function(){
					preloader.onload = null;
					
					var w = preloader.width;
					var h = preloader.height;
					img.attr('src', _this.href)
						.animate(
							{ width:w + 'px', height:h + 'px' },
							1500,
							'',
							function() {
								container.removeClass('gallery-preloader');
								gallery.init(gallery.id, gallery.count);
							}
						);
				}
				preloader.src = _this.href;
			}
		}
			
		_this.blur(); 
	}
};


