
//INCLUDE
document.write('<script type="text/javascript" src="/scripts/constants.js"></script>');
document.write('<script type="text/javascript" src="/scripts/helpers.js"></script>');
document.write('<script type="text/javascript" src="/scripts/get.js"></script>');
document.write('<script type="text/javascript" src="/scripts/ajax.js"></script>');
document.write('<script type="text/javascript" src="/scripts/add.js"></script>');
//document.write('<script type="text/javascript" src="/scripts/tutorial.js"></script>');

//Number of object generated
$(function() {
	
	//LOADING RESERVE
	$.score = { 
	    exScore: $('#exScore').val()
	};
});

//GLOBAL VARIABLE *BEFORE* DOCUMENT IS READY
var donutCredit = 0;
var duffCredit = 0;		

//STATUS
var demoCounter = 0;

//LOADING FUNCTIONS
readyLoad();

/**
 * Load all function on document.ready
 */
function readyLoad() 
{ 
	$(function() {	
		//Focus on search text-box
		$('#text-box').focus();
				
		firesMovable();
		firesDroppable();	
	 
		//ESSENTIAL LOAD
		loadDialog();
		fb_load();
					
		//LOGIN
		$('#login-button, #login-button-label').click(function() {
			$('#login-button').hide();
			$('#login-button-label').hide();
			
			$('#login-box').fadeIn();
			$('#username').focus();
		});			
		
		//day is past
		if(getDsw() == 1) {
			showHelpDialog();
		}
				
		//HIGHLIGHT IN DEMO
		$('#drag-ciocco-eccezionale').effect('pulsate', {times: 100}, 3000);		
	});
}

/**
 * Load facebook function
 */
function fb_load() {
	$('#fb_donut').click(function() {
		$(this).hide();		
		$('#shop').hide(); //might be annoying
		
		$('#facebook_comments').fadeIn();
		
		$('#fb_comments_close').click(function() {
			$('#facebook_comments').hide();
			$('#fb_donut').fadeIn();
			$('#shop').fadeIn(); 
			load();
		});
	});
}
/**
 * Load all function
 */
function load() 
{			
	firesMovable();
	firesDroppable();	
	fb_load();
}

/**
 * Load all dialog function
 */
function loadDialog() 
{
	// increase the default animation speed to exaggerate the effect
	$.fx.speeds._default = 500;
	
	//Prepare help dialog	
	$('#help-dialog').dialog({
		autoOpen: false,
		resizable: false,
		draggable: false,
		modal: true,		
		show: "fadeIn",
		hide: "fadeOut",
		width: HELP_DIALOG_WIDTH,
		height: HELP_DIALOG_HEIGHT,
		position: ['center', 100],
		open: function (event,ui) {
			$(this).css({
				'width': HELP_DIALOG_WIDTH_2,
				'height': HELP_DIALOG_HEIGHT
			});	
		}
	});
	//Fixed background
	$('#help-dialog').parent().css({position:"fixed"});
	
	//TRIGGER	
	$('#help-img').click(function() {
		showHelpDialog();
	});
	
	//INFO
	loadInfoSearchDialog();
	loadInfoTrophyDialog();
}

function loadInfoSearchDialog() {
	//Prepare report bug dialog	
	$('#info-search-dialog').dialog({
		autoOpen: false,
		resizable: false,
		draggable: false,
		modal: true,		
		show: "fadeIn",
		hide: "fadeOut",
		width: HELP_DIALOG_WIDTH,
		height: HELP_DIALOG_HEIGHT,
		position: ['center', 100],
		open: function (event,ui) {
			$(this).css({
				'width': HELP_DIALOG_WIDTH_2,
				'height': HELP_DIALOG_HEIGHT
			});
		}
	});
	//Fixed background
	$('#info-search-dialog').parent().css({position:"fixed"});
	
	//TRIGGER	
	$('#search-info').click(function() { 
		showInfoSearchDialog();
	});
}

function loadInfoTrophyDialog() {
	//Prepare report bug dialog	
	$('#info-trophy-dialog').dialog({
		autoOpen: false,
		resizable: false,
		draggable: false,
		modal: true,		
		show: "fadeIn",
		hide: "fadeOut",
		width: HELP_DIALOG_WIDTH,
		height: HELP_DIALOG_HEIGHT_2,
		position: ['center', 100],
		open: function (event,ui) {
			$(this).css({
				'width': HELP_DIALOG_WIDTH_2,
				'height': HELP_DIALOG_HEIGHT_2
			});
		}
	});
	//Fixed background
	$('#info-trophy-dialog').parent().css({position:"fixed"});
	
	//TRIGGER	
	$('#trophy-info').click(function() { 
		showInfoTrophyDialog();
	});
}

/**
 * Fires draggable objects
 */
