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() {
	$('.catalogue').find("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;
		})
});
$(function() {
$('.b-changing-frames-gallery').each(function(){
       //var bigPhoto=$(this).find('.b-big-pic');
       var me=$(this);
	  
      var points=me.find('.b-changing-frames-points .b-chfr-point');
	  
       var col=points.length;
       var currentSlide=0,prevSlide;
       var wholeTimeOut=0;
       var newIndex=0;
	   var frames=$('.b-animation-list .b-animation-list-frame');

       var t;
       var inAnimation=false,
	   titles=$('.b-years-change'),
	   titlesSource=$('.b-animation-years');
       
       points.click(function(){
	   	if ($(this).hasClass('b-chfr-point-sel')) return false;
           wholeTimeOut=1;
		   prevSlide=currentSlide;
           currentSlide= points.index($(this));
		   
           clearTimeout(t);
           slideShow();
		   inAnimation=false;
           return false;
       });
       t=setTimeout(slideShow,7000);

       function slideShow() {
	   
            if (inAnimation) return false;
            inAnimation=true;


		   if (!wholeTimeOut) {
		   	prevSlide=currentSlide;
		   	currentSlide++;
		   	if (currentSlide > col-1 ) {
				currentSlide = 0;
				prevSlide=col-1;
			} 
		   		
		   }
		   
		   me.find('.b-chfr-point-sel').removeClass('b-chfr-point-sel');
           points.eq(currentSlide).addClass('b-chfr-point-sel');
		    //$('.b-animation-list').animate({height:frames.eq(currentSlide).height()+40},500);
		/*if (!$.browser.msie) {
			titles.fadeOut(500, function(){
				$(this).text(titlesSource.eq(currentSlide).text());
				$(this).fadeIn(500);
			})
		} else {*/
			titles.text(titlesSource.eq(currentSlide).text());
			titles.show()
		/*}*/
		   frames.eq(prevSlide).fadeOut(1000);
           frames.eq(currentSlide).fadeIn(1000,function(){
		 
					
                    inAnimation=false;
                    t=setTimeout(function(){
                        
                        slideShow();
                    },3500);
	        
           });
       }

   	});

/* Objects */
if ($('.object-gallery').length) {
	slidingGallery($('.object-gallery'));
	}

});

function slidingGallery(rootElem){
	var me=this;
	var galleryWrapper=rootElem.find('.b-object-gallery');
	var nav=rootElem.find('.b-object-gallery-nav a');
	var imgCol=nav.length;
	var globalSrc=new Array(imgCol);
    var auto,timer;
	var controls={
		leftCont:rootElem.find('.b-ogc-left-arrow '),
		rightCont:rootElem.find('.b-ogc-right-arrow ')
	}
	var resizeCount=0;
	var speed=500;
	if (nav.length>1) {controls.leftCont.show(); controls.rightCont.show(); };
	
	var imgContainer=rootElem.find('.b-ogc-inner'),	
		containerWidth=imgContainer.width();

	var inMotion=false,
		currentSlide=0,
		leftPos=0;
	
	var fakeImg_first=$('<img src="" alt="">');
	fakeImg_first.attr('src',nav.eq(currentSlide).attr('href'));
	
	imgContainer.append(fakeImg_first);
	var currentImage=imgContainer.find('img');
	
	fakeImg_first.load(function(){		
		calculateDimension(fakeImg_first);
		fakeImg_first.css('left',(imgContainer.width()-fakeImg_first.width())/2)
		imgContainer.css('height',fakeImg_first.height()+'px');
	});
	

	function calculateDimension(appendImage){		
		var scrollBefore=$(window).height()-$('.left-shadow').height();		
		var imgWidth=appendImage.width();		
		var wrapperWidth=appendImage.width()+parseInt(imgContainer.parent().css('margin-left'))+parseInt(imgContainer.parent().css('margin-right'));
	
		if (wrapperWidth>rootElem.width())		{
			wrapperWidth=rootElem.width();
			imgWidth=rootElem.width()-(parseInt(imgContainer.parent().css('margin-left'))+parseInt(imgContainer.parent().css('margin-right')));
		};
		appendImage.css('width',imgWidth);
		var imgHeight=appendImage.height();
		var scrollAfter=$(window).height()-$('.left-shadow').height();		
		if ((scrollBefore>=0)&&(scrollAfter<0)) {
			imgWidth-=11;
			appendImage.css('width',imgWidth);
			};
	}

	function motion(direction) {
		
		if (inMotion) return false;
		inMotion=true;
		var fakeImg= $('<img src="" alt="">');
		
		if (direction==1) {
			currentSlide++;		
			if (currentSlide==imgCol) currentSlide=0;
			fakeImg.css({left:containerWidth,top:0});
			leftPos=-containerWidth;
		}
		else {
			currentSlide--;
			if (currentSlide<0) currentSlide=imgCol-1;
			fakeImg.css({left:-containerWidth,top:0});
			leftPos=containerWidth;
		}
		
		imgContainer.append(fakeImg);
		fakeImg.load(function(){
			calculateDimension(fakeImg);
			var fakeLeftPos=(imgContainer.width()-fakeImg.width())/2;
			fakeImg.animate({left:fakeLeftPos},speed);
            
			currentImage.animate({left:leftPos+'px'},speed,function(){
					currentImage.remove();
					currentImage=imgContainer.find('img');
					if (currentImage.height()>imgContainer.height()) imgContainer.animate({height: currentImage.height()},speed/3);
					inMotion=false;
                    if (auto) {
                     // timer=setTimeout(function(){motion(1);},3000);
                    }
				});
			
		});
		fakeImg.attr('src',nav.eq(currentSlide).attr('href'));
		
		
	}
	controls.leftCont.click(function(){
      //  clearTimeout(timer);
        auto=false;
		motion(-1);
		
		//imgContainer.css('height',$('.b-ogc-inner').find('img').height()+'px');
	});
	controls.rightCont.click(function(){
      //  clearTimeout(timer);
        auto=false;
		motion(1);
		//imgContainer.css('height',$('.b-ogc-inner').find('img').height()+'px');
	});
	

  // timer=setTimeout(function(){motion(1);auto=true;},2000);
}
/* 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');
		});
});

