
$(document).ready(function(){
  
  //$('#content').toggleClass('hidden');
  
  bubble_corners.render($('.post-bubble, .post-bubble-black, .post-bubble-white'));
  menu_corners.render($('.menu, .description'));
  
  // Dynamically set the height of the post bodies
  // in prep for the custom scroll bars
  $('.post-bubble').each(function() {
  	var bubble_height = $(this).height();
    var header_image_height = $('div.header-image', $(this)).height();
	var padding = parseInt($('div.post-wrapper', $(this)).css('padding-top').split("px")[0]) +
	              parseInt($('div.post-wrapper', $(this)).css('padding-bottom').split("px")[0]);
	$('div.post-body', $(this)).height(bubble_height - header_image_height - padding);
  });
	
  // Custom Scroll Bars
  updateScrollBars($('div.scroll-pane'));
  
  // Cycle Plugin Banners
  if ($('.cycle-horizontal').length > 0) {
		$('.cycle-horizontal').cycle({
			fx: 'scrollRight',
			speed: 1000,
			timeout: 9000,
			cleartypeNoBg: true
		});
  }
  
  // Colorbox Modals
  $(".modal").colorbox({opacity:0.7, current:"", previous:"&laquo; previous  |", next:"next &raquo;"});
  $(".modal-large").colorbox({iframe:true, width:720, height:480, opacity:0.7, current:"", previous:"&laquo; previous  |", next:"next &raquo;"});
  $(".modal-wide").colorbox({iframe:true, width:640, height:360, opacity:0.7, current:"", previous:"&laquo; previous  |", next:"next &raquo;"});
  $(".modal-standard").colorbox({iframe:true, width:640, height:480, opacity:0.7, current:"", previous:"&laquo; previous  |", next:"next &raquo;"});
  $(".modal-iframe").colorbox({iframe:true, width:"90%", height:"90%", opacity:0.7});
  $(".modal-page").colorbox({iframe:true, width:475, height:560, opacity:0.7});
  
  $().bind('cbox_open', modalOpen);

  // Sitemap
  var overhang = 8;
  var sitemap_height = parseInt($('div.sitemap-content').height());
  var menu_top = parseInt($('div.menu').css('top').split("px")[0]);
  var description_top = 180;
  //var description_top = parseInt($('div.description').css('top').split("px")[0]);
  $('div.sitemap').css('top', -sitemap_height + overhang);
  
  $('div.sitemap').hover(function(){
		$(this).stop().animate({top:0},{duration:300});
		$('div.menu').stop().animate({top:menu_top + sitemap_height - 35},{duration:300});
		$('div.description').stop().animate({top:description_top + sitemap_height - 35},{duration:300});
	}, function(){
		$(this).stop().animate({top:-sitemap_height + overhang },{duration:800});
		$('div.menu').stop().animate({top:menu_top},{duration:800});
		$('div.description').stop().animate({top:description_top},{duration:800});
  });

  // Current page link highlighting: Sitemap
  var path = location.pathname.substring(1);
  $('.sitemap a[href="/' + path + '"]').addClass('active');
  
  // Current page link highlighting: Menu
  var root_path = path.split("/")[0];
  root_path = (root_path == "") ? '/' : '/' + root_path + '/';
  $('.menu a[href="' + root_path + '"]').addClass('active');
  $('.menu a[href="' + root_path + '"]').prepend('<img width="10" height="8" src="' + wp_url_path + '/images/pointer.png" border="0">');

  // Options for SuperBGImage
  $.fn.superbgimage.options = {
	slideshow: 1, // 0-none, 1-autostart slideshow
	slide_interval: 8000, // interval for the slideshow
	randomimage: 0, // 0-none, 1-random image
	speed: 'slow', // animation speed
    transition: 90, // slide right/left
    transitionout: 1, // transition out the old image too
    preload: 0 // don't pre-load. much too slow for IE
  };

  if (isPageStill) {
    $.fn.superbgimage.options = {
	  slideshow: 0, // 0-none, 1-autostart slideshow
	  slide_interval: 1000, // interval for the slideshow
	  randomimage: 0, // 0-none, 1-random image
	  speed: 'slow', // animation speed
      transition: 90, // slide right/left
      transitionout: 1, // transition out the old image too
      preload: 0 // don't pre-load. much too slow for IE      
    };
  }

  if (isPageWhite) {
    $.fn.superbgimage.options = {
      slideshow: 1, // 0-none, 1-autostart slideshow
      slide_interval: 4000, // interval for the slideshow
      randomimage: 0, // 0-none, 1-random image
      speed: 'fast', // animation speed
      transition: 1, // 90-slide right/left, 1-fade
      transitionout: 1, // transition out the old image too
      preload: 0 // don't pre-load. much too slow for IE
    };
  }

  // Initialize SuperBGImage
  $('#bg_images').superbgimage().hide();
});


// Have to wait until the whole page, including images, are
// loaded before getting the correct height for images, etc
$(window).load(function() {
	// This space intentionally left blank
});


/**
 * Functions
 */

/* Modal has opened */
function modalOpen(){
  // Grab the element that triggered the current colorbox
  var $currentModal = $($.fn.colorbox.element());
  
  //alert('/window/' + $currentModal.attr("title"));
  pageTracker._trackPageview('/window/' + $currentModal.attr("title"));
}

/* Update the jScrollPane scroll bars */
function updateScrollBars($div_selector){
  $div_selector.jScrollPane({
    scrollbarWidth: 13,
    showArrows: true,
    arrowSize: 13,
    dragMinHeight: 12,
    dragMaxHeight: 50
  });
}

function updateTwitterScrollBars(){
  updateScrollBars($('div#twitters').closest('div.scroll-pane'));
}

function twitterFeedPulled(){
  $('div#twitters ul').simpleSpy(10, 12000, updateTwitterScrollBars);
  updateTwitterScrollBars();
}

