var isIE = false;
var backgroundImages;
var currentImage = 0;

jQuery(document).ready(function(){
	isIE = jQuery.browser.msie;
	
	if ( window.backgroundImage ) {
		var winHeight = $(window).width();

	 	var title = jQuery('#title');
/*
	 	title.css({"color":"#ffffff"});

	 	var link = title.find('a');
	 	link.css({"color":"#ffffff"});
*/

/*
	 	if ( winHeight > title.height()+50 ) {
		 	title.css({"position":"absolute","bottom": 50 });
//		 	title.find('a').css({"color":"#ffffff"});
	 	}
*/
	 	
	 	backgroundImages = window.backgroundImage.split(';');
	 	
		jQuery('<div id="back_holder" />').css({opacity:1, visibility:"visible" }).prependTo('body');
		jQuery('<img />').attr('id', 'back_image').css({opacity:0}).appendTo( jQuery('#back_holder') );
		loadBackground();
	}

	var f = function() {
		if ( window.not_resize ) return;
		jQuery('.font_replace cufon').each( function(i,e){
			var cu = jQuery(e);
			cu.height( cu.height()*0.8 );
		});
	};
//	setTimeout( f, 0 );

});

jQuery(window).resize(function(){
	resizeBackground();
});

function resizeBackground() {
	var navWidth = $(window).width();
	var navHeight = $(window).height() - 0;
	var navRatio = navWidth / navHeight;

	if ($('#back_image').width() > 1) picWidth = $('#back_image').width();
	if ($('#back_image').height() > 1) picHeight = $('#back_image').height();
	picRatio = picWidth / picHeight;
	
	
	if (navRatio > picRatio) { // if browser window is larger then picture
		var newHeight = (navWidth / picWidth) * picHeight;
		var newWidth = navWidth;
	} else {
		var newHeight = navHeight;
		var newWidth = (navHeight / picHeight) * picWidth;
	}

	newTop = 0 - ( (newHeight - navHeight) / 2 );
	newLeft =  0 - ( (newWidth - navWidth) / 2 );
	
	$('#back_holder').css({height: navHeight, width: navWidth, visibility:"visible" });
 	$('#back_image').css({height: newHeight, width: newWidth});
	$('#back_image').css({top: newTop, left: newLeft});
}

function preloadImage(source, nextEvent) {
	
	jQuery('<img />').attr('src', source).one("load",function() {

		jQuery('#back_image').fadeOut(500, function() {
			jQuery('#back_image').attr('src', source).one("load",function() {
				jQuery(this).oneTime(500, "preload", nextEvent);
			})
			.each(function(){
			if(this.complete || (isIE && parseInt(jQuery.browser.version) == 6)) 
				jQuery(this).trigger("load");
			});
		});

	}).each(function(){
	if(this.complete || (isIE && parseInt(jQuery.browser.version) == 6)) 
		jQuery(this).trigger("load");
	});

}

function loadBackground() {
		jQuery(this).stopTime("fototimer");
	// body hide
	//jQuery('body').fadeOut(0);
 		preloadImage(backgroundImages[currentImage], loadBackgroundEnd);
}

function loadBackgroundEnd() {
	currentImage++;
	if ( currentImage > backgroundImages.length - 1 )
		currentImage = 0;

 	if ( backgroundImages.length > 1 ) {
		jQuery(this).everyTime( 3000, "fototimer", loadBackground );
 	}

	resizeBackground();
	jQuery('#back_image').fadeTo(1000, 1, function() { jQuery('body').addClass('back'); });
}
