function collapse(obj)
{
	$(obj).parent().parent().prev().find('.title a').click();
}
window.onload = function(){
	vertical_gallery();
};

function vertical_gallery()
{
	$(".images").each(function(){
		big_h   = $(this).find(".big").height();
		big_w   = $(this).find(".big").width();

		var src = $(this).find("a:first").attr('href');
		var big_img = $('<img />');
		$(big_img).load(function(){
			gall_load(this);
		}).attr('src',src);
		$(this).find(".big").empty().append(big_img);

		ul_h  = $(this).find("ul").height();
		par_h = $(this).find(".ul-wrap").height();
		/*
		if(ul_h<par_h)
		{
			$(this).find("ul").css('top',(par_h-ul_h)/2);
		}
		*/
		$(this).find("a").click(function(){
			var src		= $(this).attr('href');
			var new_img = $('<img />');
			var par = $(this).parent().parent().parent().parent();
			$(new_img).load(function(){
				$(this).css('opacity',0);
				par.find(".big").append(this);
				gall_load(this);
				par.find(".big img").not(":last").animate({opacity:0},600,function(){ $(this).remove(); });
				par.find(".big img:last").animate({opacity:1},600);
			}).attr('src',src);
			return false;
		});
		$(this).css('opacity',0)
			.css('visibility','visible')
			.animate({opacity:1},300);
	});
	a_more();
}
function gall_load(big_img)
{
	var img_w = $(big_img).width();
	var img_h = $(big_img).height();
	/*
	if(img_w<big_w)
		$(big_img).css('left',(big_w-img_w)/2);
		*/
	$(big_img).css('left','0');
	/*
	if(img_h<big_h)
		$(big_img).css('top',(big_h-img_h)/2);
		*/
}

function a_more()
{
	$("a.more:not('.init')").toggle(function(){
		$(this).parent().prev('.images').slideUp(300);
		$(this).siblings(".anim").height($(this).siblings(".anim").height()).html($(this).siblings(".body").html()).animate({height:$(this).siblings(".body").height()+20},400,
			function(){
				$(this).siblings('a').text('less');
				document.location=$(this).siblings('a').attr('href');
			});
		$(this).addClass('opened');
	},function(){
		$(this).parent().prev('.images').slideDown(300);
		$(this).siblings(".anim").animate({height:$(this).siblings(".short").height()},400,function(){
			$(this).html($(this).siblings(".short").html());
			$(this).siblings('a').text('more...');
			document.location=$(this).siblings('a').attr('prev');
		});
		$(this).removeClass('opened');
	}).addClass('init');

	$("a.acomments:not('.init')").toggle(function(){
		$(this).parent().prev('.images').slideUp(300);
		$(this).siblings(".anim").height($(this).siblings(".anim").height()).html($(this).siblings(".body").html()).animate({height:$(this).siblings(".body").height()+20},400,
			function(){
				$(this).siblings('a').text('hide comments');
				document.location=$(this).siblings('a').attr('href');
			});
		$(this).addClass('opened');
	},function(){
		$(this).parent().prev('.images').slideDown(300);
		$(this).siblings(".anim").animate({height:$(this).siblings(".short").height()},400,function(){
			$(this).html($(this).siblings(".short").html());
			$(this).siblings('a').text('comments');
			document.location=$(this).siblings('a').attr('prev');
		});
		$(this).removeClass('opened');
	}).addClass('init');

	$("a.subtitle:not('.init')").click(function(){
		$(this).next().next().next().find('a').click();
	}).addClass('init');
}
function load_disqus()
{
	$('.disqus_thread:first').removeAttr('id');
}
function add_comment(f)
{
	var vars = $(f).serialize();
	$.ajax({
		type:'POST',
		url:$(f).attr('action'),
		dataType:'json',
		data:vars,
		success:function(data){
			if(data.success)
			{
				if(!$(f).siblings('.success').length)
					$(f).before("<div class='success'>Your comment added</div>");
				$(f).parents('.comments').find('.title').after(data.success);
				$(f).parents('.comments').find('.absent').remove();
				$(f).find("textarea").val('');
				var anim = $(f).parents('.anim');
				anim.height('auto');
				anim.height(anim.height()+10);
				anim.siblings('.body').html(anim.html());
				$(f).find("div.error").html('');
				$("img.captcha").attr('src',site_url+'captcha.php?x='+Math.random());
			}
			else if(data.error)
			{
				$(f).find("div.error").html(data.error);
				$(f).prev("div.success").remove();
			}
		}
	});
	return false;
}

function ScrollToElement(theElement)
{
  var selectedPosX = 0;
  var selectedPosY = 0;
              
  while(theElement != null){
    selectedPosX += theElement.offsetLeft;
    selectedPosY += theElement.offsetTop;
    theElement = theElement.offsetParent;
  }
                        		      
 window.scrollTo(selectedPosX,selectedPosY);
}

