/*******************************************************************************
		Coded by GS on 05.02.2010, True Vision
*******************************************************************************/

function gebi(n) {
	if(!n) return false;
	if(!document.getElementById(n)) return false;
	return document.getElementById(n)
}

function mail(node, name, dom, a, display) {
	if (node) {
		var m = 'mailto:';
		a = a ? '.' + a : '.lv';
		$(node).html('<a href="' + m + name + '@' + dom + a + '">' + (display ? display : name + '@' + dom + a) + '</a>');
	}
}


var portfolio = {
	count: 0,
	prev: null,
	next: null,
	current: null,
	source: null,
	navigation: null,
	$prev: null,
	$next: null,
	$current: null,
	$source: null,
	$buttons: null,

	init: function(hash) {
		var self = this;
		
		try {
			for (var i in hash) this[i] = hash[i];
		} catch(e) {
			alert(e);
		}
		
		self.$prev = $(self.prev);
		self.$next = $(self.next);
		self.$current = $(self.current);
		self.$source = $(self.source);
		self.$buttons = $(self.navigation);
		
		self.show(0);
		
	},
	
	buttons: function() {
		var self = this;
		
		if (self.count > 0) {
			self.$prev
				.html(self.$buttons.eq(self.count - 1))
				.removeClass('deactive');
			$('a', self.$prev)
				.attr('href', '#:prev')
				.click(function(){
					self.show(-1);
					return false;
				});
		} else {
			self.$prev
				.html('')
				.addClass('deactive');
		}
		
		if (self.count < self.$source.length - 1) {
			self.$next
				.html(self.$buttons.eq(self.count + 1))
				.removeClass('deactive');
			$('a', self.$next)
				.attr('href', '#:next')
				.click(function(){
					self.show(1);
					return false;
				});
		} else {
			self.$next
				.html('')
				.addClass('deactive');
		}	
	},
	
	show: function(d) {
		var self = this;
		
		if ((self.count + d < (self.$source.length)) && (self.count + d >= 0)) {
			self.count += d;
		}

		self.buttons();
		
		self.$current.html(self.$source.eq(self.count));
	}
};


/*----------------------------------------------------------- ajax -----------------------------------------------------*/
var page = {
	settings: {},

	load: function(id, url, form, data, hash) {
		if (gebi(id)) {
			this.settings[id] = new this.fc(id, url, form, data, hash);
			this.settings[id].start();
		}
		
		return false;
	},
	
	script: function(source) {
		var scripts = [];
 
		// Strip out tags
		while(source.indexOf("<script") > -1 || source.indexOf("</script") > -1) {
			var s = source.indexOf("<script");
			var s_e = source.indexOf(">", s);
			var e = source.indexOf("</script", s);
			var e_e = source.indexOf(">", e);
 
			// Add to scripts array
			scripts.push(source.substring(s_e+1, e));
			// Strip from source
			source = source.substring(0, s) + source.substring(e_e+1);
		}
		
		// Loop through every script collected and eval it
		for(var i=0; i<scripts.length; i++) {
			try {
				var fs = scripts[i].indexOf("page.manipulate");
				if (fs > -1) {
					eval(scripts[i].substring(fs));
					scripts[i] = scripts[i].substring(0, fs);
				}
			}
			catch(e) {
			}
		}
		
		if (typeof page.manipulate == 'function') {
			page.manipulate(source);
			page.manipulate = null;
		}

		for(var i=0; i<scripts.length; i++) {
			try {
				eval(scripts[i]);
			}
			catch(e) {
			}
		}		
	}
};

/* constructor */
page.fc = function(id, url, form, data, hash) {
	this.id = id;
	this.url = url;
	this.form = form;
	this.data = data;
	this.preloader = 1;
	this.manipulation = 'html'; //jquery manipulations
	
	try {
		for (var i in hash) this[i] = hash[i];
	} catch(e) {
		alert(e);
	}
}

