var messages = new Array();
messages['ALL_OK'] = 'OK';
messages['ER_EXISTS'] = 'ER_EXISTS';
messages['MS_EMPTY'] = 'MS_EMPTY';

// get browser type
function getBrowserType()
{
	if (/Konqueror|KHTML/.test(navigator.userAgent) && !/Apple/.test(navigator.userAgent))
    {
    	return "KHTML";
    }
    
    if (/KHTML/.test(navigator.userAgent) && /Apple/.test(navigator.userAgent))
    {
    	return "Safari";
    }
    
    if (!!window.opera)
    {
    	return "Opera";
    }
    
    if (!!(window.attachEvent && !window.opera))
    {
    	return "MSIE";
    }
    
    if (/Gecko/.test(navigator.userAgent) && !/Konqueror|KHTML/.test(navigator.userAgent))
    {
    	return "Gecko";
    } 
}


function checkProfileId()
{
	id = document.getElementById('profile_url').value;
	
	var ajax = new EasyAjax("POST", "profile_id="+id, "/uneseni/checkprofileid");
    //ajax.onLoading("loading");
    ajax.onSuccess("getProfileIdStatus");
    ajax.doRequest();
}

function getProfileIdStatus(json)
{
	var res = eval("("+json+")");
	
	var imageOk = '<img src="/_clip/s_okay.png" alt=""/>';
	var imageEr = '<img src="/_clip/s_warn.png" alt=""/>';
	var imageMs = '<img src="/_clip/s_info.png" alt=""/>';
	
	var msg = '<span>'+messages[res.status]+'</span>';
	
	switch (res.status)
	{
		case "ALL_OK":
			document.getElementById('profile_url_knob').innerHTML = imageOk + msg;
			break;
		case "ER_EXISTS":
			document.getElementById('profile_url_knob').innerHTML = imageEr + msg;
			break;
		case "MS_EMPTY":
			document.getElementById('profile_url_knob').innerHTML = imageMs + msg;
			break;
	}
}	

function getNextProfile(curr)
{
	var ajax = new EasyAjax("POST", "profile_id="+curr, "/uneseni/getnextprofile/");
    //ajax.onLoading("loading");
    ajax.onSuccess("getProfileStatus");
    ajax.doRequest();
    //return false;
}

function getProfileStatus(json)
{
	var type = getBrowserType();
	
	var res = eval("("+json+")");
	//alert(json);
	if (res.status == "ALL_OK")
	{
		fotoFrame = getEl('foto-frame');
		if (res.profile.foto)
		{
			fotoFrame.innerHTML = '<img src="http://foto.gambrinus.cz/uneseni/profily/'+res.profile.unique_id+'/medium/'+res.profile.foto+'" alt=""/>';
		} else {
			fotoFrame.innerHTML = '<img src="/microsite/uneseni/_images/no-image.jpg" alt=""/>';
		}
		
		var viewLink = getEl('view-link');
		var nextLink = getEl('next-link');
		var yesLink = getEl('yes-link');
		var noLink = getEl('no-link');
		
		var name = getEl('jmeno');
		var nick = getEl('nick');
		var mesto = getEl('mesto');
		var vek = getEl('vek');
		var poslatprofil = getEl('poslatprofil');
		
		viewLink.setAttribute('href', '/uneseni/na-internetu/profil/'+res.profile.profile_url+'/')
		if (yesLink) yesLink.setAttribute('href', "javascript:votePositive('"+res.profile.profile_url+"')");
		if (noLink) noLink.setAttribute('href', "javascript:voteNegative('"+res.profile.profile_url+"')");

		if (type == 'MSIE')
		{
			nextLink.onclick = function(){getNextProfile(res.profile.unique_id); return false;};
			//if (yesLink) yesLink.onclick = function(){votePositive(res.profile.profile_url); return false;};
			//if (noLink) noLink.onclick = function(){voteNegative(res.profile.profile_url); return false;};
			if (poslatprofil) poslatprofil.onclick = function(){poslatProfil(res.profile.unique_id); return false;};
		} else {
			nextLink.setAttribute('onclick', "getNextProfile('"+res.profile.unique_id+"'); return false;")
			//if (yesLink) yesLink.setAttribute('onclick', "votePositive('"+res.profile.profile_url+"'); return false;");
			//if (noLink) noLink.setAttribute('onclick', "voteNegative('"+res.profile.profile_url+"'); return false;");
			if (poslatprofil) poslatprofil.setAttribute('onclick', "poslatProfil('"+res.profile.unique_id+"'); return false;");
		}
		
		document.getElementById('unos-ajax-response').style.display = 'none';
		hideLoginForm();
		if (name) name.innerHTML = res.profile.jmeno + ' ' + res.profile.prijmeni;
		if (nick) nick.innerHTML = res.profile.nick;
		if (mesto) mesto.innerHTML = res.profile.mesto;
		if (vek) vek.innerHTML = res.profile.vek;
		if (poslatprofil) poslatprofil.setAttribute('href', '/uneseni/na-internetu/poslatprofil/?id='+res.profile.unique_id);
	}
}

