var current = 0;

function openProf(e) {

	$(".profContentLong").slideUp("fast");
	$(".closeProf").hide();

	if($(e).hasClass("active")) {
		$(e).removeClass("active");
		$(".profLink").removeClass("active");
		$(".profLink").parent("div").children(".closeProf").hide();
	} else {
		$(e).children(".profContentLong").slideDown("fast");
		$(".profLink").removeClass("active");
		$(e).addClass("active");
		$(e).parent("div").children(".closeProf").show();
	}
}

function openPage(i) {

	if($("#num"+i).hasClass("numPassive") == false) {
		$(".profPage").hide();
		
		$("#profPage"+i+" .professor").show();
		$("#profPage"+i).show();
		
		var e = $(".numAct");
		e.removeClass("numAct");
		e.addClass("num");
		
		e = $("#num"+i);
		e.removeClass("num");
		e.addClass("numAct");
		
		current = i;
		
		createSortList();
	}
}

function nextPage(i) {
	if(current < maxPage) {
		current++;
		openPage(current);
		return true;
	}
	
	return false;
}

function prevPage(i) {
	if(current > 1) {
		current--;
		openPage(current);
		return true;
	}
	
	return false;
}

function showLetters() {

	var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖ";

	for(var i = 0; i < 29; i++) {
		if($("#profPage"+letters.charAt(i)).text().length < 10) {
			$("#num"+letters.charAt(i)).addClass("numPassive");
		}
	}
	
	createSortList();
	
	$("#profPageSelect").fadeIn();
	$("#profFilterWrapper").fadeIn();
}

function openPdf() {

	var profTitle = $("#profWrapper .professor .active h1").text();
	var profText = "";
	
	$("#profWrapper .professor .active p").each(function (i) {
		var more = $(this).html();
		more = more.replace(/<br>/gi, '\n');
		more = more.replace(/<b>/gi, '');
		more = more.replace(/<\/b>/gi, '');
		more = more.replace(/&amp;/gi, '&');
		more = more.replace(/&nbsp;/gi, ' ');
		profText = profText + more + "\n\n";
	});

	window.location = "/templates/fidipro_common/pdf.php?title=" + urlencode(profTitle) + "&text=" + urlencode(profText);
}

function urlencode(str) {
	str = escape(str);
	str = str.replace('+', '%2B');
	str = str.replace('%20', '+');
	str = str.replace('*', '%2A');
	str = str.replace('/', '%2F');
	str = str.replace('@', '%40');
	return str;
}

function filterProfs() {
	var fil = $("#profFilter").attr("value");
	if(fil == "Kaikki" && current == 0) {
		$(".profPage .professor").show();
	} else if(fil == "Kaikki") {
		$("#profPage"+current+" .professor").show();
	} else {
		$(".professor").hide();
		$(".professor span:contains('"+fil+"')").parent(".professor").show();
	}
}

function createSortList() {

	var items = new Array();

	$("#profFilter").html('<option value="Kaikki">'+str_all+'</option>');
	$(".profPage:visible .vastuutaho").each(function (i) {
		if($(this).text().length > 3) {
			var txt = $(this).text();
			
			if(txt.charAt(txt.length-1) == " ") txt = (txt.substr(0, txt.length-1)); 
			
			items.push(txt);
		}
	});
	
	items = items.unique();
	items = items.sort();
	
	for(var i = 0; i < items.length; i++) {
		$("#profFilter").append('<option value="'+items[i]+'">'+items[i]+'</option>');
	}
}

$(document).ready(function() {
	showLetters();
});


Array.prototype.unique = function () {
	var r = new Array();
	o:for(var i = 0, n = this.length; i < n; i++)
	{
		for(var x = 0, y = r.length; x < y; x++)
		{
			if(r[x]==this[i])
			{
				continue o;
			}
		}
		r[r.length] = this[i];
	}
	return r;
}
