function openSend(){
	window.open('http://redir.chatnsmile.com/4p1w/hu/send', "_system");
}

function openFb(){
	window.open('https://m.facebook.com/4Kep1Szo', "_system");
}

function changeContent(url){
	$('#content').load(url);
}

function loaded(){
	$.ajax({
        type: "GET",
		url: "xml/levels.xml",
		dataType: "xml",
		success: function(xml) {
			var lx=$(xml).find('level').each(function(){
				var lvlData=new Array();
				$(this).find('question').each(function(){
					lvlData[lvlData.length]={src: $(this).attr("src"), copyright: $(this).attr("copyright"), solution: $(this).attr("solution"), letters: $(this).attr("letters")};
				});
				levels[levelCount]={copyright: $(this).attr("copyright"), data: lvlData};
				levelCount++;
			});
			loadMenu();
		},
		async: false
	});
}

function loadMenu(){
	hideAll();
	$('#menu').css('display', 'block');
}

function loadLevels(){
	hideAll();
	$('#levels').css('display', 'block');
 	$('#copyright').html(levels[level].copyright);
 	var htx="";
 	var htxn="";
 	for(var i=0; i<levels[level].data.length; i++){
 		if(window.localStorage[level+'_'+i]=='ok'){
 			htx+="<img class='preview' onclick='loadImg(\""+i+"\");' src='drawable/"+levels[level].data[i].src+"_0.jpeg' />";
 		}
 		else{
 			htxn+="<img class='preview' onclick='loadImg(\""+i+"\");' src='drawable/"+levels[level].data[i].src+"_0.jpeg' />";
 		}
 	}
 	$('#okimagesdiv').html(htx);
 	$('#notokimagesdiv').html(htxn);
 	var lz=level+1;
 	$('#header').html(lz+". szint");
}

function loadImg(id){
	imgide=id;
	hideAll();
	$('#image').css('display', 'block');
	$('#sol').html("");
	
	var htx="";
	var copyrights;
	if(typeof levels[level].data[imgide].copyright === "undefined")
	{
		copyrights=new Array(levels[level].copyright, levels[level].copyright, levels[level].copyright, levels[level].copyright);
	}
	else{
		copyrights=levels[level].data[imgide].copyright.split(";");
	}
 	for(var i=0; i<4; i++){
 		htx+="<img class='four' onclick='alert(\"&copy; "+copyrights[i]+", "+levels[level].copyright+"\");' src='drawable/"+levels[level].data[imgide].src+"_"+i+".jpeg' />";
 	}
 	$('#fourimgs').html(htx);
	 if(window.localStorage[level+'_'+imgide]!='ok'){
 		var xsole=shuffle(levels[level].data[imgide].letters.split(''));
 		var xtx="";
 		for(var i=0; i<xsole.length; i++){
 			xtx+="<div onclick='addletter(this);' class='button3'>"+xsole[i]+"</div>";
 	 	}
 		$('#letters').html(xtx);
 		ok=false;
 	}
 	else{
		$('#sol').html(levels[level].data[imgide].solution);
		$('#letters').html("Helyes válasz!");
 		ok=true;
 	}
}

function addletter(z){
	var st=$('#sol').html();
	st+=$(z).html();
	$('#sol').html(st);
	$('#letters')[0].removeChild(z);
	if(st==levels[level].data[imgide].solution){
		window.localStorage[level+'_'+imgide]='ok';
		$('#letters').html("Helyes válasz!");
		ok=true;
	}
}

function shuffle(o){
    for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
    return o;
};

function nextLevel(){
	if(level<levelCount-1){
		level++;
		loadLevels();
	}
}

function prevLevel(){
	if(level>0){
		level--;
		loadLevels();
	}
}

function nextImg(){
	if(imgide<levels[level].data.length-1){
		imgide++;
		loadImg(imgide);
	}
}

function prevImg(){
	if(imgide>0){
		imgide--;
		loadImg(imgide);
	}
}

function loadImage(name){
	$('#content').innerHTML='';
}

function hideAll(){
	$('#menu').css('display', 'none');
	$('#levels').css('display', 'none');
	$('#image').css('display', 'none');
}

function removeletter(){
	if(ok){
		loadLevels();
	}
	var st=$('#sol').html();
	if(st.length>0){
		var nli=st.substring(st.length-1);
		st=st.substring(0, st.length-1);
		$('#sol').html(st);
		var xtx=$('#letters').html();
		xtx+="<div onclick='addletter(this);' class='button3'>"+nli+"</div>";
	 	$('#letters').html(xtx);
	}
}

var levelCount=0;
var level=0;
var imgide=0;
var levels=new Array();
var doc;
var ok=false;
window.onload=loaded;