$(document).ready(function() {
   run_gallery();

  $('#header-nav > li').hover(function() {
    $(this).find('.submenu').stop(true, true);
    $(this).find('.submenu').fadeIn('50');
  }, function () {
    $(this).find('.submenu').fadeOut('50');
  });

  $('#header-nav li').click(function() {
    document.location.href = $(this).find('a').attr('href');
  });


  $('#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');
  });

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

//   $('.scroll-pane-arrows').jScrollPane({showArrows:true });
replaceText();

if(jQuery.browser.version < 8 && jQuery.browser.msie)
{
  var zIndexNumber = 1000;
  $('div').each(function() {
    if(!$(this).hasClass('attentie_kader'))
    {
      $(this).css('zIndex', zIndexNumber);
      zIndexNumber -= 10;
    }

  });
}

});

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

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

  $('#nav ul li.hoofdbutton > a').each(function() {
     var text = escape($(this).text());
     var last = '';

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

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

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


       $(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++;

   });

   $('ul li.subbutton > a').each(function() {
     var text = escape($(this).text());
     var last = '';

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

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

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


       $(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 run_gallery()
{
  var old = $("#foto").children("img").attr("id");
  var number = gallery.length;
  if(number == 1){
    return false;
  }
  next = old.substring(4); next++; next = (next % number);

  var content = $("#foto").html();
  var newcontent = '<img class="foto" src="'+gallerypath+gallery[next]+'" id="img-'+next+'" />'+content;
  $("#foto").html(newcontent);
  $("#"+old).delay(gallerytimeout).fadeOut(galleryfade, function(){
    $(this).remove();
    run_gallery();
  });
}

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