
function countdownTimers() {};
var myCdT;
var timer;

$(window).load(function() {

/*
*	Login jelszo mezo:
* 	Ha ures, akkor latszik a belepozicionalt label, ha focus-ba kerul, akkor eltunik a label. 
*	Ha blur, akkor ismet latszik a label.
*/

	var passwordInput = $('#csevego_login input#password');
	
	
	if (passwordInput.val() == '') {
		passwordInput.prev('label').show();
	}
	
	passwordInput.focus(function(){
		passwordInput.prev('label').hide();
	}).blur(function(){
		if (passwordInput.val() == '') {
			passwordInput.prev('label').show();
		}
	});

/* kategoriak tablazat infoLayer */
	
	$("#csevego_kategoriak table[class!='fejlec'] thead a").mouseover(function() {
		var $this = $(this);
		var $top = ($this.offset().top - $this.parents("#csevego_kategoriak").offset().top) + $this.height()-2;
		var layer = $("#csevego_kategoriak #infoLayer");
		clearTimeout(timer);
		if (layer.find("div").html() != $this.parents("table").attr("summary")) {
			layer.hide();
		}
		layer.css("top", $top).find("div").text($this.parents("table").attr("summary"));
		timer = setTimeout('$("#csevego_kategoriak #infoLayer").fadeIn()', 500);
	}).mouseout(function() {
		clearTimeout(timer);
		timer = setTimeout('$("#csevego_kategoriak #infoLayer").hide()', 300);
	});
	
	$("#csevego_kategoriak #infoLayer").mouseover(function() {
		clearTimeout(timer);
		$(this).show();
	}).mouseout(function() {
		clearTimeout(timer);
		timer = setTimeout('$("#csevego_kategoriak #infoLayer").hide()', 300);
	});
	
/* nyito hsz */

	$("#csevego_nyito_hsz div.fejlec a.megjelenit").click(function() {
		$(this).parents("#csevego_nyito_hsz").removeClass().addClass("nyitva");
		return false;
	});
	$("#csevego_nyito_hsz a.bezar").click(function() {
		$(this).parents("#csevego_nyito_hsz").removeClass().addClass("zarva");
		return false;
	});
	
/* uj tema infolayer */
	
	$(".ujtema_statusz ul li span").mouseover(function() {
		var $this = $(this);
		var position = $this.position();
		$this.parent("li").find("div").css("left", position.left -6).fadeIn().end().siblings().css("position", "static");
	});
	
	$(".ujtema_statusz ul li div").mouseover(function() {
		$(this).show();
		$(this).parent("li").siblings().css("position", "static");
	}).mouseout(function() {
		$(this).hide();
		$(this).parent("li").siblings().css("position", "relative");
	});
	
	$("#csevego_reszletes a.reset").click(function() {
		self.parent.tb_remove();
	});
	
	$("#newtopic_submit").click(function() {
		document.getElementById('hidden_alert').value = document.getElementById('bsz1').checked ? 1 : 0;
		document.getElementById('hidden_title').value = document.getElementById('temacim').value;
		document.getElementById('hidden_subtitle').value = document.getElementById('temaalcim').value;
		document.getElementById('hidden_allowusers').value = document.getElementById('allowusers').value;
		if (document.getElementById('statusz_nyilt').checked) {
			document.getElementById('hidden_status').value = 'e';
		} else if (document.getElementById('statusz_zart').checked) {
			document.getElementById('hidden_status').value = 'c'; // ha jól értem, mi akar ez lenni, akkor az egész zárt hülyeség
		} else if (document.getElementById('statusz_privat').checked) {
			document.getElementById('hidden_status').value = 'p';
		}
		document.getElementById('ujtemaform').submit();
	});
	
	$("#newmessage_submit").click(function() {
		document.getElementById('hidden_alert').value = document.getElementById('bsz1').checked ? 1 : 0;
		document.getElementById('ujmessageform').submit();
	});
	
	$("#keresobox_submit").click(function() {
//		alert('A szabadszavas kereső átmenetileg nem működik, kis türelmed kérjük!');
//		return false;
		if (document.getElementById('kulcsszo').value == '') {
			alert('Írj be egy keresendő szót!');
			return false;
		} else {
			document.getElementById('csevego_kereses').submit();
			return false;
		}
	});
	
//	$("#csevego_kereses").submit(function() {
//		alert('A szabadszavas kereső átmenetileg nem működik, kis türelmed kérjük!');
//		return false;
//	});
	
	$("#kerfeluj").click(function() {
		document.getElementById('reszletes_1').value = 0;
		document.getElementById('reszletes_2').value = '';
		document.getElementById('reszletes_3').value = '';
		document.getElementById('reszletes_4').value = '';
		document.getElementById('mikortol2').value = tuvixogo;
		document.getElementById('meddig2').value = '';
		document.getElementById('reszletesview').value = 'message';
	});
	
	$("a.smiley").click(function() {
		surroundText('hozzaszolastxt', 'smiley', this.getAttribute('val'));
//		document.getElementById('hozzaszolastxt').value += ' ' + this.getAttribute('val') + ' ';
		document.getElementById('hozzaszolastxt').focus();
	});
	
	$("#e_submit").click(function() {
//		alert('A szabadszavas keresőnk átmenetileg nem működik!');
//		return false;
		if (document.getElementById('reszletes_1').value == 0 &&
				document.getElementById('reszletes_2').value == '' &&
				document.getElementById('reszletes_3').value == '' &&
				document.getElementById('reszletes_4').value == '') {
			alert('Kérjük, írjon be egy keresési kritériumot!');
			return false;
		} else {
			if (document.getElementById('reszletes_2').value != '') {
				document.getElementById('e_keyword').value = document.getElementById('reszletes_2').value;
				document.getElementById('e_searchin').value = 'topic';
			}
			if (document.getElementById('reszletes_3').value != '') {
				document.getElementById('e_keyword').value = document.getElementById('reszletes_3').value;
				document.getElementById('e_searchin').value = 'message';
			}
			if (document.getElementById('reszletes_4').value != '') {
				document.getElementById('e_keyword').value = document.getElementById('reszletes_4').value;
				document.getElementById('e_searchin').value = 'user';
			}
			document.getElementById('csevego_reszletes').submit();
			return true;
		}
	});
	
//	$("#csevego_reszletes").submit(function() {
//		alert('A szabadszavas keresőnk átmenetileg nem működik!');
//		return false;
//	});
	
	$("#idezek").click(function() {
		var txt = '';
		if (window.getSelection) {
			txt = window.getSelection();
		} else if (document.getSelection) {
	        txt = document.getSelection();
	    } else if (document.selection) {
	        txt = document.selection.createRange().text;
	    }
		
		surroundText('hozzaszolastxt', 'quote', txt);
		
		//document.getElementById('hozzaszolastxt').value += '[q:' + origauthor + ']' + txt + '[/q]\n';
		document.getElementById('hozzaszolastxt').focus();
	});
	
	$("#btn_search_user").click(function() {
		$('#csevego_kereses').show();
		return false;
	});
	
	$("#keresobox_submit2").click(function() {
		document.getElementById('huss_searchin').value = document.getElementById('miben2').value;
		document.getElementById('huss_from').value = document.getElementById('mikortol2').value;
		document.getElementById('huss_keyword').value = document.getElementById('kulcsszo2').value;
		document.getElementById('csevego_kereses2').submit();
	});
	
	$("#topics_del").click(function() {
		var todel = new Array();
		$("#csevego_temak").find(".topic_checkbox:checked").each(function(){
			todel.push($(this).val());
		});
		if (todel.length > 0) {
			if (window.confirm('Biztosan törölni akarod a kijelölt témákat a kedvenceid közül?')) {
				bookmark.serviceDelete({'service': 'csevego', 'service_id': todel, 'callback': 'malacka'});
			}
		}
	});
	
	$("#statusz_nyilt").click(function() {
		$('#ujtema_felhasznalok').hide();
	});
	
	$("#statusz_zart").click(function() {
		$('#ujtema_felhasznalok').hide();
	});

	$("#statusz_privat").click(function() {
		$('#ujtema_felhasznalok').show();
	});
	
	$("li.duhongobe").click(function() {
		doMod('duhongo');
	});
	
	$("li.lezaras").click(function() {
		doMod('close');
	});
	
	$("li.msgtorles").click(function() {
		doMod('delmsg');
	});
	
	$("li.ujranyitas").click(function() {
		doMod('reopen');
	});
	
	$("li.archivalas").click(function() {
		doMod('archive');
	});
	
	$("li.torles").click(function() {
		doMod('delete');
	});
	
	$("a.btn_format_bold").click(function(){
		var txt = $("#hozzaszolastxt").getSelection().text;
		$("#hozzaszolastxt").replaceSelection("[b]" + txt + "[/b]", true);
		return false;
	});
	
	$("a.btn_format_underline").click(function(){
		var txt = $("#hozzaszolastxt").getSelection().text;
		$("#hozzaszolastxt").replaceSelection("[u]" + txt + "[/u]", true);
		return false;
	});

	$("a.btn_format_strike").click(function(){
		var txt = $("#hozzaszolastxt").getSelection().text;
		$("#hozzaszolastxt").replaceSelection("[s]" + txt + "[/s]", true);
		return false;
	});
	
	$("a.btn_format_italic").click(function(){
		var txt = $("#hozzaszolastxt").getSelection().text;
		$("#hozzaszolastxt").replaceSelection("[i]" + txt + "[/i]", true);
		return false;
	});
	
	$("a.btn_format_link").click(function(){
		var txt = $("#hozzaszolastxt").getSelection().text;
		if (txt.substring(0, 4) != 'http') {
			var txt2 = "http://" + txt;
		} else {
			var txt2 = txt
		}
		$("#hozzaszolastxt").replaceSelection("[link:" + txt2 + "]" + txt + "[/link]", true);
		return false;
	});
	
	populateCountdowns();
	
});