function voteYes(id)
{
	alert('voted yes for id '+ id);
}

function voteNo(id)
{
	alert('voted no for id '+ id);
}

function poslatProfil(id)
{
	alert('poslat profil clicked for id '+ id);
}

function setDefaultPhoto(id, profileId)
{

	var curr = getEl('currentId').value;
	
	if (id == curr) return false;
	
	var ajax = new EasyAjax("POST", "sel_photo="+id+"&profile_id="+profileId, "/uneseni/setdefaultphoto/");
    ajax.onSuccess("getPhotoUpdateStatus");
    ajax.doRequest();
}

function getPhotoUpdateStatus(json)
{
	var res = eval("("+json+")");
	if (res.status == "OK")
	{
		var curr = getEl('currentId');

		div = getEl('foto-'+curr.value);
		if (div) div.setAttribute((document.all ? 'className' : 'class'), 'foto-blok');
		
		curr.value = res.id;
		
		div = getEl('foto-'+res.id);
		div.setAttribute((document.all ? 'className' : 'class'), 'foto-blok selected-foto');
	}
}

function getEl(id)
{
	return document.getElementById(id);
}

function playVideoPopup(path, file){

  var popup = getEl("videoPopup");
  var width = 560;
  var height = 448;

	var html = '<div id="video_tab"><div class="container" style="width:'+width+'px;">';
	html+= '<embed src="http://www.gambrinus.cz/_swf/flvplayer.swf" width="'+width+'" height="'+height+'" allowfullscreen="true" flashvars="&displayheight='+height+'&file='+path+'/'+file+'&width='+width+'&location=http://www.gambrinus.cz/_swf/flvplayer.swf" />';
	html+='<a href="" onclick="stopVideoPopup(); return false;">zavřít</a></div></div>';
	
	
//	var swf = getEl("player");
//	var flashVars = swf.getAttribute('flashvars')+'&file='+path+'/'+file;
//	swf.setAttribute('flashvars', flashVars);

	popup.innerHTML = html;
	
	popup.style.display = 'block';

	scroll(0,0);
	
//	window.onresize=resizeBgLayer;
//	resizeBgLayer();

}

function playVideoPohlednice(id, message){
	if (will_be_age_check_shown) return false;	
  var popup = getEl("videoPopup");
  var width = 384;
  var height = 320;

  var html = '<div id="video_tab"><div class="container" style="width:'+width+'px;">';
    html+= '<object type="application/x-shockwave-flash" data="/microsite/uneseni/na-internetu/video-pohlednice/v5.swf" width="384" height="320">';
	html+= '<param name="movie" value="/microsite/uneseni/na-internetu/video-pohlednice/v5.swf">';
	html+= '<param name="wmode" value="transparent" />';
	html+= '<param name="flashvars" value="profilID='+id+'" />';
	html+= '</object>';
	if (message)html+= '<p>Tuto video pohlednici s údaji ze svého profilu můžeš odeslat po rozkliknutí odkazu "<strong>Poslat video e-mailem</strong>".</p>';
	html+='<a href="" onclick="stopVideoPopup(); return false;">zavřít</a></div></div>';
	popup.innerHTML = html;
	
	popup.style.display = 'block';
	
	scroll(0,0);

}
function stopVideoPopup(){

  var popup = getEl("videoPopup");
  popup.style.display = 'none';
  //popup.innerHTML = "";

}

function resizeBgLayer(){
	var winH = document.documentElement.scrollHeight;
	getEl("uneseni-pozadi").style.height= winH+"px";
}

