if (top != self) { top.location = location; }

// Get the current CSS style value for an object
function my_getComputedStyle(obj,style) {
   
   // w3c : Mozilla and Family, Safari, Opera
   if(document.defaultView) {
      value = document.defaultView.getComputedStyle(obj, "").getPropertyValue(style);
   }
   
   // Internet Explorer
   else if(obj.currentStyle) {
      value = obj.currentStyle[toCamelCase(style)];
   }
   
   return value;
}

// Get an camelcase name
function toCamelCase( sInput ) {
    var oStringList = sInput.split('-');
    if(oStringList.length == 1)    
        return oStringList[0];
    var ret = sInput.indexOf("-") == 0 ? 
    	oStringList[0].charAt(0).toUpperCase() + oStringList[0].substring(1) : oStringList[0];
    for(var i = 1, len = oStringList.length; i < len; i++){
        var s = oStringList[i];
        ret += s.charAt(0).toUpperCase() + s.substring(1)
    }
    return ret;
}


// Debug function
function debug(text) {
   if ( !text )
      return
   
   var debug_div = document.getElementById('debug');
   
   if ( !debug_div ) {
      debug_div  = document.createElement("div")
      debug_div.id = 'debug';
      debug_div.style.clear = "both"
      debug_div.className = 'clearfix';
      debug_div.style.textAlign = 'left;'
      document.getElementsByTagName('body').item(0).appendChild(debug_div)
   }
   
   debug_div.appendChild(document.createTextNode(text))
   debug_div.appendChild(document.createElement("br"))
}
   

function checkMouseEnter(element, e) {
  if (element.contains && e.fromElement ) {
    return !element.contains(e.fromElement);
  } else if (e.relatedTarget) {
    return !containsDOM(element, e.relatedTarget);
  }
}

function checkMouseLeave(element, e) {
  if (element.contains && e.toElement) {
    return !element.contains(e.toElement);
  } else if (e.relatedTarget) {
    return !containsDOM(element, e.relatedTarget);
  }
}


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


function addEvent( obj, type, fn )
{
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent)
	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}

function removeEvent( obj, type, fn )
{
	if (obj.removeEventListener)
		obj.removeEventListener( type, fn, false );
	else if (obj.detachEvent)
	{
		obj.detachEvent( "on"+type, obj[type+fn] );
		obj[type+fn] = null;
		obj["e"+type+fn] = null;
	}
}


function centerPopUp( url, name, width, height, scrollbars ) { 

	if( scrollbars == null ) scrollbars = "0" 
 
	str  = ""; 
	str += "resizable=0,"; 
	str += "scrollbars=" + scrollbars + ","; 
	str += "width=" + width + ","; 
	str += "height=" + height + ","; 
 
	if ( window.screen ) { 
		var ah = screen.availHeight - 30; 
		var aw = screen.availWidth - 10; 
 
		var xc = ( aw - width ) / 2; 
		var yc = ( ah - height ) / 2; 
 
		str += ",left=" + xc + ",screenX=" + xc; 
		str += ",top=" + yc + ",screenY=" + yc; 
	} 
	var popup = window.open( url, name, str ); 
	popup.focus();
}



/* -------------------------------------------------------------------------- */
/* 
 *
 * -------------------------------------------------------------------------- */
function check_contact(form) {
// #ff9999
   if (! form_fields) {
      alert("sorry no form fields initialized, we are going to quit")
      return false
   }
   
   var is_error = 0;
   
   for ( var i=0; i<form_fields.length; i++ ) {
      if (  ( $(form_fields[i][1]) == 1 ) && ( $F(form_fields[i][0]) == '' ) ) {
         blink_field(form_fields[i][0]);
         is_error = 1;
      } else {
         $(form_fields[i][0]).style.backgroundColor = 'white';
      }
   }
      
   if ( is_error == 1 ) {
      init_error_message()
      return false;
   } else {
      var wid = document.createElement("input");
      wid.setAttribute("type","hidden");
      wid.setAttribute("name","website_id");
      wid.setAttribute("value",website_id);
      form.appendChild(wid);
      return true;
   }
}
/* -------------------------------------------------------------------------- */


/* -------------------------------------------------------------------------- */
function blink_field(field) {
   $(field).style.backgroundColor = '#ebcacb';
   $(field).onclick = function() { this.style.backgroundColor = 'transparent' };
   new Effect.Appear(field);
}
/* -------------------------------------------------------------------------- */