function doMod(what) {
	var toadm = new Array();
	if (what != "delmsg") {
		$("#csevego_temak").find(".topic_checkbox:checked").each(function(){
			toadm.push($(this).val());
		});
	} else {
		$("#csevego_befoglalo").find(".msg_checkbox:checked").each(function(){
			toadm.push($(this).val());
		});
	}
	if (toadm.length > 0) {
		var RUSure = false;
		var wrd = toadm.length == 1 ? 'témát' : 'témákat';
		switch (what) {
			case 'duhongo':
				RUSure = window.confirm('Biztosan át akarod helyezni a ' + wrd + ' a Dühöngőbe?');
				break;
			case 'close':
				RUSure = window.confirm('Biztosan le akarod zárni a ' + wrd + '?');
				break;
			case 'reopen':
				RUSure = window.confirm('Biztosan újra akarod nyitni a ' + wrd + '?');
				break;
			case 'archive':
				RUSure = window.confirm('Biztosan archiválni szeretnéd a ' + wrd + '?');
				break;
			case 'delete':
				RUSure = window.confirm('Biztosan törölni akarod a ' + wrd + '?');
				break;
			case 'delmsg':
				RUSure = window.confirm('Biztosan törölni akarod a kijelölt üzenete(ke)t?');
				break;
		}
		if (RUSure == true) {
			var post = {
					'ids[]': toadm,
					'cmd': what
				};
			$.ajax({
				type: "POST",
				url: '/ajax/csevego/moderacio_ajax',
				data: post,
				dataType: 'json',
				async: false,
				success: function(ma){
					if (ma.status == 'ok') {
						window.location.reload();
					} else {
						$('#growl_message_err').jGrowl(ma.message, { header: 'Csevegő:', life: 3000, theme: 'babaszoba_err', speed: 'slow'});
					}
				}
			});
		}
	}
}

