var minHeight = 620;
var map;
var gdir;
var geocoder = null;
var addressMarker;

$(document).ready(function() {
	
	initRollovers();
	setDivHeight('', minHeight);
	
 	$('#slideshow').innerfade({
		speed: 'slow',
		timeout: 3500,
		type: 'random',
		containerheight: '198px' 
	}); 

	$(function(){
		$('div.columnHome').bind('hover',function(){
			$('body').css('cursor', 'pointer');
			$(this).children("div").animate({"height": "300px"}, "slow");
		})
		.bind('hoverend',function(){
	    	$('body').css('cursor', 'default');
	    	$(this).children("div").animate({"height": "45px"}, "slow");
		});
	});

 	$('#banner-slideshow').innerfade({
		speed: 'slow',
		timeout: 3500,
		type: 'random',
		containerheight: '175px'
	});
		
});

function initialize() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map_canvas"));
    gdir = new GDirections(map, document.getElementById("directions"));
    GEvent.addListener(gdir, "load", onGDirectionsLoad);
    GEvent.addListener(gdir, "error", handleErrors);

    setDirections("De Waterlaat 2, bergeijk", "De Waterlaat 2, bergeijk", "nl_NL");
  }
}

function setDirections(fromAddress, toAddress, locale) {
  gdir.load("from: " + fromAddress + " to: " + toAddress,
            { "locale": locale });

}

function handleErrors(){
 if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
   alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
 else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
   alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);

 else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
   alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);

 else if (gdir.getStatus().code == G_GEO_BAD_KEY)
   alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

 else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
   alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);

 else alert("An unknown error occurred.");

}

function onGDirectionsLoad(){
  // Use this function to access information about the latest load()
  // results.

  // e.g.
  // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
// and yada yada yada...

}	

/**************************************************************

	Set a div to a height if smaller... Or to page height or to a
	static height, depending on parameters and content of said divs.

	Author : Tom van Gemert
	For: Trimedia Interactive Projects
	http://www.trimedia.nl
	Added to project: 20051207

	Script based on: http://www.saila.com/attic/sandbox/set-height.html

**************************************************************/

function compareNum(a,b) {
	return a-b
}


function pageHeight() {

  var myWidth = 0, myHeight = 0;

  if( typeof( window.innerWidth ) == 'number' ) {
	//Non-IE
	myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	//IE 6+ in 'standards compliant mode'
	myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	//IE 4 compatible
	myHeight = document.body.clientHeight;
  }
  //window.alert( 'Height = ' + myHeight );
  return myHeight;

}

function setDivHeight(staticHeight, minHeight) {

	var contentHeight;
	pageHeight = pageHeight();
	d = document;

	if (d.getElementById('content') != null) 
	{
		contentHeight = document.getElementById('content').offsetHeight;
	}
	
	if (contentHeight > 284) 
	{
		box = new Array("container","container_image","content","content_bg");
		boxTopSpace = new Array(64,64,244,244);
	}
	else
	{
		box = new Array("container","container_image");
		boxTopSpace = new Array(64,64);	
	}
	
	boxLength = box.length;
	boxHeights = new Array(boxLength);

	for(x = 0; x < boxLength; x++) {

		boxname = box[x];

		if (d.getElementById(box[x]) != null) {
	  		h = d.getElementById(box[x]).offsetHeight + boxTopSpace[x];
	  		boxHeights[x] = h;
	  	} else {
	  		boxHeights[x] = 0;
	  	}

	 }

	boxHeights.sort(compareNum);
	tallest = boxHeights[(boxLength-1)];
	//alert("min_height: " + minHeight + " tallest_h: " + tallest + " page_height: " + pageHeight);

	if (staticHeight != "") {

		for(x = 0; x < boxLength; x++) {
		  if (d.getElementById(box[x]) != null) {
		  	d.getElementById(box[x]).style.height = staticHeight - boxTopSpace[x] + "px";
		  }
		}

	} else {

		if (tallest < minHeight) {

			for(x = 0; x < boxLength; x++) {
			  if (d.getElementById(box[x]) != null) {
			  	d.getElementById(box[x]).style.height = minHeight - boxTopSpace[x] + "px";
			  }
			}

		} else {

			//alert("resizing to page_height: " + page_height);

			if (tallest < pageHeight) {

				for(x = 0; x < boxLength; x++) {
					if (d.getElementById(box[x]) != null) {
						d.getElementById(box[x]).style.height = pageHeight - boxTopSpace[x] + "px";

					}
				}

			} else {

				for(x = 0; x < boxLength; x++) {
					if (d.getElementById(box[x]) != null) {
						d.getElementById(box[x]).style.height = tallest - boxTopSpace[x] + "px";
					}

				}

			}

		}

	}

}

/**************************************************************
Standards Compliant image Rollover Script
Author : Daniel Nolan
http://www.bleedingego.co.uk/webdev.php
**************************************************************/

function initRollovers() {

	if (!document.getElementById) return

	var aPreLoad = new Array();
	var sTempSrc;
	var aImages = document.getElementsByTagName('img');

	for (var i = 0; i < aImages.length; i++) {

		if (aImages[i].className == 'imgover') {

			var src = aImages[i].getAttribute('src');
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
			var hsrc = src.replace(ftype, '_on'+ftype);

			aImages[i].setAttribute('hsrc', hsrc);

			aPreLoad[i] = new Image();
			aPreLoad[i].src = hsrc;

			aImages[i].onmouseover = function() {
				sTempSrc = this.getAttribute('src');
				this.setAttribute('src', this.getAttribute('hsrc'));
			}

			aImages[i].onmouseout = function() {
				if (!sTempSrc) sTempSrc = this.getAttribute('src').replace('_on'+ftype, ftype);
				this.setAttribute('src', sTempSrc);
			}

		}

	}

}
