
// ////////////////////////////////////////////////////////////////////////////
// Plugins
//////////////////////////////////////////////////////////////////////////////////
(function($) {
  // Overlabel
  jQuery.fn.overlabel = function() {
    this.each(function(index) {
      var $this = $(this).not('.readonly'); 
      var field;
      var id = this.htmlFor || $this.attr('for');
      
      if (id && (field = document.getElementById(id))) {
        var control = $(field);
        $this
          .addClass("overlabel-apply");
          if (field.value !== '') {
            $this.css("display", "none");
          }
        
        control
        .focus(function () {
          $this
            .not('.readonly')
              .css("display", "none");
        })
        .blur(function () {
          if (this.value === '') {
            $this.css("display", "block");
          }  
        }); 
        
      }
    });  
  };

  // Corners
  jQuery.fn.roundCorners = function() {
    
    this.each(function(index) {
      var $this = $(this),
          y = $this.outerHeight(),
          x = $this.outerWidth();

      // Adjust For Borders
      $this
        .css({'height' : y - 2, 'width' : x - 2})
      // Wrap Image
      $this
        .wrap('<span class="img-round-wrapper"></span>');
      // Adjust Wrapper For Corners
      $this
        .parents('.img-round-wrapper')
        .css({'height' : y, 'width' : x});
      // Detect Ie6
      if(typeof document.body.style.maxHeight === "undefined") {
        return; // At least we gave you the border
      } else {
        // Add Corners
        $this
          .parents('.img-round-wrapper')
            .prepend('<span class="nw"></span><span class="ne"></span>');
        $this
          .parents('.img-round-wrapper')
            .append('<span class="se"></span><span class="sw"></span>');
      }
      
    });
  };

  // Center Image
  jQuery.fn.centerImage = function() {
    
    this.each(function(index) {
      var $this = $(this),
          src = $this.attr('src'),
          wrap = '<div style="background: url('+src+') no-repeat 50% 50%"></div>'; 
      $this
        .hide()
          .wrap(wrap);
    });
  };



})(jQuery);