/* -------------------------------------------------------------------------- */
function init_error_message() {
   var message = 'Onderstaande formulier bevat fouten, controleer deze en probeer het opnieuw!';
   
   $('error_msg').innerHTML = message;
   new Effect.Appear('error_msg');
}
/* -------------------------------------------------------------------------- */


// run a search
function submit_search_form() {
   var query = $('search_query')
   if ( query.value.length == 0 ) {
      new Effect.Shake(query);
      query.style.backgroundColor = '#ebcacb';
      query.onclick = function() { this.style.backgroundColor = '#ffffff' };
      return false;
   }
   return true;
}


// check the login_form on submit
function submit_login_form(form) {
   var lfields = [ $('member_login'), $('member_password') ];
   
   var submit_ok = false
      
   for ( var i=0; i<lfields.length; i++ ) {
      if ( (lfields[i].value.length == 0) || (lfields[i].value == '') ) {
         blink_field(lfields[i])
      } else {
         submit_ok = true
      }
   }
   
   if (!submit_ok) {
      new Effect.Appear('login_form_error');
   }
   
   return submit_ok
}


// select a province from the dropdown (load new window)
function load_province(province_url) {
	if ( !province_url || (province_url == '0') ) {
		return false
	} else {
		window.location = province_url
	}
}

// function highlight apotheek
function highlight_apotheek(id) {
	new Effect.Highlight(id, {startcolor:'#DCDFEF', endcolor:'#ffffff', duration: 3 } )
	return true
}

function initMap(place) {
	if (GBrowserIsCompatible()) {
      map = new GMap2(document.getElementById("map"));
//      map.setCenter(new GLatLng(52.34873,5.5426), 7);
      map.setCenter(new GLatLng(52.30, 5.45),11)
      map.addControl(new GLargeMapControl())
      
      // show the coordinates
      GEvent.addListener(map,'click',function(marker,point) {
	      $('coordinates').innerHTML = "<b>Coordinaten:</b>" + point
      })

      // load the points from xml
		GDownloadUrl("/napco/markers/?place=" + place, function(data, responseCode) {
		  var xml = GXml.parse(data);
		  var markers = xml.documentElement.getElementsByTagName("marker");
		  var lat = 0.0; var lon = 0.0;
		

		
		  for (var i = 0; i < markers.length; i++) {
		    var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
		                            parseFloat(markers[i].getAttribute("lng")));
	       if ( i == 0 ) {
		       map.setCenter(point)
	       }
	       
	       var marker = createMarker(point,$('apotheek_'+parseInt(markers[i].getAttribute("id"))))
		    map.addOverlay(marker);
		  }
		});		
   }
}

// Creates a marker at the given point with the given number label
function createMarker(point, node) {
   // Create a nice Napco Image
	var icon = new GIcon();
	icon.image            = "/images/marker_napco.png"
	icon.shadow           = "http://www.google.com/mapfiles/shadow50.png";
	icon.iconSize         = new GSize(20, 34);
	icon.shadowSize       = new GSize(37, 34);
	icon.iconAnchor       = new GPoint(9, 34);
	icon.infoWindowAnchor = new GPoint(9, 2);
	icon.infoShadowAnchor = new GPoint(18, 25);
	
	var marker = new GMarker(point,icon);

	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindow(markerHTML(node))
	});
   return marker;
}

function GotoGPoint(latitude,longitude,dom_id) {
	window.location = '#top'
	var point = new GLatLng(latitude,longitude)
	map.setCenter(point,16)
	map.panTo(point)
}

function markerHTML(node) {
	var div = node.cloneNode(true)
	div.setAttribute("id","marker_"+node.id)
	// remove the link_to map
	// var span = div.getElementsByTagName("span")[0]
	// span.parentNode.removeChild(span)
	return div
}

function randomMarkers(how_many) {
	
	if ( !how_many ) {
		how_many = 10
	}
	
	var bounds = map.getBounds();
	var southWest = bounds.getSouthWest();
	var northEast = bounds.getNorthEast();
	var lngSpan = northEast.lng() - southWest.lng();
	var latSpan = northEast.lat() - southWest.lat();
	for (var i = 0; i < parseInt(how_many); i++) {
	   var point = new GLatLng(southWest.lat() + latSpan * Math.random(),
                              southWest.lng() + lngSpan * Math.random());
	   map.addOverlay(new GMarker(point));
   }
}