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

  /* Better center code */
  if (!$.center) {
    $.fn.center = (function(options) {
      var pos = {
        sTop : function() {
          return window.pageYOffset || document.documentElement && document.documentElement.scrollTop ||	document.body.scrollTop;
        },
        wHeight : function() {
          return window.innerHeight || document.documentElement && document.documentElement.clientHeight || document.body.clientHeight;
        }
      };
      return this.each(function(index) {
        if (index == 0) {
          var $this = $(this);
          var elHeight = $this.outerHeight();
          var elTop = pos.sTop() + (pos.wHeight() / 2) - (elHeight / 2);
          $this.css({
            position: 'absolute',
            margin: '0',
            top: elTop,
            left: (($(window).width() - $this.outerWidth()) / 2) + 'px'
          });
        }
      });
    });
  }
  
  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 video - Light Box
      if ($('#' + showcase_id + ' .detail .video-close').length) {
        $('#' + showcase_id + ' .detail .video-close').click(function() {
          $('#' + showcase_id + ' .detail .video').toggle();
          $('#movie').remove();
          $('body #blinder').fadeOut(100,function(){$(this).remove();});
        });
      }

      if ( $('#' + showcase_id + ' .detail .video-button').length && typeof(video) != 'undefined' ) {
        $('#' + showcase_id + ' .detail .video-button').click(function() {
          $('.video').append("<iframe id='movie' width=640 height=480 src='http://player.dealertrend.com/player.html?t=Wheels%20TV&autoplay=0&v=" + video + "'></iframe>");
          $('body').append('<div id="blinder"></div>');
          
          $('#blinder').css({height:$(document).height()}).fadeIn().click(function() {
            $('#' + showcase_id + ' .detail .video').toggle();
            $('#movie').remove();
            $('body #blinder').fadeOut(100,function(){$(this).remove();});
          });

          $('#' + showcase_id + ' .detail .video').center().toggle();
        });
      }
      
      // 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();
        });
      }
    });
  }
});