function sendProfileToFriend(){
	var id = document.getElementById('profile_id').value;
	var name = document.getElementById('name').value;
	var from = document.getElementById('emailfrom').value;
	var recipient = document.getElementById('emailto').value;
	var text = document.getElementById('vzkaz').value;
  var type = 1;
  var errors = new Array();
  
  var recipients = recipient.split(",");
  if(recipients.length>1){
    for(var key in recipients){
      if(!validMail(recipients[key])){
        errors.push("Jedna nebo více adres příjemce má nesprávný formát");
        break;
      }
    }
  }else{
    if(!validMail((trim(recipient)))){
      errors.push("Zadaný e-mail příjemce má nesprávný formát");
    }
  }

  if(trim(name) == ""){
    errors.push("Musíte uvést své jméno");
  }
  
  if(trim(from) == ""){
    errors.push("Musíte uvést svůj e-mail");
  }else if(!validMail((trim(from)))){
    errors.push("Zadaný e-mail má nesprávný formát");
  }
  
  if(trim(text) == ""){
    errors.push("Musíte napsat text vzkazu");
  }
  
  if(errors.length == 0){
  
    if(document.getElementById('type2') != null && document.getElementById('type2').checked){
      type=2;
    }

    var ajax = new EasyAjax("POST", "name="+encodeQuery(name)+"&type="+type+"&from="+encodeQuery(from)+"&recipient="+encodeQuery(recipient)+"&text="+encodeQuery(text)+"&pid="+encodeQuery(id), "/smallapp/ajax/sendprofiletofriend");
    ajax.onSuccess("profileSended");
    ajax.doRequest();
  }else{
    document.getElementById("message").innerHTML = errors.join("<br />");
  }
}
function profileSended(data){
  var res = eval("("+data+")");
  if(res["status"] == 1){
    document.getElementById("message").innerHTML = "Zpráva byla odeslána";
    setTimeout("closeSendForm()",1000);
  }else{
    document.getElementById("message").innerHTML = "Při odesílání zprávy nastala chyba";
  }
}
function closeSendForm(){
	document.getElementById("sendToFriendBox").style.display="none";
}
function encodeQuery(str) {
str = encodeURIComponent(str);
str = str.replace("~", '%7E');
str = str.replace("!", '%21');
str = str.replace("'", '%27');
str = str.replace('(', '%28');
str = str.replace(')', '%29');
return str;
}
function validMail(string){
 var mail=/^.+@.+\..{2,4}$/
 return mail.test(string);
}
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

//

function showLoginBox()
{
	var loginbox = document.getElementById("loginbox");
	var age_check = document.getElementById("uneseni-pozadi");
	loginbox.style.display = 'block';
	//age_check.style.display = 'block';
}
function hideLoginBox()
{
	var loginbox = document.getElementById("loginbox");
	var age_check = document.getElementById("uneseni-pozadi");
	loginbox.style.display = 'none'
	//age_check.style.display = 'none';
}

function getUserQueue()
{
	var ajax = new EasyAjax("POST", "pid="+profileId, "/uneseni/getuserqueue/");
    //ajax.onLoading("loading");
    ajax.onSuccess("getUserQueueStatus");
    ajax.doRequest();
}

var isQueueEmpty = true;

function getUserQueueStatus(json)
{
	var res = eval("("+json+")");
	var el = getEl('queue');
	if (res.status == 'EMPTY')
	{
		//el.style.display = 'none';
		if (!isQueueEmpty)
		{
			switch (jQuery("#profile_tabs").tabs().data('selected.tabs'))
			{
				case 0:
					var lnk = window.location.href;
					break;
				case 1:
					var lnk = 'http://www.gambrinus.cz/uneseni/na-internetu/reloadprofileform/?tab=fototab';
					break;
				case 2:
					var lnk = 'http://www.gambrinus.cz/uneseni/na-internetu/reloadprofileform/?tab=videotab';
					break;
			}
			el.innerHTML = 'Všechny soubory byly zpracovány. Kliknutim <a href="'+lnk+'">zde</a> obnov stránku.';
			//window.location.href = lnk;
		}
	} else {
		isQueueEmpty = false;
		el.style.display = 'block';
		
		el.innerHTML = 'Právě teď máš tyto soubory ve frontě na zpracování:';
		el.innerHTML += res.queue;
	}
	setTimeout("getUserQueue()", 5000);
}

