
var geoopened = new Array();
geoopened[1] = false;
geoopened[2] = false;
geoopened[3] = false;

var geoCurrRegion = -1;
var geoCurrType = -1;
var geoLastInnerRowColor = "";

$(document).ready(function() {
	//fillCities(miasta);
    //alert('laduje skrypt');
    var alt = false;

    $(document).keydown(function(event) {
        if(geoopened[2] && event.keyCode == 17) {
            alt = true;
        }
    });

    $(document).keyup(function(event) {
        if(event.keyCode == 17) alt = false;
        if(geoopened[2]) {
            var letter;
            if(alt) {
                switch(event.keyCode) {
                    case 65:
                        letter = 'A';
                        break;
                    case 67:
                        letter = 'C';
                        break;
                    case 69:
                        letter = 'E';
                        break;
                    case 76:
                        letter = 'L';
                        break;
                    case 78:
                        letter = 'N';
                        break;
                    case 79:
                        letter = 'O';
                        break;
                    case 83:
                        letter = 'S';
                        break;
                    case 88:
                        letter = 'Z';
                        break;
                    case 90:
                        letter = 'Z';
                        break;
                }
            }
            else {
                letter = String.fromCharCode(event.keyCode);
            }
            
            $("#geo_ddl_list2 .dll_scroll").scrollTop(0);

            $("#geo_ddl_list2 ul li").each(function(i) {
                if($(this).text() != 'województwo') {
                    if($.browser.msie) {
                        txt = $(this).text().substr(0, 1);
                    }
                    else {
                        txt = $(this).text().substr(1, 1);
                    }

                    if(txt == letter) {
                        var pos = $(this).position();
                        $("#geo_ddl_list2 .dll_scroll").scrollTop((pos.top - 5));
                        return false;
                    }
                }
            });
        }
    });



	
   addListenersToGeoRegionList();
   addListenersToTypesList();
   addListenersToGeoTabsList();


	$("#geo_ddl_content2").click(function() {
		$("#geo_ddl2_img").click();
	});

	$("#geo_ddl2_img").toggle(
		function() {
			var pos = $("#geo_ddl_content2").position();
			$("#geo_ddl_list2").css("top", (pos.top + 20));
			$("#geo_ddl_list2").css("left", pos.left);
			
			$("#geo_ddl_list2 .ddl_border_right").removeClass("dll_scroll");
			var boxH = $("#geo_ddl_list2").height();
			if(boxH > 200) {
                if($.browser.msie) {
    				$("#geo_ddl_list2 .ddl_border_right").addClass("dll_scroll");
    				$("#geo_ddl_list2 .ddl_t").css("width", "166px");
    				$("#geo_ddl_list2 .ddl_b").css("width", "166px");
    				$("#geo_ddl_list2").css("width", "180px");
                }
                else {
    				$("#geo_ddl_list2 .ddl_border_right").addClass("dll_scroll");
    				$("#geo_ddl_list2 .ddl_border_right ul").css("width","100%");
    				
    				$("#geo_ddl_list2 .ddl_t").css("width", "156px");
    				$("#geo_ddl_list2 .ddl_b").css("width", "156px");
    				$("#geo_ddl_list2").css("width", "170px");
				}
			}
			else {
				$("#geo_ddl_list2").css("width", "140px");
				$("#geo_ddl_list2 .ddl_t").css("width", "126px");
				$("#geo_ddl_list2 .ddl_b").css("width", "126px");
			}

			$("#geo_ddl_list2").show();
			geoopened[2] = true;
			geoCloseDdls(2);
      
    	closeAllBaseLists();  			
		},
		function() {
			$("#geo_ddl_list2").hide();
			geoopened[2] = false;		
	    closeAllBaseLists();  
		}
	);
	// zamykanie list z drugiej grupy
  $("#ddl1_img").click(function(){
			closeAllGeoLists();     
  });	
  $("#ddl2_img").click(function(){
			closeAllGeoLists();     
  });
  $("#ddl3_img").click(function(){
			closeAllGeoLists();  
  });	
         /*
  $("#geo_ddl1_img").click(function(){
			closeAllBaseLists();     
  });	
  $("#geo_ddl2_img").click(function(){
			closeAllBaseLists();     
  });
  $("#geo_ddl3_img").click(function(){
			closeAllBaseLists();  
  });	
   */
	$("#geo_ddl_content3").click(function() {
		$("#geo_ddl3_img").click();
	});

	$("#geo_ddl3_img").toggle(
		function() {
			var pos = $("#geo_ddl_content3").position();
			$("#geo_ddl_list3").css("top", (pos.top + 20));
			$("#geo_ddl_list3").css("left", pos.left);
			$("#geo_ddl_list3 .ddl_t").css("width", "107px");
			$("#geo_ddl_list3 .ddl_b").css("width", "107px");
			$("#geo_ddl_list3").show();
			geoopened[3] = true;
			geoCloseDdls(3);
 		  closeAllBaseLists();  
		},
		function() {
			$("#geo_ddl_list3").hide();
			geoopened[3] = false;
   		closeAllBaseLists();  
		}
	);
         
    if($.browser.msie) {

        $("#geo_ddl_list3 label input.geo_chklayer").click(function(e){
          //alert('clicked23');
          //alert($(this).attr('checked'));//pokazuje stan po clicku
          
          var id = $(this).attr("id").substr(8);
          //pokaze stan, ktory bedzie po zakonczeniu obslugi zdarzenia
          var bFutureChecked = $(this).attr('checked');
          //alert('ustawie taki stan: ' + bFutureChecked);
          $("#chk_" + id).attr("checked", bFutureChecked);
          
          if(bFutureChecked) {
                showTab(id);
                restoreTab(id);
               
          }else {
                hideTab(id);
                removeTab(id);
          }          
          
          reqObjects(1,"all",false,true);
        }); 
               
       
    }

	$(".geo_chklayer").change(function() {
        if(!$.browser.msie) {
        
            var id = $(this).attr("id").substr(8);
            var ischecked = $("#geo_chk_" + id).attr("checked");

            if(this.checked) {
                showTab(id);
                restoreTab(id);
               
            }
            else {
                hideTab(id);
                removeTab(id);
            }
            $("#geo_chk_" + id).attr("checked", ischecked);
            $("#chk_" + id).attr("checked", ischecked);
            //$("#chk_" + id).attr("checked", !$("#chk_" + id).attr("checked"));

            //$("#geo_ddl3_img").click();
            //reqObjects(1,"all",false,true);
           // $("#chk_"+id).change();
            
        }
    });

  /** DODAJ synchronizacje miedzy starymi, a geokoderem */
  /*
	$(".chklayer").change(function() {
        if(!$.browser.msie) {
            var id = $(this).attr("id").substr(4);
            $("#geo_chk_" + id).attr("checked", !$("#geo_chk_" + id).attr("checked"));
        }
    });
    */	

    /*
  $("div.btn_search span").click(function(){
        if ((currCity !== 0) && (currCity !== -1)){
          resetMapToCity(currCity);        
        }else {
          if ((geoCurrRegion !== 0) && (geoCurrRegion !== -1)){
          	reqCoords("region",geoCurrRegion,1,0);
          }else{           
            resetMap();          
          }
        }
			$("#ddl_list3").hide();
			geoopened[3] = false;
			$("#ddl_list2").hide();
			geoopened[2] = false;        
    
  });
   */
});

