var sizer;
var houses;

$(document).ready(function(){
	$(document).pngFix();
	
	$('.sub-gallery li a,a.fancy').fancybox();
	
	$("a[rel*=blank]").click(function(){
		window.open(this.href);
		return false;	
	});
	
	$(document).keyup(function(event){
	    if (event.keyCode == 27) {
	    	$('.close').click();
	    }
	});
	
	$('#wr-index .map').click(function(){
		window.location.href="map/"
	});
	
	$('#order-form').validate({
	   submitHandler: function(form) {
	   	$(form).ajaxSubmit(function(){
			beforeSubmit: {
				
			}
			success: {
				$('.o-form').hide();
		    	lay = $('.o-form-ok');
				layers();	
			}
			return false
		})
	   }
	});
	
	$('.o-form-ok .o-form-ok-text a').click(function(){
		$('.o-form-ok .close').click();
		return false
	});
	
	$('.houses').append('<div id="preloader"><img src="/i/h/house-1-2.gif" alt="" /><img src="/i/h/house-2-2.gif" alt="" /><img src="/i/h/house-3-2.gif" alt="" /><img src="/i/h/house-4-2.gif" alt="" /><img src="/i/h/house-5-2.gif" alt="" /><img src="/i/h/house-6-2.gif" alt="" /><img src="/i/h/house-7-2.gif" alt="" /></div>')
	
	$('.bl-1').css('visibility','visible');
	
	$('a.order').click(function(){
		lay = $('.o-form');
		layers();
		$('#oname').focus();
		return false
	});
	/*$('a.gosles').click(function(){
		lay = $('.m-form');
		layers();
		if ($('body').height() < 845){
			$('.m-form').css('top','0px').css('margin-top','0px');
			$('.m-form .bg,.m-form .form').css('top',yScroll+30+'px').css('margin-top','0px');
		}
		return false
	});*/
	$('a.sert, .sert a').click(function(){
		lay = $('.s-form');
		layers();
		$('.s-form select').change(function () {
     		str_date = $('.s-form .date option:selected').html();
     		str_seria = $('.s-form .seria option:selected').attr('value');
			$('.s-form .send a').attr('href','/docs/certificates/'+str_date+'/seriya_obligaciy_'+str_seria+'.pdf');
   		});	
		$(document).keyup(function(){
			$(".s-form select").change();
		});
		return false
	});
	$('.download a,.download-footer').click(function(){
		lay = $('.h-form');
		layers();
		$('.h-form select').change(function () {
     		str_title = $('.h-form option:selected').attr('value');
     		str_num = $('.h-form option:selected').text();
     		$('.h-form .flats span').html(str_title);
			$('.h-form .send a').attr('href','/docs/houses/house_'+str_num+'.pdf');
   		});	
		$(document).keyup(function(){
			$(".h-form select").change();
		});
		return false
	});	
	$('.layout,.close').click(function(){
		$('.o-form,.o-form-ok,.h-form,.s-form,.m-form,.layout,.h-gallery').hide();
		$('.gallery-wrap').css('z-index','1');	
		$('.gallery .main li').removeClass('active');
	});
	
	
	$(window).resize(function(){
		layheight = $(document).height();
		laywidth = $('#menu').width();
		$('.layout').css('height',layheight).css('width','1%').css('width',laywidth);
		$('.sub-gallery, .sub-gallery .jcarousel-clip').css('width',$('#footer').width()-96+'px');
	});
	
	/* i-form valid */
	function oform() {
		name = $('#oname').val();
		otel = $('#otel').val();
		oemail = $('#oemail').val();
		if ((name.length > 0) && (otel.length > 0) && (oemail.length > 0)) {
			$('.o-form .send input').removeAttr('disabled').css('opacity','1');
		} else {
			$('.o-form .send input').attr('disabled','disabled').css('opacity','0.3');
		}
	}		
	oform();
	$('.o-form input.text').keydown(function(){oform();}).keyup(function(){oform();}).blur(function(){oform();}).focus(function(){oform();});
	
	$('.gallery').css('overflow','visible');
	$('.sub-gallery').css('overflow','visible');
	
	$('.field').css('opacity','0').show();
	$('#header #square div span').css('opacity','0.2');
		
	$('#submenu li a').click(function(){
		sizer = $(this).parent().prevAll().size();
		if ($(this).parent().hasClass('active')){return false}
		$('#submenu li').removeClass('active');
		$(this).parent().addClass('active');
		hash = $(this).attr('href');
		window.location.hash = hash;
		if ($('#content').hasClass('act')){
			if($.browser.msie) {
				$('.bl-2').hide();	
				$('.l .block').hide();
				pics();
			} else {
				$('.bl-2').fadeOut(500,function(){
					$('.l .block').hide();
					pics();
				});	
			}			
		} else {
			$('.field').animate({opacity:1},500);		
			if($.browser.msie) {
				$('.bl-1').hide();	
				$('#content').addClass('act');
				pics();
			} else {
				$('.bl-1').fadeOut(500,function(){
					$('#content').addClass('act');
					pics();
				});	
			}	
		}
		return false
	});
	
	$('.block li div').click(function(){
		$(this).children().click();
	});
	$('.scheme-pic .zoom').click(function(){
		$(this).prev().click();
		$('#fancy_wrap').attr('class','show_print');
	});
	
	$('.scheme-pic a').click(function(){
		$('#fancy_wrap').attr('class','show_print');
	});
	
	$('.block li a').click(function(){
		if($(this).parent().parent().hasClass('active')){return false}
		$('.block li').removeClass('active');
		$(this).parent().parent().addClass('active');		
		var link3 = $('.block li.active a').attr('href');
		
		if($.browser.msie) {
			$('.r .img img').attr('src',link3);
		} else {
			$('.r .img img').fadeOut(500,function(){
				$('.r .img img').attr('src',link3);
				$('.r .img img').fadeIn(500);
			});		
		}		
		return false
	});
	
	
	$('#submenu li:eq(0) a').click(function(){
		$('#square').stop().animate({
			left:2+'px',
			width:175+'px'
		},500);
		$('#header em').stop().animate({left:84+'px'},500);
		return false
	});
	$('#submenu li:eq(1) a').click(function(){
		$('#square').stop().animate({
			left:190+'px',
			width:65+'px'
		},500);
		$('#header em').stop().animate({left:220+'px'},500);
		return false
	});
	$('#submenu li:eq(2) a').click(function(){
		$('#square').stop().animate({
			left:254+'px',
			width:145+'px'
		},500);
		$('#header em').stop().animate({left:360+'px'},500);
		return false
	});
	$('#submenu li:eq(3) a').click(function(){
		$('#square').stop().animate({
			left:398+'px',
			width:160+'px'
		},500);
		$('#header em').stop().animate({left:500+'px'},500);
		return false
	});
	$('#submenu li:eq(4) a').click(function(){
		$('#square').stop().animate({
			left:563+'px',
			width:150+'px'
		},500);
		$('#header em').stop().animate({left:620+'px'},500);
		return false
	});
	$('#submenu li:eq(5) a').click(function(){
		$('#square').stop().animate({
			left:716+'px',
			width:71+'px'
		},500);
		$('#header em').stop().animate({left:770+'px'},500);
		return false
	});
	$('#submenu li:eq(6) a').click(function(){
		$('#square').stop().animate({
			left:790+'px',
			width:153+'px'
		},500);
		$('#header em').stop().animate({left:915+'px'},500);
		return false
	});
	
	
	if(window.location.hash == '#ambassador'){
		number=0
		residense();
		overall();
	}
	if(window.location.hash == '#business'){
		number=1
		residense();
		$('#square').css('left','190px');
		$('#square').css('width','65px');
		$('#header em').css('left','220px');	
		overall();
	}
	if(window.location.hash == '#familydeluxe'){
		number=2
		residense();
		$('#square').css('left','254px');
		$('#square').css('width','145px');
		$('#header em').css('left','360px');	
		overall();
	}
	if(window.location.hash == '#country'){
		number=3
		residense();
		$('#square').css('left','398px');
		$('#square').css('width','160px');
		$('#header em').css('left','500px');	
		overall();
	}
	if(window.location.hash == '#president'){
		number=4
		residense();
		$('#square').css('left','563px');
		$('#square').css('width','150px');
		$('#header em').css('left','620px');	
		overall();
	}
	if(window.location.hash == '#businessdeluxe'){
		number=5
		residense();
		$('#square').css('left','716px');
		$('#square').css('width','71px');
		$('#header em').css('left','770px');	
		overall();
	}
	if(window.location.hash == '#family'){
		number=6
		residense();
		$('#square').css('left','790px');
		$('#square').css('width','153px');
		$('#header em').css('left','915px');	
		overall();
	}
	
	$('.select a').click(function(){
		selector = $('.select option:selected').attr('value');
		if($('body').hasClass('eng')){
			window.open('/eng/houses/#'+selector);
		} else {
			window.open('/houses/#'+selector);
		}		
		return false
	});
	
	$('.pdf.link a').click(function(){
		if ($(this).parent().hasClass('active')) {
			$(this).parent().removeClass('active');
			$('.pdfs').slideUp();
		}
		else {
			$(this).parent().addClass('active');
			$('.pdfs').slideDown();
		}
		return false
	});
	
	$('.l .print a,.m-print a,#fancy_print a').click(function(){	
		window.print();
		return false
	});
	
	$('.sub-gallery .main').jcarousel();
	$('.sub-gallery, .sub-gallery .jcarousel-clip').css('width',$('#footer').width()-96+'px');

	$('.gallery-menu.one li a').click(function(){
		if ($(this).parent().parent().hasClass('active')){return false}
		$('.gallery-menu.one li').removeClass('active');
		$(this).parent().parent().addClass('active');
		link5 = $(this).attr('href');
		$.ajax({
			url: link5,
			cache: false,
			success: function(msg){
		    $('.sub-gallery.one').html('');
		    $('.sub-gallery.one').append(msg);
			$('.sub-gallery.one .main').jcarousel();
			$('.sub-gallery li a').fancybox();
			$(document).pngFix();
		   }
		});
		return false
	});
	
	$('.gallery-menu.two li a').click(function(){
		if ($(this).parent().parent().hasClass('active')){return false}
		$('.gallery-menu.two li').removeClass('active');
		$(this).parent().parent().addClass('active');
		link5 = $(this).attr('href');
		$.ajax({
			url: link5,
			cache: false,
			success: function(msg){
		    $('.sub-gallery.two').html('');
		    $('.sub-gallery.two').append(msg);
			$('.sub-gallery.two .main').jcarousel();
		$('.sub-gallery li a').fancybox();
		$(document).pngFix();
		   }
		});
		return false
	});
	
	$('.scheme-blocks p:eq(0)').show();
	$('.scheme-menu li a').click(function(){
		if($(this).parent().parent().hasClass('active')){return false}
		$('.scheme-menu li').removeClass('active');
		$(this).parent().parent().addClass('active');
		number = $(this).parent().parent().prevAll().size();
		$('.scheme-blocks p').fadeOut(500);
		$('.scheme-blocks p:eq('+number+')').fadeIn(500);
		return false
	});
	$('.scheme-pic').hover(function(){
		$('.zoom').show();
	},function(){
		$('.zoom').hide();
	});
	
});


