IE = document.all&&!window.opera?true:false;
var id_obiektu=-1;
var i_zabytku=-1;
//////////////
function aEL(o,t,f){var a=Array.prototype.slice.call(arguments,3)
o[t+f]=function(e){return !1===f.apply(o,[e||window.event].concat(a))?e.preventDefault?e.preventDefault():!1:1}
return(o.x=o.addEventListener)?o.x(t,o[t+f],!1):(o.x=o.attachEvent)?o.x('on'+t,o[t+f]):!1}
////////
[].indexOf || (Array.prototype.indexOf = function(v){
       for(var i = 0; i<this.length && this[i] !== v;i++);
       return i>=this.length?-1:i;
});
[].indexOfID || (Array.prototype.indexOfID = function(v){
       for(var i = 0; i<this.length && this[i]['id'] != v;i++);
       return i>=this.length?-1:i;
});
//////////////
var map;
function gMapStart(){
   map = new GMap(document.getElementById("map"));
   map.addControl(new GLargeMapControl());
   var mt=map.getMapTypes();
   for(var i=0;i<mt.length;i++){
          if(mt[i].getName()=='Hybrid'){mt[i].getName=function(){return "Hybrydowa";}}
          if(mt[i].getName()=='Map'){mt[i].getName=function(){return "Mapa";}}
          if(mt[i].getName()=='Satellite'){mt[i].getName=function(){return "Satelitarna";}}
   }
   map.enableDoubleClickZoom();
   map.enableContinuousZoom();

   map.addControl(new GMapTypeControl());
   map.addControl(new GOverviewMapControl());
   _mPreferMetric=true;
   map.hideControls();
   GEvent.addListener(map, "mouseover", function(){
     map.showControls();
   });
   GEvent.addListener(map, "mouseout", function(){
       map.hideControls();
   });
   map.setCenter(new GLatLng(ck[0],ck[1]), 14);
   //mappam.register( map, '7859681082600759296', '5' );

}


function gMapLoad(){
      function createTabbedMarker(point,label,tabs,icon) {
        var marker = new GMarker(point,icon);
        var marker_num = gmarkers.length-1;
        marker.marker_num = marker_num;
        marker.tabs = tabs;
        gmarkers[marker_num]['marker'] = marker;
	

        GEvent.addListener(marker, "click", function() {//gmarkers[marker_num]
          marker.openInfoWindowTabsHtml(marker.tabs);
        });
     //   GEvent.addListener(marker,"mouseover", function() {
     //     marker.openInfoWindowTabsHtml(marker.tabs);
     //   });
     //   GEvent.addListener(marker,"mouseout", function() {
     //     marker.closeInfoWindow(marker.tabs);
     //   });
       // add a line to the sidebar html
     //   sidebar_html += '<a href="javascript:myclick(' + marker_num + ')" OnMouseOver="hover(' + marker_num + ')" OnMouseOut="close(' + marker_num + ')">' + (marker_num+1) + '<em>' + label + '<b></b></em></a>';
  //      sidebar_html += '<a href="#' + marker_num + '" OnMouseOver="hover(' + marker_num + ')" OnMouseOut="close(' + marker_num + ')">' + (marker_num+1) + '<em>' + label + '<b></b></em></a>';
         aEL(document.getElementById("hs"+gmarkers[marker_num]['id']),'mouseover',myclick,gmarkers[marker_num]['id']);
         return marker;
      }
      // ==================================================



function getNodeValue(Element) {
if ((Element.length>0) && Element[0] && Element[0].firstChild && Element[0].firstChild.nodeValue)
   return Element[0].firstChild.nodeValue;
}

      // Read the data from example.xml
      var request = GXmlHttp.create();
      request.open("GET", filename, true);
      request.onreadystatechange = function() {
        if (request.readyState == 4) {
if ((request.status != 200) && (request.status != 304)) {
alert("file not found: "+filename);
return;
}
          var xmlDoc = request.responseXML;
          // obtain the array of markers and loop through it
          var markers = xmlDoc.documentElement.getElementsByTagName("marker");
           //alert("processing "+markers.length+" markers");
    var icon = new GIcon();
    icon.image = "images/house2.png";
    icon.shadow = "images/shadow-house2.png";
    icon.iconSize = new GSize(28.0, 29.0);
    icon.shadowSize = new GSize(43.0, 29.0);
    icon.iconAnchor = new GPoint(14.0, 28.0);
    icon.infoWindowAnchor = new GPoint(14.0, 14.0);
    minGmarkerInd=gmarkers.length;
    for (var i = 0; i < markers.length; i++) {
	    icon.image = "images/h/h"+(markers[i].getAttribute("id"))+".png";
    
            // obtain the attributes of each marker
            gmarkers[minGmarkerInd+i]=new Array();
            gmarkers[minGmarkerInd+i]['id']=markers[i].getAttribute("id");//*1+0;

            var point=daj_point(gmarkers[minGmarkerInd+i]['id']);
            gmarkers[minGmarkerInd+i]['odl']=daj_odl(point);
            gmarkers[minGmarkerInd+i]['kier']=daj_kier(point);
            gmarkers[minGmarkerInd+i]['visible']=true;
	    gmarkers[minGmarkerInd+i]['rodzaj']='hostel';
//            if(i==0) alert(i+gmarkers[i]['id']+point);
            var label = markers[i].getAttribute("label");

            // get the tab info
            tabInfo = markers[i].getElementsByTagName("tab");
            tabs = new Array();
            if (tabInfo.length > 0) {
               // alert("processing "+tabInfo.length+" tabs");
               for (var j = 0; j < tabInfo.length; j++) {
                  var tabLabel = getNodeValue(tabInfo[j].getElementsByTagName("label"));
                  var tabHtml = getNodeValue(tabInfo[j].getElementsByTagName("contents"));
                  // alert("point["+i+"] tab["+j+"] label="+tabLabel+", contents="+tabHtml);
                  if ((j==0) && (tabInfo.length > 2)){ //  adjust the width so that the info window is large enough for this many tabs
                      tabHtml = '<div style="width:'+tabInfo.length*88+'px">' + tabHtml + '</div>';
                  }
                  tabs.push(new GInfoWindowTab(tabLabel,tabHtml));
               }
            } else { alert("no tabs point "+i); }

            // create the marker
            var marker = createTabbedMarker(point,label,tabs,icon);


            map.addOverlay(marker);
            if($("#map").attr("class")!='ab' && gmarkers[minGmarkerInd+i]['odl']==0) {
               a=$("#map_parent > h3").eq(0);
               txt=a.html();
               var re = new RegExp("(.*)"+label+"(.*)", "gi");
               if(txt.match(re)){
                 //a.prepend((i+1)+".");
                 id_obiektu=gmarkers[minGmarkerInd+i]['id'];
               }
             }
         //   var label = new ELabel(point, i+'.', "label");
         //  map.addOverlay(label)
          }
          // put the assembled sidebar_html contents into the sidebar div
         // document.getElementById("sidebar").innerHTML = sidebar_html;
         if($("#map").attr("class")!='ab' && id_obiektu>=0)
        window.setTimeout("myclick(id_obiektu,id_obiektu)", 100);
        }
      }
      request.send(null);

    // This Javascript is based on code provided by the
    // Blackpool Community Church Javascript Team
    // http://www.commchurch.freeserve.co.uk/
    // http://www.econym.demon.co.uk/googlemaps/
}

