var will_be_age_check_shown = 0; // priznak, jestli budu zobrazovat AgeCheck, pokud ano, vypnu TV

function createCookie(cookie_name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = cookie_name+"="+value+expires+"; path=/";
}
function readCookie(cookie_name) {
	var nameEQ = cookie_name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function checkTrue(){
	createCookie("RadegastAgeCheck","true",1);
	document.getElementById("age_check_screen").style.display = "none";
    document.getElementById("age_check_tab").style.display = "none";
    document.getElementById("age_check_header").style.display = "none";
	document.getElementById("age_check_tab").innerHTML= "";
    //window.location.reload(); // refresh stranky
}
function checkFalse(){
	createCookie("RadegastAgeCheck","false",1);
	
	// posuneme pozadi pullitru a zmenime text
	document.getElementById("age_check_bottle").style.backgroundPosition="0px -316px";
	document.getElementById("age_check_nadpis").style.backgroundImage="url(/_clip/age-check/tit-jenamlito.gif)";
	
    document.getElementById("ac-content").style.display = "none";
    document.getElementById("ac-content").innerHTML= "";
    document.getElementById("age_check_login").style.display = "none";
	document.getElementById("age_check_login").innerHTML= "";
}
function eraseCookie(cookie_name) {
	createCookie(cookie_name,"",-1);
}
function resizeLayer(){
	var winH = document.documentElement.scrollHeight;
	document.getElementById("age_check_screen").style.height= winH+"px";
}
function startAgeCheck(){
	/* var server = getServer(); */
	var checked = readCookie("RadegastAgeCheck");

	if(checked == null || checked == "false"){
		var sel = '<select name="rok-narozeni" id="rok-narozeni" class="year"><option value="">----</option>';
		var d = new Date();	var yr = d.getFullYear()-18;
		for (var i = yr; i >= 1900; i--)
		{
			sel = sel+'<option value="'+i+'">'+i+'</option>';
		}
		sel = sel+'</select>';
	    document.write('<div id="age_check_screen"></div><div id="age_check_header"><div id="age_check_bottle"></div></div>'+
				'<div id="age_check_tab"><div id="age_check_content"><div id="age_check_left_holder"><div id="age_check_left"></div></div>'+
				'<div id="age_check_main_holder"><div id="age_check_main"><div id="age_check_nadpis"></div>'+
				'<div id="ac-content"><p><strong>Zadejte den, měsíc a rok Vašeho narození:</strong></p>'+
				'<table class="form-box">'+
				'<tr><th>Den:</th><td>'+
				'<input type="button" value="&lt;" class="spinner" onmousedown="startDecrement(\'den-narozeni\', 1, 31);" onmouseup="stopDecrement(\'den-narozeni\')" onmouseout="stopDecrement(\'den-narozeni\')"/>'+
				'<input name="den-narozeni" id="den-narozeni" value="1" type="text" class="year"/>'+
				'<input type="button" value="&gt;" class="spinner" onmousedown="startIncrement(\'den-narozeni\', 1, 31);" onmouseup="stopIncrement(\'den-narozeni\')" onmouseout="stopIncrement(\'den-narozeni\')" />'+
				'</td></tr>'+
				'<tr><th>Měsíc:</th><td>'+
				'<input type="button" value="&lt;" class="spinner" onmousedown="startDecrement(\'mesic-narozeni\', 1, 12);" onmouseup="stopDecrement(\'mesic-narozeni\')" onmouseout="stopDecrement(\'mesic-narozeni\')"/>'+
				'<input name="mesic-narozeni" id="mesic-narozeni" value="1" type="text" class="year"/>'+
				'<input type="button" value="&gt;" class="spinner" onmousedown="startIncrement(\'mesic-narozeni\', 1, 12);" onmouseup="stopIncrement(\'mesic-narozeni\')" onmouseout="stopIncrement(\'mesic-narozeni\')" />'+
				'</td></tr>'+
				'<tr><th>Rok:</th><td>'+
				sel+
				'</td></tr>'+
				'<tr><td><a href="" onclick="checkAgeDate(); return false;" title="Vstoupit"><img src="/_clip/age-check/but-vstoupit-on.gif" alt="Vstoupit" class="btn"></a></td><td></td>'+
				'</tr></table>'+
				'</div><div id="age_check_login"><div class="ac-login-text"><p>Členové Kmene Radegast mohou rovnou vstoupit.</p></div><div class="ac-login-form"><div id="err_code"></div><div id="input-data"><span>Login</span>&nbsp;<input name="ach_login" id="ach_login" value="" type="text" />&nbsp;<span>Heslo</span>&nbsp;<input name="ach_pswd" id="ach_pswd" type="password" />'+
				'<input src="/_clip/age-check/buton-prihlasitse.gif" value="PŘIHLÁSIT" title="Přihlásit se do Radegast Kmene" class="ac-btn-login" onclick="login();" type="image" /></div></div><div class="ac-login-footer"></div><p class="cookie"><em>Pro správnou funkci stránek je nuté ve Vašem prohlížeci zapnout podporu cookies.</em></p></div></div></div></div></div>');
	    window.onresize=resizeLayer;
		window.onload=resizeLayer;
	}
	if (checked == "false") {
	    checkFalse();
	}
}
function getServer(){
	var host = window.location.toString();
	host = host.split('://');
	var protocol = host[0];
	host = window.location.host;

	return protocol+"://"+host;
}


function checkAgeDate()
{
	url = "&den=" + document.getElementById("den-narozeni").value;
	url += "&mesic=" + document.getElementById("mesic-narozeni").value;
	url += "&rok=" + document.getElementById("rok-narozeni").value;
		
	document.getElementById('age_check_nadpis').innerHTML = "";
	
    var ajax = new EasyAjax('POST', url, '/smallapp/ajax/agecheckdate/');
    ajax.onSuccess("checkDateCallback");
    ajax.doRequest();
    
    return false;	
}

function checkDateCallback(data)
{
	var res = eval("("+data+")");
	switch (res.status)
	{
		//case 'ERR_EMPTY':
		//	document.getElementById('age_check_nadpis').innerHTML = "Musite uvest mesic a rok narozeni";
		//	break;
		case 'ALL_OK':
			checkTrue();
			break;
		case 'ERR_TOOYOUNG':
			checkFalse();
			break;	
	}
}

function spinDecrement(item, min, max) {
	element = document.getElementById(item);
	if(isNaN(element.value)) {
		element.value = Math.round((max+min) / 2);
	} else {
		if(element.value <= min) {
			element.value = max;
		} else {
			element.value = Number(element.value) - 1;
		}
	}
}

function spinIncrement(item, min, max) {
	element = document.getElementById(item);
	if(isNaN(element.value)) {
		element.value = Math.round((max+min) / 2);
	} else {
		if(element.value >= Number(max)) {
			element.value = min;
		} else {
			element.value = Number(element.value) + 1;
		}
	}
}

function startDecrement(item, min, max) {
	spinDecrement(item, min, max);
	if( (window) && (window['spin'+item]) ) {
	} else {
		window['spin'+item] = setInterval("spinDecrement('"+item+"', '"+min+"', '"+max+"')", 200);
	}
}

function stopDecrement(item) {
	if( (window) && (window['spin'+item]) ) {
		clearInterval(window['spin'+item]);
		window['spin'+item] = false;
	}
}

function startIncrement(item, min, max) {
	spinIncrement(item, min, max);
	if( (window) && (window['spin'+item]) ) {
	} else {
		window['spin'+item] = setInterval("spinIncrement('"+item+"', '"+min+"', '"+max+"')", 200);
	}
}

function stopIncrement(item) {
	if( (window) && (window['spin'+item]) ) {
		clearInterval(window['spin'+item]);
		window['spin'+item] = false;
	}
}

startAgeCheck();