function sub_gallery_one(x){
	$.ajax({
		url: '/gallery/'+x+'.php',
		cache: false,
		success: function(msg){
	    $('.sub-gallery.one').html('');
	    $('.sub-gallery.one').append(msg);
		$('.sub-gallery.one .main').jcarousel();
		$('.sub-gallery li a').fancybox();
		$(document).pngFix();
	   }
	});
}
function sub_gallery_two(x){
	$.ajax({
		url: '/gallery/'+x+'.php',
		cache: false,
		success: function(msg){
	    $('.sub-gallery.two').html('');
	    $('.sub-gallery.two').append(msg);
		$('.sub-gallery.two .main').jcarousel();
		$('.sub-gallery li a').fancybox();
		$(document).pngFix();
	   }
	});
}

function gallery(x){
	$.ajax({
		url: '/gallery/'+x+'.php',
		cache: false,
		success: function(msg){
	    $('.gallery-wrap').html('');
	    $('.gallery-wrap').append(msg);
		$(document).pngFix();
		$('.gallery ul.main').jcarousel();
		gal();
	   }
	});
}
function eng_gallery(x){
	$.ajax({
		url: '/eng/gallery/'+x+'.php',
		cache: false,
		success: function(msg){
	    $('.gallery-wrap').html('');
	    $('.gallery-wrap').append(msg);
		$(document).pngFix();
		$('.gallery ul.main').jcarousel();
		gal();
	   }
	});
}

