function centerPopup(){
    windowHeight = document.documentElement.clientHeight;
    $(".js-popup").each(
          function()
          {
            popupHeight = $(this).height();
            posTop = $(window).scrollTop() + windowHeight/2-popupHeight/2;
            $(this).css({
            "top": posTop
            });
          }
    );
}

function popupShow(popup){
    $(".js-overlay").show();
    centerPopup(); 
    $(popup).show();
}

function contactSellerShow(ob) {
    $(".js-overlay").show();
    centerPopup(); 
	
	var title = $(ob).parents('.b-offer-item').find('.offit__title').text();
	t = 'Связаться с поставщиком ' + title;
	$('#pop-contact-seller .popup__title').text(t);
    $('#pop-contact-seller').show();
}


jQuery(document).ready(function() {
    $("body").append("<div class='b-overlay js-overlay'></div>");
    
    $(".js-overlay, .js-close").bind("click", function() {		
		$(".js-popup").hide();
        $(".js-overlay").hide();
        return false;
	});
    
    $(window).bind("resize", function(){
        centerPopup(); 
    });
    
    /*
    $(window).scroll(function () { 
        centerPopup(); 
    });
    */
    
    $(".js-offers").bind("click", function() {      
            
        if (
        
        
            $(this).parents(".b-offer-item").hasClass("b-offer-item_opened")) {
            $(".b-offer-item .js-expand").slideUp("medium");
            $(".b-offer-item").removeClass("b-offer-item_opened"); 
            $(this).parents(".b-offer-item").find(".js-expand").slideUp("medium");
            $(this).parents(".b-offer-item").removeClass("b-offer-item_opened");  
            
              

            $(this).parents(".b-offer-item").find(".js-offers-more").show(); 
            
            
           
            
            
                 
        }
        else
        {
        
               $(this).parents(".b-offer-item").find(".js-offers-more").hide();
            $(".b-offer-item .js-expand").slideUp("medium");
            $(".b-offer-item").removeClass("b-offer-item_opened"); 
            $(this).parents(".b-offer-item").find(".js-expand").slideDown("medium");
            $(this).parents(".b-offer-item").addClass("b-offer-item_opened"); 
            
            
 
            
            
            
         
        }
        
        return false;
    });    
    
    $(".js-exhib").bind("click", function() {      
            
        if ($(this).parents(".b-exhib-item").hasClass("b-exhib-item_opened")) {
            $(".b-exhib-item .js-expand").slideUp("medium");
            $(".b-exhib-item").removeClass("b-exhib-item_opened"); 
            $(this).parents(".b-exhib-item").find(".js-expand").slideUp("medium");
            $(this).parents(".b-exhib-item").removeClass("b-exhib-item_opened"); 
                 
        }
        else
        {
            $(".b-exhib-item .js-expand").slideUp("medium");
            $(".b-exhib-item").removeClass("b-exhib-item_opened"); 
            $(this).parents(".b-exhib-item").find(".js-expand").slideDown("medium");
            $(this).parents(".b-exhib-item").addClass("b-exhib-item_opened"); 
        }
        
        return false;
    });     
	
    
    $(".js-datepicker").datepicker({showOn: 'button', buttonImage: '/i/calendar.png', buttonImageOnly: true});

    $(".js-cat-select").bind("click", function() {   
         if ($(this).hasClass("opened")) {
             $(this).parents(".js-parent").find(".js-cat-selector").hide();
             $(this).removeClass("opened");
         }
         else
         {
			 
             $('.js-cat-selector').hide();		
             $('.js-cat-select').removeClass('opened');		
             var l = $(this).offset().left;
             var t = $(this).offset().top;
             $(this).parents(".js-parent").find(".js-cat-selector").css("left", l);
             $(this).parents(".js-parent").find(".js-cat-selector").css("top", t+20);
             $(this).parents(".js-parent").find(".js-cat-selector").show();
             $(this).addClass("opened");
         }
         return false;
    }); 
    
    $(".js-cat-selector a").live("click", function() {   
        var rel = $(this).attr("rel");
        var title = $(this).html();
        var parent = $(this).parents(".js-parent");
		
        //parent.find("input").attr("value", rel);
        parent.find(".js-cat-selector").hide();
        parent.find(".js-cat-select").html(title).removeClass("opened");
		parent.find("input").val(title);
		//alert(title);
		//alert($("input[name='category']").val());		
		
		// Для выставок
		if ($(parent).attr('id') == 'category') {
			parent.find("input").val(rel);
		}

		
		// Если изменили страну, подгружаем список городов
		if (title != 'все' && title != 'tutte') {
			if ($(parent).attr('id') == 'countries') {
				$.post('/ajax/main.php?action=towns', {country:title}, function(data){
					$('div#towns').css('display', 'block');		
					if (sid == 1) {
					    $('div#towns .js-cat-select').html('город');
					} else {
					    $('div#towns .js-cat-select').html('citta');	
					}
					$('div#towns ul').html(data);
				});
			}
		} else {
			parent.find("input").val('');
		}
		
		
		// Для недвижимости
		if (title != 'все' && title != 'tutte') {
			
			// Если изменили страну, подгружаем список регионов и городов
			if ($(parent).attr('id') == 'country') {
				parent.find("input").val(rel);
				$.post('/ajax/main.php?action=r_country', {country:rel}, function(data){
					if (sid == 1) {
					    $('div#towns .js-cat-select').html('все');
					} else {
					    $('div#towns .js-cat-select').html('tutte');	
					}
					var s = data.split('::::');
					
					$('div#region ul').html(s[0]);
					$('div#city ul').html(s[1]);
				});
			}
			
			// Если изменили регион, подгружаем список городов
			if ($(parent).attr('id') == 'region') {
				parent.find("input").val(rel);
				$.post('/ajax/main.php?action=r_region', {region:rel}, function(data){
					if (sid == 1) {
					    $('div#city .js-cat-select').html('все');
					} else {
					    $('div#city .js-cat-select').html('tutte');	
					}
					$('div#city ul').html(data);
					
				});
			}
			
		} else {
			parent.find("input").val('');
		}
        
        return false;
    }); 
    
    $(".js-exhib-popup").bind("click", function() {   
        var parent = $(this).parents(".b-exhib-item");
        var id = parent.find(".js-exhib").attr("rel");
        var title = parent.find(".js-exhib").html();
        var popup = $(this).attr("href");
        
        $(popup).find(".js-exh-title").html(title);
        $(popup).find(".js-exh-id").attr("value", id);
        popupShow(popup);    
    }); 
	
    $(".js-realty-popup").bind("click", function() {   
        var parent = $(this).parents(".b-exhib-item");
        var id = parent.find(".js-exhib").attr("rel");
        var title = parent.find(".js-exhib").text();
        var popup = $(this).attr("href");
        
        $(popup).find(".js-exh-title").html(title);
        $(popup).find(".js-exh-id").attr("value", id);
        popupShow(popup);    
    }); 
	
	
	
    $(".js-place-popup").bind("click", function() {   
        var id = $(this).attr("rel");
        var title = $(this).html();
        var popup = '#pop-simple';
		var content = $('div#place-'+id).html();
        
        $(popup).find(".js-exh-title").html(title);
		$(popup).find(".popup__text").html(content);
		
        popupShow(popup);    
    }); 


    $(".js-provider-popup").bind("click", function() {   
        var id = $(this).attr("rel");
        var title = $(this).html();
        var popup = '#pop-simple';
		var content = $('div#provider-'+id).html();		
        
        $(popup).find(".js-exh-title").html(title);
		$(popup).find(".popup__text").html(content);		
		
        popupShow(popup);    
    }); 

    
    $(".js-reminder-type-phone").bind("click", function() {        
        var parent = $(this).parents(".js-parent");
        parent.find(".js-reminder-phone").show();
        parent.find(".js-reminder-email").hide();
    }); 
    
    $(".js-reminder-type-email").bind("click", function() {  
        var parent = $(this).parents(".js-parent");
        parent.find(".js-reminder-phone").hide();
        parent.find(".js-reminder-email").show();
    });   
	
	$(document).bind('click', function(e){
		var t = e.target;							   
		$('.js-cat-selector').hide();		
		$('.js-cat-select').removeClass('opened');		
	});

	$(document).bind('keydown', function(e){
		var t = e.target;	
		if (e.keyCode == 27) {
			$('.js-cat-selector').hide();
			$('.js-cat-select').removeClass('opened');
		}
		if (e.keyCode == 13) {
			$(t).parents('form').find('input.b-green-btn').click();
		}

	});

	$('form#indpoisk table tr').each(function() {
		if (!$('td:eq(0)', this).attr('colspan')) {
		    $('td:eq(0)', this).attr('align', 'right');
		}
    });
	
	
});


