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

$(document).ready(function() {

  $('div#nav ul li').bind('mouseover', jsddm_open);
  $('div#koksgroup ul li').bind('mouseover', jsddm_open);

  $('div#nav ul li').bind('mouseout', jsddm_timer_close);
  $('div#koksgroup ul li').bind('mouseout', jsddm_timer_close);

  replaceText();

  $('a[rel=_blank]').each(function() {
    $(this).attr('target','_blank');
  });

  load_google_map();
});

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

function jsddm_open()
{
  jsddm_canceltimer();
  jsddm_close();
  ddmenuitem = $(this).find('ul').css('visibility', 'visible');
  if($(this).attr('class').search('mainbuttonli') >= 0)
  {
    $(this).addClass('mainbuttonover');
  }
}

function jsddm_close()
{

  if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');
  $('.mainbuttonli').each(function() {

    $(this).removeClass('mainbuttonover');

  });
}

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

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

function jsddm_timer_close()
{
  jsddm_canceltimer();
  jsddm_timer();
}

document.onclick = jsddm_close;

function replaceText()
{
  overs = new Array();
  outs = new Array();
  var teller = 0;

  $('#titel_div').each(function() {
    var titel_grijs = $(this).find('h1.titel_grijs').html();
    var titel_rood = $(this).find('h1.titel_rood').html();

    if(!titel_grijs)
    {
      titel_grijs = '';
    }

    if(!titel_rood)
    {
      titel_rood = '';
    }

    $(this).html('<img src="images/gd/image.php?type=maintitel&text_grijs='+urlencode(titel_grijs)+'&text_rood='+urlencode(titel_rood)+'" />');
  });


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

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


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


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


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

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

  $('.producttitel').each(function() {

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


  $('#content_rechts h2').each(function() {

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


  $('.rood_subtitel').each(function() {

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



  $('.titel').each(function() {

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

  $('.subtitel').each(function() {

    var text = escape($(this).text());

    $(this).html('<img src="images/gd/image.php?type=subtitel&text='+urlencode(text)+'" />');
  });


//   $('#nav ul li a.mainbutton').each(function() {

//     var text = escape($(this).text());

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

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

//     $(this).html('<img src="images/gd/image.php?type=mainbutton&text='+urlencode(text)+'" id="mainbutton' + teller + '" />');
//     $(this).find('img').css('padding', '0');
//     $(this).css('padding', '0');
//     $(this).find('img').css('margin', '0');
//     $(this).find('img').css('margin-left', '-4px');
//     $(this).css('margin', '0');
//     $(this).parent().parent().css('padding-left', '0');
//     $(this).parent().parent().css('margin-left', '4px');
//     $(this).parent().parent().css('width', '956px');

//     $(this).hover(function() {
//       var id = $(this).find('img').attr('id').replace('mainbutton','');
//       document.getElementById($(this).find('img').attr('id')).src = overs[id].src;
//       $(this).next().css('display', 'block');
//     },
//     function() {
//       var id = $(this).find('img').attr('id').replace('mainbutton','');
//       document.getElementById($(this).find('img').attr('id')).src = outs[id].src;
//       $(this).next().css('display', 'none');
//     });

//     teller++;
//   });


}

function load_google_map() {

  var markerHTML;

  markerHTML = "<div id=\"google_address\"><b>Koks Milieu- en Voertuigtechniek B.V</b> <br><br>Diamantweg 1<br />1812 RC Alkmaar<br /><a href =\"mailto:info@koksmvt.com\">info@koksmvt.com</a> <br />tel. +31 (0)72-540 66 99<br /><br> Routebeschrijving? <a href='http://maps.google.com/maps?f=q&source=s_q&hl=nl&geocode=&q=Koks+Milieu-+en+Voertuigtechniek+B.V,+diamantweg+1,+1812+RC,+alkmaar,+netherlands&sll=52.612397,4.759234&sspn=0.075152,0.154324&ie=UTF8&cd=1&hq=Koks+Milieu-+en+Voertuigtechniek+B.V,&hnear=Diamantweg+1,+1812+Alkmaar,+Nederland&z=16&iwloc=A' target='_blank'>Klik hier</a></div>";

  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 RunSwf(swf, width, height)
{
  document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="' + width + '" height="' + height + '" id="europa" align="middle">\n');
  document.write('<param name="allowScriptAccess" value="sameDomain" />\n');
  document.write('<param name="movie" value="' + swf + '" />\n');
  document.write('<param name="quality" value="high" />\n');
  document.write('<param name="wmode" value="transparent" />\n');
  document.write('<embed src="' + swf + '" quality="high" wmode="transparent" width="' + width + '" height="' + height + '" name="europa" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />\n');
  document.write('</object>\n');
}


function load_video()
{
  var flash_video = document.getElementById('flash_video');

  if(flash_video != null)
  {
    var so = new SWFObject("flash/moodloader.swf", "", "360", "245", "8", "#FFF");
    so.addParam("wmode", "transparent");
    so.write("flash_video");
  }

}

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;
}

function CreateBookmarkLink()
{
  title = document.title;
  url = location.href;
  if (window.sidebar)
  {
    // Mozilla Firefox Bookmark
    window.sidebar.addPanel(title, url,"");
  }
  else if( window.external )
  {
    // IE Favorite
    window.external.AddFavorite( url, title);
  }
  else if(window.opera && window.print)
  {
    // Opera Hotlist
    return true;
  }
}