function gal(){
	$('.gallery .main li a').click(function(){	
		if ($(this).parent().hasClass('active')) {return false}	
		$('.h-gallery').show();
		$('.gallery-wrap').css('z-index','999');
		winheight = $(window).height();
		layheight = $(document).height();
		laywidth = $('#menu').width();
		$('.layout').css('height',layheight).css('width',laywidth).css('opacity','0.7').show();
		linkhref = $(this).attr('href');
		if($.browser.msie || $.browser.opera) {
			$('.big-pic img').attr('src',linkhref);
		} else {
			$('.big-pic img').fadeOut(300,function(){
				$('.big-pic img').attr('src',linkhref).load(function(){
					$('.big-pic img').fadeIn(300);
				});
			});			
		}
		
		
		$('.gallery .main li').removeClass('active');
		$(this).parent().addClass('active');			
		return false
	});	
	$('.layout,.close').click(function(){
		$('.o-form,.o-form-ok,.h-form,.s-form,.s-form,.layout,.h-gallery').hide();
		$('.gallery-wrap').css('z-index','1');	
		$('.gallery .main li').removeClass('active');
		$('.big-pic img').attr('src','/i/s.gif');
	});
	
	$('.navi-left').click(function(){
		navactt = $('.gallery .main li.active').prevAll().size();
		navactt = navactt -1;
		$('.gallery .main li:eq('+ navactt +') a').click();
		if(navactt==3){$('.jcarousel-prev').click()}
		if(navactt==7){$('.jcarousel-prev').click()}
		if(navactt==11){$('.jcarousel-prev').click()}
		if(navactt==15){$('.jcarousel-prev').click()}
		if(navactt==19){$('.jcarousel-prev').click()}
		if(navactt==23){$('.jcarousel-prev').click()}
	});
	$('.navi-right').click(function(){
		navactt = $('.gallery .main li.active').prevAll().size();
		navactt = navactt +1;
		$('.gallery .main li:eq('+ navactt +') a').click();
		if(navactt==4){$('.jcarousel-next').click()}
		if(navactt==8){$('.jcarousel-next').click()}
		if(navactt==12){$('.jcarousel-next').click()}
		if(navactt==16){$('.jcarousel-next').click()}
		if(navactt==20){$('.jcarousel-next').click()}
		if(navactt==24){$('.jcarousel-next').click()}
	});
	
	$('.h-gallery .menus li a').click(function(){
		$('.h-gallery .menus li').removeClass('active');
		$(this).parent().addClass('active');
		newheader = $('.menus li.active a').text();
		$('.gallery-content .header').html(newheader);
		link2 = $(this).attr('href');
		$.ajax({
			url: link2,
			cache: false,
			success: function(msg){
		    $('.gallery-wrap').html('');
		    $('.gallery-wrap').append(msg);
			$('.gallery ul.main').jcarousel();
			gal();
			$(document).pngFix();
			$('.gallery-wrap .main li:eq(0) a').click();
		   }
		});
		return false
	});
	
}