function flash(file,width,height,flashvars,winmode,id,classs,alternative) {

	var shockmode = false;
	if(id!=''){
		id=" id=\""+id+"\"";
	}
	if(classs!=''){
		classs=' class="'+classs+'"';
	}
	var mimetype = 'application/x-shockwave-flash';

	if (navigator.mimeTypes) {
		if (navigator.mimeTypes[mimetype] != null) {
			if (navigator.mimeTypes[mimetype].enabledPlugin != null) {
				shockmode = true;
		      }
		}
	}

	if (!shockmode && navigator.appVersion.indexOf('MSIE') !=-1) {
		shockmode = true;
	}
	if (shockmode) {
		document.write("<object"+id+" type=\"application/x-shockwave-flash\" data=\""+file+"\""+classs+" width=\""+width+"\" height=\""+height+"\">");
		document.write("<param name=\"movie\" value=\""+file+"\" />");
		document.write("<param name=\"menu\" value=\"false\" />");
		document.write("<param name=\"wmode\" value=\""+winmode+"\" />");
		if(flashvars!=''){
			document.write("<param name=\"flashvars\" value=\""+flashvars+"\" />");
		}
		document.write(alternative);
		document.write("</object>");
	}
	else {
		document.write(alternative);
	}
}
function getFlashMovieObject(movieName)
{
  if (window.document[movieName])
  {
      return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1)
  {
    if (document.embeds && document.embeds[movieName])
      return document.embeds[movieName];
  }
  else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
  {
    return document.getElementById(movieName);
  }
}
function PlayFlashMovie(anim)
{
	try {
		var flashMovie=getFlashMovieObject(anim);
		flashMovie.Play();
		//embed.nativeProperty.anotherNativeMethod();
	} catch(e) {
		// nejaky problem s flashem, co s tim?
	}
}

function redirectTo(lnk)
{
	window.location.href = lnk;
}


function setFileStatus(status, id, type)
{
	var el = getEl('sendmail'+id);
	var sm = 0;
	if (el && el.checked && status == 'BAD') sm = 1;
	
	var ajax = new EasyAjax("POST", "file_id="+id+"&status="+status+"&type="+type+"&sm="+sm, "/uneseni/setfilestatus");
    //ajax.onLoading("loading");
    ajax.onSuccess("setFileStatusResponse");
    ajax.doRequest();
}

function setFileStatusResponse(json)
{
	var res = eval("("+json+")");
	
	switch (res.status)
	{
		case 'NOADMIN':
			alert('YOU HAVE NO PERMISSION');
			break;
		case 'ERROR':
			alert('Some error happened');
			break;
		case 'OK':
			var filediv = getEl('file-'+res.id);
			var pNode = filediv.parentNode;
			
			switch (pNode.id)
			{
				case 'tocontrol':
					var nNode = getEl((res.set == 'BAD') ? 'denied':'allowed');
					nNode.appendChild(filediv);
					break;
				case 'allowed':
					if (res.set == 'BAD') {
						var nNode = getEl('denied');
						nNode.appendChild(filediv);
					}
					break;
				case 'denied':
					if (res.set != 'BAD') {
						var nNode = getEl('allowed');
						nNode.appendChild(filediv);
					}
					break;
								
			}
			
			if (res.set == 'BAD')
			{
				filediv.setAttribute((document.all ? 'className' : 'class'), 'file-blok invalid-file');
			} else {
				filediv.setAttribute((document.all ? 'className' : 'class'), 'file-blok');
			}
			
			break;
	}
}

var editorPid = '';
var editorPhoto = '';

function openEditorWindow(pid, photo)
{
	editorPid = pid;
	editorPhoto = photo;
	var editorPopupWin = window.open('http://www.gambrinus.cz/uneseni/na-internetu/editor/', 'Nech se unest', 'width=650,height=300,toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=no,scrollbars=no');
	editorPopupWin.focus();
}

////////// hra

// Otevreni pop up okna - dokument
var docWin=null;
function DocOpen(DocName,w,h,idw) //DocName - jmeno souboru,w - sirka,h - vyska,idw - identifikator okna
{
	if (!idw) cesta = "Gambrinus";		//def hodnota
	 w+=2; h+=2;
	winFeatures="width="+w+",height="+h+",toolbar=no,location=no,"+
	"directories=no,status=no,menubar=no,resizable=no,scrollbars=no";
	docWin = "";
	docWin = window.open(DocName, idw, winFeatures);
	window.open(DocName, idw, winFeatures);
}

function odhazovani() {
  if (!arguments[0]) cid = 309;
  else cid = arguments[0];
  DocOpen("/souteze/souteze-o-ceny/2008-06_odhazovani/src/"+cid,600,370,"Odhazovani");
}
