function Home()
{
	this.bgImg = [];
	this.bgImg.push('/_global/_img/bg/01.jpg');
	this.bgImg.push('/_global/_img/bg/02.jpg');
	this.bgImg.push('/_global/_img/bg/03.jpg');
	this.bgImg.push('/_global/_img/bg/04.jpg');

	this.bgPhoto = [];
	this.bgPhoto.push('/_global/_img/home/01.png');
	this.bgPhoto.push('/_global/_img/home/02.png');
	this.bgPhoto.push('/_global/_img/home/03.png');
	this.bgPhoto.push('/_global/_img/home/04.png');

	this.bgClass = [];
	this.bgClass.push('bg01');
	this.bgClass.push('bg02');
	this.bgClass.push('bg03');
	this.bgClass.push('bg04');
	
	this.bgImage = '#bg';
	this.bgContainer = '#bgContainer';

	this.currentIndex = -1;
	this.timer = 3000;
	this.timerID;
}

Home.prototype.init = function()
{	
	$(function() 
	{
		$.preload(
			[
				'/_global/_img/bg/01.jpg', 
				'/_global/_img/bg/02.jpg', 
				'/_global/_img/bg/03.jpg', 
				'/_global/_img/bg/04.jpg',
				'/_global/_img/home/01.png',
				'/_global/_img/home/02.png',
				'/_global/_img/home/03.png',
				'/_global/_img/home/04.png'
			], 
			{loaded_all:function(loaded, total){home.loadFile();}}
		);
	}); 

	//home.loadFile();
	//$("#trc-bg").fadeIn("slow");
}

Home.prototype.loadFile = function()
{	
	home.currentIndex++;
	
	if(home.currentIndex > home.bgPhoto.length-1) home.currentIndex = 0;
	
	var fileImg = home.bgImg[home.currentIndex];
	var filePhoto = home.bgPhoto[home.currentIndex];
	var bgClassId = home.bgClass[home.currentIndex];
	var bgID = 'bg'+home.currentIndex;

	$(home.bgImage).css("background", "url('" + fileImg + "') repeat center top");
	$(home.bgContainer).append("<img id='"+bgID+"' class='bg' src='"+filePhoto+"' style='visibility:hidden'/>");

	$(home.bgImage).css("width", $(window).width()+'px');
	$(home.bgImage).css("height", $(window).height()+'px');
	$(home.bgImage).css("margin-top", '2000px');

	$("#"+bgID).load(function()
	{
		$("#"+bgID).css({visibility:'visible', marginTop:$("#"+bgID).height()+535});
		$("#"+bgID).fullBg();

		if($(home.bgContainer).find('img').size() > 1) 
		{
			$(home.bgContainer+' img:first-child').remove();
		}
		
		$("#"+bgID).delay(750).animate({ 
			 marginTop: 35 
			 },  2000, "easeOutExpo", function(){  } );

		$(home.bgImage).animate({ 
			 marginTop: 0 
			 },  2000, "easeOutExpo", function(){ home.count(); } );

		$.ajax({ 
			type: "POST", 
			url: "/_global/_php/check-bg.php", 
			data: "bgPages="+bgClassId
		}).done(function( msg ) { 
			//alert( "Data Saved: " + msg ); 
		});
	});
}

Home.prototype.appear = function()
{
	var bgID = 'bg'+home.currentIndex;

	$("body").css("background", "url('" +home.bgImg[home.currentIndex] + "') repeat fixed center top");

	$("#"+bgID).delay(750).animate({ 
			 marginTop: $("#"+bgID).height()+35 
			 },  1000, "easeInExpo", function(){ 
				 home.loadFile(); 
				 } );
}

Home.prototype.count = function()
{
	home.timerID = requestTimeout(home.appear, home.timer);
}

var home = new Home();

$(document).ready(home.init);
