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


function checkTrue(){
	createCookie("GambrinusAgeCheck","true",1);
	// document.getElementById("age_check").style.display= "none";
	// document.getElementById("age_check_tab").style.display= "none";
	// document.getElementById("age_check_tab").innerHTML= "";
	window.location.reload(); // refresh stranky
}
function checkFalse(){
	createCookie("GambrinusAgeCheck","false",1);

	document.getElementById("age-check-phrase").innerHTML= '<h2>Pro vstup na tyto stránky musíte být starší osmnácti let.</h2><p class="leave-link">&raquo; <a href="http://www.iniciativapivovaru.cz/" title="Budete přesměrováni na stránky IZP"><strong>Iniciativa zodpovědných pivovarů</strong></a></p>';
	document.getElementById("ac-login-box").innerHTML= '';
}

function resizeLayer(){
	var winH = document.documentElement.scrollHeight;
	ac = document.getElementById("age_check");
	if (ac)	ac.style.height= winH+"px";
}
function checkTvQualityRadio() {
	var tv_quality = getTvQuality();
	if ( tv_quality == 0 ) document.getElementById("quality0").checked = true;
	if ( tv_quality == 1 ) document.getElementById("quality1").checked = true;
	if ( tv_quality == 2 ) document.getElementById("quality2").checked = true;
}
function startAgeCheck(){
	var server = getServer();
	var checked = readCookie("GambrinusAgeCheck");

	if(checked == null || checked == "false"){

		//document.write('<img src="/_clip/agecheck_0' + (Math.floor(1 + Math.random() * 3)) + '.jpg" alt="" />');
		document.write('<div id="age_check"></div><div id="age_check_tab"><div class="container bg_agecheck_0' + (Math.floor(1 + Math.random() * 4)) +'"><div class="ac-content">'+
				'<h1 class="hide">www.gambrinus.cz</h1><div class="age-check-phrase" id="age-check-phrase">'+
				'<h2>Pro vstup na stránky musíš být starší 18 let<br />nebo osoba podléhajíci právu USA starší 21 let.</h2>'+
				'<p class="pokyn">Vyber den, měsíc a zadej rok svého narození:</p>'+
				'<table class="inputs"><tbody>'+
				'<tr><td>Den:<br />'+
				'<input type="button" value="&lt;" onmousedown="startDecrement(\'den-narozeni\', 1, 31);" onmouseup="stopDecrement(\'den-narozeni\')" onmouseout="stopDecrement(\'den-narozeni\')" class="next-prev_btns" />'+
				'<input name="den-narozeni" id="den-narozeni" value="1" type="text" class="day"/>'+
				'<input type="button" value="&gt;" onmousedown="startIncrement(\'den-narozeni\', 1, 31);" onmouseup="stopIncrement(\'den-narozeni\')" onmouseout="stopIncrement(\'den-narozeni\')" class="next-prev_btns" />'+
				'</td>'+
				'<td>Měsíc:<br />'+
				'<input type="button" value="&lt;" onmousedown="startDecrement(\'mesic-narozeni\', 1, 12);" onmouseup="stopDecrement(\'mesic-narozeni\')" onmouseout="stopDecrement(\'mesic-narozeni\')" class="next-prev_btns" />'+
				'<input name="mesic-narozeni" id="mesic-narozeni" value="1" type="text" class="month"/>'+
				'<input type="button" value="&gt;" onmousedown="startIncrement(\'mesic-narozeni\', 1, 12);" onmouseup="stopIncrement(\'mesic-narozeni\')" onmouseout="stopIncrement(\'mesic-narozeni\')" class="next-prev_btns" />'+
				'</td>'+
				'<td>Rok:<br />'+
				'<input name="rok-narozeni" id="rok-narozeni" value="" type="text" class="year"/>'+
				'</td>'+
				'<td class="vstoupit"><a href="javascript:void(0);" onclick="checkAgeDate(); return false;"title="Vstoupit"><img src="/_clip/age-check/2010/btn_vstoupit.gif"alt="Vstoupit"class="btn-vstoupit"></a></td></tr>'+
				'<tr><th colspan="4" id="age_check_message"></th></tr>'+
				'</tbody></table></div><div class="ac-login-box" id="ac-login-box"><h3>Členové našeho G-klubu se mohou rovnou přihlásit.</h3><div id="ac-login-form" class="ac-login-form"><span>Login: </span><input name="ach_login"id="ach_login"type="text"value=""/> <span>Heslo:</span> <input name="ach_pswd"id="ach_pswd"type="password"/> <input type="button"value="Přihlásit"title="Přihlásit se do G-klubu"class="ac-btn-login"onclick="login();"/></div><div id="err_code"></div></div></div><div class="clear"></div></div><p class="ac-info">Pro správné zobrazení stránek je třeba mít zapnutou podporu cookies ve svém prohlížeči.</p></div>');

    will_be_age_check_shown = 1;

		window.onresize=resizeLayer;
		window.onload=resizeLayer;
		window.onload=checkTvQualityRadio;
	}else{
	    will_be_age_check_shown = 0;
	}
}
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;
		
    var ajax = new EasyAjax('POST', url, '/smallapp/ajax/agecheckdate/');
    ajax.onSuccess("checkDateCallback");
    ajax.doRequest();
    
    return false;	
}

function checkDateCallback(data)
{
	var server = getServer();
	var res = eval("("+data+")");
	switch (res.status)
	{
		case 'ERR_EMPTY':
			document.getElementById('age_check_message').innerHTML = "Musíte uvést rok narození";
			break;
		case 'ALL_OK':
			checkTrue();
			break;
		case 'ERR_TOOYOUNG':
			checkFalse();
			window.location(server+'/opustit-stranky.php');
			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();