/* methods */
page.fc.prototype = {
	offset: function () {
		var h = gebi(this.id).offsetHeight + 'px';
	
		$('#' + this.id + ' div.preloader div.preoverlay').css({ height:h });
		$('#' + this.id + ' div.preloader div.pretimer').css({ height:h });
	},
	
	overlay: function() {
		var self = this;
		var tag = $('#' + this.id).get(0).tagName.toLowerCase();
		
		if (tag == 'tr') {
			$('#' + this.id).before('<tr id="' + this.id + '_preloader" class="preloader"><td><div class="pretimer"></div></td></tr>');
		} else {
			$('#' + this.id).prepend('<div id="' + this.id + '_preloader" class="preloader"><div class="preoverlay"></div><div class="pretimer"></div></div>')
		}
		
		this.offset();
		
		$(window).resize(function() {
			self.offset();
		});
	},
	
	success: function(html) {
		var bodyStart = html.toLowerCase().indexOf("<body>");
		var bodyEnd = html.toLowerCase().indexOf("</body>");

		if (bodyStart > -1 && bodyEnd > -1)	{
			html = html.substring( bodyStart + 6, bodyEnd );
		}
	
		switch(this.manipulation) {
			case 'append':
				$('#' + this.id).append(html);
				$('#' + this.id + '_preloader').remove();
				break;
			case 'prepend':
				$('#' + this.id).prepend(html);
				$('#' + this.id + '_preloader').remove();
				break;
			case 'replaceWith':
				$('#' + this.id).replaceWith(html);
				break;	
			case 0:
			case 'none':
				page.script(html);
				$('#' + this.id + '_preloader').remove()
				break;
			default:
				$('#' + this.id).html(html);
		}
		$('#' + this.id).removeClass('preloader-container');
	},
	
	start: function () {
		var self = this;
		
		$('#' + this.id).addClass('preloader-container');

		if (this.preloader) this.overlay();
		
		if (this.form) {
			var options = {
				type: 'post',
				url: self.url,
				data: self.data ? self.data : false,
				success: function(html) {			
					if (self.preloader) {
						setTimeout(function(){
							self.success(html);
						}, 500);
					} else {
						self.success(html);
					}
				}
			};
			$(this.form).ajaxSubmit(options);
		} else {
			$.ajax({
				type: 'get',
				url: self.url,
				data: self.data ? self.data : false,
				success: function(html) {
					if (self.preloader) {
						setTimeout(function(){
							self.success(html);
						}, 500);
					} else {
						self.success(html);
					}
				}
			});
		}
	}
};


/*---------------------------------------------------------- toggle ----------------------------------------------------*/
var toggle = {
	settings: {},

	init: function(_this, id, hash) {
		this.settings[id] = new this.fc(_this, id, hash);
		return this.settings[id];
	},
	
	show: function(_this, id, hash) {
		if (gebi(id)) {
			this.init(_this, id, hash).show();
		}
		_this.blur();
		
		return false;	
	},
	
	hide: function(_this, id, hash) {
		if (gebi(id)) {
			this.init(_this, id, hash).hide();
		}
		_this.blur();
		
		return false;	
	},
	
	execute: function(_this, id, hash) {
		if (gebi(id)) {
			this.init(_this, id, hash).execute();
		}
		_this.blur();
	
		return false;	
	},

	move: function(_this, id, hash) {
		var parent = hash['parent'];
		 
		if (gebi(id) && parent && gebi(parent)) {		
			this.init(_this, id, hash);
			this.settings[id].move();
		}
		_this.blur();
		
		return false;	
	},
	
	tabs: function(_this, hide, show, tabs) {
		if (_this.className.indexOf('toggle-active') != -1) return;
	
		if (tabs && _this.tagName.toLowerCase() != 'select') {
			var bind = Math.floor(_this.offsetLeft + _this.offsetWidth / 2);
			
			if (show) $(show + ' div.tab-spacer').css({ marginLeft:bind + 'px' });
			$(tabs + ' .toggle-active').removeClass('toggle-active');
			$(_this).addClass('toggle-active');
		}

		$(hide).css({ display:'none' });
		if (show) $(show).fadeIn();
			
		if (_this.tagName.toLowerCase() == 'a') _this.blur();
	},
	
	quantity: function(id, item, qid) {
		var $id = $('#' + id);
		var q = 0;
		
		$('#' + id + ' ' + item).each(function(){
			if (this.style.display != 'none') q++;
		});
			
		if (q == 0) {
			$id.css({ display:'none' });
		} else {
			$id.css({ display:'block' });
		}
		
		$('#' + qid).text(q);
	},
	
	toolbar: function (_this, d) {
		var $toolbar = $('div.toolbar', _this);
	
		$toolbar.stop();
	
		$toolbar.mouseover(function(e) {
			e.stopPropagation();
			$(this).stop().css({ opacity:1 });
		});

		if (d > 0) {
			$(_this).addClass('hover');
			$toolbar.css({ visibility:'visible' }).delay(200).animate({ opacity:1 }, 1000);
		} else {
			$(_this).removeClass('hover');
			$toolbar.animate({ opacity:0 }, 500, function() { $(this).css({ visibility:'hidden' }); });
		}

	},
	
	ischild: function (s, d) {
		while (s) {
			if (s == d) return true;
			s = s.parentNode;
		}
		return false;
	}
};

/* constructor */
toggle.fc = function(_this, id, hash) {
	var cur = toggle.settings[id];
	if (cur && cur.opener != _this && cur.opener.className.indexOf('toggle-active') != -1) {
		$(cur.opener).removeClass('toggle-active');
	}

	this.id = id;
	this.opener = _this;
	this.modal = 1;
	this.effect = 'slide';
	this.showtime = 0;
	this.move = 0;

	try {
		for (var i in hash) this[i] = hash[i];
	} catch(e) {
		alert(e);
	}
}

