var animationspeed = 200;
var magibg;
var magibghome = -1070;
var magibgoffset = 36;

$(document).ready(function() {
  magicbg = $(".splash .headerbg");
    
  $(".splash .modelnames table td").splashcat();
    
  $(".peugeot-gallery .back a").click(function() {
    hidegallery();
  });
    
  $("div.modelselector2").mouseleave(function() {
    $(this).css({
      "height" : "35px"
    });
    magicbg.stop(false, true).css({
      "left" : (magibghome) + "px",
      "height" : "35px"
    });
    $(".splash .modelnamessub table").stop(false, true).hide();
  });

  $(".techinfolink").click(function() {
    if ($(".peugeot-pricing").length) {
      $(".peugeot-pricing").stop(true, true);
      $(".peugeot-pricing").slideUp("fast", function(){
        $(".techdetails").stop(true, true);
        $(".techdetails").slideToggle("slow");
      });
    } else {
      $(".techdetails").stop(true, true);
      $(".techdetails").slideToggle("slow");     
    }
    return false;
  });
  $(".pricelistopen").click(function() {
    if ($(".techdetails").length) {
      $(".techdetails").stop(true, true);
      $(".techdetails").slideUp("fast", function(){
        $(".peugeot-pricing").stop(true, true);
        $(".peugeot-pricing").slideToggle("slow");
      });
    } else {
      $(".peugeot-pricing").stop(true, true);
      $(".peugeot-pricing").slideToggle("slow");

    }
    return false;
  });
  var hash=location.hash;
  if(hash == '#pricelist') {
    $(".pricelistopen").trigger('click');
  }
  $('a.issuu').issuu();
  if ($('div.peugeot-gallery').get(0)) {



    var gallery = [],
    galleryloaded = false,
    galleryparent = $('div.peugeot-gallery'),
    prevButton = galleryparent.find('div.nav a.previous'),
    nextButton = galleryparent.find('div.nav a.next'),
    calleryindex = null,
    callerylength = null,
    hidegallery = function(){
      $('div.contentIFrame1').show();
      galleryparent.hide();
      $('.srmenu a.gallery').show();
      $('.srmenu a.showroom').hide();
            
      //make splash large
      $(".splash").css({
        "height" : $('div.contentIFrame1').height() + "px"
      });
      $("div.modelselector2").css({
        "height" : "35px"
      });
      magicbg.stop(false, true).css({
        "left" : (magibghome) + "px",
        "height" : "35px"
      });
      $(".splash .modelnamessub table").stop(false, true).hide();
    },
    showgallery = function(){
      $('div.contentIFrame1').hide();
      galleryparent.show();
      $('.srmenu a.gallery').hide();
      $('.srmenu a.showroom').show();
            
      //make splash small and reset selector
      $(".splash").css({
        "height" : "0px"
      });
      $("div.modelselector2").css({
        "height" : "35px"
      });
      magicbg.stop(false, true).css({
        "left" : (magibghome) + "px",
        "height" : "35px"
      });
      $(".splash .modelnamessub table").stop(false, true).hide();
    },
    toggleButtonsVisibility = function(){
      if (calleryindex == 0)
        prevButton.hide();
      else
        prevButton.filter(':hidden').show();
      if (calleryindex >= (callerylength - 1))
        nextButton.hide();
      else
        nextButton.filter(':hidden').show();

    },
    showitem = function(i){
      galleryparent.find('div.image img').attr('src', gallery[i].normal);
      galleryparent.find('div.nav a.zoom').attr('href', gallery[i].original);
      calleryindex = i;
      toggleButtonsVisibility();
    };
    prevButton.click(function(e){
      e.preventDefault();
      var p = calleryindex - 1;
      showitem((p < 0)? 0 : p);
    });
    nextButton.click(function(e){
      e.preventDefault();
      var n = calleryindex + 1;
      showitem((n >= callerylength)? callerylength - 1 : n);
    });
    $('.srmenu a.gallery').click(function(e){
      e.preventDefault();
      if (!galleryloaded) {
        $.loadingIndicator.init();
        $.json('modelstatic', 'gallerydata', {
          model : document.location.href.split('/').pop()
        }, function(j) {
          if(j.status == 'ok') {
            galleryloaded = true;
            gallery = j.images;
            var ul = $('<ul />');
            galleryparent.find('div.thumbs').append(ul);
            var i, l = gallery.length, j = 1;
            callerylength = l;
            for (i = 0; i < l; i++) {
              var li = $('<li />');
              if (j == 2) {
                li.addClass('lastcol');
                j = 1;
              } else {
                j++;
              }
              if (i + 2 > l) {
                li.addClass('lastrow');
              }
              li.append('<a href="' + gallery[i].normal + '"><img alt="" src="' + gallery[i].thumb + '"></a>');
              ul.append(li);
              li.children('a')
              .data('calleryindex', i)
              .click(function(e){
                e.preventDefault();
                showitem($(this).data('calleryindex'));
              });
            }
            if (l > 0) {
              showitem(0);
              showgallery();
              ul.jScrollPane({
                scrollbarWidth : 15,
                dragMinHeight : 75,
                dragMaxHeight : 175,
                scrollbarMargin:0
              });

            } else {
              $('.srmenu a.gallery').hide();
            }
                        
          }
          $.loadingIndicator.stop();
        });
      } else {
        showgallery();
           
      }

    });
    $('.srmenu a.showroom').click(function(e){
      e.preventDefault();
      hidegallery();
    });
  }
      
});


$.fn.splashcat = function(options) {
  return this.each(function() {
    var cat = $(this);
    var width = cat.innerWidth();
    var id = cat.attr("id").substr(9);
    var position = cat.position();
          
    cat.mouseenter(function() {
      $("div.modelselector2").css({
        "height" : "70px"
      });
      magicbg.css({
        "height" : "70px"
      }).stop().animate({
        "left" : (magibghome + position.left + (width / 2) + magibgoffset) + "px"
      }, animationspeed, "swing");

      $(".splash .modelnamessub table").stop(false, true).hide();
        
      $(".splash .modelnamessub").stop(false, true).animate({
        "left" : ((position.left + $(".splash #models_" + id).width() > 970) ? (970 - $(".splash #models_" + id).width()) : position.left)
      }, animationspeed, "swing")
      $(".splash #models_" + id).show();
    });

  });
}
