var activeId="slide-1";
var inactiveId="slide-2";
var navDirect;
var widthTemp;
var widthThis;
var temp;
var year;

$(document).ready(function(){
		
	$(document).pngFix();
		
	if (navigator.appVersion.indexOf("Mac") != -1) {
		$('.house-line ul.main-line li a').css('font-weight','normal');
	}	
	if ($.browser.version == '525.27.1') {$('.house-line ul.main-line li a').css('font-weight','normal')}
	
	
	if ($('.beauty-pic').hasClass('eng')) {
		$('.beauty-pic').parent().css('background','url(/i/load_eng.gif) 50% 400px no-repeat')
	} else {
		$('.beauty-pic').parent().css('background','url(/i/load.gif) 50% 400px no-repeat')
	}
	
	$('.sub-line div:eq(0)').show();
	
	$("a[rel*=blank]").click(function(){
		window.open(this.href);
		return false;	
	});
	
	$('#index-gallery ul').jcarousel();	
	$('#gallery #index-gallery,.no .jcarousel-clip').css('width',$('.ajax').width()-33+'px');
	
	if ($.browser.msie) {
		if ($.browser.version == "6.0") {		
			$('#index-gallery ul').jcarousel();
		}
	}
	
	$("#index-gallery.yes a,.fancy").fancybox({'zoomSpeedIn':0, 'zoomSpeedOut':0}); 
	
	$('#random div:first-child').show();
	$('#pic img:first-child').show();
	$('.events .one:eq(0)').show();
	
	pics = 0;
	setInterval("randompic()",10000);
	/*eventr = 0;
	setInterval("randomevent()",7000);*/
	num = 0;
	setInterval("random()",3000);
	
	$('.house-line li a').click(function(){
		if ($(this).parent().hasClass('active')){return false}
		numm = $(this).parent().prevAll().size();	
		if($.browser.msie) {	
			$('.scheme-pic .picss div').hide();
			$('.scheme-pic .picss div:eq('+numm+')').show();
			$('.house-text').hide();
			$('.house-info').show();
			$('.green-line').show();
			$('.flats-in').show();
		} else {
			$('.scheme-pic .picss').fadeOut(500,function(){
				$('.scheme-pic .picss div').hide();
				$('.scheme-pic .picss div:eq('+numm+')').show();
				$('.scheme-pic .picss').fadeIn(500);
			});	
			$('.house-text').fadeOut(1000, function(){
				$('.house-info').fadeIn(1000);
				$('.green-line').fadeIn(1000);
				$('.flats-in').fadeIn(1000);
			});
		}
		
		$('.house-line li').removeClass('active');
		$(this).parent().addClass('active');
				
		$('.house-line li:not(.active) a').stop().animate({color:'#AC996A'},1000);
		$(this).stop().animate({color:'#006f51'},1000);
		offsetleft = $(this).offset().left - $('.main .left').width() - $('.main').offset().left;	
		num = $(this).parent().prevAll().size();
		
		if ($.browser.msie && $.browser.version == "6.0") {
			$('.green-dot').css('left',offsetleft);			
			$('.green-str').css('left',offsetleft + 3);
		} else {
			$('.green-dot').stop().animate({left:offsetleft},1000);			
			$('.green-str').stop().animate({left:offsetleft + 3},1000);
		}
			
		$('.sub-line div').hide();
		$('.sub-line div:eq('+num+')').show();	
		return false
	});
	
	
	
	$('#gallery .list a').click(function(){
		if ($(this).parent().hasClass('active')){return false}
		$('#gallery .list li').removeClass('active');
		$(this).parent().addClass('active');
		name = $(this).attr('href');	
		$.ajax({
			url: name,
			cache: false,
			success: function(msg){
		     $('.ajax').html('');
		     $('.ajax').append(msg);
			 $('#index-gallery ul').jcarousel();
			 $('#gallery #index-gallery,.no .jcarousel-clip').css('width',$('.ajax').width()-33+'px');
			 if ($.browser.msie) {
				if ($.browser.version == "6.0") {		
					$('#index-gallery ul').jcarousel();
				}
			 }
			 gallery();
			 $(document).pngFix();
			 $("a[rel*=group]").each(function(){
			 	hreff=$(this).attr('href')
				$('#preloader').append('<img src="'+hreff+'" alt="" width="1" height="1" />')
			 });
		   }
		});
		return false
	});
	
	
	
	$('#gallery .list li:first-child a').click();
	
	$(window).resize(function(){
		$('#gallery #index-gallery,.no .jcarousel-clip').css('width',$('.ajax').width()-33+'px');
		indexpic();
		try {
			offsetleft = $('.main-line li.active').offset().left - $('.main .left').width() - $('.main').offset().left;	
			$('.green-dot').css('left','1%').css('left', offsetleft);
			$('.green-str').css('left', offsetleft + 3);
		} 
		catch (e) {;}
		
	});
	 
	 
	indexpic();
	
	
	
	$('.left-part.in li a').click(function(){
		$('.left-part li').removeClass('active');
		$(this).parent().addClass('active');
		$('.left-part li:not(.active) a').stop().animate({color:'#AC996A'},1000);
		$(this).stop().animate({color:'#006f51'},1000);		
		$('.right-part div').hide();
		num = $(this).parent().prevAll().size();
		$('.right-part div:eq('+num+')').show();
		$('.beauty-pic').css('background','url(/i/pic-infra-'+num+'.jpg) 50% 0% no-repeat');
		if (num=='9'){
			$('.beauty-pic.infra').css('background-position','0% 0%');
		}
		return false
	});
	
	
	
	$('.infra-block h3 a').click(function(){
		$('.infra-block h3 a').removeClass('active');
		$(this).addClass('active');
		year = $('.infra-block h3 a.active').html();
		$('.left-part.nw ul li').removeClass('active');
		$('.left-part.nw ul li a').css('color','#AC996A');
		$('.left-part.nw ul').removeClass('cur');
		$('.left-part.nw ul.y-'+year).addClass('cur');
		var widthTemp = 0;
		leftStr(widthTemp, 'none');
		return false
	});
	
	
	$('.news-list h3 a').click(function(){
		$('.news-list h3 a').removeClass('active');
		$(this).addClass('active');
		year = $('.news-list h3 a.active').html();
		$('.news-list ul').removeClass('cur');
		$('.news-list ul.y-'+year).addClass('cur');
		return false
	});
	
	
	$('.left-part.nw li a').click(function(){
		$('.left-part li').removeClass('active');
		$(this).parent().addClass('active');
		$('.left-part li:not(.active) a').stop().animate({color:'#AC996A'},1000);
		$('.left-part li.before:not(.active) a').stop().animate({color:'#8F8C8E'},1000);
		$(this).stop().animate({color:'#006f51'},1000);		
		$('.right-part div.year div').hide();
		year = $('.infra-block h3 a.active').html();
		num = $(this).parent().prevAll().size();
		$('.right-part div.year.t-'+year+' div:eq('+num+')').show();
		//$('.beauty-pic').css('background','url(/i/pic-news-'+num+'.jpg) 50% 0% no-repeat');
		img = $(this).attr('name');
		$('.beauty-pic').attr('class','beauty-pic')
		$('.beauty-pic').addClass('beauty-pic').addClass(img);
		var widthTemp = 0;
		leftStr(widthTemp, 'active');
		return false
	});
	
	
	
	if (window.location.hash){
		hashh = window.location.hash.substr(1);
		$('.left-part.nw .cur li:eq('+hashh+') a').click()
	} else {
		$('.left-part.nw li.active a').click()
	}
	
	
	$('.year div .more').click(function(){
		$('.popup').show();
		return false
	});
	$('.popup .close').click(function(){
		$('.popup').hide();
	});
	
	
	$('.left-part li').hover(function(){
		var widthTemp = 0;		
		$('.left-part li').removeClass('clicked');
		$(this).addClass('clicked');
		leftStr(widthTemp, 'clicked');		
	}, function(){
		var widthTemp = 0;
		if ($('.left-part li').hasClass('active'))
		{
			leftStr(widthTemp, 'active');
		} else {
			leftStr(widthTemp, 'none');
		}
		
	});
	gallery();	
	
	
	$('.adress-main #print a').click(function(){
		window.print();
		return false
	});
	
		
});

