/**
 * @author    Matt Ryan <matt@devaldez.com> De Valdez LLC
 **/ 

(function(){
  
  // AJAX CONTENT LOADING CONFIG OPS
  var base_url = 'http://' + window.location.host + '/';
  var timing   = 500;
  var height = $(window).height();
  $('div#overlay').height(height*4);
  
  // AJAX CONTENT LOADING KEEPS MUSIC PLAYING, READS URL ON LANDING
  var location = (window.location.hash) ? window.location.hash.replace('#', '') : '';
  var pathname = (window.location.pathname) ? window.location.pathname.replace('/', '') : '';
  if (pathname != 'guestbook' && pathname != 'contact') {
    if (location != '') {
      $.ajax({
        url: base_url + location, 
        beforeSend: function() {
          $('div#overlay').toggleClass('hidden');
        },
        success: function(data) {
          var response = $(data).find('#ajax');
          $('div#ajax').html(response);
          $('div#overlay').toggleClass('hidden');
          window.location.hash = location;
          var height = $(window).height();
          $('div#overlay').height(height*4);
          Cufon.replace('.cufon, caption', { hover: true });
        },
        error: function(msg) {
          $.ajax({
            url: base_url + 'wp-content/themes/hiphopheals/404.html',
            success: function(data) {
              var response = $(data);
              $('div#ajax').html(response);
              $('div#overlay').toggleClass('hidden');
              window.location.hash = '404';
              var height = $(window).height();
              $('div#overlay').height(height*4);
            },
            error: function(data) {
              log('even the 404 page jumped ship');
            }
          });
        }      
      });
    } else {
      $.ajax({
        url: base_url + '/home', 
        beforeSend: function() {
          $('div#overlay').toggleClass('hidden');
        },
        success: function(data) {
          var response = $(data).find('#ajax');
          $('div#ajax').html(response);
          $('div#overlay').toggleClass('hidden');
          window.location.hash = 'home';
          var height = $(window).height();
          $('div#overlay').height(height*4);
          Cufon.replace('.cufon, caption', { hover: true });
        },
        error: function(msg) {
          $.ajax({
            url: base_url + 'wp-content/themes/hiphopheals/404.html',
            success: function(data) {
              var response = $(data);
              $('div#ajax').html(response);
              $('div#overlay').toggleClass('hidden');
              window.location.hash = '404';
              var height = $(window).height();
              $('div#overlay').height(height*4);
            },
            error: function(data) {
              log('even the 404 page jumped ship');
            }
          });
        }      
      });
    } 
  }
  
  // AJAX CONTENT LOADING, CLICK BINDING
  $('header, div#main').delegate('a:not(.outbound)', 'click', function(e){    
    var location = $(this).attr('href').replace('/', '');
    if (location != 'guestbook' && location != 'contact') {
      e.preventDefault();
      $.ajax({
        url: base_url + location, 
        beforeSend: function() {
          $('div#overlay').toggleClass('hidden');
        },
        success: function(data) {
          var response = $(data).find('#ajax');
          $('div#ajax').html(response);
          $('div#overlay').toggleClass('hidden');
          window.location.hash = location;
          var height = $(window).height();
          $('div#overlay').height(height*4);
          Cufon.replace('.cufon, caption', { hover: true });
        },
        error: function(msg) {
          $.ajax({
            url: base_url + 'wp-content/themes/hiphopheals/404.html',
            success: function(data) {
              var response = $(data);
              $('div#ajax').html(response);
              $('div#overlay').toggleClass('hidden');
              window.location.hash = '404';
              var height = $(window).height();
              $('div#overlay').height(height*4);
            },
            error: function(data) {
              log('even the 404 page jumped ship');
            }
          });
        }      
      });
    }
  });
    
  // INITIALIZE AND AUTO PLAY BEATBOX 
  $("#boomBox").jPlayer({
    ready: function () {
      $(this).jPlayer('setMedia', {
        mp3: base_url + 'wp-content/themes/hiphopheals/audio/track1.mp3'
      }).jPlayer('play');
    },
    ended: function (event) {
    	$(this).jPlayer("play");
    },
    swfPath: base_url + 'wp-content/themes/hiphopheals/js/mylibs',
    supplied: "mp3"        
  });
  
  // BIND BEATBOX CLICKS
  $('a#switch').click(function(e){
    e.preventDefault();
    $(this).toggleClass('active');
    if ($(this).hasClass('active')) {
      $("#boomBox").jPlayer('play');
    } else {
      $("#boomBox").jPlayer('pause');
    }
  });
  
  $('.gfield_required').html('*required');
  
  // DROP DOWN MENUS, HOVERINTENT + CSS IMPLEMENATION CSS
  var hoverConfig = {    
       over: function() { $(this).find('ul.subMenu').stop(true, true).slideDown().toggleClass('show'); },
       timeout: 100, 
       out: function() { $(this).find('ul.subMenu').stop(true, true).slideUp().toggleClass('show'); }
  }; $('li.dropDown').hoverIntent( hoverConfig );
  
  // ADDS BACK-BUTTON SUPPORT TO HASH PAGES
  $(window).bind('hashchange', function() {
    var location = (window.location.hash) ? window.location.hash.replace('#', '') : '';
    $.ajax({
      url: base_url + location, 
      beforeSend: function() {
        $('div#overlay').toggleClass('hidden');
      },
      success: function(data) {
        var response = $(data).find('#ajax');
        $('div#ajax').html(response);
        $('div#overlay').toggleClass('hidden');
        window.location.hash = location;
        var height = $(window).height();
        $('div#overlay').height(height*4);
        Cufon.replace('.cufon, caption', { hover: true });
      },
      error: function(msg) {
        $.ajax({
          url: base_url + 'wp-content/themes/hiphopheals/404.html',
          success: function(data) {
            var response = $(data);
            $('div#ajax').html(response);
            $('div#overlay').toggleClass('hidden');
            window.location.hash = '404';
            var height = $(window).height();
            $('div#overlay').height(height*4);
          },
          error: function(data) {
            log('even the 404 page jumped ship');
          }
        });
      }      
    });
  });
  
})();
