//--------------------------------------------------
// Author: Chris Lock
// Copyright (c) 2009
//--------------------------------------------------

//--------------------------------------------------
// VARIABLES
//--------------------------------------------------
var bgRatio = 1.6;
	
var minWidth = 975;

var bgWidth = 976;
var bgHeight = 609;

var bgInt = 8000;
var bgFade = 2000;


//--------------------------------------------------
// ROTATE BACKGROUND
//--------------------------------------------------
function rotateBg() {
	var active = $('div#bg img.active');
	var next = active.next();
	
	if ( active.is(':last-child') ) {
		var next = $('div#bg img:first');
	}
	
	active.fadeOut(bgFade);
	next.fadeIn(bgFade, function() {
		next.addClass('active')
		active.removeClass('active');
	});
	
	setTimeout("rotateBg()", bgInt);
}


//--------------------------------------------------
// ROTATE BACKGROUND
//--------------------------------------------------
function rotateLoadBg() {
	if ( imageQueue.length < 1 ) {
		$('div#bg').append('<img id="bg-0" class="resize active" src="' + defaultImage + '" />');
		$('div#bg img.active').load( function() {
			$('div#bg').fadeIn(1000);
		});
	}
	
	else {
		if ( $('div#bg img.active').length == 0 ) {
			$('div#bg').append('<img id="bg-0" class="resize active" src="' + imageQueue[0] + '" />');
			$('div#bg img.active').load( function() {
				$('div#bg').fadeIn(1000);
				if ( imageQueue.length > 1 ) {
					rotateLoadBg();
				}
			});
		}
		
		else {
			var activeImage = $('div#bg img.active').attr('id');
			var activeImageNum = parseInt( activeImage.replace('bg-', '') );
			var addImageNum = activeImageNum + 1;
			
			if ( imageQueue.length > 1 ) {
				if (activeImageNum >= imageQueue.length - 1) {
					setTimeout("rotateBg()", bgInt);
				}
				
				else {
					$('<img id="bg-' + addImageNum + '" class="resize next" src="' + imageQueue[addImageNum] + '" />').appendTo('div#bg');
					resizeImg();
					
					setTimeout(function () {
						$('<img id="bg-' + addImageNum + '" class="resize next" src="' + imageQueue[addImageNum] + '" />').load( function() {
							$('div#bg img.active').fadeOut(bgFade);
							$('div#bg img.next').fadeIn(bgFade, function() {
								$('div#bg img.active').removeClass('active');
								$('div#bg img.next').addClass('active').removeClass('next');
								
								rotateLoadBg();
							});
						});
					}, bgInt);
				}
			}
		}
	}
}


//--------------------------------------------------
// RESIZE
//--------------------------------------------------
function resizeImg() {
	var pageHeight = $('div#page').height();
	
	var winWidth = $(window).width();
	var winHeight = $(window).height();
	
	if ( winHeight < bgHeight ) {
		if ( winWidth < minWidth ) {
			$('img.resize').css('width', bgWidth).css('height', bgHeight).css('left', (minWidth-bgWidth)/2);
		}
		
		else {
			if ( winWidth > bgWidth ) {
				$('img.resize').css('width', winWidth).css('height', (winWidth/bgRatio)).css('left', 0);
			}
			
			else {
				$('img.resize').css('width', bgWidth).css('height', bgHeight).css('left', (winWidth-bgWidth)/2);
			}
		}
	}
	
	else {
		if ( winWidth < minWidth ) {
			var newWidth = ( winHeight*bgRatio );
			
			$('img.resize').css('width', newWidth).css('height', winHeight).css('left', (minWidth-newWidth)/2);
		}
		
		else {
			if ( (winWidth/winHeight) > bgRatio ) {
				$('img.resize').css('width', winWidth).css('height', (winWidth/bgRatio)).css('left', 0);
			}
			
			else {
				var newWidth = ( winHeight*bgRatio );
				
				$('img.resize').css('width', newWidth).css('height', winHeight).css('left', (winWidth-newWidth)/2);
			}
		}
	}
}

function resizeBg() {
	var pageHeight = $('div#page').height();
	
	var winWidth = $(window).width();
	var winHeight = $(window).height();
	
	if ( winHeight < bgHeight ) {
		if ( pageHeight > winHeight ) {
			$('div#bg').css('height', pageHeight);
		}
		
		else {
			$('div#bg').css('height', winHeight);
		}
		
		if ( winWidth < minWidth ) {
			$('div#bg').css('width', minWidth);
		}
		
		else {
			$('div#bg').css('width', winWidth);
		}
	}
	
	else {
		if ( winWidth < minWidth ) {
			$('div#bg').css('width', minWidth);
		}
		
		else {
			$('div#bg').css('width', winWidth);
		}
		
		$('div#bg').css('height', winHeight);
	}
}


//--------------------------------------------------
// FADE CONTENT
//--------------------------------------------------
function contentFade() {
	if($.browser.msie && $.browser.version>'6.0') {
		$('a.content-fade').toggle( function() {
			$(this).blur().html('&#91; + &#93; Show Text').attr('href', '#/show-text/');
			$('div#content').fadeOut(0, function() {
				resizeImg();
			});
		}, function() {
			$(this).blur().html('&#91; - &#93; Hide Text').attr('href', '#/hide-text/');
			$('div#content').fadeIn(0, function() {
				resizeImg();
			});
		});
	}
	
	else {
		$('a.content-fade').toggle( function() {
			$(this).blur().html('&#91; + &#93; Show Text').attr('href', '#/show-text/');
			$('div#content').stop().fadeOut(750, function() {
				resizeImg();
			});
		}, function() {
			$(this).blur().html('&#91; - &#93; Hide Text').attr('href', '#/hide-text/');
			$('div#content').stop().attr('style','display: none;').fadeIn(750, function() {
				resizeImg();
			});
		});
	}
}


//--------------------------------------------------
// START
//--------------------------------------------------
$(document).ready( function() {
	if ( $('div#bg').css('display') == 'none' ) {
		rotateLoadBg();
		
		resizeImg();
		contentFade();
		

//--------------------------------------------------
// RESIZE
//--------------------------------------------------
		$(window).bind('resize', function() { 
			resizeImg();
		});
	}
});