function gallery(){
	$("#index-gallery.no a").click(function(){
		var navbefore = $(this).parent().parent().prevAll().size();
		var navact = $('#index-gallery.no div.active').parent().prevAll().size();
		if (navbefore < navact) {navDirect = -1;}
		if (navbefore > navact) {navDirect = 1;}		
		if ($('.slide').is(':animated')) {return false}
		if ($(this).parent().hasClass('active')){return false}
		$('#index-gallery.no div').removeClass('active');
		$(this).parent().addClass('active');
		link = $(this).attr('href');
		text = $(this).attr('title');
		$("."+inactiveId).css("left",(1300*navDirect)+"px");
		$("."+activeId).animate({left:(1300*navDirect*(-1))+"px"}, 500);
		$("."+inactiveId).animate({left:"0"}, 500);			
		if (activeId == "slide-1") {
			activeId = "slide-2";
			inactiveId = "slide-1";
		}
		else {
			activeId = "slide-1";
			inactiveId = "slide-2";					
		}
		if(activeId!="slide-1"){$('.slide-2 img').attr('src',link);}
		else{$('.slide-1 img').attr('src',link);}
		$('.photo-txt').html(text);
		$(document).pngFix();
		return false
	});
	
	$('.nav-left').click(function(){
		allsize = $('#index-gallery.no li').size();
		navactt = $('#index-gallery.no div.active').parent().prevAll().size();
		navactt = navactt -1;
		$('#index-gallery.no li:eq('+ navactt +') div a').click();	
		if(navactt==7){$('.jcarousel-prev').click()}
		if(navactt==14){$('.jcarousel-prev').click()}
		if(navactt==21){$('.jcarousel-prev').click()}
		if(navactt==25){$('.jcarousel-prev').click()}
		if(navactt==31){$('.jcarousel-prev').click()}
	});
	$('.nav-right').click(function(){
		allsize = $('#index-gallery.no li').size();
		navactt = $('#index-gallery.no div.active').parent().prevAll().size();
		navactt = navactt +1;
		$('#index-gallery.no li:eq('+ navactt +') div a').click();	
		if(navactt==8){$('.jcarousel-next').click()}
		if(navactt==15){$('.jcarousel-next').click()}
		if(navactt==22){$('.jcarousel-next').click()}
		if(navactt==26){$('.jcarousel-next').click()}
		if(navactt==32){$('.jcarousel-next').click()}
	});
}

