
// google map default variables
// http://maps.google.com/maps?f=q&source=s_q&hl=cs&geocode=&q=asko+nabytek+praha&sll=48.625691,21.363629&sspn=1.287121,3.570557&ie=UTF8&ll=50.054466,14.334733&spn=0.009052,0.027895&t=h&z=16
var centerLatitude = 48.5788; //50.054466;
var centerLongitude = 19.1319;
var startZoom = 7; //13;
var map;

var markersData; //global variable for all the markers

//create array of areas

//icons.items[20] = new GIcon();
//icons.items[20].image = "images/";
//icons.items[20].iconSize = new GSize(20, 34);
//icons.items[20].iconAnchor = new GPoint(10, 34);
//icons.items[20].infoWindowAnchor = new GPoint(22, 1);


//function ShowTable(data) {
//    return 'a';
//}
////FUNCTIONS============================================================================
//function getInfo(data) {
//
//  var str = '<div class="t_info">';
//
//  if (data.name != null && data.name.length > 0) str += '<div class="t_info_name">'+data.name+'</div>';
//
//
//  if (data.photo != null && data.photo.length >  0) str += '<div class="t_info_photo"><img src="images/'+data.photo+'"></div>';
//
//  if (data.summary != null && data.summary.length > 0) str += '<div class="t_info_cap">'+data.summary+'</div>';
//
//if (data.link != null && data.link.length > 0) str += '<div class="t_info_link"><a href="'+data.link+'" target="_blank"><img src="adv/link.gif" height="20" width="80" alt="link" /></a> </div>';
//  str += '</div>';
//  return str;
//}
//
//
//function getListItem(data) {
//
//  var str = '' // <div class="list">';
//
//  if (data.name != null && data.name.length > 0) str += '<div class="list_name">'+data.name+'</div>';
//
//
//  //str += '</div>';
//  return str;
//}
//

function getHhtmlInfo(data){
    html = '<div class="t_desc">';
    html +='<strong>'+data.name+'</strong><br />';
    html += data.description;
    html += '</div>';
    return html;
    //        '<div class="t_desc">'++'</div>'

}

function addMarker(data) {
    //alert('volam add marker');
  // Our info window content
  //var infoTabs = [ new GInfoWindowTab("info", '<div class="t_desc">'+data.description+'</div>') ];

//
//  //add item into list of records right to the map
//  //$('listobjects').innerHTML += getListItem(data);
//$('listobjects').innerHTML += '<div class="list_name">'+data.name+'</div>';
//
  var newdiv = document.createElement('span');
  newdiv.setAttribute('className', 'list');
  newdiv.setAttribute('class', 'list');
  newdiv.setAttribute('id', 'object'+data.id);
  //newdiv.className = 'object'+data.id;
  //newdiv.innerHTML += '<div class="list_name">'+data.name+'</div>';
  newdiv.innerHTML += data.name + ' ';
  document.getElementById("listobjects").appendChild(newdiv);
//
//
  //add marker into a map
  //var type = data.type;
  //var tmp_ico = icons.items[type];
   var icon = new GIcon();
    icon.image = "/imgs/google_maps_marker.png";
    icon.shadow = "/imgs/shadow-google_maps_marker.png";
    icon.iconSize = new GSize(50.0, 50.0);
    icon.shadowSize = new GSize(76.0, 50.0);
    icon.iconAnchor = new GPoint(50.0, 25.0);    
    icon.infoWindowAnchor = new GPoint(50.0, 0.0);

  var marker = new GMarker(new GLatLng(data.latitude, data.longitude) , {icon: icon} /*, {draggable: true}*/ );

  //alert('new marker');
  
  //marker.openInfoWindowHtml(intoTab);

  //alert( intoTab );
  GEvent.addListener(marker, 'click',
  function() {      
      map.setCenter(new GLatLng(data.latitude, data.longitude), parseInt(data.zoom) , G_HYBRID_MAP);
      //map.setZoom(data.zoom);
      //alert(data.zoom);
      intoTab = getHhtmlInfo(data);
      marker.openInfoWindowHtml(intoTab);
      }
  );
  //alert('ze pridavam marker?!');
  map.addOverlay(marker);
  Event.observe('object'+data.id, 'click', function() { 
      map.setCenter(new GLatLng(data.latitude, data.longitude), parseInt(data.zoom) , G_HYBRID_MAP);
      intoTab = getHhtmlInfo(data);
      marker.openInfoWindowHtml(intoTab);
    }
  );
}
//
//
//
//  //newdiv.onclick = marker.openInfoWindowTabsHtml(infoTabs);

//  Event.observe('object'+data.id, 'mouseover', function() {
//      marker.setImage('http://www.bicon2007.org.uk/static/pics/marker_34_yellow.png');}
//    );
//  Event.observe('object'+data.id, 'mouseout', function() {
//      marker.setImage(icons.items[type].image); }
//    );
//
//  // set listener to open "bubble" over the marker after clicking
//  GEvent.addListener(marker, 'click',
//  function() {
//      marker.openInfoWindowTabsHtml(infoTabs);
//      }
//  );
//
//
//  map.addOverlay(marker);
//}
//
//function showNumRecords(num) {
//  $('numrecords').innerHTML = 'Počet nájdených objektov: <b>'+num+'</b>';
//}
//
//
function getResponse(oReq, oJSN) {

    var data = eval(oReq.responseText);

    for (var i = 0; i < data.length; i++) {
    	//alert(data[i].name);
        addMarker(data[i]);
        //
    }
}
//
//
function getMarkers() {
        //$('loader').show();
      	map.clearOverlays();
      	var markers = new Object;

        var url = 'js/maps/data.json?d=20100803';

        //$('listobjects').innerHTML = '';

        //$('debug').innerHTML = pars+'<br />';
      	// notice the use of a proxy to circumvent the Same Origin Policy.
        //alert('Idem volat AJAX');
      	new Ajax.Request(url, {
      	    method: 'get',
      	    onSuccess: getResponse
        } );

}

function init() {
if (GBrowserIsCompatible()) {

    map = new GMap2(document.getElementById("map"));
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom, G_HYBRID_MAP);

    //show markers for default area and types after loading the script
    getMarkers();
  }
}

window.onload = init;
window.onunload = GUnload;


