function gallery(elem) {
	this.list=$(elem);
	
	this.items=this.list.find('li');
	
	this.selected_item=this.list.find('li.selected');
	
	this.big_picture=$('#big-photo-container');
	
	this.events();
	
	this.anim_func = EEQ.Quartic.easeInOut;
	
	this.anim_dur = 20;
	
	this.animating=false;
};

gallery.prototype = {
	events:function() {
		var me=this;
		this.list.find('a').click(
			function() { 
			if ($(this).parent().is('.selected')){
			return false;
			}
			if (this.animating){
				return false;
			}
			var par=$(this).parent();
			var target=$(this).attr('href');
			return me.changePictures(target,par);
		}
		);
	
	},
	changePictures:function(target,parent) {
		this.animating=true;
		var me=this;
		this.loader_icon = parent.find('i').eq(0);
		this.icon_timeout = setTimeout(function(){me.loader_icon.addClass("on");},100);
		var old_img=this.big_picture.find('img').eq(0);
		
		var new_img=$('<img>');
		new_img.attr('src', target);
		
		
		this.big_picture.prepend(new_img);
		
		
		var new_left=this.big_picture.width()+'px';
		var old_left=-this.big_picture.width()+'px';
		new_img.css({
					position:'absolute',
					left:new_left,
					zIndex:'10'});
		
		var tw1 = new Tween(old_img, 'left', this.anim_func, '0', old_left, this.anim_dur);
		
		tw1.onMotionFinished = function(obj){
			
			$(obj).remove();
		};
		clearInterval(me.icon_timeout);
		this.loader_icon.removeClass("on");
		var tw2 = new Tween(new_img, 'left', this.anim_func, new_left, '0', this.anim_dur);
		this.animating=false;
		
		
		tw2.onMotionFinished = function(obj){
			me.list.find('li.selected').find('img').eq(0).css('display','block');
			me.list.find('li.selected').removeClass('selected');
			parent.toggleClass('selected');
			parent.find('img').eq(0).css('display','none');
		};
		return false;
		//new_img.animate({ left:"0" },500);
		//old_img.animate({ left:"-100%" },500);
		//old_img.remove();
	}
};
$(function(){
			new gallery('#gallery');
});

/* catalouge gallery */
$(function(){
	$(".image.overeffect").each(function(i, v) {
		v.thumb = $(".thumbnail", v);
		v.full = $(".full-size", v);
		var fullimg = $("img",v.full);
		v.full.addClass('scripted');
		fullimg.load(function() {
			if(this.naturalWidth)
				this.parentNode.style.width = this.naturalWidth +'px';
		});
		this.thumb.hover(function() {
			this.parentNode.full.fadeIn("fast");
		}, function() {
			this.parentNode.full.fadeOut("slow");
		});
	});
});

/* tooltips */
$(function() {
	$("a[title]").each(function(){
		this.tooltip=$('<div class="tooltip">'+ this.title +'</div>');
		this.tooltip.appendTo('body');
		this.title='';
		$(this).hover(function() {
			this.tooltip
				.animate({opacity: 1.0}, 100) /* delay */
				.fadeIn("fast");
		}, function() {
			this.tooltip
				.stop() /* stop delay */
				.stop() /* stop fadeIn */
				.fadeOut("fast");
			
		});
	});
	window.tooltips = $("body>div.tooltip");
	$(document).mousemove(function(e) {
		var x = e.pageX + 20;
		var y = e.pageY + 20;
		
		var w = document.body.offsetWidth;
		
		var tw = parseInt(window.tooltips.css('width'))
			+ parseInt(window.tooltips.css('padding-left'))
			+ parseInt(window.tooltips.css('padding-right'));
		
		if(x + tw + 4 >= w) x = w - tw - 4;
		window.tooltips.css({
			left: x,
			top: y
		});
	});
});

/* guest book */
$(function() {
	$(".collapsible")
		.show()
		.each(function(i, v) {
			v.tlink = $(this).parent().find('.toggle-link');
			v.tlinka = $('a', v.tlink);
			v.tlinka[0].tblock = v;
			
			v.show = function() {
				$(this).slideDown('slow');
				this.tlink.addClass('active');
			}
			v.hide = function() {
				$(this).slideUp('slow');
				this.tlink.removeClass('active')
			}
			v.toggle = function() {
				if($(this).css('display')=='none') {
					this.show();
				} else {
					this.hide();
				}
			};
			
			v.tlinka.click(function() {
				if(this.tblock.toggle) this.tblock.toggle();
				return false;
			});
		});
	
	$("a[href$=#show-all]")
		.click(function() {
			if($(this).text()=='Показать все ответы') {
				$("div.post .collapsible").each(function(i, v) {
					v.show();
				});
				$(this).text('Скрыть все ответы');
			} else {
				$("div.post .collapsible").each(function(i, v) {
					v.hide();
				});
				$(this).text('Показать все ответы');
			}
			return false;
		})
});

/* Objects */
$(function() {
	$(".object-gallery > a")
		.attr('rel', 'lightbox-object')
		.nyroModal();
});

/* Top dropdown menu */
$(function() {
	$('#menu-primary li ul').each(function() {
		var $t = $(this);
		$t.css('height', '1px');
		$t.show();
		var w = this.offsetWidth;
		if($.browser.mozilla && $.browser.version.slice(0,3) < 1.9 && w>=0)
			$t.css('width', w +'px');
		$t.css('display', '');
		$t.css('height', 'auto');
		$t.children('li a').css('width', '100%');
	});
	$('#menu-primary > ul > li')
		.hover(function() {
			$(this).addClass('hover');
		}, function() {
			$(this).removeClass('hover');
		});
});