//////////////////////////////////////////////////////////////////////////////////
// Cufon
//////////////////////////////////////////////////////////////////////////////////
var cufon = [
  '#page h1',
  '#page h2',
  '#page h3',
  '#colorSelect h1'
];
Cufon.replace(cufon); 
//////////////////////////////////////////////////////////////////////////////////
// Document Ready
//////////////////////////////////////////////////////////////////////////////////
$(document).ready(function(){
  // Png Fix
  if (jQuery.fn.pngFix) {
    // fixes transparent bg images
    $('#collectionVideo, #map').pngFix();
  }
  
  // Links In New Windows
  $('a[rel*=external]').click(function(){
		window.open(this.href);
		return false;   
	});
  // Rounded Corners
  $('img.round, .pages img, .slides img, #featuredProducts img').roundCorners();
  // Overlabel
  $('#commentForm label').overlabel();
  // Center Image
  $('.gallery img').centerImage();
  // Misc Hovers
  $('#otherFilms li a, #featuredVideo a, .carousel a').hover(function(){
		$(this).find('img').not('.playOverlay').stop().fadeTo("fast", 0.75);
	}, function(){
		$(this).find('img').not('.playOverlay').stop().fadeTo("fast", 1);
	});
  // Cycles
  var $cycle = $('.pages, .slides, .gallery'),
      $item = $cycle.find('>*'),
      $subitem = $item.find('>*'),
      style = $cycle.attr('class');

  var cycleOpts = {
        carousel : {
          fx : 'scrollHorz',
          timeout : 0,
          prev : '.cycleNav .prev',
          next : '.cycleNav .next'
        },
        slide : {
          fx : 'fade',
          timeout : 0,
          prev : '.cycleNav .prev',
          next : '.cycleNav .next'
        },
        gallery : {
          fx : 'fade',
          timeout : 0,
          prev : '.cycleNav .prev',
          next : '.cycleNav .next',
          before : updateCaption
        }
      };
  
  function updateCaption() {
    var caption = $(this).find('img').attr('alt'); 
    $('.caption').text(caption);
  }

  // Cycle Navigation
  var nav = ' '; 
  if (style == 'gallery') {
    nav += '<p class="caption"></p>'
  }
  nav += '<ul class="cycleNav">'
  nav += '<li class="w"><a class="prev" href="#" title="Previous">Previous</a></li>'
  nav += '<li class="e"><a class="next" href="#" title="Next">Next</a></li>'
  nav += '</ul>' 

  if ($item.length > 1) {
   $cycle.after(nav);
    if(style == 'pages') {
      $cycle.find('ul').each(function(){
        var $content = $(this).find('li');
        if($content.length == 0) {
          $(this).remove();
        }
      })
      .end()
      .cycle(cycleOpts.carousel);
    } else if(style == 'gallery') {
      $cycle.cycle(cycleOpts.gallery);
    } else {
      $cycle.cycle(cycleOpts.slide);
    }
  }
  // Remove Empty Uls
  $('.pages ul')
  // Lightboxes
  if (jQuery.fn.fancybox) {
    $('.carousel .pages a').fancybox({
      overlayColor : "#FFF",
      overlayOpacity : 0.75,
      padding: 0,
      centerOnScroll : true,
      showCloseButton : false,
      frameWidth: 480,
      frameHeight: 500,
      callbackOnShow : function() {
        Cufon.replace('#productPopup h3');
        $('#productPopup .pic img').roundCorners();
        $('#productPopup .buy').click(function(){
          window.open(this.href);
          $.fn.fancybox.close();
          return false;  
        });   
      } 
    });
    $('#mobileUploads a').fancybox({
      overlayColor : "#FFF",
      overlayOpacity : 0.75,
      padding: 20,
      centerOnScroll : true 
    });  
  }
  // Map
  $('#map').append('<div class="blur" />');
  $('.blur').css('opacity', 0);
  $('#map li a').hover(function(){
    $(this).find('img').css({
      "-webkit-transform": "scale(1.15) rotate(3deg)",
      "-moz-transform": "scale(1.15) rotate(3deg)"
    });
    $(this).parents('li').siblings('li').not(this).find('img').css({
      "-moz-transform" : "scale(0.75)", 
      "-webkit-transform" : "scale(0.75)"
    });
    $('#map .blur').stop().fadeTo(250, 0.75);
  }, function(){
    $(this).find('img').css({
      "-webkit-transform": "scale(1)",
      "-moz-transform": "scale(1)"
    });
    $(this).parents('li').siblings('li').not(this).find('img').css({
      "-moz-transform" : "scale(1) rotate(-3deg)", 
      "-webkit-transform" : "scale(1) rotate(-3deg)" 
    });
    $('#map .blur').stop().fadeTo(200, 0);
  });
  
  // Text Wrap
  $('#aboutOb p').prepend('<span class="bumper"></span>');
  $('.bumper').css({
    "display" : "block",
    "height" : 50,
    "width" : 100,
    "float" : "right"
  });
  
  // Play Watermarks
  $('a.thumb').each(function(){
    var overlay = '',
        x = $(this).find('>img').width(),
        y = $(this).find('>img').height();
    if($(this).hasClass('comingsoon')) {
      var overlay = 'comingSoon.png'
    } else {
      var overlay = 'playIcon.png'
    }
    $(this).append('<img src="/includes/newyork/swf/skins/obBasic/display/'+ overlay +'"  width="'+ x +'" height="'+ y +'" alt="play" class="playOverlay" />');
    $('.playOverlay').css({'position' : 'absolute', 'bottom' : 0, 'left' : 0});
  });
  $('a.comingsoon').click(function(){return false;});

  
  /*

  // Tracking
  $('#design-contest').click(function(){
    $.post('http://clk.atdmt.com/CHI/go/205265152/direct/01/');
  });
    
  $('.buy').click(function(){
    $.post('http://clk.atdmt.com/CHI/go/205265156/direct/01/');     
  });
  
  $('#storeBu').click(function(){
    $.post('http://clk.atdmt.com/CHI/go/205265153/direct/01/');
  });
  
  $('.twit').click(function(){
    $.post('http://clk.atdmt.com/CHI/go/205265155/direct/01/'); 
  });
*/
  
});
