var appear = 2000;
var appear2 = 1000;
var delay = 5000;

jQuery.noConflict();
jQuery(document).ready(function($) 
{  
    var click = false;
	
	if (!$.browser.safari)  {
		  $(".scroll-list li").each(function(){
		    if($(this).attr('class')!='ban1 img_act') $(this).fadeOut(5);
		  });
	} else {
		  $(".scroll-list li").each(function(){
		    if($(this).attr('class')!='ban1 img_act') $(this).hide(5);
		  });
	}
	
	function sw(btn){
	    
	    $('.img_act').fadeOut(appear2).removeClass('img_act');
		$('.'+$(btn).attr('id')).fadeIn(appear2).addClass('img_act');
		$('.ban_active').removeClass('ban_active');
		$(btn).addClass('ban_active');
	}
	
  $('.control li').click(function(){
    sw($(this));
	click = true;
  });
  
  function change(){
    if(!click) {
	  var count = $('.scroll-list li').length;
	  var curr = parseInt($('.ban_active').attr('id').substring(3));
	  var next = curr%count + 1;
	  sw($('#ban'+next));
      setTimeout(function() {change();}, delay);
	}
  };
  if($('.gallery-frame').size() > 0){
    setTimeout(function() {change();}, delay);
  }
  
  $('.my_table tr:odd').addClass('even');
  
  $('.contacts_a',$('#header')).unbind().click(function(){
     //alert('1');
     $('.citys_data_box').show();
     return false;
  });
  /*$('.citys_data_box a').hover(function(){
      $(this).css({''});
  },function(){
      $(this).removeClass('active');
  });*/
   
  $('.gallery2 a').lightBox(); 

  $('.citys_data_box a',$('#header')).live('click',function(){
      
     //alert(document.co);
     
     var rel = $(this).attr('rel'); 
     //alert(citys_data[parseInt(rel)]);
     $('.citys_data_box a.active').removeClass('active');
     $(this).addClass('active');
     $('.contacts_a span').html($(this).text());
     $('.contacts ul').html(citys_data[parseInt(rel)]);
     
     setCookie("city_rel",parseInt(rel) , "Mon, 01-Jan-2099 00:00:00 GMT", "/");
     
     $('.citys_data_box').hide();
  });
  $('.request').unbind().click(function(){
      if($('.object_var').size() > 0){
        $('#object-select [value='+$('.object_var').attr('rel')+']').attr("selected", "selected");
        //alert($('.object_var').attr('rel'))
      }
      $(this).toggleClass('act_req'); 
      $('.zapros-form .error').html('');
      $('.zapros-form').show();
      if(!/act_req/i.test($(this).attr('class'))) {$('.zapros-form').fadeOut();}
  });
  
  $('.inner').click(function(){
     // $('.citys_data_box,.zapros-form').hide();
  });
  
  $('.gozapros').unbind().click(function(){
      var error = '';
      $('.zapros-form .buyding').each(function(){
         if($(this).attr('name') != 'email'){
             if($(this).attr('name') == 'object' && $(this).val() == 0){
                error += $(this).attr('rel')+"<br />";
             }
             if($(this).val().length == 0){
                 error += $(this).attr('rel')+"<br />";
             }
         }else{
             if(!/^[-a-z0-9!#$%&'*+/=?^_`{|}~]+(\.[-a-z0-9!#$%&'*+/=?^_`{|}~]+)*@([a-z0-9]([-a-z0-9]{0,61}[a-z0-9])?\.)*(aero|arpa|asia|biz|cat|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|net|org|pro|tel|travel|[a-z][a-z])$/.test($(this).val())){
                 error += $(this).attr('rel')+"<br />";
             }
         }
      });
      //alert()
      if(error != ''){
          $('.zapros-form .error').html(error);
      }else{
        var flag = true;
        //alert($('.zapros-form form').serialize()+'&type=sendMsg');
        $.ajax({
            url: '/ajax-request.php',
            type : 'POST',
            data : $('.zapros-form form').serialize()+'&type=sendMsg',
            success: function(data) {
              if(data == '0'){
                  $('.zapros-form .error').html('<p style="color:red;">Ошибка при отправке письма. Попробуйте ещё раз.</p>');
              
              }else{
                
                  $('.zapros-form .error').html('<p style="color:green;">Запрос на расчет стоимости успешно отправлен</p>');
                  $('.zapros-form .buyding').each(function(){
                      $(this).val('');
                  });
                  
                  setTimeout(function(){
                      $('.zapros-form').fadeOut(400);
                      $('.request').removeClass('act_req');
                  },2000);
              }
               
            }
        });
          
          
          
      }
      return false; 
  });
  
  
  $(document).click(function(e){ // убиваем все окна при нажатии вне окна
  /*var req = $(e.target).parents('body').find('.request').attr('class');
    if (!/act_req/i.test(req) && $(e.target).attr('class') != 'request') {
    $('.zapros-form').fadeOut();
    $('.request').removeClass('act_req');
    }*/
    if ($(e.target).parents('.zapros-form').length != 1 && !/request/gi.test($(e.target).attr('class'))){
         $('.zapros-form').fadeOut();
         $('.request').removeClass('act_req');
    }
  });
  
  
});


function setCookie (name, value, expires, path, domain, secure) 
{
      document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

