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) return true;
	return false;
}


function mail(id, name, dom, a, display) {
	var m = 'mailto:';
	a = a ? '.' + a : '.com';
	$('#' + id).html('<a href="' + m + name + '@' + dom + a + '">' + display + '</a>');
}


toggle = {
	timer: false,
	interval: 12000,
	item: 2,

	pagination: function(_this, id, item) {
		if (_this.className.indexOf('toggle-active') == -1){
			clearInterval(toggle.timer);
			
			if (_this) {
				$('#' + id + ' div.pagination a').removeClass('toggle-active');
				$(_this).addClass('toggle-active');
			}
			
			$('#' + id + ' div.item-active').css({ display:'none' });
			$('#' + item)
				.css({ display:'block' })
				//.fadeIn('fast')
				.addClass('item-active');
				
			var tmp = item.split('i');
			toggle.item = parseInt(tmp[1]) + 1;
			if (!gebi('i' + toggle.item)) toggle.item = 1;
		}
		
		_this.blur();
	},
	
	slideshow: function(id, item){	
		toggle.pagination(gebi('a'+toggle.item), id, item);
		


		toggle.timer = setInterval(
			function(){ toggle.slideshow(id, 'i' + toggle.item)},
			toggle.interval
		);
	},
	
	tools: function(_this, id) {
		if (_this.className.indexOf('slideshow-active') == -1){
			$(_this).addClass('slideshow-active');
			toggle.slideshow(id, 'i' + toggle.item);
		} else {
			$(_this).removeClass('slideshow-active');
			clearInterval(toggle.timer);
		}
		
		_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);
				}
			});
		}
	}
};


$(function(){
	gallery.init();
});


gallery = {
	id: 'gallery',

	init: function() {
		$('a.gallery-preview').click(function(){
			var group = this.rel || false;
			gallery.show(this.href, group);
			this.blur();
			return false;
		});
	},

	show: function(url, group) {
		clearInterval(toggle.timer);
		
		try {
			var type = /\.jpg|\.jpeg|\.png|\.gif/g;
			type = url.toLowerCase().match(type);
					
			if (type == '.jpg' || type == '.jpeg' || type == '.png' || type == '.gif') {
				var self = this;
				
				window.scroll(0, 0);
				
				this.navigator(url, group);	
				
				preloader = new Image();
				preloader.onload = function(){
					preloader.onload = null;
					
					var w = preloader.width;
					var h = preloader.height;
					
					$('body').append('<div id="' + self.id + '" class="modal"><table class="overlay"><tr><td id="' + self.id + '_overlay" class="overlay"><table id="' + self.id + '_container" class="modal modal-gallery"><tr><td class="modal-11 pngscale"><div></div></td><td class="modal-12 pngscale"><div></div></td><td class="modal-13 pngscale"><div></div></td></tr><tr><td class="modal-21 pngscale"><div></div></td><td class="modal-22"><div id="' + self.id + '_content" class="modal-content modal-preloader"><a href="#close" onclick="gallery.remove(\'' + self.id +'\'); this.blur; return false;"><img id="' + self.id +'_image" class="modal-image" style="visibility:hidden;" src="' + url + '" alt="" width="60px" height="60px" /></a>' + ( group ? '<div id="' + self.id + '_navigator"></div>' : '') + '</div></td><td class="modal-23 pngscale"><div></div></td></tr><tr><td class="modal-31 pngscale"><div></div></td><td class="modal-32 pngscale"><div></div></td><td class="modal-33 pngscale"><div></div></td></tr></table></td></tr></table><div class="overlay"></div><iframe class="overlay"></iframe></div>');
					
					var $navigator = $('#' + self.id + '_navigator');
					var $img = $('#' + self.id + '_image');
					
					if (self.next) $navigator.append(self.next);
					if (self.prev) $navigator.append(self.prev);
					
					self.animate($img, w, h);

					$('#' + self.id + '_container').click(function(e){
						e.stopPropagation();
					});					

					$('#' + self.id + '_overlay').click(function(){
						self.remove(self.id);
					});					
				}
				preloader.src = url;
			}
		} catch(e) {
			alert( e );
		}
	},
	
	animate: function($img, w, h) {
		var self = this;
		$img.animate(
			{ width:w + 'px', height:h + 'px' },
			1000,
			'',
			function() {
				$img.css({ visibility:'visible' });
				$('#' + self.id + ' a.modal-next').css({ visibility:'visible' });
				$('#' + self.id + ' a.modal-prev').css({ visibility:'visible' });
			}
		);
		
	},
	
	navigator: function(url, group) {
		this.prev = null;
		this.next = null;
	
		if (group) {
			var self = this;
			var flag = 0;
			var tmp = $('a[rel=' + group + ']').get();
			
			for (i = 0; ((i < tmp.length) && (this.next == null)); i++) {
				if (!(tmp[i].href == url)) {
					if (flag == 1) {
						this.next	=	$('<a></a>')
											.addClass('modal-next pngscale')
											.attr('href', tmp[i].href)
											.click(
												function() {
													self.navigate(this.href, group);
													this.blur();
													return false;
												}
											);
					} else {
						this.prev	=	$('<a></a>')
											.addClass('modal-prev pngscale')
											.attr('href', tmp[i].href)
											.click(
												function() {
													self.navigate(this.href, group);
													this.blur();
													return false;
												}
											);
					}
				} else {
					flag = 1;
				}
			}
		}
	},
	
	navigate: function(url, group) {
		var type = /\.jpg|\.jpeg|\.png|\.gif/g;
		type = url.toLowerCase().match(type);
				
		if (type == '.jpg' || type == '.jpeg' || type == '.png' || type == '.gif') {
			var self = this;
			var $img = $('#' + this.id + '_image');
			
			$('#' + this.id + ' a.modal-next').remove();
			$('#' + this.id + ' a.modal-prev').remove();
			
			$img.css({ visibility:'hidden' });
			
			this.navigator(url, group);	
		
			preloader = new Image();
			preloader.onload = function(){
				preloader.onload = null;
				
				var $navigator = $('#' + self.id + '_navigator');
				var w = preloader.width;
				var h = preloader.height;
				
				$img.attr('src', url);
				
				if (self.next) $navigator.append(self.next);
				if (self.prev) $navigator.append(self.prev);
				
				self.animate($img, w, h);
			}
			preloader.src = url;
		}
	},
	
	remove: function(id) {
		$('#' + id + ' div.modal-close').remove();
		$('#' + id + '_navigator').remove();
		$('#' + id +'_image')
			.css({ visibility:'hidden' })
			.animate(
				{ width:0, height:0 },
				1000,
				'',
				function() {
					$('#' + id).remove();
				}
			);
	}
};