/*function max(){
	maxheight = 0;
	$('.bl').each(function(){
		 if ($(this).height() > maxheight) {maxheight = $(this).height();}
	});
	$('#events,#anons,#events-anons,#dots').css('height',maxheight + 40 + "px");
}*/
	
	

function leftStr(heightTemp, classes) {
	
	$('.left-part li').each(function(){
		if ($(this).hasClass(classes)) {	
			return false;
		}
		heightTemp += $(this).height();
	});
	
	if (classes == 'none') {
		$('.left-part .str').stop().animate({top:-34},200);
	} else {
		$('.left-part .str').stop().animate({top:heightTemp},200);
	}
	
}


function randompic() {
 	pics = pics+1; 
	if (pics != 3) {
		$('#pic img').fadeOut(300);
		setTimeout("$('#pic img:eq('+pics+')').fadeIn()",300); 
	} else {pics=-1}	
} 

function random() {
 	num = num+1; 
	if (num != 10) {
		$('#random div').fadeOut(300);
		setTimeout("$('#random div:eq('+num+')').fadeIn()",300); 
	} else {num=-1}	
} 

function randomevent() {
 	eventr = eventr+1; 
	if (eventr != 2) {
		$('.events .one').fadeOut(300);
		setTimeout("$('.events .one:eq('+eventr+')').fadeIn()",300); 
	} else {eventr=-1}	
} 

function indexpic(){
	if ($(window).width() < 1150) {
		$('#pic img').css('right','-123px')	
	} else {
		$('#pic img').css('right','0px');			
	}
}
