$(document).ready(function() {
	$(function() {
		$(window).resize();
	});
});

$(window).resize(function(){
	onResize = function(){

		var cleared = false;

		var maxHeight = new Array();
		$('.index-block').each( function(){
			if($(this).attr('id') != 'funnyBlock')
			{
				if(maxHeight[$(this).offset().top] == undefined)
				{
					maxHeight[$(this).offset().top] = 0;
				}
				maxHeight[$(this).offset().top] = Math.max(maxHeight[$(this).offset().top], $(this).height());
			}
		});

		var minTop = 100000;
		var maxLeft = 0;
		$.fn.reverse = [].reverse; 
		$('.index-block').reverse().each( function(){
			if($(this).attr('id') != 'funnyBlock')
			{
				minTop = Math.min(minTop, $(this).offset().top);
				maxLeft = Math.max(maxLeft, $(this).position().left);
				$(this).height(maxHeight[$(this).offset().top]);
			}
		});
		if(maxLeft > 600)
		{
			var delta = $('#blockContainer').width() - maxLeft + 30;
			if($('#funnyBlock').offset().top == minTop + 50)
			{
				delta = delta - $('#funnyBlock').width() - 50;
			}
			$('#funnyBlock').css({left:-delta});
		}
	}
	window.setTimeout(onResize, 100);
	return;
});

function doTogle()
{
	var this_ = $('#feed-back-area');//$(this).parents().get(1);
	var h = $(this_).find('.header-slide').height();
	if(!$(this_).hasClass('header-slide-opened')){
		$(this_).animate({height: h}, 'fast', function(){$(this_).addClass('header-slide-opened');});
	}else{
		$(this_).animate({height: 30}, 'fast', function(){$(this_).removeClass('header-slide-opened');});
	}
}

$(function(){
	$('#feed-back-area .open-close, .open-close-label').click(function(){
		doTogle();
	});
});

$(function(){
	$('#footer-contact .open-close, .footer-slide-links').click(function(){
		var h = $('#footer-contact').find('.footer-slide').height();
		if(!$('#footer-contact').hasClass('footer-slide-opened')){
			//$('#clearfooter').animate({height: h}, 'fast');
			$('#footer').animate({height: h}, 'fast');
			$('#footer-contact').animate({height: h}, 'fast', function(){$('#footer-contact').addClass('footer-slide-opened'); });
		}else{
			//$('#clearfooter').animate({height: 30}, 'fast');
			$('#footer').animate({height: 30}, 'fast');
			$('#footer-contact').animate({height: 40}, 'fast', function(){$('#footer-contact').removeClass('footer-slide-opened');});
		}
	});
});

$(function(){
	var drop = $('.drop-menu');
	for(var i = 0; i < drop.length; i++){
		var curr = $(drop[i]).find('.current');
		if(curr.length) $(drop[i]).addClass('curr-oppened');
	}
	$('.drop-menu .drop-menu-title').click(function(){
		if(!$($(this).parent().get(0)).hasClass('curr-oppened')) $($(this).parent().get(0)).addClass('curr-oppened');
		else $($(this).parent().get(0)).removeClass('curr-oppened');
	});
});

$(function(){
	$('#details .close').click(function(){
		$('#details').hide();
	});
	$('.catalog-item-pic').click(function(){
		$(this).parent().find('.catalog-item-link').click();
	});
	$('.catalog-item-link').click(function(){
		$("#details-inner-html").html($(this).parent().find('.description-source').html());
		$('#details').css({visibility: 'hidden'});
		var w = $(this).width();
		var h = $(this).height();
		var offset = $(this).offset();
		var dh = $(document).height();
		var dw = $(document).width();
		var wh = $(window).height();
		var ww = $(window).width();
		var deth = $('#details').height();
		var detw = $('#details').width();
		var left = offset.left;
		var top = offset.top;
		left = (left + detw + 30) > ww ? (left + w - 50 - detw):left;
		//top = (top + deth + 30) > dh ? (top + h - deth):top;
		top = offset.top - 300;
		$('#details').css({visibility: 'visible', left: left, top: top}).show();
	});
});

function getPeriodValue(id, arr){
	var str = '';
	for(var i = 0; i < arr.length; i++){
		var period = arr[i];
		if(period.s && period.w){
//			str += '<div class="period" style="width: ' + parseInt(300*period.w/365) + 'px; left: ' + parseInt(300*period.s/365) + 'px;"></div>';
			str += '<div class="period" style="width: ' + parseInt(300*period.w/360) + 'px; left: ' + parseInt(300*(period.s-30)/360) + 'px;"></div>';
		}
	}
	$('#' + id).html(str);
}