function addListenersToTypesList(){
	$(".geo_ddl3 ul li").click(function() {
		var stext = $(this).text();
		$("#geo_ddl_content3").text(stext);
		$("#geo_ddl3_img").click();
    geoCurrType = $(this).attr("value");
	//	geoCurrRegion = $(this).attr("value");  
	});	
	
	$(".geo_ddl3 ul li").mouseover(function() {
		$(this).addClass("dll_over");
	}).mouseout(function() {
		$(this).removeClass("dll_over");
	});  
}

function addListenersToGeoRegionList(){
	$(".geo_ddl2 ul li").click(function() {
		var stext = $(this).text();
		$("#geo_ddl_content2").text(stext);
		$("#geo_ddl2_img").click();

		geoCurrRegion = $(this).attr("value");  
	});	
	
	$(".geo_ddl2 ul li").mouseover(function() {
		$(this).addClass("dll_over");
	}).mouseout(function() {
		$(this).removeClass("dll_over");
	});  
}

 /*
function geoCloseDdls(currentId) {
	for(var i=0; i<=3; i++) {
		if(i != currentId) {
			if(geoopened[i]) {
				$("#geo_ddl" + i + "_img").click();
			}
		}
	}
}
   */
   function closeAllGeoLists(){
  	for(var i=0; i<=3; i++) {
			if(geoopened[i]) {
  				$("#geo_ddl" + i + "_img").click();
  			}
  	}   
   }
  function closeAllBaseLists(){
  	for(var i=0; i<=3; i++) {
			if(opened[i]) {
  				$("#ddl" + i + "_img").click();
  			}
  	}   
  } 
   function geoCloseDdls(currentId) {
   
	for(var i=0; i<=3; i++) {
		if(i != currentId) {
			if(geoopened[i]) {
				$("#geo_ddl" + i + "_img").click();
			}
		}
	}
}

   
function getValidPointFromPlacemarks(Placemark){

    var hmg = Placemark.length;
    
    var idRegion = getSelectedIdRegion();
    
    var sRegion = geoIdRegionToString(idRegion);
  //  console.debug(sRegion);
  //  console.debug(hmg);
    for (var i=0;i<hmg;i++){
        //dla kazdej lokalizacji zwroconej przez Google                    
      var place = Placemark[i];
      
      if (typeof(place.AddressDetails) !== 'undefined' && typeof(place.AddressDetails.Country) !== 'undefined' && typeof(place.AddressDetails.Country.AdministrativeArea) !== 'undefined' ){
        var country =  place.AddressDetails.Country;
   //     console.debug(country);
        
        if (country.CountryNameCode === "PL"){
          if (idRegion > 0){
            //jest wybrane wojewodztwo
            if (country.AdministrativeArea.AdministrativeAreaName == sRegion){
              if (typeof(place.Point.coordinates) === 'object'){
              if (typeof(place.Point.coordinates[0]) === 'number' && typeof(place.Point.coordinates[1]) === 'number'){
                return new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
              }
              }
            } else {
              //wojewodztwo sie nie zgadza
              
            }            
          } else {          
            //nie ma, wiec zaufaj pierwszemu wynikowi
              if (typeof(place.Point.coordinates) === 'object'){
              if (typeof(place.Point.coordinates[0]) === 'number' && typeof(place.Point.coordinates[1]) === 'number'){
                return new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
              }
            
          }
        }
      }
//var point = Placemark[i].Point;
     } else if (typeof(place.AddressDetails) !== 'undefined' && typeof(place.AddressDetails.Country) === 'undefined' ){
         //dummy sprawdzenie, czy to Polska
		 if (typeof(place.Point.coordinates) === 'object'){
        	if (typeof(place.Point.coordinates[0]) === 'number' && typeof(place.Point.coordinates[1]) === 'number'){
        	    if (place.Point.coordinates[0] < 25 && place.Point.coordinates[0] > 14){
        	    	if (place.Point.coordinates[1] < 55 && place.Point.coordinates[1] > 48){
						return new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);				
				    }
				} 
	 		}
	 	}
	 }                     
    }    
}

function addListenersToGeoTabsList(){
  $("div#search_bar_tab_1 label").click(function(){
    $("div#search_bar_tab_2").removeClass("tab_active");
    $("div#search_bar_tab_2").addClass("tab_inactive");
    $("div#search_bar_tab_1").removeClass("tab_inactive");
    $("div#search_bar_tab_1").addClass("tab_active");

    setTimeout(function(){
    $("div#search_bar_tab_2_content").hide();
    $("div#search_bar_tab_1_content").show();    
    },100);

  });
  $("div#search_bar_tab_2 label").click(function(){
    
    $("div#search_bar_tab_1").removeClass("tab_active");
    $("div#search_bar_tab_1").addClass("tab_inactive");
    $("div#search_bar_tab_2").removeClass("tab_inactive");
    $("div#search_bar_tab_2").addClass("tab_active");

    setTimeout(function(){
    $("div#search_bar_tab_1_content").hide();
    $("div#search_bar_tab_2_content").show();
    },100);

  });

}   