/* methods */
toggle.fc.prototype = {
	success: function() {
		var self = this;
		
		$(this.opener).removeClass('toggle-active');
		$(document).unbind('click', self.docevnt);
	},

	hide: function() {
		var self = this;
		
		if (this.showtime) clearTimeout(this.showtime);
		
		switch (this.effect) {
			case 'fade':
				$('#' + this.id).fadeOut(
					'slow',
					function() {
						self.success();
					}
				);
				break;
			case 'slide':
				$('#' + this.id).slideUp(
					'slow',
					function() {
						self.success();
					}
				);
				break;
			default:
				$('#' + this.id).css({ display:'none' });
				self.success();
		}
	},
		
	show: function() {
		var self = this;
		
		if (self.move) {
			modal.move(self.opener, $('#' + self.id + ' div.modal-w').get(0), self.move);
		}

		switch (this.effect) {
			case 'fade':
				$('#' + this.id).fadeIn();
				break;
			case 'slide':
				$('#' + this.id).slideDown();
				break;
			default:
				$('#' + this.id).css({ display:'block' });

		}
		
		$(this.opener).addClass('toggle-active');
		
		if (this.showtime) {
			this.showtime = setTimeout(function(){ toggle.hide(self.opener, self.id, { effect:self.effect }); }, this.showtime);
		}
			
		if (!this.modal) {
			self.docevnt = function(e){
				var target = e.srcElement || e.target;
				if (!toggle.ischild(target, self.opener) && !toggle.ischild(target, gebi(self.id))) {
					self.hide();
				}
			};
			$(document).bind('click', self.docevnt);
		}
	},

	execute: function() {
		var self = this;
		
		$('#' + this.id).click(function(e) {
			e.stopPropagation();
		});
		
		if ($('#' + this.id).css('display') == 'none') {
			self.show();
		} else {
			self.hide();
		}
	},
	
	move: function() {
		var show = 1;
		
		if (gebi(this.id).style.display != 'none') show = 0;
	
		$container = $('#' + this.id);
		
		var html = $container.html();
		var clas = $container.attr('class');
		
		$container
			.css({ display:'none' })
			.remove();
			
		var tmp = $('<div></div>')
				.attr('id', this.id)
				.addClass(clas)
				.html(html);
		
		if (show) tmp.css({ display:'none' });
		
		$('#' + this.parent).append(tmp);
		
		$(this.opener).addClass('toggle-active');
		
		if (show) this.show();
	}
};


/*---------------------------------------------------------- layers ----------------------------------------------------*/
var modal = {
	settings: {},

	init: function(_this, id, url, hash, data) {
		this.settings[id] = new this.fc(_this, id, url, hash, data);
		return this.settings[id];
	},

	add: function(_this, id, url, hash, data) {
		if (!gebi(id)) {
			this.init(_this, id, url, hash, data).add();
		} else {
			var $win = $('#' + id);
			if ($win.css('display') == 'none') {
				if (hash && hash['cache'] && hash['cache'] == 1) {
					modal.show(id);
				} else {
					$win.remove();
					this.init(_this, id, url, hash, data).add();
				}
			} else {
				modal.remove(id);
			}
		}

		if (_this) _this.blur();
	},
	
	remove: function(id) {
		var win = this.settings[id];
		if (win) win.remove();
	},
	
	show: function(id) {
		var win = this.settings[id];
		if (win) win.show();
	},
	
	move: function(_this, obj, d) {
		if (_this && obj) {
			switch (d) {
				case 'right':
					$(obj).css({ marginLeft:_this.offsetLeft + _this.offsetWidth + 'px' });
					break;
					
				default:
					$(obj).removeClass('modal-moved');
				
					var position = $(_this).position();
					var w = Math.floor(_this.offsetWidth / 2 - obj.offsetWidth / 2);
				
					if ((position.left + w) >= 0) {
						$(obj)
							.css({ marginLeft:w + 'px' })
							.addClass('modal-moved');
					}
			}
		}
	}
};

/* constructor */
modal.fc = function(_this, id, url, hash, data) {
	this.id = id;
	this.opener = _this;
	this.url = url;
	this.html = '';
	this.data = data ? data : 0;
	this.scroll = 0;
	this.modal = 1;
	this.overlay = 1;
	this.view = 1;
	this.move = 0;
	this.close = 0;
	this.container = 'body';
	this.spacermove = 0;
	this.cache = 0;
	
	try {
		for (var i in hash) this[i] = hash[i];
	} catch(e) {
		alert(e);
	}
};