// Callback form

$(document).ready(function(){
	if ($('form#pop-callback-form').length > 0) {
		$('form#pop-callback-form input').bind('keyup', function(){
			$('div#popup__error').fadeOut('slow');
		});
	}
	
	$('.b-popup form input').bind('keyup', function(){
		$(this).parents('.b-popup').find('div.b-errors').fadeOut('slow');
		$(this).parents('.b-popup').find('input').removeClass('required');
	});
	
});


function submit_ind() {
	var error = false;
	$('form#indpoisk input.required').each(function(){
		if ($(this).val() == '') {
			alert('Поле ' + $(this).attr('name') + ' должно быть заполнено');
			error = true;
			return false;
		}
	});

	if (!error) {
	    document.form2.submit();
	}
}

function send_bisform() {
    var name = $("form#bisform input[name='name']").val();
	var tel = $("form#bisform input[name='tel']").val();
	var email = $("form#bisform input[name='email']").val();
    
    if (name == '') {
        $("form#bisform div#err1").show('fast');
        return false;
    } else {
        $("form#bisform div#err1").hide('fast');
    }

    if (tel == '') {
        $("form#bisform div#err2").show('fast');
        return false;
    } else {
        $("form#bisform div#err2").hide('fast');    
    }    
	
    if (email == '') {
        $("form#bisform div#err3").show('fast');
        return false;
    } else {
        $("form#bisform div#err3").hide('fast');
    }
	

    document.bisform.submit();
}