function firesMovable() 
{
	$('.donut').draggable({
		containment: 'body',
		
		stop: function() {
			//AJAX EVENT - sets the object coordinate in the database
			setCoordAjax($(this).attr('id'), 'donut', $(this).css('left'), $(this).css('top'));
		}		
	});
	$('.duff').draggable({
		containment: 'body',
				
		stop: function() {
			//AJAX EVENT - sets the object coordinate in the database
			setCoordAjax($(this).attr('id'), 'duff', $(this).css('left'), $(this).css('top'));
			
		}		
	});
	
	//CURRENT OBJECTS !!!
	
	$('.cent').draggable({
		containment: 'body',
		 
		start: function() 
		{
			//set homer hungry
			hungry();
		},
		stop: function() {
			if(isLogged()) {
				//AJAX EVENT - sets the object coordinate in the database
				setCoordAjax($(this).attr('id'), 'cent', $(this).css('left'), $(this).css('top'));
				
				
				//set homer sated
				sated();
			}			
		}		
	});
		
	$('.dollar').draggable({
		containment: 'body',
		
		stop: function() {
			//AJAX EVENT - sets the object coordinate in the database
			setCoordAjax($(this).attr('id'), 'dollar', $(this).css('left'), $(this).css('top'));
		}		
	});
}
/**
 * Fires droppable objects
 */
function firesDroppable() 
{
	feedHomer();
	
	piggyBank();
	buyDonut();
	buyDuff();
}
/**
 * Fires quenced Homer event
 */
function feedHomer() 
{
	$('#homer-mouth').droppable({
		drop: function(event, ui) {
			
			//demo
			if ( ! isLogged()) {
				demoCounter++;
				if (demoCounter >= 3) {
					sated();
					$('#drag-ciocco-eccezionale').hide();
				}
			}
			
			var exScore = $('#score').text();
			
			var id_food = $(ui.draggable).attr('id');
							
			var classe = $(ui.draggable).attr('class');
			//ONLY FOR DUFF
			if (contains(classe, 'cent-' + OBJECT_DUFF)) {
				
				var pointDuff = 1;
				
				incrementLabel('#score', 'score', pointDuff);
														
				//setTimeout('sated()', FEED_HOMER_FREQUENCY);
								
				//AJAX FUNCTION
				feedHomerAjax(id_food, OBJECT_DUFF);
				
				$('#homer-mouth').append('<span class="points" style="color: green; font-size: 64px; font-weight: bolder;">+' + pointDuff + 'kg</span>');	
				
				
			} else if (contains(classe, 'cent-' + OBJECT_DONUT)) {	
				
				var pointDonut = 4;
				
				incrementLabel('#score', 'score', pointDonut);			
						
				//setTimeout('sated()', FEED_HOMER_FREQUENCY);
								
				//AJAX FUNCTION	
				feedHomerAjax(id_food, OBJECT_DONUT);
				
				$('#homer-mouth').append('<span class="points" style="color: green; font-size: 64px; font-weight: bolder;">+' + pointDonut + 'kg</span>');	
						
			} else if (contains(classe, 'cent-' + OBJECT_PANNOSA)) {	
				
				var pointPannosa = 8;
				
				incrementLabel('#score', 'score', pointPannosa);					
				
				//quences();
				
				//quences homer
				//this.quenced = true;
				
				//AJAX FUNCTION
				feedHomerAjax(id_food, OBJECT_PANNOSA);
				
				$('#homer-mouth').append('<span class="points" style="color: green; font-size: 64px; font-weight: bolder;">+' + pointPannosa + 'kg</span>');	
					
				
			} else if (contains(classe, 'cent-' + OBJECT_COLESTEROLICA)) {	
				
				var pointColesterolica = 9;
				
				incrementLabel('#score', 'score', pointColesterolica);					
				
			//	quences();
				
				//quences homer
				//this.quenced = true;
				
				//AJAX FUNCTION
				feedHomerAjax(id_food, OBJECT_COLESTEROLICA);
				
				$('#homer-mouth').append('<span class="points" style="color: green; font-size: 64px; font-weight: bolder;">+' + pointColesterolica + 'kg</span>');	
						
			} else if (contains(classe, 'cent-' + OBJECT_CIOCCO_ECCEZIONALE)) {	
				
				var pointCioccoEccezionale = 10;
				
				incrementLabel('#score', 'score', pointCioccoEccezionale);					
				
			//	quences();
				
				//quences homer
				//this.quenced = true;
				
				//AJAX FUNCTION
				feedHomerAjax(id_food, OBJECT_CIOCCO_ECCEZIONALE);
				
				$('#homer-mouth').append('<span class="points" style="color: green; font-size: 64px; font-weight: bolder;">+' + pointCioccoEccezionale + 'kg</span>');	
								
			}
			
			//COMMON
			
			setTimeout('hidePoints()', 1000);
			
			//After cancel draggable property
			$(ui.draggable).hide('explode', FEED_HOMER_FREQUENCY).draggable('disable');	
			
			checkTrophies();
						
		}		
	});
}
/**
 * Fires when puts cents in the piggy-bank
 */
