// ------------- global objects ----------------

/** @constructor
 * @param x the x coordinate
 * @param y the y coordinate
 */
function Coordinate(x,y){
    this.x = x;
    this.y = y;
}

// ------------- global functions ---------------------

window.onload=init;

var clearButton = '<input type="button" value="Clear" onclick="initDebug();" /><br>';

var searchFeatureSelected = '';

var enableDebug = false;

function initDebug(){
  if(enableDebug){
    var d = document.getElementById('debug');
    d.innerHTML = clearButton;
    show('debug');
  }
}

/** Append the specified text to the innerHTML of element with id debug if 
 * flag enableDebug is set to true.
 * @param txt the text to append
 */
function debug(txt){
  if(enableDebug){
    var d = document.getElementById('debug');
    d.innerHTML += txt+"<br>";
  }
}

/** Assigns functions to the map's event handlers
 */
function init(){
    
   var dmap = document.getElementById('dmap');
   dmap.onmousedown = Map_onMouseDown;
   dmap.onmousemove = Map_onMouseMove;
   dmap.onmouseup   = Map_onMouseUp;   
   
   var menu = document.getElementById('menu_dragHook');
   menu.onmousedown = menu_startDrag;   
   menu.onmouseup   = menu_stopDrag;

   hide('progress');
      
   debug("Initialized");
}

/** Determine if some LayerName's name in first array exists in 
 * second array.
 * @param arr1 array of LayerName objects
 * @param arr2 array of LayerName objects
 * @return true if one name is in common, false otherwise
 */
function commonLayerNameInArrays(arr1, arr2){
    for(var i = 0; i < arr1.length; i++){
        var v1 = arr1[i];
        if( strInLayerNameArray( v1.name, arr2 ) ){
            return true;
        }
    }
    return false;
}

/** Determine if a layername exists in array of LayerName objects
 * @param name name of layer to look for as string
 * @param arr array with LayerName objects
 * @return true if name exists in array, false otherwise
 */
function strInLayerNameArray(name, arr){   
    for(var i = 0; i < arr.length; i++){        
        if( name == arr[i].name )
            return true;
    }
    return false;
}

/** Compare function for layergroups that 
 * compares the z-index value.
 * @param a first layergroup
 * @param b second layergroup
 */
function compareLayerGroup(a, b) {
   return b.zIndex - a.zIndex;
}

/** Compare function for layers. Compared values are the
 * order attribute.
 * @param a first layer
 * @param b second layer
 */
function compareLayer(a, b) {
   return b.order - a.order;
}

/** Format a integer value to a string that has a space char after 
 * every third char.
 * @param value the integer to format
 * @return a string
 */
function formatNumber(value){
    var asStr = new String(value);
    var chunkSize = 3;
    var currPos = chunkSize;
    var orgLen = asStr.length;
    var res = '';
    while( currPos < orgLen){
        res = ' ' + asStr.substring(asStr.length - chunkSize , asStr.length)+res;
        asStr = asStr.substring(0, asStr.length - chunkSize);
        currPos = currPos + chunkSize;
    }
    if(asStr.length>0){
        res = asStr + res;
    }
    return res;
}       
    
/** First set border to white (if not specified) for all a-tags and then 
 * set border to red for a-tag with id prefix+id.
 * @param prefix
 * @param id
 * @param clearColor color to clear all tags with, if not spec the default is white
 */
function mark(prefix, id, clearColor){
  if( !clearColor )
    var clearColor = 'white'
    
  clearMarks(prefix, 'dummy', clearColor);
  
  var tag = document.getElementById(prefix+id); 
  tag.style.border = '1px solid red'; 
}

/** Toggles the border between a red and a white for
 * a specified element.
 * @param tagId the id of element to toggle
 */
function toggleMark(tagId){  
  var tag = document.getElementById(tagId);  
  if( tag.style.border.indexOf('red') != -1 ){
    tag.style.border = '1px solid white'; 
  }
  else{   
    tag.style.border = '1px solid red'; 
  }  
}

/** Set border color for all a-tags that has an id that starts with
 * prefix and don't equals value of doNotClear parameter.
 * @param prefix start of id value 
 * @param doNotClear id of tag that is ignored 
 * @param clearColor the border color
 */
function clearMarks(prefix, doNotClear, clearColor){
  var atags = document.getElementsByTagName('a');
  for(i=0;i<atags.length;i++){
    var atag = atags[i];
    if( atag.id.indexOf( prefix ) == 0 && atag.id != doNotClear){
        atag.style.border = '1px solid '+clearColor;
    }
  }
}

/**
 * Shows an element, ie set its style.visibility attribute to visible
 * @param tagID the id value for the tag to show
 */
function show(tagID){
  var div = document.getElementById(tagID);
  if(div)
    div.style.visibility = 'visible';
}

/**
 * Hides an element, ie set its style.visibility attribute to hidden
 * @param tagID the id value for the tag to hide
 */
function hide(tagID){   
  var div = document.getElementById(tagID);
  if(div)
    div.style.visibility = 'hidden';
}

/**
 * Toggle visibility for a tag, ie set the tag.style.visibility
 * attribute to hidden if it is visible and vice versa
 * @param tag the tag to toggle
 */
function toggleVisibility(tag){
    if(tag.style.visibility == 'visible')
        tag.style.visibility = 'hidden';
    else
        tag.style.visibility = 'visible';
}

function show_props(obj, objName) {
   var result = "";
   for (var i in obj) {
      //if (i.indexOf('border') > -1)
      result += objName + "." + i + " = " + obj[i] + "\n";
   }
   return result;
}

function arrToStr(arr){
    var s = '';
    for(var i = 0; i < arr.length; i++){
        s += arr[i].name + ', ';
    }
    return s;
}