var checkflag = 'false';
function check (field) {
	if (checkflag == 'false') {
		for (i = 0; i < field.length; i++) {
			field[i].checked = true;
		}
		checkflag = 'true';
	}
	else {
		for (i = 0; i < field.length; i++) {
			field[i].checked = false;
		}
		checkflag = 'false';
	}
}

function reportingForm() {
	$('reasonBox').addEvent("click", function () {
		if ($('reasonBox').get('value') == 'Other') {
			$('reasonBoxOther').setStyle('display', 'block');
		}
		else {
			$('reasonBoxOther').setStyle('display', 'none');
		}
	});
}

var pstring;
var jcode;
var cid;
function vote(basePath, pageLang, compositionId, userId, option) {
	if (option == 'plus' || option == 'minus') {
		if (intval (compositionId) && (intval (userId) || userId == 0)) {
			pstring = basePath;
			jcode = pageLang;
			cid = compositionId;

			var voteRequest = new Request.HTML({url:basePath + '/vote.php?params=id:' + compositionId + '|user:' + userId + '|option:' + option + '|lang:' + pageLang,
				onSuccess: function(html) {
					$('voteConfirm' + compositionId).set('text', '');
					$('voteConfirm' + compositionId).adopt(html);
				},
				async: true
			});
			voteRequest.send();
			// Dumb cheat >_>
			updateResults();
			setTimeout("updateResults()", 200);
			setTimeout("updateResults()", 500);
			setTimeout("updateResults()", 1000);
			setTimeout("updateResults()", 1500);
		}
	}
}
function updateResults() {
	var basePath = pstring;
	var pageLang = jcode;
	var compositionId = cid;

	var updateRequest = new Request.HTML({url:basePath + '/vote.update.php?params=id:' + compositionId + '|lang:' + pageLang,
		onSuccess: function(html) {
			$('voteResults' + compositionId).set('text', '');
			$('voteResults' + compositionId).adopt(html);
		}
	});
	updateRequest.send();
}

function intval(mixed_var, base) {
    // version: 1006.1915
    // discuss at: http://phpjs.org/functions/intval
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: stensi
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   input by: Matteo
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    var tmp;
 
    var type = typeof( mixed_var );
 
    if (type === 'boolean') {
        return (mixed_var) ? 1 : 0;
    } else if (type === 'string') {
        tmp = parseInt(mixed_var, base || 10);
        return (isNaN(tmp) || !isFinite(tmp)) ? 0 : tmp;
    } else if (type === 'number' && isFinite(mixed_var) ) {
        return Math.floor(mixed_var);
    } else {
        return 0;
    }
}

function spellHandlers() {
	$('title').addEvent('keydown', function(event) {
		var titleLength;

		if (event.key != 'up' && event.key != 'down' && event.key != 'left' && event.key != 'right' && event.key != 'esc' && event.key != 'tab' && event.key != '$' && event.key != '#' && event.key != '"' && event.key != '!') {
			if (event.key == 'delete' || event.key == 'backspace') {
				if ($('title').get('value').length > 0) {
					titleLength = $('title').get('value').length - 1;
				}
				else {
					titleLength = 0;
				}
			}
			else {
				titleLength = $('title').get('value').length + 1;
			}

			$('submit').disabled = 0;
			if (titleLength <= 25)
				$('currentTitleLength').set('html', 'Current length: ' + titleLength + '/25');
			else {
				$('submit').disabled = 1;
				$('currentTitleLength').set('html', '<span class="error">Max length is 25</span>');
			}
		}
	});

	$('describe').addEvent('keydown', function(event) {
		var titleLength;

		if (event.key != 'up' && event.key != 'down' && event.key != 'left' && event.key != 'right' && event.key != 'esc' && event.key != 'tab' && event.key != '$' && event.key != '#' && event.key != '"' && event.key != '!') {
			if (event.key == 'delete' || event.key == 'backspace') {
				if ($('describe').get('value').length > 0) {
					titleLength = $('describe').get('value').length - 1;
				}
				else {
					titleLength = 0;
				}
			}
			else {
				titleLength = $('describe').get('value').length + 1;
			}

			if (titleLength <= 60)
				$('currentDescribeLength').set('html', 'Current length: ' + titleLength + '/60');
			else
				$('currentDescribeLength').set('html', '<span class="error">Max length is 60</span>');
		}
	});
}


function core() {
	// Search
	var initialSearchText = $('searchQuery').get('value');
	$('searchForm').addEvent("submit", function(e) {
		e.stop();
		var search = $('searchQuery').get('value');
		if (search != '')
			top.location.href = this.getProperty("action") + encodeURI(search.replace(/ /g, "+"));
	})
	$('searchQuery').addEvent("focus", function() {
		if ($('searchQuery').get('value') == initialSearchText)
			$('searchQuery').set('value', '');
	});
	
	// Social widget
	$('widget_social').addEvents({
		mouseenter: function() {
			this.morph({'right': '0px'})
		},
		mouseleave: function() {
			this.morph({'right': '-205px'})
		}
	});

	// Rankings panel
/*	$('ranksPanel').addEvents({
		mouseenter: function() {
			$('rank').addClass('active');
			$('rankings').morph({'display': 'block'})
		},
		mouseleave: function() {
			if (isRankingActive == false) {
				$('rank').removeClass('active');
			}
			$('rankings').morph({'display': 'none'})
		}
	});*/

	// Logo return link
	$('logo').addEvent("click", function() {
		top.location.href = '/';
	});
}
window.addEvent("domready", core);


FB.init({xfbml: true});
FB.XFBML.parse();
