var urlRoot = 'http://' + location.host + '/';

var images = new Array('passatempo/1.png',
                       'passatempo/2.png',
                       'passatempo/3.png',
                       'passatempo/4.png',
                       'passatempo/5.png',
                       'passatempo/6.png',
                       'passatempo/ecra1.png',
                       'passatempo/ecra2.png',
                       'passatempo/enviarAmigos.png',
                       'passatempo/participar.png',
                       'passatempo/regulamento.png',
                       'passatempo/resultadoCerto.png',
                       'passatempo/resultadoErrado.png',
                       'passatempo/top40.png');
images.each( function(item) {
	var img = new Image();
	img.src = 'http://www.millennium-ofilme.com/img/' + item;
});
window.addEvent('domready', function() {
	
	/**
	 * Forms
	 */
	$$('.error').each(function(item) {
		item.addEvent('change', function() { item.removeClass('error'); });
	});
	
	/**
	  */
	$$('.inputDate').each(function(item) {
		var elems = new Array();
		
		elems[0] = item.getChildren('.years');
		if( elems[0].length < 1 ) { return false; }
		
		elems[1] = item.getChildren('.months');
		if( elems[1].length < 1 ) { return false; }
		
		var date_onchange_func = function() { wd_select_days(this); }
		date_onchange_func = date_onchange_func.bind(item);
		
		for(var i = 0; i < 2; ++i) {
			elems[i].addEvent('change', function() { date_onchange_func(); });
		}
		wd_select_days(item);
	});
	
	if( $$('body')[0].get('id') == 'pg-game' ) {
		showTop();
		$$('a').each(function(item) {
			item.addEvent('click', function(ev) { ev.preventDefault(); })
		})
	}
});

var saveBody = true;
var elemTmpBody = null;
function hideBody() {
	var elemBody = $('body');
	
	if( saveBody ) {
		elemTmpBody = elemBody.clone(true, true);
		elemTmpBody.set('display', 'none');
		saveBody = false;
	}
	
	var elem = new Element('div', {id: 'body'});
	elem.replaces(elemBody);
	elemBody.destroy();
	
	return true;
}

function showBody() {
	var elemBody = $('body');
	
	if( elemTmpBody ) {
		elemTmpBody.replaces(elemBody);
		elemTmpBody.set('display', 'block');
		elemBody.destroy();
		elemTmpBody = null;
		saveBody = true;
	}
	
	return true;
}

function showTop() { showTopData({}); }

function showTopData(data) {
	hideBody();

	var url = urlRoot + 'html/top.php';
	var req = new Request.HTML({
	    url: url,
	    method: 'post',
	    data: data,
		onSuccess: function(html) {
			var elemBody = $('body');
			elemBody.set('text', '');
			elemBody.adopt(html);
			$('doSendScore').addEvent('click', function() {
				var value = $('sendScoreEmail').get('value');
				showTopData({doSendScore: 'Enviar', email: value});
			});
		},
		onFailure: function() {
			showBody(); alert('Ocorreu um erro.');
		}
	});
	req.send();
	return false;
}

function showRules() {
	hideBody();

	var url = urlRoot + 'html/regulamento.php';
	var req = new Request.HTML({
	    url: url,
		onSuccess: function(html) {
			var elemBody = $('body');
			elemBody.set('text', '');
			elemBody.adopt(html);
		},
		onFailure: function() {
			showBody(); alert('Ocorreu um erro.');
		}
	});
	req.send();
	return false;	
}

function showHowJoin() {
	hideBody();

	var url = urlRoot + 'html/comoParticipar.php';
	var req = new Request.HTML({
	    url: url,
		onSuccess: function(html) {
			var elemBody = $('body');
			elemBody.set('text', '');
			elemBody.adopt(html);
		},
		onFailure: function() {
			showBody(); alert('Ocorreu um erro.');
		}
	});
	req.send();
	return false;	
}














/**
 */
function wd_select_clean(select, data) { select.options.length = 0; }

/**
 */
function wd_select_push(select, label, value) {
	select.options[select.options.length] = new Option(label, value, false, false);
}

/**
 */
function wd_select_set(select, value) {
	var len = select.options.length;
	var options = select.options;
	for(var i = 0; i < len; ++i) {
		if( options[i].value == value ) { options[i].selected = true; }
		else { options[i].selected = false; }
	}
}

/**
 */
function wd_select_get(select) {
	var options = select.options;
	return options[select.selectedIndex].value;
}

/**
 */
function wd_days_in_month(year, month) {
	var dd = new Date(year, month, 0);
	return dd.getDate();
}

/**
 */
function wd_select_days(item) {
	var years_elem = item.getChildren('.years');
	years_elem = years_elem[0];
	
	var months_elem = item.getChildren('.months');
	months_elem = months_elem[0];
	
	var days_elem = item.getChildren('.days');
	days_elem = days_elem[0];
	
	var days_in_month = wd_days_in_month(years_elem.value, months_elem.value);
	
	var selected_day = wd_select_get(days_elem);
	wd_select_clean(days_elem);
	wd_select_push(days_elem, '(dia)', '');
	for(var i = days_in_month; i > 0; --i) { wd_select_push(days_elem, i, i); }	
	wd_select_set(days_elem, selected_day);
}
