var $jq = jQuery.noConflict();

var isIE6 = $jq.browser.msie && $jq.browser.version == "6.0";

$jq(document).ready(function() {

  if (!isIE6) {
    setInterval('swapImages()', 6000);
    setTimeout("setInterval('swapImagesSmall()', 6000)", 500);
  }


  if (isIE6) {
    $jq('.link_pijlen2, .slidelink, .link_pijlen, .actieVestigingen ul li').supersleight();
  }
  
  $jq('.buttons li').hover(function(event) {
    event.preventDefault();

    $jq('a', this).addClass('activehover');

    $jq('.submenu', this).css('display', 'block');

  }, function(event) {
    event.preventDefault();
    $jq('a', this).removeClass('activehover');
    $jq('.submenu', this).css('display', 'none');
  });

  // alleen laden als de occs zoeker NIET gevonden is.
  if ($jq("body").find(".zoeker").length == 0) {
    $jq("a[rel^='prettyPhoto']").prettyPhoto({ theme: 'facebook' });
  }
  
    $jq(".zoekMailEenVriend").prettyPhoto({ theme: 'facebook' });
  

  /* alleen op pagina's waar de snelzoeker staat dit uitvoeren*/
  if ($jq("#snelzoeken").attr("id") == "snelzoeken") {

    /* inladen van merken en modellen als er een snelzoeker is.*/
    $jq.ajax({
      type: "POST",
      url: chAppCall,
      dataType: "text",
      data: {
        appactie: "occssearch",
        functietype: chFunctietype,
        gropcd: chGropcd
      },
      success: function(response) {

        // Convert text to XML DOM
        var response = fuTextToXml(response);

        $jq("#merk option").attr("value", "").text(chSelectboxDefault);
        $jq(response).find("MERK").each(function() {
          var merk = $jq(this);
          $jq("#merk").append($jq("<option></option>").attr("value", merk.attr("merkcd")).text(merk.attr("merkom")));
        });
        $jq(response).find("BRST").each(function(i) {
          var brst = $jq(this);
          $jq("#brst").append($jq("<option></option>").attr("value", brst.attr("brstcd")).text(brst.attr("brstom")));
        });
      },
      error: function(requestobject, errortype, exeptionobject) {
        fuAjaxError(requestobject, this);
      }
    });
    $jq.ajax({
      type: "POST",
      url: chAppCall,
      dataType: "text",
      data: {
        appactie: "occsrandom",
        aantal: "8",
        foto: true,
        gropcd: chGropcd
      },
      success: function(response) {

        // Convert text to XML DOM
        var response = fuTextToXml(response);
        var arRandomFoto = [];
        var arRandomOmschrijving = [];
        var arRandomOccsno = [];
        var arRandomPrijs = [];

        // arrays vullen met retourwaarden
        $jq(response).find("OCCS-R").each(function(i) {
          // foto
          var werkfoto = "";
          var currentFoto = "";
          var omschrijving = "";
          var prijs = "";
          werkfoto = $jq("FOTOLOCATIE", this).text() + $jq("FOTONAAM", this).text();
          werkfoto = werkfoto.substring(0, werkfoto.length - 4) + "_tile.jpg"

          currentFoto = chFotoWebsite + werkfoto;

          // omschrijving
          omschrijving = $jq("OMS", this).text();

          // occsno
          arRandomOccsno[i] = $jq("OCCSNO", this).text();

          //prijs
          prijs = "EUR " + fuFormatDuizendtallen($jq("OCCSPRVK", this).text()) + ",-";

          $jq("#carousel")
            .append($jq("<a></a>").attr("href", "/Occasions/Occasions/GeavanceerdZoeken.aspx?occsno=" + arRandomOccsno[i]).attr("title", omschrijving + " - " + prijs).attr("rel", "")
            .append($jq("<img></img>").attr("src", currentFoto).attr("rel", "").attr("style", "width: 100%;")));
        });

        setTimeout("fuSchrijfCarousel(200,150,false)", 1000);

      },
      error: function(requestobject, errortype, exeptionobject) {
        fuAjaxError(requestobject, this);
      }
    });

    $jq("#merk").bind("change", function(e, data) {

      $jq.ajax({
        type: "POST",
        url: chAppCall,
        dataType: "xml",
        data: {
          appactie: "getmodel",
          functietype: chFunctietype,
          gropcd: chGropcd,
          merkcd: $jq(this).val()
        },
        success: function(response) {

          $jq("#model option").remove();
          $jq("#model").append($jq("<option></option>").attr("value", "").text("-- Uw keuze --"));
          $jq(response).find("MODL").each(function() {
            var model = $jq(this);
            $jq("#model").append($jq("<option></option>").attr("value", model.attr("modlcd")).text(model.attr("modlom")));
          });
        },
        error: function(requestobject, errortype, exeptionobject) {
          fuAjaxError(requestobject, this); ;
        }
      });
    });

    // change merk, modellen inladen

    // klikafhandeling van snelzoekknop
    $jq("#snelzoeken").click(function() {

      location.href = "/Occasions/Occasions/GeavanceerdZoeken.aspx?merk=" + $jq("#merk").val() + "&model=" + $jq("#model").val();
      return false;

    });

  }

});

function lightboxClick(objectSrc) {
  $jq('#imageFlow .diapo').lightBox();
  alert('lightboxFunctie gaat af');
}

function swapImages(item) {
  var active = $jq('.activeBig');
  var next = $jq(active).next();
  
  if (next.attr('rel') > active.attr('rel')) {
    $jq(active).fadeOut('slow', function() {
      $jq(active).removeClass('activeBig');
      $jq(next).fadeIn('slow').addClass('activeBig');
    });
  } else {

    if ($jq('.slideholder').size() > 1) {
      $jq(active).fadeOut('slow', function() {
        $jq(active).removeClass('activeBig');
        $jq('.firstBig').fadeIn('slow').addClass('activeBig');
      });
    }
  }
}

function swapImagesSmall(item) {
  var active = $jq('.activeSmall');
  var next = $jq(active).next();

  if (next.attr('rel') > active.attr('rel')) {
    $jq(active).fadeOut('slow', function() {
    $jq(active).removeClass('activeSmall');
    $jq(next).fadeIn('slow').addClass('activeSmall');
    });
  } else {
    if ($jq('.slider_action').size() > 1) {
      $jq(active).fadeOut('slow', function() {
        $jq(active).removeClass('activeSmall');
        $jq('.firstSmall').fadeIn('slow').addClass('activeSmall');
      });
    }
  }


} 
