$(document).ready(function(){
	$("#odczytane").keydown(function(e){
		if (e.keyCode == 13) {
			e.preventDefault();
			saveQuestionReadTime();
			$(this).blur();
			return false;
		}
	});	
});

function pokaz(id) {
	document.getElementById("wiecej"+id).style.display="none";
	document.getElementById("rozw"+id).style.display="block";
}

function sprawdzFormularz() {
	if (document.getElementsByName("s_nazwa")[0].value.length<1) {
		alert("Wymagana jest nazwa strony");
		document.getElementsByName("s_nazwa")[0].focus();
		return false;
	}
	str = document.getElementsByName("s_tid")[0].value;
	if (str.length<1) {
		alert('Wymagana jest nazwa strony w jej adresie w konwencji "nazwa_w_adresie".');
		document.getElementsByName("s_tid")[0].focus();
		return false;
	}	
	for (i=0;i<str.length;i++)
	{
		if ((str.substring(i,i+1)<"A" || str.substring(i,i+1)>"Z") && (str.substring(i,i+1)<"a" || str.substring(i,i+1)>"z") && (str.substring(i,i+1)<"0" || str.substring(i,i+1)>"9") && str.substring(i,i+1)!="_" && str.substring(i,i+1)!="-") {
			alert("Niedozwolony znak w adresie.");
			document.getElementsByName("s_tid")[0].focus();
			return false;
		}
	}
	return true;
}

function goWhenConfirmed(targetLocation) {	
	if (window.confirm("Czy na pewno chcesz usunąć ten element?")) {
		document.location=targetLocation;
	}
}

function opacity(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = document.getElementById(id).style; 
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}

function shiftOpacity(id, millisec) {
	//if an element is invisible, make it visible, else make it ivisible
	if(document.getElementById(id).style.opacity == 0) {
		opacity(id, 0, 100, millisec);
	} else {
		opacity(id, 100, 0, millisec);
	}
}

function blendimage(divid, imageid, imagefile, millisec) {
	var speed = Math.round(millisec / 100);
	var timer = 0;
	
	//set the current image as background
	document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";
	
	//make image transparent
	changeOpac(0, imageid);
	
	//make new image
	document.getElementById(imageid).src = imagefile;

	//fade in image
	for(i = 0; i <= 100; i++) {
		setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed));
		timer++;
	}
}

function getOpacity(id) {
	//standard opacity is 100
	var currentOpac = 100;
	
	//if the element has an opacity set, get it
	if(document.getElementById(id).style.opacity < 100) {
		currentOpac = document.getElementById(id).style.opacity * 100;
	}
	
	return currentOpac;
}

function currentOpac(id, opacEnd, millisec) {
	var currentOpac = getOpacity(id);

	//call for the function that changes the opacity
	opacity(id, currentOpac, opacEnd, millisec)
}

var shownOverlay=null;

function showOverlay(name,imgSrc) {
	changeOpac(0, name)
	document.getElementById(name).style.display="block";
	document.getElementById(name+"img").src=imgSrc;
	currentOpac(name,100,500);
	shownOverlay=name;
}

function hideOverlay() {
	currentOpac(shownOverlay,0,500);
	setTimeout("hideOverlayDisplay();",500);
}

function hideOverlayDisplay() {
	document.getElementById(shownOverlay).style.display="none";
	shownOverlay=null;
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function showChart(id) {
	var name="chart"+id;
	changeOpac(0, name)
	document.getElementById(name).style.display="block";
	
	var images=getElementsByClass(name+"img",null,"img");
	for (key in images) {
		if (images[key].title.match(/^\d+$/)) {
			images[key].src="/pictures/"+images[key].title+".gif";
			images[key].title="";
		}
	}
	
	currentOpac(name,100,500);
	shownOverlay=name;
}

function saveQuestionReadTime() {
	$("#loading").show();
	$.post("/ajax/saveQuestionReadTime.php", $("#editor").serialize(), 
		function (data, textStatus) { 
			if (data.length>0) {
				alert(data);
			}
			$("#loading").hide();
		}
	);		
}

function checkQuestionForm(langSuffix) {
	if ($("#pytanie textarea").val().length<10) {
		if (langSuffix=="_pl") {
			alert("Proszę o zadanie pytania.");
		} else {
			alert("Please enter your question.");
		}
		$("#pytanie textarea").focus()
		return false;
	}
	if ($("#email input").val().length<6) {
		if (langSuffix=="_pl") {
			alert("Proszę o podanie adresu e-mail.");
		} else {
			alert("Please enter your email address.");
		}
		$("#email input").focus();
		return false;
	}
	return true;
}