///////////////////////////
function gZabLoad(){
function createMarker(point,icon, txt) {
  var marker = new GMarker(point,icon);
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml("<b>" + txt + "</b>");
  });
      //   GEvent.addListener(marker,"mouseover", function() {
     //     marker.openInfoWindowTabsHtml(marker.tabs);
     //   });
     //   GEvent.addListener(marker,"mouseout", function() {
     //     marker.closeInfoWindow(marker.tabs);
     //   });
  return marker;
}
    var icon = new GIcon();
    icon.image = "images/house2.png";
    icon.shadow = "images/shadow-house2.png";
    icon.iconSize = new GSize(28.0, 29.0);
    icon.shadowSize = new GSize(43.0, 29.0);
    icon.iconAnchor = new GPoint(14.0, 28.0);
    icon.infoWindowAnchor = new GPoint(14.0, 14.0);
    var gmMinInd=gmarkers.length;
    var tempImg=new Image();
    for (var i = 0; i < z.length; i++) {
	if(z[i]['marker_icon'])
		icon.image=z[i]['marker_icon'];
	else
		icon.image="images/markers/"+z[i]['rodzaj']+'_def.png';
	var point=daj_point(z[i]['id']);
        z[i]['odl']=daj_odl(point);
        z[i]['kier']=daj_kier(point);
        z[i]['visible']=true;
//      if(i==0) alert(i+gmarkers[i]['id']+point);
//alert("hs"+z[i]['id']+'. '+document.getElementById("hs"+z[i]['id']));
        aEL(document.getElementById("hs"+z[i]['id']),'mouseover',zclick,i);
        z[i]['marker']= createMarker(point,icon,"<div style='width:270px'>"+(z[i]['foto']!=''?"<img style='float:left;padding:0 5px 5px 0;' 'src='"+z[i]['foto']+"'>":'')+z[i]['description']+"</div>")
        map.addOverlay(z[i]['marker']);
        if(z[i]['odl']==0) {
               a=$("#map_parent > h4").eq(0);
               txt=a.html();
               var re = new RegExp("(.*)"+z[i]['description']+"(.*)", "gi");
		if(txt){
			if(txt.match(re)){
				a.prepend((i+1)+".");
				i_zabytku=i;
			}
		}
        }
	
	gmarkers[gmMinInd + i] = new Array();
	gmarkers[gmMinInd + i]['odl']=z[i]['odl'];
	gmarkers[gmMinInd + i]['kier']=z[i]['kier'];
        gmarkers[gmMinInd + i]['visible']=true;
	gmarkers[gmMinInd + i]['rodzaj']=z[i]['rodzaj'];
	gmarkers[gmMinInd + i]['marker']=z[i]['marker'];
	gmarkers[gmMinInd + i]['id']=z[i]['id'];
	if(z[i]['rodzaj']!="zabytek"){
		z[i]['marker'].hide();
	}
    }
    if($("#map").attr("class")!='ab' && i_zabytku>=0)
        window.setTimeout("zclick(i_zabytku,i_zabytku)", 100);


}
////////////////////
      function myclick(e,i) {
    //  alert(i+'j');
    
        i=gmarkers.indexOfID(i);

        //alert(gmarkers[i]['marker']);
       // tmp=document.getElementById("tmp");
       // if(tmp)
       //   tmp.innerHTML = 'distanse: '+(gmarkers[i]['odl']<1?(gmarkers[i]['odl']*1000)+'m':gmarkers[i]['odl']+'km')+'\ndirection: '+gmarkers[i]['kier'];
       if(!gmarkers[i]['visible'])
	return;
	GEvent.trigger(gmarkers[i]['marker'], 'click');
      }
      function hover(i) {
        GEvent.trigger(gmarkers[i], 'mouseover');
      }
      function close(i) {
        GEvent.trigger(gmarkers[i], 'mouseout');
      }
      function zclick(e,i) {
        //document.getElementById("tmp").innerHTML = 'distanse: '+(z[i]['odl']<1?(gmarkers[i]['odl']*1000)+'m':z[i]['odl']+'km')+'\ndirection: '+z[i]['kier'];
        GEvent.trigger(z[i]['marker'], 'click');
      }
