var DDSPEED = 10;
var DDTIMER = 20;

function ddMenu(id, d) {
	var h = document.getElementById(id + '-ddheader');
	var c = document.getElementById(id + '-ddcontent');
	clearInterval(c.timer);
	if (d == 1) {
		clearTimeout(h.timer);
		if (c.maxh && c.maxh <= c.offsetHeight)
			return
		else if (!c.maxh) {
			c.style.display = 'block';
			c.style.height = 'auto';
			c.maxh = c.offsetHeight;
			c.style.height = '0px';
		}
		c.timer = setInterval(function(){ ddSlide(c, 1) }, DDTIMER);
	} else
		h.timer = setTimeout(function(){ ddCollapse(c) }, 50);
}

function ddCollapse(c) {
	c.timer = setInterval(function(){ ddSlide(c, -1) }, DDTIMER);
}

function cancelHide(id){
	var h = document.getElementById(id + '-ddheader');
	var c = document.getElementById(id + '-ddcontent');
	clearTimeout(h.timer);
	clearInterval(c.timer);
	if (c.offsetHeight < c.maxh)
		c.timer = setInterval(function(){ ddSlide(c, 1) }, DDTIMER);
}

function ddSlide(c,d){
	var currh = c.offsetHeight;
	var dist;
	if (d == 1)
		dist = (Math.round((c.maxh - currh) / DDSPEED));
	else
		dist = (Math.round(currh / DDSPEED));
	if (dist <= 1 && d == 1)
		dist = 1;
	c.style.height = currh + (dist * d) + 'px';
	c.style.opacity = currh / c.maxh;
	c.style.filter = 'alpha(opacity=' + (currh * 100 / c.maxh) + ')';
	if ((currh < 2 && d != 1) || (currh > (c.maxh - 2) && d == 1))
		clearInterval(c.timer);
}

function rules() {
	document.form.submit.disabled = true;
	if (document.form.agree.checked == true)
		document.form.submit.disabled = false;
	else
		document.form.submit.enabled = false;
}
		
function updateFlag() {
	document.images.flag.src = 'images/flags/' + document.forms.country.country.options[document.forms.country.country.selectedIndex].value + '.png';
}

var popupStatus = 0;

function loadPopup() {
		$("#background").css({
			"opacity": "0.8"
		});
		$("#background").fadeIn("slow");
		$("#popup").fadeIn("slow");
}

function disablePopup(){
	$("#background").fadeOut("slow");
	$("#popup").fadeOut("slow");
}

function centerPopup(){
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popup").height();
	var popupWidth = $("#popup").width();
	
	$("#popup").css({
		"position": "absolute",
		"top": windowHeight / 2 - popupHeight / 2,
		"left": windowWidth / 2 - popupWidth / 2
	});
	
	$("#background").css({
		"height": windowHeight
	});
	
}

function extendPopup() {
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = 500;
	var popupWidth = 800;

	$("iframe").css({
		"height": popupHeight - 36,
		"width": popupWidth - 10,
		"display": "inline"
	});
	
	$("#vote").css({
		"display": "none"
	});
	
	$("#popup").css({
		"height": popupHeight,
		"width": popupWidth,
		"position": "absolute",
		"top": windowHeight / 2 - popupHeight / 2,
		"left": windowWidth / 2 - popupWidth / 2
	});	
}

$(document).ready(function() {
	$("#background").click(function(){
		disablePopup();
	});

	$(document).keypress(function(e){
		if (e.keyCode == 27 && popupStatus == 1)
			disablePopup();
	});
	
	$(document).oneTime(5000, function() {
		$("#info").fadeOut("slow");
	});
	
	$(".click").click(function() {
		extendPopup();
	});
	
	$(".close").click(function() {
		disablePopup();
	});
});

function toggle(ansID) {
	if (document.getElementById(ansID).className == "hide")
		document.getElementById(ansID).className = "show";
	else
		document.getElementById(ansID).className = "hide";
}

function rules() {
	document.form.submit.disabled = true;
	if (document.form.agree.checked == true)
		document.form.submit.disabled = false;
	else
		document.form.submit.enabled = false;
}