if (!this.console) {
	console = {
		log: function() {}
	};
}

$('#loading').hide();

$("#loading").ajaxStart(function(){
   $(this).show();
 });

 $("#loading").ajaxStop(function(){
    $(this).hide();
  });
  
$(document).ready(function() {
    	
      //fade in some elements when you move the mouse
      var doFadeIn = function() {
        $('.fadein').css({ opacity:0, visibility:'visible'}).fadeTo(650,1);
      };
      $('body').one('mousemove',doFadeIn);
      $('#s').one('blur',doFadeIn);

      //add last & first classes for the drop down menu
      function addNthClasses() {
          //add last class for handicapped browsers
          $('.sub li:nth-child(2n)').addClass('end-row');
          $('.sub li:nth-child(2n+1)').addClass('first-row');
          $('.sub li:last-child').addClass('no-border');
          $('.sub').each(function () {
              if ($(this).find('li').length % 2 === 0) {
                  $(this).find('li').last().prev().addClass('no-border');
              }
          });
      }
      addNthClasses();

  // Keyboard control 
      $(window).keyup(function(event) {
          if(event.keyCode == 27) {
                  $('header a').first().click();
          }
          return false;
      });

  $('#mnav a').live('click', function () {
      $('#close_btn, #main').fadeIn(500);
      if (!($("#close_btn").length)) {
          var closeBtn = '<div id="close_btn" style="display:none"></div>';
          $('#container').append(closeBtn).fadeIn;
      };
  });


//video overlay
var video_overlay = '\
  <div id="video_fullscreen" class="clearfix">\
      <div class="video_fullscreen_inner clearfix">\
          <div id="close_video"></div>\
  		<div id="video_director"></div>\
  		<div id="video_title"></div>\
  		<div id="video_container"></div>\
  	</div>\
  </div>';
  
 

$('#container').before(video_overlay);
 
$('.play_vimeo').live('click', function () {
    $('#video_fullscreen iframe').show();
    var videourl = $(this).attr('rel');
    $('#video_fullscreen').fadeIn('slow');
    $('h1').clone().appendTo('#video_director');
    
    $("#video_container").oembed(videourl, 
                            {
                            embedMethod: "append", 
                            maxWidth: 790,
                            maxHeight: 500,
                            vimeo: { autoplay: true, maxWidth: 790, maxHeight: 500}                 
                            });
    var oHeight = $('.oembed-container').outerHeight(),
        oWidth = $('.oembed-container').outerWidth();
        
    //$('.oembed-container iframe').css({'width': oWidth, 'height': oHeight, 'top': oHeight/2, 'margin-left': oWidth/2})
    $('.activeslide img').hide();
    $('#video_container').append('<img src="/design/i/progress.gif" class="progress">');
    return false
});

$('#close_video').live('click', function () {
    $('#video_fullscreen').fadeOut('slow');
    $('.activeslide img').show();
    $('.progress, #video_fullscreen .fullgallery, #video_director h1, #video_fullscreen iframe, .slidecontrol, #video_fullscreen h4').remove();
    $('#video_container').removeClass('galleryview');
});


$('.show_gallery').live('click', function () {
    
    $('#video_container').append('<div class="slidecontrol"><div id="slidenav_wrap" /><div id="prev">Previous</div><div id="next">Next<div></div>')
    
    $(this).parent().find('.fullgallery').clone().prependTo('#video_container').removeClass('hidden');
    $('#video_fullscreen').fadeIn('slow');
    $('#video_fullscreen .slideshow').cycle({ 
    	    fx:     'scrollHorz', 
    	    speed: 500,
    	    prev:   '#prev', 
    	    next:   '#next, .slideshow', 
    	    timeout: 0,
    	    pager:  '#slidenav_wrap'
    	});
    
    $(this).parent().find('h4').clone().appendTo('#video_director');
    $('#video_container').addClass('galleryview');
    
});

});


//jquery address stuff
  
  $.address.state('/');
  $.address.init(function(event) {

  if (window.location.hash == "") {
      $.address.value(window.location.pathname);
  }

// Initializes plugin support for links
var NOHASH_SELECTOR = ".nohash";
$('a:not([href^=mailto]):not([href^=http])').not(NOHASH_SELECTOR).address();
      }).change(function(event) {
      // Identifies the page selection
      var page = $.address.path(),
          handler = function(data) {
              $('#ajax_wrap, #close_btn').fadeOut(200, function() {
                  $('#ajax_wrap').html($('#ajax_wrap', data).html()).show();
                  $('#close_btn, #main').show();
                  $.address.title(/>([^<]*)<\/title/.exec(data)[1]);
              });
              return false;
              };

              $.ajax({
                  url: page,
                  error: function(XMLHttpRequest, textStatus, errorThrown) {
                      handler(XMLHttpRequest.responseText);
                      },
                  success: function(data, textStatus, XMLHttpRequest) {
                      handler(data.replace(/<script(.|\s)*?\/script>/gi, ''));
                      runstuff();
                      $('#ajax_wrap a:not([href^=mailto]):not([href^=http])').address();
                      }
                  });
              });

function runstuff() {

    setTimeout(function() {
        $('#slideshow1').fadeIn(500).cycle({
            fx: 'scrollHorz',
            speed: 820,
            next: '#slideshow1',
            timeout: 0 
        });
        
    }, 300);
    
    var page = $.address.path();

    $('.works .work_item::nth-child(2n)').addClass('last-row');
    $('.works .work_item::nth-child(2n+1)').addClass('first-row');
    
    $(".sub").hide();


    if($('#video-item').length){
        //pause
        $('#pauseplay').click();
    }else {
        //play
    };
    
    //if we're on subpage
    if(!(page == "/")) {

    //if it already exists don't create it again.
        if (!($("#close_btn").length)) {
            var closeBtn = '<div id="close_btn" style="display:none"></div>';
                $('#container').append(closeBtn).fadeIn;
            } else {
                $('#close_btn').show();
        }

    //fadeout the main content
        $('#close_btn').live('click', function () {
                $('#main').hide();
                $('#close_btn').remove();
                $('header a').first().click();
        });
}; 


          $('header a').live('click', function () {
              $('#close_btn').remove();
        });

        if($('#video-item').length){
            $('.tagline').fadeOut();
            $('.block_1').css({'clear':'both', 'width': '100%'});
            $('#main').css({'top': '0', 'left':'0', 'width':'1500px'});
            $('.activeslide img').hide();
        } else {
            $('.activeslide img').show();
        };
        
        

        setTimeout(function() {
            if($('.ipad').length){
                 var mHeight = $('#main').outerHeight();
                 $('#container').css('height', mHeight);
                                  
                 if($('#video-item').length){
                     $('.tagline').fadeOut();
                     $('.block_1').css({'clear':'both', 'width': '100%'});
                     $('#main').css({'top': '0', 'left':'0', 'width':'1500px'});
                     $('.activeslide img').hide();
                     $('iframe').click();
                 } else {
                     $('.activeslide img').show();
                 };

               


            };
        },250)

};
