$(document).ready(function(){

	$('.sliding_left').hide();

	$('.sliding_center').addClass("sliding_center_out");
	$('.sliding_center').each(function()
	{
		if (this.innerHTML == 1 ) $(this).removeClass("sliding_center_out").addClass("sliding_center");
	}
	);

    s_w = parseInt($('.c_reducer').width());
    $('.slider ul li').css('width', $('.slider .wrapper').width());
    //$('.slider').css('width',s_w);
    //$('.slider .wrapper').css('width',s_w-80);
    //$('.slider .wrapper ul').css('width',s_w-80);

	$('.slider').anythingSlider({
        easing: "easeInOutExpo",
        autoPlay: false,
        startStopped: false,
        delay: 3000,
        animationTime: 600,
        hashTags: true,
        buildNavigation: false,
        pauseOnHover: true,
        startText: "Start",
        stopText: "Stop"
	});

	$(".sliding_center").click(function(){
		s_id = parseInt($(this).attr('rel'));
		$('.slider').anythingSlider(s_id);
		$(".sliding_center").removeClass("sliding_center").addClass("sliding_center_out");
		$(this).removeClass("sliding_center_out").addClass("sliding_center");
		
		if (s_id<=1)
		{
			$('.sliding_left').hide();
			$('.sliding_right').show();
		}
		else if (s_id>=($(".slider .wrapper ul.slideli li.slideli").length-2))
		{
			$('.sliding_right').hide();
			$('.sliding_left').show();
		}
		else
		{
			$('.sliding_right').show();
			$('.sliding_left').show();
		}
		return false;
	});

	$('.sliding_right').click(function() {
		s_id = parseInt($(".sliding_center").attr("rel"));
		$('.slider').anythingSlider(s_id+1);
		$(".sliding_center").removeClass("sliding_center").addClass("sliding_center_out");
		$(".sliding_center_out").eq(s_id).removeClass("sliding_center_out").addClass("sliding_center");
		if (s_id+1<=1)
		{
			$('.sliding_left').hide();
			$('.sliding_right').show();
		}
		else if (s_id+1>=($(".slider .wrapper ul.slideli li.slideli").length-2))
		{
			$('.sliding_right').hide();
			$('.sliding_left').show();
		}
		else
		{
			$('.sliding_right').show();
			$('.sliding_left').show();
		}


		return false;
	});

	$('.sliding_left').click(function() {
		s_id = parseInt($(".sliding_center").attr("rel")-2);
		$('.slider').anythingSlider(s_id+1);
		$(".sliding_center").removeClass("sliding_center").addClass("sliding_center_out");
		$(".sliding_center_out").eq(s_id).removeClass("sliding_center_out").addClass("sliding_center");
		if (s_id+1<=1)
		{
			$('.sliding_left').hide();
			$('.sliding_right').show();
		}
		else if (s_id+1>=($(".slider .wrapper ul.slideli li.slideli").length-2))
		{
			$('.sliding_right').hide();
			$('.sliding_left').show();
		}
		else
		{
			$('.sliding_right').show();
			$('.sliding_left').show();
		}
		return false;
	});

	$("#content img").each(function() {
		if (!$(this).parent().hasClass('photoitem')) {
			if ($(this).attr('align')=='left') {
				$(this).css('margin', '5px 20px 20px 0px');
			} else if ($(this).attr('align')=='right') {
				$(this).css('margin', '5px 0px 20px 20px');
			}
		}
	});

	$("#content a > img").each(function() {
		var link = $(this).parent();
		if (!link.hasClass('photoitem')) {
			var linkhref = link.get(0).href;
			var ext = linkhref.substr(linkhref.length-4);
			if (ext=='.jpg' || ext=='.gif' || ext=='.png') {
				link.addClass('photoitem');
				link.attr('rel', 'fancy-tour');
			}
		}
	});

	$("#content a.photoitem").fancybox({

	});

	if ($('#layout').height()<$(document).height()) $('#layout').height($(document).height());

    var moved = -1;

	$(".houses1").click(function() {

		if ($('#house1info').hasClass("full"))
		{
			document.location = $('#house_1').attr('href');
			return false;
		}
        else
        {
		$('td.full').removeClass('full');
		$('#house1info').addClass('full');
		$('#house1info ins').addClass('full');
		$('#house2info ins').removeClass('full');
		$('#house3info ins').removeClass('full');
		if ($('#back').is(':hidden')) {$('#back').show();}
		if (moved==-1)
		{
		$("#house_1").animate({
			"left": "-=200%"
		}, 3000);
		$("#house_2").animate({
			"left": "-=200%"
		}, 3000);
		$("#house_3").animate({
			"left": "-=200%"
		}, 3000);
		moved = 1;
		}
		$("#house_4").animate({
			"left": "50%"
		}, 3000);
		$("#house_5").animate({
			"left": "200%"
		}, 3000);
		$("#house_6").animate({
			"left": "200%"
		}, 3000);
		return false;
		}
	});

    $('#house_4').attr('href',$('#house_1').attr('href'));
    $('#house_5').attr('href',$('#house_2').attr('href'));
    $('#house_6').attr('href',$('#house_3').attr('href'));

	$(".houses2").click(function() {

		if ($('#house2info').hasClass("full"))
		{
			document.location = $('#house_2').attr('href');
			return false;
		}
        else
        {
		$('td.full').removeClass('full');
		$('#house2info').addClass('full');
		$('#house2info ins').addClass('full');
		$('#house1info ins').removeClass('full');
		$('#house3info ins').removeClass('full');
		if ($('#back').is(':hidden')) {$('#back').show();}
		if (moved==-1)
		{
		$("#house_1").animate({
			"left": "-=200%"
		}, 3000);
		$("#house_2").animate({
			"left": "-=200%"
		}, 3000);
		$("#house_3").animate({
			"left": "-=200%"
		}, 3000);
		moved = 1;
		}
		$("#house_5").animate({
			"left": "50%"
		}, 3000);
		$("#house_4").animate({
			"left": "200%"
		}, 3000);
		$("#house_6").animate({
			"left": "200%"
		}, 3000);
		return false;
		}
	});

	$(".houses3").click(function() {

		if ($('#house3info').hasClass("full"))
		{
			document.location = $('#house_3').attr('href');
			return false;
		}
        else
        {
		$('td.full').removeClass('full');
		$('#house3info').addClass('full');
		$('#house3info ins').addClass('full');
		$('#house1info ins').removeClass('full');
		$('#house2info ins').removeClass('full');
		if ($('#back').is(':hidden')) {$('#back').show();}
		if (moved==-1)
		{
		$("#house_1").animate({
			"left": "-=200%"
		}, 3000);
		$("#house_2").animate({
			"left": "-=200%"
		}, 3000);
		$("#house_3").animate({
			"left": "-=200%"
		}, 3000);
		moved = 1;
		}
		$("#house_6").animate({
			"left": "50%"
		}, 3000);
		$("#house_4").animate({
			"left": "200%"
		}, 3000);
		$("#house_5").animate({
			"left": "200%"
		}, 3000);
		return false;
		}
	});

	$("#back").click(function() {
		$('td.full').removeClass('full');
		$('#house1info ins').removeClass('full');
		$('#house2info ins').removeClass('full');
		$('#house3info ins').removeClass('full');
		$('#back').hide();
        moved = -1;

		$("#house_1").animate({
			"left": "25%"
		}, 3000);
		$("#house_2").animate({
			"left": "50%"
		}, 3000);
		$("#house_3").animate({
			"left": "75%"
		}, 3000);

		$("#house_4").animate({
			"left": "200%"
		}, 3000);
		$("#house_5").animate({
			"left": "200%"
		}, 3000);
		$("#house_6").animate({
			"left": "200%"
		}, 3000);

		return false;

	});





  //IVI - begin

  $('.window_IVI .close_IVI').click(function () {
    $('#mask_IVI, .window_IVI').hide();
  });

  $('#saveOrder_IVI').click(function(){
  	var flag = true;
		if($('#fio_IVI').attr('value') == ''){
	 		$('#fioError_IVI').html('поле заполненно некорректно');
	 		flag = false;
	 	}
	 	else
	 		$('#fioError_IVI').html('');
		if($('#phone_IVI').attr('value') == ''){
			$('#phoneError_IVI').html('поле заполненно некорректно');
			flag = false;
		}
		else
			$('#phoneError_IVI').html('');
		if(flag){
			$('#mask_IVI, .window_IVI').hide();
			$('#order_IVI').submit();
		}
  });
  //IVI - end



});

$(window).resize(function() {
	$('.sliding li').width($('.c_reducer').width());
	$('#layout').height('auto');
	if ($('#layout').height()<$(document).height()) $('#layout').height($(document).height());
});




