function sleep (seconds) {
    // Delay for a given number of seconds
    // *     example 1: sleep(1);    // *     returns 1: 0

    var start = new Date().getTime();
    while (new Date() < start + seconds*1000) {}
    return 0;
}
$(document).ready(function(){

    $("#contactLink").click(function(){
        if ($("#contactForm").is(":hidden")){
            $("#contactForm").slideDown("slow");
            $('#contactForm').focus();

        }
        else{
            $("#contactForm").slideUp("slow");
        }
    });

    
    $("#contactLink2").click(function(){
	var loading = '<img src="'+getBaseURL()+'public/themes/rc/images/loading.gif">';

        if ($("#contactForm2").is(":hidden")){
            $("#contactForm2").slideDown("slow");
            
            if($('#ul_hidden_project_list').children().size()==0){
                $.ajax({
                    type: "GET",
                    url: "process.php?act=load-hidden-project",
                    beforeSend: function(){
                        $(".content_form").html(loading);
                    },
                    success: function(res){
                        $(".content_form").html(res);
                        
                    	$("#search-pj").autocomplete(pj_list, {
                    		minChars: 1,
                    		width: $("#search-pj").width,
                    		matchContains: "word",
                    		max:0,
                    		autoFill: false,
                    		formatItem: function(row) {
                    			return row.name;
                    		},
                    		formatResult: function(row) {
                    			return row.name;
                    		}
                    	});
                    	$("#go-pj").click(function() {
                            $.ajax({
                                type: "GET",
                                url: "process.php?act=search_pj&key="+$("#search-pj").val(),
                                beforeSend: function(req){
                            		$(".list-duan").html(loading);
                                },
                                error: function(req, status, info){
                                },
                                complete: function(res){
                                    	$(".list-duan").html(res.responseText);
                                }
                            });
                    	});
                    }
                });
            }
            var url = window.location.href;
            url = url.replace("#contactForm2","");            	
            window.location.href = url+"#contactForm2";
        }else{
            $("#contactForm2").slideUp("slow");
        }
         
        return false;
    });
    
    /**
     * clear saved position     * 
     */
    $("#imgClearSavedMap").click(function(){
        $(this).css('display','none');
        $('#displayAddress').html('&nbsp;&nbsp;');
        /*$('#map').val('');
        $('#onMapAddress').html('');*/
        $('#onMapAddress').html('');
        $('#txtTempLatLng').val('');
        $('#txtTempAddress').val('');
        return false;
    });
    
    /**
     * search address
     */
    $("#btnSearchMap").click(function(){
        var keyword = $('#txtAddress').val();
        if(keyword!='Đường/Phố Phường, Quận/Huyện, Tỉnh/TP' && keyword!=''){
        	getByAddress(keyword);//showAddress(keyword);        	
        }
        return false;
    });
    
    /**
     * show map
     */
    $("#showMapPopup").click(function(){
        scroll(0,0);
        if ($("#box-map").is(":hidden")){
            $('#showMapPopup').css('display','block');$('#fade').css('display','block');
            
            $('#upload_pid').hide();
            $('#upload_did').hide();
            $('#mode_price').hide();
            $('#upload_cid').hide();
            $('#upload_sub_cid').hide();
            /*if(typeof geocoder=="undefined"){
                //loadMap();
            }*/
            
            $("#box-map").show();
            
            //initial map
            initialize();
            
            var tmplatlng = $('#txtTempLatLng').val();
            if(tmplatlng!='')
            {
            	var ls = tmplatlng.split(',');
            	
            	if(!tmpllng)
            	{
            		var tmpllng = new google.maps.LatLng(ls[0],ls[1]);
            	}
            	map.setCenter(tmpllng);
            	getByLatLng(tmpllng);
            }
            else if($('#upload_did option:selected').val()>0)
            {
                //showAddress($('#upload_did option:selected').text()+ ', ' +$('#upload_pid option:selected').text()+', Vietnam');
            	//alert($('#upload_did option:selected').text()+ ', ' +$('#upload_pid option:selected').text());
            	getByAddress($('#upload_did option:selected').text()+ ', ' +$('#upload_pid option:selected').text());
            }
            else if($('#upload_pid option:selected').val()>0){
                //showAddress($('#upload_pid option:selected').text()+', Vietnam');
            	getByAddress($('#upload_pid option:selected').text());
            }
            else if($('#upload_pid option:selected').val()==0)
            {
                //showAddress('ha noi, Vietnam');
            }
            
            $("#box-map").show("slow");
            
            
            
        }
        else
        {
            $('#box-map').css('display','none');$('#fade').css('display','none');
            $('#upload_pid').show();
            $('#upload_did').show();
            $('#mode_price').show();
            $('#upload_cid').show();
            $('#upload_sub_cid').show();
            $("#box-map").hide("slow");
        }
        return false;
    });
    
    $("#saveMap").click(function(){
        $('#box-map').css('display','none');$('#fade').css('display','none');
        $('#upload_pid').show();
        $('#upload_did').show();
        $('#mode_price').show();
        $('#upload_cid').show();
        $('#upload_sub_cid').show();

        $("#imgClearSavedMap").css('display', '');
        //$('#showMapPopup').click();
        $('#box-map').css('display','none');
        $('#fade').css('display','none');
        
        //save position: latlng, address        
        $('#txtTempLatLng').val($('#txtMapLatLng').val());
        //alert($('#txtTempLatLng').val());
        $('#txtTempAddress').val($('#txtMapAddress').val());        
        $('#displayAddress').html($('#txtMapAddress').val());
        $('#txtAddress').val('Đường/Phố Phường, Quận/Huyện, Tỉnh/TP');
        return false;
    });
		
});


function closeForm(){
    $("#messageSent").show("slow");
    setTimeout('$("#messageSent").hide();$("#contactForm").slideD("slow");$("#contactLink").slideUp("slow");', 2000);
}

