
function gallery(elem) {
	var me=this;
	this.opts={
		direction:1,
		acceleration:3,
		step:2
	}
	this.root=$(elem).eq('0');
	
	this.toLeft=$('.b-photogallery').find('.b-back');
	
	this.toRight=$('.b-photogallery').find('.b-next');
	
	var v=document.getElementById('gallery');
	if (($.browser.msie)&&($.browser.version=='7.0')) {
	//v=v.getElementsByTagName('div')[0];	
	}
	if(v.offsetWidth>this.root[0].clientWidth){
		this.toRight.addClass('b-stop-right');
		this.toLeft.addClass('b-stop-left');
	};
	v.scrollLeft=0;

	if (v.scrollLeft==0) {
		me.toLeft.addClass('b-stop-left');		
	}
	
	//alert($('#gallery .b-container').find('.b-item').eq(0).width());
	var l=$('#gallery .b-container').find('.b-item').length;

	this.toLeft.hover(
	function(){
		me.opts.direction=1;
		motion(me.root);
	},
	function(){
		stop();
	});
	this.toRight.hover(
	function(){
		me.opts.direction=-1;
		motion(me.root);
	},
	function(){
		stop();
	});
	
	var i=0,j=0;
	function motion(e){
		
		if (me.opts.direction==1) {
			v.scrollLeft-=me.opts.step;
			if (v.scrollLeft==0) {
				me.toLeft.addClass('b-stop-left');
				i=0;
			}
			else {
				if (i==0) {
					me.toRight.removeClass('b-stop-right');
					i=1;
					j=0;
				}
			}
		}
		else {
			v.scrollLeft+=me.opts.step;
			//v.scrollWidth-v.offsetWidth
			//$('#my').text(v.scrollLeft);
			if (v.scrollLeft==v.scrollWidth-v.offsetWidth) {
				me.toRight.addClass('b-stop-right');
				j=0;
			}
			else {
				if (j==0) {
					me.toLeft.removeClass('b-stop-left');
					j=1;
					i=0;
				}
			}
			//$('#gallery').scrollLeft(-100);
		}
		//alert(v.scrollLeft);
		//e.animate({marginLeft:-i+'px'},1,'swing')
		t=setTimeout(function(){motion(e)},1);
	}
	function stop() {
		clearTimeout(t);
		return false;
	}
}
$(function(){
	if($('.b-photogallery-inner .b-container').length)
		var g=new gallery('.b-photogallery-inner .b-container');
});

$(function() {
	$('.b-gallery-inner #gallery .b-item a').click(
		function() {
			if($(this).parent().hasClass('selected')) {
			return false;	
			}
		
		var in_animation=true;
		var currentLink=$(this);
		var main_container=$('#big-picture');
		var speed=500;
		var contH=main_container.height();
		var contW=main_container.width();
		
		if(proxy_image){
			//there is old proxy image with loading image, remove it
			proxy_image.onload = null;
			proxy_image = null;
		}
		/*if(loader_icon){
				clearInterval(icon_timeout);
				loader_icon.css('display','none');
				loader_icon = null;
			}*/
		
		var proxy_image = new Image();
		
		//var loader_icon = currentLink.parent().find('i').eq(0);
		//icon_timeout = setTimeout(function(){loader_icon.css('display','block');},500);
		
		proxy_image.onload = function(){
			
			var h=proxy_image.height;
			var w=proxy_image.width;
			var old_img=main_container.find('img').eq('0');
			old_img.css('z-index','2');
			
			var newImg=$("<img/>");
			main_container.prepend(newImg);
			
			newImg.css({position:'absolute',top:contH,left:'0',zIndex:'4'});
			
			newImg.attr({src:proxy_image.src});
			var v=Math.floor((contH-h)/2)+'px';
				
				var selected=$('#gallery').find('.selected');
				selected.find('img').animate({top:0},speed,function(){
						selected.removeClass('selected');										  
				});
				
				
				currentLink.find('img').animate({top:'-80px'},speed);
				currentLink.parent().addClass('selected');	
				old_img.animate({top:contH},speed);
				
					newImg.animate({top:0,left:0},speed,function(){
						old_img.remove();
					});	
				
			
		}
		proxy_image.src = $(this).attr('href');	
		return false;													  
		});
$('.b-content-2 .b-gallery:not(.b-objects-gallery) #gallery .b-item img').hover(function() {
		this.big = $('<div class="b-image-big"></div>').appendTo(this.parentNode.parentNode.parentNode.parentNode.parentNode).hide();
		var img = $('<img />').appendTo(this.big);
		var s = this.parentNode.parentNode.parentNode.parentNode.scrollLeft;
		if(s == 0) s = this.parentNode.parentNode.parentNode.scrollLeft; 
		
		img.get(0).cleft = this.parentNode.parentNode.offsetLeft - s + 46;
		img.get(0).gallery = this.parentNode.parentNode.parentNode.parentNode.parentNode;
		
		img.load(function() {
			var i = new Image();
			i.src = this.src;
			
			var l = this.cleft;
			if(l < 46) {
				l = 46;
			}
			if(l + i.width + 50 > this.gallery.offsetWidth) {
				l = this.gallery.offsetWidth - i.width - 96;
			}
			$(this).parent()
				.css('left', l +'px')
				.fadeIn('fast');
		});
		img.attr('src', this.src.replace('_foto', '_foto/big'));
	}, function() {
		this.big.fadeOut('normal', function() {
			$(this).remove();
		});
	});
	
	
});
$(function() {
  $(".collapsible")
    //.hide()
    .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.question .collapsible").each(function(i, v) {
          v.show();
        });
        $(this).text('Скрыть все ответы');
      } else {
        $("div.question .collapsible").each(function(i, v) {
          v.hide();
        });
        $(this).text('Открыть все ответы');
      }
      return false;
    });
    