function malacka(op) {
	if (op.message) {
		window.location.reload();
	} else {
		$('#growl_message_err').jGrowl('Nem tudtuk a kijelölt elemeket törölni!', {header: 'Hiba', life: 3000, theme: 'babaszoba_err', speed: 'slow' });
	}
}

function populateCountdowns() {
	myCdT = new countdownTimers();
	mySpans = document.getElementsByTagName('span');
	mySpansLen = mySpans.length;
	myCdT['num'] = {num: 0};
	for (var a = 0; a < mySpansLen; a ++) {
		if (mySpans[a] && mySpans[a].getAttribute && mySpans[a].getAttribute('id') && mySpans[a].getAttribute('id').substring(0, 10) == 'countdown_') {
			id = mySpans[a].getAttribute('id');
			myCdT[id] = {obj: mySpans[a]};
			myCdT['num'].num ++;
		}
	}
	if (myCdT['num'].num > 0) {
		timer = window.setInterval(countdown, 1000);
	}
}

function countdown () {
	for (var a in myCdT) {
		if (a != 'num') {
			var myO = myCdT[a].obj;
			myO.innerHTML = myO.innerHTML - 1;
			if (myO.innerHTML <= 0) {
				myCdT['num'].num --;
				delete myCdT[a];
				myO.parentNode.style.display = 'none';
				if (myCdT['num'].num == 0) {
					window.clearInterval(timer);
				}
			}
		}
	}
}

function csevegoNotify(op) {
	if (op.status == 'insert_ok') {
		$('#growl_message_ok').jGrowl('A témát felvettük a kedvenceid közé!', { header: 'Kedvenc témák:', life: 3000, theme: 'babaszoba_ok', speed: 'slow'});
	} else {
		$('#growl_message_err').jGrowl(op.message, { header: 'Kedvenc témák:', life: 3000, theme: 'babaszoba_err', speed: 'slow'});
	}
}

function surroundText(textareaid, tag) {
	var textarea = document.getElementById(textareaid);
	switch (tag) {
		case 'smiley':
			text1 = arguments[2];
			text2 = '';
	 	break;
		case 'quote':
			text1 = '[q:' + origauthor + ']' + arguments[2] + '[/q]\n';
			text2 = '';
	 	break;
	}
	// Can a text range be created?
	if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange) {
		var caretPos = textarea.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text1 + caretPos.text + text2 + ' ' : text1 + caretPos.text + text2;
		caretPos.select();
	}
	// Mozilla text range wrap.
	else if (typeof(textarea.selectionStart) != "undefined") {
		var begin = textarea.value.substr(0, textarea.selectionStart);
		var selection = textarea.value.substr(textarea.selectionStart, textarea.selectionEnd - textarea.selectionStart);
		var end = textarea.value.substr(textarea.selectionEnd);
		var newCursorPos = textarea.selectionStart;
		var scrollPos = textarea.scrollTop;

		textarea.value = begin + text1 + selection + text2 + end;

		if (textarea.setSelectionRange) {
			if (selection.length == 0) {
				textarea.setSelectionRange(newCursorPos + text1.length, newCursorPos + text1.length);
			} else {
				textarea.setSelectionRange(newCursorPos, newCursorPos + text1.length + selection.length + text2.length);
			}
		textarea.focus();
		}
		textarea.scrollTop = scrollPos;
	} else {
		textarea.value += text1 + text2;
		textarea.focus(textarea.value.length - 1);
	}
}