/* methods */
modal.fc.prototype = {
	add: function() {
		var self = this;
		
		$(this.opener).addClass('toggle-active');
		
		if (this.container == 'body') {
			if (!this.scroll) window.scroll(0, 0);
			
			$('body').append('<div id="' + this.id + '" class="modal" style="visibility:hidden;"><table class="overlay' + (this.scroll ? ' overlay-scrollable' : '') + '"><tr><td id="' + this.id + '_overlay" class="overlay"><table id="' + this.id + '_container" class="modal' + (this.view == 1 ? '' : 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 png">'+ (this.close == 1 ? '<div class="modal-close"><a class="png" href="#close" onclick="modal.remove(\'' + this.id + '\'); return false;"></a></div>' : '') + '<div class="modal-content"><div id="' + this.id + '_content"><div class="modal-preloader"></div></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>' + (this.overlay ? '<div class="overlay' + (this.overlay == 1 ? '' : this.overlay) + '"></div><iframe class="overlay"></iframe>' : '') + '</div>');
		} else {
			$(this.container).append('<div id="' + this.id + '" class="modal-bind' + (this.view == 1 ? '' : this.view) + '" style="visibility:hidden;"><div class="modal-w"><div class="modal-spacer png"></div><table id="' + this.id + '_container" class="modal' + (this.view == 1 ? '' : 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-content">' + (this.close == 1 ? '<div class="modal-close"><a class="png" href="#close" onclick="modal.remove(\'' + this.id + '\'); return false;"></a></div>' : '') + '<div id="' + this.id + '_content"><div class="modal-preloader"></div></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></div></div>');
			if (this.overlay) $('body').append('<div id="' + this.id + '_overlay" style="visibility:hidden;"><div class="overlay' + (this.overlay == 1 ? '' : this.overlay) + '"></div><iframe class="overlay' + (this.overlay == 1 ? '' : this.overlay) + '"></iframe></div>');
		}
		
		$('#' + this.id).css({ visibility:'visible' });
		$('#' + this.id + '_overlay').css({ visibility:'visible' });
		
		if (!this.modal) {
			self.docevnt = function(e){
				var target = e.srcElement || e.target;
				if (!toggle.ischild(target, self.opener) && !toggle.ischild(target, gebi(self.id))) {
					self.remove();
				}
			};
			$(document).bind('click', self.docevnt);
		}
				
		if (this.url) {
			$.ajax({
				type: 'get',
				url: self.url,
				data: self.data,
				success: function(html) {
					var bodyStart = html.toLowerCase().indexOf("<body>");
					var bodyEnd = html.toLowerCase().indexOf("</body>");

					if (bodyStart > -1 && bodyEnd > -1)	{
						html = html.substring( bodyStart + 6, bodyEnd );
					}
				
					$('#' + self.id + '_content').html(html);
					$('#' + self.id + '_overlay').removeClass('overlay-preloader');
					if (self.move) {
						modal.move(self.opener, $('#' + self.id + ' div.modal-w').get(0), self.move);
					}
				}
			});
		} else {
			$('#' + self.id + '_content').html(self.html);
			$('#' + self.id + '_overlay').removeClass('overlay-preloader');
		}
	},
	
	show: function() {
		var self = this;
		
		$(this.opener).addClass('toggle-active');
		$('#' + this.id).css({ display:'block' });
		
		if (this.container == 'body') {
			if (!this.scroll) window.scroll(0, 0);
		} else if (this.overlay) {
			$('body').append('<div id="' + this.id + '_overlay"><div class="overlay' + (this.overlay == 1 ? '' : this.overlay) + '"></div><iframe class="overlay"></iframe></div>');
		}
		
		if (!this.modal) $(document).bind('click', self.docevnt);
	},

	remove: function() {
		var self = this;
		
		$(document).unbind('click', self.docevnt);
		
		if (this.cache) {
			$('#' + this.id).css({ display:'none' });
		} else {
			$('#' + this.id).remove();
		}
		
		if (this.container != 'body' && this.overlay) $('#' + this.id + '_overlay').remove();
		
		$(this.opener).removeClass('toggle-active');
	}
};


$(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" style="display:none;"></div><iframe class="overlay"></iframe></div>');
					
					$('#' + self.id + ' div.overlay').fadeIn('500', function(){ $(this).css({ opacity:0.8 }) });
					
					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' },
			300,
			'',
			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 />')
											.addClass('modal-next pngscale')
											.attr('href', tmp[i].href)
											.click(
												function() {
													self.navigate(this.href, group);
													this.blur();
													return false;
												}
											);
					} else {
						this.prev	=	$('<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.overlay').fadeOut();
		$('#' + id + ' div.modal-close').remove();
		$('#' + id + '_navigator').remove();
		$('#' + id +'_image')
			.css({ visibility:'hidden' })
			.animate(
				{ width:0, height:0 },
				300,
				'',
				function() {
					$('#' + id).remove();
				}
			);
	}
};

