$(document).ready(function() {
  replaceText();
  load_google_map();
  load_flash();

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

  $('#login img').hover(function() {
    $(this).attr('src', 'images/login2_over.gif');
  },
  function() {
    $(this).attr('src', 'images/login2.gif');
  });


});

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

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

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

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

  //diensten_submenu



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

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

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

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

  $('#menu ul li a').each(function() {
      var text = escape($(this).text());

      if($(this).attr('class') == "navItemActive")
      {
        $(this).html('<img src="images/gd/image.php?type=buttonActive2&text='+urlencode(text)+'" id="button' + teller + '" />');
      }
      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=button&text=' + urlencode(text);

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

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

      teller++;
  });


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

    if($(this).attr('class') == "subnavitem subnavItemActive")
    {
      $(this).html('<img src="images/gd/image.php?type=subbuttonActive&text='+urlencode(text)+'" id="button' + teller + '" />');
    }
    else
    {
      overs[teller] = new Image();
      overs[teller].src= 'images/gd/image.php?type=subbuttonActive&text=' + urlencode(text);

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

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

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

    teller++;

});
}

function load_google_map() {

  var markerHTML;

  markerHTML = "<div id=\"google_address\"><b>DHV Accountancy</b> <br><br>Oslohaven 7B <br />1448 KH Purmerend <br /><a href =\"mailto:info@dhvaccountancy.nl\">info@dhvaccountancy.nl</a><br> Routebeschrijving? <a href='http://maps.google.nl/maps?hl=nl&q=oslohaven+7b+purmerend&ie=UTF8&hq=&hnear=Oslohaven+7b,+Purmerend,+Noord-Holland&gl=nl&t=h&z=16' 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 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;
}