//  $('div.question:first .collapsible').show();
//  $('div.question:first .toggle-link').addClass('active');
	$('.popup').click(function(event){
		event.preventDefault();
		var src=$(this).attr('href');
		var title=$(this).attr('title')||'';
		
		var newPic=new Image();
		newPic.onload=function() {
			var h=this.height;
			var w=this.width;
			
			if (title=='') {
			header='';
			}
			else {
				header='<h3 class="l-block b-medium-title">'+title+'</h3>';
			}
			if (!$('.b-popup-window').length) {
				var html='<div class="b-popup-window"><div class="b-close r-block">Закрыть страницу<i></i></div>'+header+'<div class="cl"></div><div class="b-line"></div><div class="b-imageholder"><img alt="" /></div></div>';
				$("#layout").prepend(html);
				
			}
			
				$('.b-imageholder img').attr('src',src);
				$('.b-popup-window h3').text(title);
			
			var blockWidth=parseInt($('.b-popup-window').css('padding-left'))*2+w;
			$('.b-popup-window')
			.css({left:'50%',marginLeft:-(blockWidth/2+30)+'px', top:'70px',width:blockWidth+'px'})
			.fadeIn(300);
			
			$('.b-close').click(function(){
				$('.b-popup-window').fadeOut(300);
			});
			
		}
		newPic.src=$(this).attr('href');
		return false;
		
	});
});

function popupWindow() {
	
	
	
}

/* Materials */
$(function() {
	var $sw = $('.b-list-material-switcher');
	var $notice = $('#notice');
	if($sw.length) {
		$('a', $sw).each(function() {
			//this.material = this.href.split('/').pop()
			this.material = this.id
			//alert(this.material);
			$(this).bind("mouseenter", function() {
				$notice.children('.b-material.g-active').removeClass('g-active');
				$notice.children('.b-material-' + this.material).addClass('g-active');
			});
		});
	}
	
function previewGallery(elem) {
    var previews=elem.find('.b-item a');
    var bigPic=$('#bigPic');
    var oldPic=bigPic.find('img').eq(0);
    var speed=600;
    var animationStatus=false;
    function previewClick(me) {
        oldPic=bigPic.find('img').eq(0);
        var newImg=$('<img alt="">');
        var fakeImg=new Image();

        newImg.css({top:bigPic.height(),zIndex:1});
        bigPic.append(newImg);

        newImg.load(function(){
          var me=$(this);
          
          var position=calculatePosition(me[0].width,me[0].height,bigPic);
          newImg.css({left:position.leftValue});
          oldPic.animate({top:bigPic.height()},speed,function(){

                me.animate({top:position.topValue},speed,function(){
                    oldPic.remove();
                    $(this).css({zIndex:2});
                    animationStatus=false;
                });
          });
        });
        newImg.attr('src',me.attr('href'));
        newImg.attr('alt',me.attr('alt'));
        fakeImg.src=me.attr('href');
        
        return false;
    }
    return {
        run:function() {
           previews.click(function(){
               var me=$(this);
               if (!animationStatus) {
                   if ((!me.parent().hasClass('b-selected'))) {
                       animationStatus=true;
                       elem.find('.b-selected').removeClass('b-selected').removeAttr('style');
                       me.parent().addClass('b-selected');
                       previewClick(me);
                   }
               }
               return false;
           });
           if($.browser.msie) {
               elem.find('.b-item').hover(function(){
                   $(this).css('border-color','#51a193');
               },function(){
                   if (!$(this).hasClass('b-selected'))
                   $(this).css('border-color','#c5c5c5');
               });
           }
        }
    };
}
function calculatePosition(iWidth, iHeight,container) {
        var topValue,leftValue;
        var picContainerWidth=container.width();
        var picContainerHeight=container.height();

			if (iWidth<picContainerWidth) {
			leftValue=picContainerWidth/2-iWidth/2+'px';
			}
			else {
				leftValue=0;
			}
			if (iHeight<picContainerHeight) {
				topValue=picContainerHeight/2-iHeight/2+'px';
			}
			else {
				topValue=0;
			}
            
			return ({
				topValue:topValue,
				leftValue:leftValue
			});
	}
		
var pG=previewGallery($('#gallery'));
    pG.run();	
	$(document).ready(function(){
		$('.close-header').click(function() {
			$.cookie('ltm_show_action', 0, {path:'/'});
			$('.action-text p, #action, .close-header').css('display','none');
			$('.b-header-panel, .open-header').css('margin-bottom', '0px');
			$('.b-header-panel').addClass('b-header-panel close-panel');
			$('.open-header').show();
		});
		$('.open-header').click(function() {
			$.cookie('ltm_show_action', 1, {path:'/'});
			$('.action-text p, #action, .close-header').show();
			$('.open-header').css('display','none');
			$('.b-header-panel').removeClass('b-header-panel close-panel').addClass('b-header-panel');
			$('.open-header, .b-header-panel').css('margin-bottom', '11px');
		});
		if ($.cookie('ltm_show_action') == null || $.cookie('ltm_show_action') == 1) {
			$('.open-header').click();
		}
		if ($.cookie('ltm_show_action') == 0) {
			$('.close-header').click();
		}
	});
});	


