 var markerHTML;

function goto(url)
{
  window.location = url;
} 
 
$(document).ready(function() {
  replaceText();

  $('ul#jsddm li ul').each(function() {
		$(this).mouseover(function() {
			var imgsrc = $(this).parent().find('img').attr('src');
			$(this).parent().find('img').attr('src', imgsrc.replace('buttonInactive', 'buttonActive_submenu'))
		});

		$(this).mouseout(function() {
			var imgsrc = $(this).parent().find('img').attr('src');
			$(this).parent().find('img').attr('src', imgsrc.replace('buttonActive_submenu', 'buttonInactive'))
		});
  });

//   $('ul#jsddm li ul li').each(function() {
// 		$(this).mouseover(function() {
// 			var imgsrc = $(this).parent().parent().find('img').attr('src');
// 			$(this).parent().parent().find('img').attr('src', imgsrc.replace('buttonInactive', 'buttonActive_submenu'))
// 		});

// 		$(this).mouseout(function() {
// 			var imgsrc = $(this).parent().parent().find('img').attr('src');
// 			$(this).parent().parent().find('img').attr('src', imgsrc.replace('buttonActive_submenu', 'buttonInactive'))
// 		});
//   });

//   $('ul#jsddm li ul li a').each(function() {
// 		$(this).mouseover(function() {
// 			var imgsrc = $(this).parent().parent().parent().find('img').attr('src');
// 			$(this).parent().parent().parent().find('img').attr('src', imgsrc.replace('buttonInactive', 'buttonActive_submenu'))
// 		});

// 		$(this).mouseout(function() {
// 			var imgsrc = $(this).parent().parent().parent().find('img').attr('src');
// 			$(this).parent().parent().parent().find('img').attr('src', imgsrc.replace('buttonActive_submenu', 'buttonInactive'))
// 		});
//   });

 
  //load_flash();

	  $('ul#gallerij').innerfade({
	    speed: 1000,
	    timeout: 5000,
	    type: 'sequence',
	    containerheight: '435px'
  });

   //$("img.nav").mouseover(function(){
				//console.log("mouseover");
				//console.log($(this).attr('id'));
				//showSubNav($(this).attr('id'));
    //}).mouseout(function(){
			//console.log("mouseout");
   // });

		var timeout         = 0;
		var closetimer		= 0;
		var ddmenuitem      = 0;

		function jsddm_open()
		{	jsddm_canceltimer();
		jsddm_close();
		ddmenuitem = $(this).find('ul').eq(0).css('visibility', 'visible');}

		function jsddm_close()
		{	if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');}

		function jsddm_timer()
		{	closetimer = window.setTimeout(jsddm_close, timeout);}

		function jsddm_canceltimer()
		{	if(closetimer)
		{	window.clearTimeout(closetimer);
		closetimer = null;}}

		$(document).ready(function()
		{	$('#jsddm > li').bind('mouseover', jsddm_open);
		$('#jsddm > li').bind('mouseout',  jsddm_timer);});

		document.onclick = jsddm_close;

});

$(document).unload(function() {
  GUnload();
});

function showSubNav(elem_id) {
	var $innerHTML = "<div style=\"width: 200px; height: 400px; background: lime;\"></div>";
}

function replaceText()
{

  //replace buttons
  $('#splash_menu ul li a').each(function() {
    var text = escape($(this).text());
    $(this).html('<img src="images/gd/image.php?type=button&text='+urlencode(text)+'" />');
  });

  /*
  //replace page title
  $('#page_titel h1').each(function() {
    var text = escape($(this).text());
    $(this).html('<img src="images/gd/image.php?type=titel&text='+urlencode(text)+'" />');
  });
  */

  //replace page title

	$('#title_container h1').each(function() {
    var text = escape($(this).text());
    $(this).html('<img src="images/gd/image.php?type=titel&text='+urlencode(text)+'" />');
		//$(this).html('<img src="images/gd/image.php?type=eventheader&text='+urlencode(text)+'" />');
  });


	  $('#event_header h2').each(function() {
	    var text = escape($(this).text());
	    $(this).html('<img src="images/gd/image.php?type=eventheader&text='+urlencode(text)+'" />');
  });


	 $('#contact_container h2').each(function() {
	    var text = escape($(this).text());
	    $(this).html('<img src="images/gd/image.php?type=subtitle&text='+urlencode(text)+'" />');
  });

	 $('#contact_container h3').each(function() {
	    var text = escape($(this).text());
	    $(this).html('<img src="images/gd/image.php?type=subtitle_red&text='+urlencode(text)+'" />');
  });

	$('.event_title').each(function() {
	    var text = escape($(this).text());
	    $(this).html('<img src="images/gd/image.php?type=event_title&text='+urlencode(text)+'" />');
  });

	$('.event_label').each(function() {
	    var text = escape($(this).text());
	    $(this).html('<img src="images/gd/image.php?type=event_subtitle_grey&text='+urlencode(text)+'" />');
  });

	$('.event_text').each(function() {
	    var text = escape($(this).text());
	    $(this).html('<img src="images/gd/image.php?type=event_subtitle_red&text='+urlencode(text)+'" />');
  });


	var teller = 0;
  overs = new Array();
  outs = new Array();
  submenu_overs = new Array();


  $('.navlink').each(function() {
	    var text = escape($(this).text());

	    if($(this).attr('class') == "navlink submenu navItemActive" || $(this).attr('class') == "navlink navItemActive")
	    {
	      $(this).html('<img class="nav" src="images/gd/image.php?type=buttonActive&text='+urlencode(text)+'" id="button' + teller + '" />');
        $(this).css('margin-left', '-5px');
	      $(this).css('margin-right', '-5px');
        
         $(this).hover(
          function() {
           document.getElementById($(this).find('img').attr('id')).src = "images/gd/image.php?type=buttonActive2&text=" +urlencode(text);
          },
          function() {
           document.getElementById($(this).find('img').attr('id')).src = "images/gd/image.php?type=buttonActive&text=" +urlencode(text);
          }
        );          
	    }
	    else
	    {
    
	      overs[teller] = new Image();
	      overs[teller].src= 'images/gd/image.php?type=buttonActive&text=' + urlencode(text);

	      outs[teller] = new Image();
	      outs[teller].src= 'images/gd/image.php?type=buttonInactive&text=' + urlencode(text);

	      submenu_overs[teller] = new Image();
	      submenu_overs[teller].src= 'images/gd/image.php?type=buttonActive_submenu&text=' + urlencode(text);

	      $(this).html('<img class="nav" src="images/gd/image.php?type=buttonInactive&text='+urlencode(text)+'" id="button' + teller + '" />');
	      $(this).css('margin-left', '-5px');
	      $(this).css('margin-right', '-5px');

	      $(this).hover(function() {
	          var id = $(this).find('img').attr('id').replace('button','');
	          var clss = $(this).attr('class').indexOf('submenu');
	          if(clss >=0)
	          {
		          document.getElementById($(this).find('img').attr('id')).src = submenu_overs[id].src;
	          }
	          else
	          {
            	document.getElementById($(this).find('img').attr('id')).src = overs[id].src;
          	}

            /*
            if($(this).attr('class') == "navlink submenu navItemActive" || $(this).attr('class') == "navlink navItemActive")
            {
              $(this).html('<img class="nav" src="images/gd/image.php?type=buttonActive&text='+urlencode(text)+'" id="button' + teller + '" />');
              $(this).css('margin-left', '-5px');
              $(this).css('margin-right', '-5px');
              
               $(this).hover(
                function() {
                  $(this).html('<img class="nav" src="images/gd/image.php?type=buttonActive2&text='+urlencode(text)+'" id="button' + teller + '" />');
               }
              );
            } 
            */            

	        },
	        function() {
            var id = $(this).find('img').attr('id').replace('button','');
	          document.getElementById($(this).find('img').attr('id')).src = outs[id].src;
	        });
	    }

				if(teller > 0)
	      {
	    	 // $(this).css('margin-left', '-4px');
				//	$(this).css('margin-right', '-4px');
	      }else{
				//	$(this).css('margin-right', '-4px');
				}

	    teller++;

  });



}

function load_google_map() {

  var google_div = document.getElementById('google_map');

  if(google_div != null)
  {
    if (GBrowserIsCompatible())
    {
      var map = new GMap2(document.getElementById("google_map"));
      map.addControl(new GSmallMapControl());
      map.setCenter(new GLatLng(latitude, longitude), 12);
      var point = new GPoint(longitude, latitude);
      map.addOverlay(new GMarker(point));
        var marker = new GMarker(point);
      map.addOverlay(marker);
      marker.openInfoWindowHtml(markerHTML);
    }
  }
}

function load_flash()
{
  var flash_div = document.getElementById('flash_div');

  if(flash_div != null)
  {
    var so = new SWFObject("flash/home.swf", "", "962", "345", "8", "#FFF");
    so.addParam("wmode", "transparent");
    so.write("flash_div");
  }
}

function isValidEmailAddress(emailAddress)
{
  var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
  return pattern.test(emailAddress);
}

function urlencode( str ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir
    // %          note: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
    // *     example 1: urlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin+van+Zonneveld%21'
    // *     example 2: urlencode('http://kevin.vanzonneveld.net/');
    // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
    // *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
    // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'

    var histogram = {}, tmp_arr = [];
    var ret = str.toString();

    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };

    // The histogram is identical to the one in urldecode.
    histogram["'"]   = '%27';
    histogram['(']   = '%28';
    histogram[')']   = '%29';
    histogram['*']   = '%2A';
    histogram['~']   = '%7E';
    histogram['!']   = '%21';
    histogram['%20'] = '+';

    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);

    for (search in histogram) {
        replace = histogram[search];
        ret = replacer(search, replace, ret) // Custom replace. No regexing
    }

    // Uppercase for full PHP compatibility
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });

    return ret;
}
