//==================
// GLOBAL VARIABLES
//==================


//=================================================
// IMPORTANT!!! ON DOCUMENT.READY --> FINALIZEPAGE
//=================================================
function finalizePage(){
    enableScriptwiseBlocks();
    //createSearchFacetsMoreBlocks();
    synchronizeVerticalDimensions();
}

//===========================================
// CHECK HEIGHTS ON CHANGE WEBSITE DIMENSION
//===========================================
function synchronizeVerticalDimensions(){
    var heightContent = $("#content").height();
    var heightLeftnav = $("#content-nav").height();

    if (heightContent > heightLeftnav){
        $("#content-nav").height(heightContent + "px");
    }
}

function enableScriptwiseBlocks(){
    if ($.browser.msie && $.browser.version.substr(0,1)<7)
        $('table.scriptwise').css('display', 'block');
    else
        $('table.scriptwise').css('display', 'table');
}

//===============================================================
// ONCLICK EVENT THAT REMOVES THE DEFAULT KEYWORD OF INPUTFIELDS
//===============================================================
function cleanUpKeyword(oObject){
	oObject.value = '';
}

//===============================================================
// ONCLICK EVENT THAT REMOVES THE DEFAULT KEYWORD OF INPUTFIELDS
//===============================================================
function cleanUpDefaultKeyword(oObject, sKeyword){
	if(oObject.value == sKeyword){
		oObject.value = '';
	}
}

//========================================================
// CREATE SEARCH FACETS MORE MORE BLOCKS: LEFTNAV TOGGLER
//========================================================
function createSearchFacetsMoreBlocks(){
	/*
    $('#content-nav span.searchfacets-toggler').toggle (
        function() {
            $(this).removeClass('open');
            $(this).addClass('close');
            $(this).next().slideUp('normal');
        },
        function() {
            $(this).removeClass('close');
            $(this).addClass('open');
            $(this).next().slideDown('normal');
        }
    )
	*/
}

//====================================================================================================
// ONCLICK EVENT THAT REMOVES THE DEFAULT KEYWORD OF THE INPUT FIELD AND SUBMITS THE SURROUNDING FORM
//====================================================================================================
function cleanUpInputFieldAndSubmit(elementId, formObject){
    document.getElementById(elementId).value = '';
    formSubmit(formObject);
}

function removeFacet(oObject, url){
    $(oObject).parent().fadeOut(700);
    setTimeout(function(){redirect(url);}, 1000);
}

//=============
// SUBMIT FORM
//=============
function formSubmit(formObject){
    formObject.submit();
}

//============================
// OPEN GOOGLE MAP IN OVERLAY
//============================
function openGoogleOverlay(sTitle, sAddress, sCoordinateLat, sCoordinateLong){
    Shadowbox.open({
        content:    '<div id="googleMapOverlay"><div class="two-col-layout clearfix"><div class="column1"><div id="map_canvas_big"></div></div><div class="column2">' + sTitle +  '</div></div></div>',
        player:     "html",
        height:     520,
        width:      740,
        options: {
            overlayOpacity:     0.7,
            onFinish:           function () {initialize(sAddress, sCoordinateLat, sCoordinateLong, 15, 'true', 'true', 'true', 'map_canvas_big');
        }}
    });
	$('#sb-title').after($('#sb-info'));
}

//====================
// Doorklik Navigatie
//====================

function setHover(field){
	var sClass = field.className
	if(sClass.search('hovered') < 1){
		field.className = sClass + ' hovered';
	}
}
function releaseHover(field){
	var sClass = field.className
	if(sClass.search('hovered') > 0){
		field.className = sClass.replace(' hovered', '');
	}

}
function redirect(url){
	if (url.match("http")) {
		window.open(url);
	}
	else {

		window.location = url;
	}
}
function opennew(url){
	window.open(url);
}

//==================
// GOOGLE MAPS CODE
//==================
var map = null;
var geocoder = null;
var gdir;
var geocoder = null;
var addressMarker;

function initialize(address, coordinateLat, coordinateLong, zoom, bGMapTypeControl, bGSmallMapControl, bGOverviewMapControl, htmlControl) {
    if (GBrowserIsCompatible()) {
      	if (zoom == null || zoom == '')
            zoom = 14;

      	if(coordinateLat != null && coordinateLat != '' && coordinateLong != null && coordinateLong != '')
            address = coordinateLat + ',' + coordinateLong;

      	map = new GMap2(document.getElementById(htmlControl));
      	map.setCenter(new GLatLng(50.879, 4.700), zoom);

      	if(bGMapTypeControl == 'true')
            map.addControl(new GMapTypeControl());
      	if(bGSmallMapControl == 'true')
            map.addControl(new GSmallMapControl());
      	if(bGOverviewMapControl == 'true')
            map.addControl(new GOverviewMapControl());

        if(address != null && address != ''){
            geocoder = new GClientGeocoder();
            geocoder.getLatLng(address,
                function(point) {
                    if (!point) {
                        alert(address + ' not found');
                    } else {
                        map.setCenter(point, zoom);
                        var marker = new GMarker(point);
                        map.addOverlay(marker);
                    }
                }
            );
        }
        gdir = new GDirections(map, document.getElementById("directions"));
        GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);
    }
}

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_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
	//alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\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(){}

//==================
// GET URL PARAMETER
//==================
function gup(name)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}