$(document).ready(function(){
	
  $('div[id^=cityid_]').click(function(){
  	if($('#b'+this.id).css('display') == 'none')
  	{
  		$('#b'+this.id).show("fast");
  	}
  	else
  	{
  		$('#b'+this.id).hide("fast");
  	}
  });
  
  $('#showMapLink').click(function(){
  	if($('#showMap').css('display') == 'none')
  	{
  		$('#showMap').show("fast");
  		
  	}
  	else
  	{
  		$('#showMap').hide("fast");
  	}
  });
  
  $(function() {
	$('a[rel=lightbox]').lightBox();
  });
  
	$('#buttonClick').click(function(){
		$('#result').html('<div align="center"><img src="/images/loader.gif" /></a>');
		$('#sendMail').ajaxForm(function() {
	   		var qString = $("#sendMail").formSerialize();
	   		$('#result').load("/new/files/sendmail.php?"+qString, function() {
			$('#captch').attr("src","/captcha/"+Math.random()+".jpg");
			});
	    });
	});
  
  var base = 'http://www.pragaestate.ru/new/core/run/';  
  $(".modalReg").click(function(){
    if($("#modal_code_enter").length>0){
       $("#modal_code_enter").fadeTo(1000,1,function(){});
    }
    $("body").append('<div id=fog></div>');
    $("#fog").fadeTo(1000,0.6,function(){}); 
    return false;
  }); 
  $(".closeForm").click(function(){
     $(this).parent().fadeOut(1000);
     if($('#fog').length>0){
        $('#fog').fadeTo(1000,0,function(){
            $(this).remove();
            if($('.loading').length>0){
                $('.loading').remove();
            }
            if($(".errorA").length>0){
                $(".errorA").remove();
            }
        });
     }     
  });
  $(".reg_code").click(function(){
     $(this).closest('#modal_code_enter').fadeOut(1000);
     if($("#modal_form_reg").length>0){
        $("#modal_form_reg").fadeTo(1000,1,function(){});
     }
  }); 
  $(".send_enter_req").click(function(){
     var data=$("#modal_form_reg").serialize();
     $("#modal_form_reg").append('<div class="loading opacity"></div>');
     //$(".send_enter_req").before("<span class=\"loader\"></span>");
     var rezult = jQuery.ajax({url: base+"runInvestProject.php", 
                              global: false,
                              type: "post",
                              data: data+"&action=reg",
                              dataType: "json",
                              success: is_ok                             
     });
     $(".errorBorder").removeClass('errorBorder');
     $(".errorA").remove();
     $(".msg_ok").remove();        
     
     function is_ok(r){
       $(".loading").remove(); 
       if(r.status){
        $("#modal_form_reg ul").after("<span class=\"msg_ok\">"+r.msg+"</span>");
        $('#modal_form_reg').delay(3000).fadeOut(1000,function(){
            $(".msg_ok").remove();
        });
        $('#fog').delay(3000).fadeOut(1000);
       }
       else{       
        $("#modal_form_reg ul").after("<span class=\"errorA\">"+r.errorMsg+"</span>");
        if(r.errorFields.length>0){        
           for (var key in r.errorFields) { 
             $("#modal_form_reg input[name="+r.errorFields[key]+"]").addClass('errorBorder');
           }   
        }               
       }
    }
  }); 
  $(".enter").click(function(){
    var data = $("#modal_code_enter").serialize();
    $("#modal_code_enter input[name=code]").after("<span class=\"m_load\"></span>");
    var rezult = jQuery.ajax({url: base+"runInvestProject.php", 
                              global: false,
                              type: "post",
                              data: data+"&action=aut",
                              dataType: "json",
                              success: is_ok                             
     }); 
     function is_ok(r){
        $(".m_load").remove();       
        if(r.status){
           $.cookie('id', r.data['id_user'], { expires: 7, path: '/' });
           $.cookie('hash', r.data['hash'], { expires: 7, path: '/' });
           location.replace(location.href);          
        }
        else{
            if($(".errorA").length>0){
                $(".errorA").remove();
            }
           $(".msg_code").before("<span class=\"errorA\">"+r.errorMsg+"</span>");  
        }
     }
  });
  $(".leave").click(function(){
     $.cookie('id', null,{ expires: -1,path: '/' });
     $.cookie('hash', null,{ expires: -1,path: '/' });
     location.replace(location.href);  
  }) 
});

function OpenWindow(url, w, h) {
	open(url, "", 'menubar=no,directories=no,location=no,resizable=no, target=viv scrollbars=yes,width=' + w + ',height=' + h);
}

