/**
    * FULL SCREEN IMAGES
    * Deals with full screen BG images that stretch but retain proportion
    * ----
*/
var bgimg;
var init = function() {
  if (arguments.callee.done) return;
  arguments.callee.done = true;
  bgimg = document.getElementById('bgimg');
  if (!bgimg) return;
  bgimg.onload = resizeBg;
  bgimg.src = bgimg.src;
  if (window.addEventListener) window.addEventListener('resize', resizeBg, false);
  else if (window.attachEvent) window.attachEvent('onresize', resizeBg);
  else window.onresize = resizeBg;
}
var resizeBg = function() {
  var w = self.innerWidth || document.documentElement.clientWidth;
  var h = self.innerHeight || document.documentElement.clientHeight;
  if (bgimg.width !== w) {
    bgimg.parentNode.style.left = '0';
    bgimg.parentNode.style.top = '0';
    bgimg.height = bgimg.parentNode.height = (w / bgimg.width) * bgimg.height;
    bgimg.width = bgimg.parentNode.width = w;
  }
  if (bgimg.height < h) {
    bgimg.width = bgimg.parentNode.width = (h / bgimg.height) * bgimg.width;
    bgimg.height = bgimg.parentNode.height = h;
    bgimg.parentNode.style.left = '-' + ((bgimg.width - w) / 2) + 'px';
  }
  else if (bgimg.height > h) {
    bgimg.parentNode.style.top = '-' + ((bgimg.height - h) / 2) + 'px';
  }
}

if (document.addEventListener) document.addEventListener("DOMContentLoaded", init, false);

/*@cc_on @*/
/*@if (@_win32 || @_win64)
  document.write('<script id="ie_onload" defer src="javascript:void(0);"></script>');
  document.getElementById('ie_onload').onreadystatechange = checkState;
  function checkState() {
    if (this.readyState && this.readyState != 'complete') return;
    else init();
  }
/*@end @*/

if (/KHTML|Webkit|iCab/i.test(navigator.userAgent)) {
	var khtmltimer = window.setInterval(function() {
		if (/loaded|complete/.test(document.readyState)) {
  		window.clearInterval(khtmltimer);
  		init();
		}
	}, 10);
}



/**
    * BODY RESIZE
    * Resizes the body element exactly to the dimensions of the window when resized
    * ----
*/
function body_resize() {
    var window_height = $(window).height();
    //alert(window_height);
    $('body.fsbg').css('height',window_height);
}
window.onresize = body_resize;



/**
    * FADE IN BACKGROUND IMAGE
    * Fades in full screen bg image after 3 secs, hopefully after it's loaded
    * ----
*/
function fade_in_bg(){
	$("#bg").addClass("hidden").fadeTo(2000, 0, function() {
		$(this).removeClass("hidden")
		$('#bg img').css('display','block');
	}).fadeTo(1000, 1);
}




$.fn.scroller = function()
{
    return this.each( function()
    {
        $(this).click( function()
        {
            var scroll_target = $(this).attr('href');
            $.scrollTo(scroll_target,800); 
            return false;
        });  
    });
}



/**
    * SHORT URL
    * When 'short url' link is clicked, a window pops up with copy to clipboard link
    * ----
*/
function short_url() {
	$("div#shorturl_popup").fadeTo(0, 0);
	
	$('a.shorturl').click(
		function(){
			$("div#shorturl_popup").addClass("show").fadeTo(250, 1);
			return false;
		}
	);
	
	$('div#shorturl_popup input[type="submit"]').click(
		function(){
			$('div#shorturl_popup').fadeTo(250,0);
		}
	);
}



$(document).ready(function(){
	
	// BITS AND BOBS
	$('body').addClass('js-enabled').removeClass('js-disabled');
	$('div#comments_wrapper, div#bg.hidden, div#bg img').hide();
	
	// COLORBOX
	$(".comments_link").colorbox({width:"625px", height:"480px", transition:"none", inline:true, href:"#journal_comments"});
	
	// MAKES SURE BODY HEIGHT = WINDOW HEIGHT (Safari fix)
	var window_height = $(window).height();
	$('body.fsbg').css('height',window_height);
	
	// CALL PREVIOUS JQUERY FUNCTIONS
	fade_in_bg();
	$('a.scrollto').scroller();
	short_url();

});