//robie formularz
/**
 *
 * @access public
 * @return void
 **/
function make_form(){
if($("#map").attr("class")=='ab') return;
    nazwa=$("#map_parent > h4").eq(0).html();
    nazwa=nazwa.replace(/([^\-]*) - (.*)/,'$1');
    $("#map_parent > h3").append("                                                     \
    <form id ='myform' action='#' method='post' onsubmit='search(this);return false;'> \
         Look for objects in <select name='aa'>                                        \
                              <option>1</option>                                       \
                              <option>2</option>                                       \
                              <option>5</option>                                       \
                              <option>10</option>                                      \
                             </select> km from "+nazwa+"<br>                           \
         <input name='submit' type='submit' value='Search'>                            \
    </form>").prepend("<img src='images/searchform.gif'>").find("img").eq(0).click(function(){
       if($(this).next().is(":visible"))
         $(this).attr("src","images/searchform.gif").next().hide();
       else
         $(this).attr("src","images/searchform-no.gif").next().show("slow")});
}

function pokaz_ukryj(odl){
    switch(odl){
      case 0:
         i=gmarkers.length;
         while(i--) gmarkers[i]['visible']=true;
         i=z.length;
         while(i--) z[i]['visible']=true;
         break;
      default:
         i=gmarkers.length;
         while(i--) gmarkers[i]['visible']=gmarkers[i]['odl']<=odl?true:false;
         i=z.length;
         while(i--) z[i]['visible']=z[i]['odl']<=odl?true:false;
    }
//alert(odl);
    $("#sidebar > b").each(function(i){  $(this).next().find('a').each(function(j){
       //biore id bazowe z id a hrefa i sprawdzam w ktorej tablicy jest
       
      id=$(this).attr('id');
      id=id.replace(/hs(.*)/,'$1');
      ii=z.indexOfID(id);
      if(ii>=0){
        if(z[ii]['visible']) $(this).show();
        else $(this).hide();
      }
      ii=gmarkers.indexOfID(id);
      if(ii>=0){
        if(gmarkers[ii]['visible']){ $(this).show(); gmarkers[ii]['marker'].show();}
        else {$(this).hide();gmarkers[ii]['marker'].hide();}
      }
 //   if(j==0) alert(id+'. '+z.indexOfID(id));
    }); });
    //szczapzmiana
    $("#zabytki_lp > b").each(function(i){  $(this).next().find('a').each(function(j){
       //biore id bazowe z id a hrefa i sprawdzam w ktorej tablicy jest
      id=$(this).attr('id');
      id=id.replace(/hs(.*)/,'$1');
      ii=z.indexOfID(id);
      if(ii>=0){
        if(z[ii]['visible']) $(this).show();
        else $(this).hide();
      }
      ii=gmarkers.indexOfID(id);
      if(ii>=0){
        if(gmarkers[ii]['visible']){ $(this).show(); gmarkers[ii]['marker'].show();}
        else {$(this).hide();gmarkers[ii]['marker'].hide();}
      }
 //   if(j==0) alert(id+'. '+z.indexOfID(id));
    }); });
    //koniec szczapzmiana
    
    
}

function search(f){
    odl=parseInt($("select", $("#myform")).val());
    pokaz_ukryj(odl);
    //alert(f.aa.value+' '+$("select", $("#myform")).val().length);
}

