jQuery(document).ready(function($){

  if (!$.center) {
    jQuery.fn.center = function () {
      this.css("position", "fixed");
      this.css("top",  (( $(window).height() - this.height() ) / 2) + "px");
      this.css("left", (( $(window).width()  - this.width() )  / 2) + "px");
      return this;
    }
  }

  if ($('.dt-showcase.main-showcase-tab-box').length) {
    $('.dt-showcase.main-showcase-tab-box').each(function(){
      var showcase_id = $(this).attr('id');

      // Showcase Index Tabs
      if ($('#' + showcase_id + ' .index .showcase-tabs').length) {
        $('#' + showcase_id + ' .index .showcase-tabs').tabs('#' + showcase_id + ' .index > .showcase-pane')
      }

      else

      // Showcase Detail Tabs
      if ($('#' + showcase_id + ' .detail .showcase-tabs').length) {
        $('#' + showcase_id + ' .detail .showcase-tabs').tabs('#' + showcase_id + ' .detail > .showcase-pane')
      }

      /* Showcase Hover Overs */
      if ($('#' + showcase_id + ' .vehicle').length) {
        $('#' + showcase_id + ' .vehicle').stop().hover(
          function(){ $(this).children('.trims').show(); },
          function(){ $(this).children('.trims').hide(); }
        );
      }

      /* Showcase color picker */
      if ($('#' + showcase_id + ' .photos')) {
       $('#' + showcase_id + ' .photos .exterior-colors .color a').hover(function(){
         color_class = $(this).attr('href').replace(/#/,'');
         $('#' + showcase_id + ' .photos .exterior-images .file').hide();
         $('#' + showcase_id + ' .photos .exterior-images .file.' + color_class).show();
        });
       $('#' + showcase_id + ' .photos .exterior-colors .color a').click(function(){
         color = $(this).children('label').html();
         $('.add-color').append('External Color: '+color+"\n");
         return false;
       });
       $('#' + showcase_id + ' .photos .interior-colors .color a').click(function(){
         color = $(this).children('label').html();
         $('.add-color').append('Interior Color: '+color+"\n");
         return false;
       });
      }

      /* Showcase Button to tabs */
      if ($('#' + showcase_id + ' .detail .vehicle-info .opt-buttons a').length) {
        $('#' + showcase_id + ' .detail .vehicle-info .opt-buttons a[target!=_blank]').click(function(){
          $('#' + showcase_id + ' .showcase-tabs li a[href='+$(this).attr('href')+']').click();
          return false;
        });
      }

      // Showcase photo gallery - Light Box Gallery
      if ($('#' + showcase_id + ' .detail .photo-gallery-close').length) {
        $('#' + showcase_id + ' .detail .photo-gallery-close').click(function() {
          $('#' + showcase_id + ' .detail .photo-gallery').toggle();
          $('body #blinder').fadeOut(100,function(){$(this).remove();});
        });
      }

      if ($('#' + showcase_id + ' .detail .photo-gallery-button').length) {
        $('#' + showcase_id + ' .detail .photo-gallery-button').click(function() {

          $('body').append('<div id="blinder"></div>');
          $('#blinder').css({height:$(document).height()}).fadeIn().click(function() {
            $('#' + showcase_id + ' .detail .photo-gallery').toggle();
            $('body #blinder').fadeOut(100,function(){$(this).remove();});
          });

          $('#' + showcase_id + ' .detail .photo-gallery .photo-thumbs img').each(function(){
            $(this).click(function(){
              resized_href_image = $(this).attr('src').replace('WIDTH=100&HEIGHT=50','WIDTH=1024&HEIGHT=768');
              resized_image_src = $(this).attr('src').replace('WIDTH=100&HEIGHT=50','WIDTH=640&HEIGHT=400');
              $('#' + showcase_id + ' .detail .photo-gallery .gallery-medium-image img').attr('src', resized_image_src);
              return false;
            })
          });

          $('#' + showcase_id + ' .detail .photo-gallery').center().toggle();
        });
      }
    });
  }
});