function overall(){
	$('#content').addClass('act');
	$('.field').css('opacity','1');
	$('.bl-1').css('display','none');
	$('.bl-2').show();
	link1 = $('.block li.active a').attr('href');
	$('.r .img img').attr('src',link1);
	forprint();
};

function pics(){	
	$('.l .block:eq('+sizer+')').show();
	$('.l .block li').removeClass('active');
	$('.l .block:eq('+sizer+') li:eq(1)').addClass('active');
	link2 = $('.block li.active a').attr('href');		
	if($.browser.msie) {
		$('.r .img img').attr('src',link2);
		$('.bl-2').show();
	} else {
		$('.r .img img').attr('src',link2);
		$('.bl-2').fadeIn(500);
	}
	
	forprint();
};

function forprint(){	
	$('.img-print').empty();
	$('.block:visible li').each(function(){
		link4 = $(this).children().children('a').attr('href');
		$('.img-print').append('<div><img src='+link4+' alt="" /></div><br/>');
	});
};

function getscroll(){
	if (self.pageYOffset){
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body){
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
};

function indexpage(){
	$(window).load(function(){
		rand = Math.round(2*Math.random())+1;
	    dwidthbg = $(window).width(); 
		dheighbg = $(window).height();
		if (dwidthbg < 1920){$('#wr-index').css('background','url(/i/bg/bg'+rand+'-1900.jpg) 50% 50% no-repeat')}
		if (dwidthbg < 1620){$('#wr-index').css('background','url(/i/bg/bg'+rand+'-1600.jpg) 50% 50% no-repeat')}
		if (dwidthbg < 1300){$('#wr-index').css('background','url(/i/bg/bg'+rand+'-1280.jpg) 50% 50% no-repeat')}
		if (dwidthbg < 1050){if (dheighbg > 890) {return false}else {$('#wr-index').css('background', 'url(/i/bg/bg' + rand + '-1024.jpg) 50% 100% no-repeat');}}
 	});
	$(window).resize(function(){
		dwidthbg = $(window).width(); 
		dheighbg = $(window).height();
		if (dwidthbg < 1920){$('#wr-index').css('background','url(/i/bg/bg'+rand+'-1900.jpg) 50% 50% no-repeat')}
		if (dwidthbg < 1620){$('#wr-index').css('background','url(/i/bg/bg'+rand+'-1600.jpg) 50% 50% no-repeat')}
		if (dwidthbg < 1300){$('#wr-index').css('background','url(/i/bg/bg'+rand+'-1280.jpg) 50% 50% no-repeat')}
		if (dwidthbg < 1050){if (dheighbg > 890) {return false}else {$('#wr-index').css('background', 'url(/i/bg/bg' + rand + '-1024.jpg) 50% 100% no-repeat');}}
	});
};

function layers(){	
	getscroll();
	winheight = $(window).height();
	layheight = $(document).height();
	laywidth = $('#menu').width();
	$('.layout').css('height',layheight).css('width',laywidth).css('opacity','0.7').show();
	lay.css('top',yScroll+winheight-winheight/2).show();
}
function residense(){	
	$('#submenu li:eq('+number+')').addClass('active');
	$('.l .block:eq('+number+')').show();
	$('.l .block:eq('+number+') li:eq(1)').addClass('active');
	$('.r .img img').attr('src',$('.l .block:eq('+number+') li:eq(1) a').attr('href'));
}
