$(document).ready(function() {
  replaceText();
  load_google_map();
  load_flash();

  $('ul#gallery').innerfade({
    speed: 1000,
    timeout: 6000,
    type: 'sequence',
    containerheight: '152px'
  });

});

$(document).unload(function() {
  GUnload();
});

function UpdateKleur(product_id, kleur_id)
{
  xajax_updateKleur(product_id, kleur_id);
}

function UpdateGewicht(product_id, gewicht_id)
{
  xajax_updateGewicht(product_id, gewicht_id);
}

function UpdateWinkelwagen(product_id, aantal, gewicht_id)
{
  xajax_updateWinkelwagen(product_id, aantal, gewicht_id);
}


function SetWinkelwagen(Content)
{
  Content = Content.split("|");
  document.getElementById('bezorgkosten').innerHTML = Content[0] ? Content[0] + '&nbsp;' : '-&nbsp;';
  document.getElementById('subtotaal').innerHTML = Content[1] + '&nbsp;';
  document.getElementById('btw').innerHTML = Content[2] + '&nbsp;';
  document.getElementById('totaal').innerHTML = Content[3] + '&nbsp;';
}

function replaceText()
{

  //replace buttons
  var teller = 0;
  overs = new Array();
  outs = new Array();

  $('.navlink').each(function() {
     var text = escape($(this).text());
     var buttontype = 'normal';

    if($(this).attr('class') == "navlink last" || $(this).attr('class') == "navlink last navItemActive")
    {
     buttontype = 'last';
    }
    if($(this).attr('class') == "navlink first" || $(this).attr('class') == "navlink first navItemActive")
    {
     buttontype = 'first';
    }

     if($(this).attr('class') == "navlink navItemActive" || $(this).attr('class') == "navlink last navItemActive" || $(this).attr('class') == "navlink first navItemActive")
     {
       $(this).html('<img src="images/gd/image.php?type=buttonover&buttontype=' + buttontype + '&text='+urlencode(text)+'" id="button' + teller + '" />');
     }
     else
     {
       overs[teller] = new Image();
       overs[teller].src= 'images/gd/image.php?type=buttonover&buttontype=' + buttontype + '&text=' + urlencode(text);

       outs[teller] = new Image();
       outs[teller].src= 'images/gd/image.php?type=button&buttontype=' + buttontype + '&text=' + urlencode(text);

       $(this).html('<img src="images/gd/image.php?type=button&buttontype=' + buttontype + '&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++;

   });



  $('#top_nv ul li a').each(function() {
    var text = escape($(this).text());
    $(this).html('<img src="images/gd/image.php?type=headersub&text='+urlencode(text)+'" />');
  });

    $('.headersub').each(function() {
    var text = escape($(this).text());
    $(this).html('<img src="images/gd/image.php?type=headersub&text='+urlencode(text)+'" />');
  });

  //replace buttons
  var teller2 = 0;
  overs2 = new Array();
  outs2 = new Array();

  $('#top_left_inner ul li a').each(function() {
     var text = escape($(this).text());
     var buttontype = 'normal';

     if($(this).attr('class') == "subsub")
     {
     }
     else
     {
       if($(this).attr('class') == "activeSubNav")
       {
         $(this).html('<img src="images/gd/image.php?type=subnav_over&buttontype=' + buttontype + '&text='+urlencode(text)+'" id="button_' + teller2 + '" />');
       }
       else
       {
         overs2[teller2] = new Image();
         overs2[teller2].src= 'images/gd/image.php?type=subnav_over&buttontype=' + buttontype + '&text=' + urlencode(text);

         outs2[teller2] = new Image();
         outs2[teller2].src= 'images/gd/image.php?type=subnav&buttontype=' + buttontype + '&text=' + urlencode(text);

         $(this).html('<img src="images/gd/image.php?type=subnav&buttontype=' + buttontype + '&text='+urlencode(text)+'" id="button_' + teller2 + '" />');

         $(this).hover(function() {
             var id = $(this).find('img').attr('id').replace('button_','');
             document.getElementById($(this).find('img').attr('id')).src = overs2[id].src;
           },
           function() {
             var id = $(this).find('img').attr('id');
             var id = $(this).find('img').attr('id').replace('button_','');
             document.getElementById($(this).find('img').attr('id')).src = outs2[id].src;
           });
       }

       teller2++;
    }
   });

  //replace page title
  $('#container_top_left h2').each(function() {
    var text = escape($(this).text());
    $(this).html('<img src="images/gd/image.php?type=titel&trim=1&text='+urlencode(text)+'" />');
  });

  $('#container_top_middle h2, #container_top_right h2').each(function() {
    var text = escape($(this).text());
    $(this).html('<img src="images/gd/image.php?type=titel&text='+urlencode(text)+'" />');
  });

  $('.bc_sep').each(function() {
    var text = escape($(this).text());
    $(this).html('<img src="images/gd/image.php?type=titel&text='+urlencode(text)+'" />');
  });


  $('.bc_1').each(function() {
    var text = escape($(this).text());
    $(this).html('<img src="images/gd/image.php?type=titel&text='+urlencode(text)+'" />');
  });

  $('.bc_2').each(function() {
    var text = escape($(this).text());
    $(this).html('<img src="images/gd/image.php?type=titelorange&text='+urlencode(text)+'" />');
  });

  $('.bc_2_blue').each(function() {
    var text = escape($(this).text());
    $(this).html('<img src="images/gd/image.php?type=titel&text='+urlencode(text)+'" />');
  });

  $('.bc_3').each(function() {
    var text = escape($(this).text());
    $(this).html('<img src="images/gd/image.php?type=titelorange&text='+urlencode(text)+'" />');
  });


   $('.title').each(function() {
    var text = escape($(this).text());
    $(this).html('<img src="images/gd/image.php?type=titel&text='+urlencode(text)+'" />');
  });

  $('.homepage_headersub').each(function() {
    var text = escape($(this).text());
    $(this).html('<img src="images/gd/image.php?type=blok_titel&text='+urlencode(text)+'" />');
  });


}

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 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;
}
