

var map, timer;
var chosen = [];
 
/* Array of GLayers
 * The 'name' label is not being used yet
*/
var layers = [
 { name: "Pano", obj: new GLayer("com.panoramio.all") },
 { name: "Wiki", obj: new GLayer("org.wikipedia.fr") }
];


function hideAll() {

 var boxes = document.getElementsByName("mark");

 for(var i = 0; i < boxes.length; i++) {
  if(boxes[i].checked) {
   boxes[i].checked = false;
   switchLayer(false, layers[i].obj);
   chosen.push(i);
  }
 }
}


function checkChecked() {

 /* Returns true if a checkbox is still checked
 *  otherwise false
 */
 var boxes = document.getElementsByName("mark");
 for(var i = 0; i < boxes.length; i++) {
  if(boxes[i].checked) return true;
 }
 return false;
}


function switchLayer(checked, layer) {

 /* Function was originally borrowed from Esa:
 *  http://esa.ilmari.googlepages.com/dropdownmenu.htm
 */
 var box = document.getElementById("box");
 var boxlink = document.getElementById("boxlink");
 var button = document.getElementById("more_button");

 if(checked) {
   map.addOverlay(layer);
   // Reset chosen array
   chosen.length = 0;
   /* Highlight the link and
   *  make the button font bold.
   */
   boxlink.className ="highlight";
   box.className ="highlight";
   button.className ="highlight";
 }
 else {
   map.removeOverlay(layer);
   /*  Reset the link and the button
    * if all checkboxes were unchecked.
   */
   if(!checkChecked()) {
    boxlink.blur();
    boxlink.className ="";
    box.className ="";
    button.className ="";
   }
 }
}


function showBox() {

 if(window.timer) clearTimeout(timer);
 document.getElementById("box").style.display ="block";
}


function hideBox(element) {

 element.style.display = "none";
}


function setClose(e) {

 if(!e) e = window.event;
 var element = document.getElementById("box");

 if(checkMouseLeave(element, e))
  timer = setTimeout("hideBox(document.getElementById('box'))", 400);
}


function checkMouseLeave(element, evt) {

 /* Avoid firing a mouseout event
 *  when the mouse moves over a child element.
 *  Borrowed from:
 *  http://www.faqts.com/knowledge_base/view.phtml/aid/1606/fid/145
 */
 if(element.contains && evt.toElement) {
   return !element.contains(evt.toElement);
 }
 else if(evt.relatedTarget) {
   return !containsDOM(element, evt.relatedTarget);
 }
}


function containsDOM(container, containee) {

 var isParent = false;
 do {
  if((isParent = container == containee))
   break;
   containee = containee.parentNode;
 }
 while(containee != null);
 return isParent;
}


function MoreControl(){};
MoreControl.prototype = new GControl();
MoreControl.prototype.initialize = function(map) {

 var more = document.createElement("div");
 more.id = "more_button";
 this.assignClickEvent_(more);
 more.title= "More Layers";
 more.appendChild(document.createTextNode("More..."));
 more.onmouseover = showBox;
 more.onmouseout = setClose;
 map.getContainer().appendChild(more);
 return more;
}

MoreControl.prototype.assignClickEvent_ = function(button) {

 GEvent.addDomListener(button, "click", function() {

  if(chosen.length > 0 ) {

    /* Make an independent copy of chosen array since switchLayer()
    *  resets the chosen array, which may not be useful here.
    */
    var copy = chosen.slice();

    for(var i = 0; i < copy.length; i++) {
     var index = parseInt(copy[i]);
     switchLayer(true, layers[index].obj);
     document.getElementsByName("mark")[index].checked = true;
    }
  }
  else {
    hideAll();
  }
 });
}
MoreControl.prototype.getDefaultPosition = function() {
 return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(200, 7));
}


function BoxControl(){};
BoxControl.prototype = new GControl();
BoxControl.prototype.initialize = function(map) {
 var box = document.getElementById("box");
 box.onmouseout = setClose;
 map.getContainer().appendChild(box);
 return box;
}
BoxControl.prototype.getDefaultPosition = function() {
 return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(167, 25));
}


function showAddress(address) {

 var geocoder = new GClientGeocoder();
 geocoder.getLatLng(address, function(point) {

  if(!point) {
   alert(address + " not found.");
  }
  else {
   map.setCenter(point, 14);
  }
 });
}

//]]>