function piggyBank() 
{
	$('#piggy-bank-hole').droppable({
		drop: function(event, ui) {
			
			var id_cent = $(ui.draggable).attr('id');
			
			var classe = $(ui.draggable).attr('class');
			//ONLY FOR CENTS
			if (contains(classe, 'cent')) {
					
				//IF HOMER IS NOT HUNGRY
				var hungry = isHungry();
				if (!hungry) { 
					//CHANGE SCORE LABEL
					incrementLabel('#score', 'score', 50);					
				}									
				//AJAX FUNCTION	
				piggyBankAjax(id_cent);
				
				checkTrophies();
				
				//After hide andcancel draggable property	
				$(ui.draggable).hide('slide', {direction: 'down'}, PIGGY_BANK_FREQUENCY).css({opacity: 1}).draggable('disable');
			}
		}		
	});
}
/**
 * Fires when puts money in the donut shop
 */
function buyDonut() 
{
	$('#donut-shop-img').droppable({
		drop: function(event, ui) {
			
			var id_money = $(ui.draggable).attr('id');			
			var classe = $(ui.draggable).attr('class');
			
			//ONLY FOR CENTS AND DOLLARS
			if (contains(classe, 'cent') || contains(classe, 'dollar')) {
								
				//Cents or dollars
				if (contains(classe, 'cent')) {
					addDonutCredit(id_money, 5);					
					
				} else {				
					
					//AJAX FUNCTION	DOLLARS
					buyDonutAjax(id_money, 'dollar');
					//1 dollar = 100 cents = 2 donut in the proper box
					addDonut(2);
				}
									
				//After cancel draggable property
				$(ui.draggable).hide('scale', BUY_FREQUENCY).css({opacity: 1}).draggable('disable');				
			}
		}		
	});
}
/**
 * Fires when puts money in the duff shop
 */
function buyDuff() 
{
	$('#duff-shop-img').droppable({
		drop: function(event, ui) { 
			
			var id_money = $(ui.draggable).attr('id');			
			
			var classe = $(ui.draggable).attr('class');
			//ONLY FOR CENTS AND DOLLARS
			if (contains(classe, 'cent') || contains(classe, 'dollar')) {
													
				//Cents or dollars
				if (contains(classe, 'cent')) {
					addDuffCredit(id_money, 5);
					
				} else {					
					//AJAX FUNCTION	DOLLARS
					buyDuffAjax(id_money, 'dollar');
					//1 dollar = 100 cents = 1 duff in the proper box
					addDuff(1);
				}	
								
				$(ui.draggable).hide('scale', BUY_FREQUENCY);	
			}
		}		
	});
}

/**
 * Check if user has reached determined score
 */
function checkTrophies() 
{
	var score = parseInt(getScore()); 
			
	//CHECK TROPHIES 
	switch(true) {
		
		case ( score >= 200 && score < 500):	
				
			if($.score.exScore < 200) {
				
				alert(getLanguageText('index_reached_bronze'));	
			
				setTrophy('bronze');
				
				$.score.exScore = score;
				
				$('#trophy').append('<img id="donut-bronze" src="' + DONUT_BRONZE + '" />');					
				
			}	break;			
			
		case ( score >= 500 && score < 1000):	
			
			if($.score.exScore < 500) {	
					
				alert(getLanguageText('index_reached_silver'));				
					
				setTrophy('silver');
				
				$.score.exScore = score;	
				
				$('#trophy').append('<img id="donut-silver" src="' + DONUT_SILVER + '" />'); 		
			}	
			break;
			
		case (score >= 1000 && score < 10000):	
			
			if($.score.exScore < 1000) {	
					
				alert(getLanguageText('index_reached_gold'));
				
				setTrophy('gold');
				
				$.score.exScore = score;					
				
				$('#trophy').append('<img id="donut-gold" src="' + DONUT_GOLD + '" />');
			}
			break;
			
		case (score >= 10000 && score < 50000):	
			
			if($.score.exScore < 10000) {	
					
				alert(getLanguageText('index_reached_sapphire'));
				
				setTrophy('sapphire');
				
				$.score.exScore = score;					
				
				$('#trophy').append('<img id="donut-bronze" src="' + DONUT_SAPPHIRE + '" />');
			}
			break;
		
		case (score >= 50000 && score < 250000):	
			
			if($.score.exScore < 50000) {	
					
				alert(getLanguageText('index_reached_ruby'));
				
				setTrophy('ruby');
				
				$.score.exScore = score;					
				
				$('#trophy').append('<img id="donut-silver" src="' + DONUT_RUBY + '" />');
			}
			break;
			
		case (score >= 250000):	
			
			if($.score.exScore < 250000) {	
					
				alert(getLanguageText('index_reached_emerald'));
				
				setTrophy('emerald');
				
				$.score.exScore = score;					
				
				$('#trophy').append('<img id="donut-gold" src="' + DONUT_EMERALD + '" />');
			}
			break;
	}	

}

