$(document).ready(function() {

    var options = { path: '/', expires: 365 };
	var display = null;
	
	
	$('span#hol_t').click(function() {
			if ($('div.hol_opciok').css('display') == 'none') { $('span#hol_t a').attr('class', 'on'); } else { $('span#hol_t a').removeClass('on'); }
			$('div.hol_opciok').slideToggle('fast');
			
		});
	
	$('div.hol_opciok a').each(function() {
		$(this).click(function() {
			var ertek = $(this).attr('id');
			var szoveg = $(this).text();
			$('input#hol').val(ertek);
			$('span#hol_t a').text(szoveg);
			$('span#hol_t a').removeClass('on');
			$('div.hol_opciok').hide();
		});
	});
	
	$('ul.scroll li a').each(function() {
		$(this).click(function() {
			var id = $(this).attr('id');
			if ($('ul.' + id).attr('class') != id + ' show') {
				$('ul.show').fadeOut('fast', function() {
					$('ul.show').removeClass('show');
					var uj = 'ul.' + id;
					$(uj).fadeIn('fast', function() {
						$(uj).attr('class', id + ' show');
					});
					if ((id == 's_minden') || (id == 's_hir') || (id == 's_coverage') || (id == 's_blog') || (id == 's_klanhir') || (id == 's_forum')) $.cookie('scroller_aktiv', id, options);
				});
			}
			
			var aktiv = $('ul.scroll li a.aktiv');
			
			$(aktiv).removeClass('aktiv');
			$(aktiv).attr('class', $(aktiv).attr('id'));
			$(this).attr('class', id + '_on aktiv');
			
		});
	});
	
	$('ul.scroll li a#' + $.cookie('scroller_aktiv')).click();
	
	$('ul.fade li a').each(function() {
		$(this).click(function() {
			var id = $(this).attr('id').replace(/m_/g, '').replace(/_on/g, '');
			if ($('div.' + id).attr('class') != 'profilinfo ' + id + ' aktiv') {
				$('div.aktiv').fadeOut('fast', function() {
					$('div.aktiv').removeClass('aktiv');
					var uj = 'div.' + id;
					$('ul.fade a.aktiv').attr('id', $('ul.fade a.aktiv').attr('id').replace(/_on/g, ''));
					$('ul.fade a.aktiv').removeClass('aktiv');
					$('ul.fade a#m_' + id).attr('id', 'm_' + id + '_on');
					$('ul.fade a#m_' + id + '_on').attr('class', 'aktiv');
					$(uj).fadeIn('fast', function() {
						$(uj).attr('class', 'profilinfo ' + id + ' aktiv');
					});
				});
			}
			
		});
	});
	
	$('input#msearch').focus(function() {
		var val = $(this).attr('value');
		if (val == 'Keresés') $(this).attr('value', '');
		$('span.search_t').attr('class', 'search_t_on');
	}).blur(function() {
		var val = $(this).attr('value');
		if (val == '') $(this).attr('value', 'Keresés');
		$('span.search_t_on').attr('class', 'search_t');
	});
	
	$('div.valasz').each(function() {
		$(this).children("span.valasz").click(function() {
			var div = $(this).parent("div").children("div");
			var span = $(this).parent("div").children("span.valasz");
			if ($(span).attr('class') == 'valasz') { $(span).attr('class', 'becsuk'); } else { $(span).attr('class', 'valasz'); }
			$(div).slideToggle('fast');
		});
	});
	
	
	$('a.jatekinfo').each(function() {
		$(this).click(function() {
			var id = 'div#' + $(this).attr('id') + '_info';
			if ($(id).attr('class') != 'jatekinfo aktualis') {
				$('ul li.on').removeClass('on');
				$('div.aktualis').fadeOut('fast', function() {
					$('div.aktualis').removeClass('aktualis');
					$(id).fadeIn('fast');
					$(id).attr('class', 'jatekinfo aktualis');
				});
				$(this).parent().attr('class', 'on');
			}
		});
	});
	
	$('div.profil div.aktualis').show();
	
	$('a.pont').click(function() {
		$('ul.oldalszam li.hide').show();
		$('a.pont').parent().hide();
	});
	
	$('a#ismerosmutat').click(function() {
		$('div.ismerosok ul li.hide').slideDown('fast');
		$(this).hide('fast');
	});
	
	$('input#jatek_' + $('select#elsodlegesjatek').val()).attr('disabled', 'disabled');
	$('input#jatek_' + $('select#elsodlegesjatek').val()).attr('checked', 'checked');
	$('div#' + $('select#elsodlegesjatek').val() + '_info').attr('class', 'jatekinfo aktualis');
	
	$('select#elsodlegesjatek').change(function() {
		var id = $(this).val();
		$('input.jatekok').show();
		$('input.jatekok').attr('disabled', '');
		$('input#jatek_' + id).attr('disabled', 'disabled');
		$('input#jatek_' + id).attr('checked', 'checked');
		$('ul li#ful_' + id + ' a').click();
	});
	
	$('input.jatekok').each(function() {
		$(this).change(function() {
			var id = $(this).attr('id').replace(/jatek_/g, '');
			if ($(this).is(':checked')) {
				$('ul li#ful_' + id).removeClass('hide');
				$('ul li#ful_' + id).attr('class', 'on');
				$('ul li#ful_' + id + ' a').click();
			} else {
				if ($('ul li#ful_' + id).attr('class') == 'on')
					$('ul li#ful_' + $('select#elsodlegesjatek').val() + ' a').click();
				$('ul li#ful_' + id).attr('class', 'hide');
			}
		});
	});
	
	$('input#blogolok').change(function() {
		if ($(this).is(':checked')) {
			$('div.blogol').slideDown('fast');
		} else {
			$('div.blogol').slideUp('fast');
		}
	});
	
	$('input.cim').click(function() {
		if (($(this).val() == 'Fórumtéma címe' || $(this).val() == 'A bejegyzés címe')) $(this).val('');
	});
	
	$('textarea.bejegyzes').click(function() {
		if ($(this).val() == 'Fórumtéma leírása' || $(this).val() == 'A bejegyzés hosszabb része (a Tovább gomb utáni rész).' || $(this).val() == 'A bejegyzés rövid bekezdése (a Tovább gomb előtti rész).') $(this).val('');
	});
				
	$('div.jatekok').animate({width: 'show', opacity: '1'});
	
	$('a.logok').each(function() {
		$(this).click(function() {
			var id = $(this).attr('id');
			if ($('div.' + id).css('display') == 'none') {
				$('a.logok').each(function() {
					$(this).attr('id', $(this).attr('id').replace(/_on/g, ''));
				});
				$('div.tarolo').animate({width: 'hide', opacity: '0'});
				$('div.' + id).animate({width: 'toggle', opacity: '1'});
				$(this).attr('id', id + '_on');
			} else {
				$('div.' + id).animate({width: 'toggle', opacity: '0'});
			}
		});
	});
	
	

	var index = 0;
	var indit;
	var tarolo = $('div.nagyhirdetes ul.adscroll');
	var next = index + 1;
	var prev = $(tarolo).children().size() - 1;
	var valtido = 10000;
	var befejezve = true;
	
	function hirdvalt(index) {
	
		if (befejezve) {
		
			befejezve = false;
		
			clearInterval(indit);
			
			if (index >= $(tarolo).children().size()) {
				index = 0;
				next = index + 1;
				prev = $(tarolo).children().size() - 1;
			} else {
				if (index < 0) { index = $(tarolo).children().size() - 1; }
				prev = index - 1;
				next = index + 1;
			}
			
			$(tarolo).find('li.aktiv').fadeOut('fast', function() {
				$(tarolo).find('li.aktiv').removeClass('aktiv');
				$(tarolo).children().css('display', 'none');
				$(tarolo).find('li#ad_' + next).fadeIn('fast', function() {
					$(tarolo).find('li#ad_' + next).attr('class', 'aktiv');
					befejezve = true;
				});
			});
					
			indit = setInterval(function() { hirdvalt(next); }, valtido);
					
			$('ul.adpage li a').removeClass('aktiv');
			$('ul.adpage li a#ads_' + next).attr('class', 'aktiv');
			
		}
		
	}
	
	indit = setInterval(function() { hirdvalt(next); }, valtido);
	
	$(tarolo).mouseover(function() {
		clearInterval(indit);
	}).mouseout(function() {
		indit = setInterval(function() { hirdvalt(next); }, 2000);
	});
	
	$('ul.adpage li a').each(function() {
		$(this).click(function() {
			if (next != $(this).attr('id').replace(/ads_/g, '')) {
				clearInterval(indit);
				var kov = $(this).attr('id').replace(/ads_/g, '');
				kov--;
				hirdvalt(kov);
			}
		});
	});
	
	$('ul.adscroll li a:not(:has(img))').addClass('adlink');
			
	$("div.chat").draggable({ handle: 'div.head', containment: 'parent', stop: function() {
			$.cookie('chat_position_top', $("div.chat").css('top'), options);
			$.cookie('chat_position_left', $("div.chat").css('left'), options);
		}
	});
	
	$('a.exit').click(function() {
		if ($('div.chat').css('display') == 'block') {
			var display = 'none';
			if (typeof periodical != 'undefined') {
				clearTimeout(periodical);
			}
		} else {
			var display = 'block';
			periodical = setTimeout(function() {chatTimeRefresh()}, 1000);
		}
		$('div.chat').css('display', display);
		$.cookie('chat_display', display, options);
	});
	
	$("div.chat").css({'display': ($.cookie('chat_display') == null) ? 'block' : $.cookie('chat_display'), 'top': $.cookie('chat_position_top'), 'left': $.cookie('chat_position_left')});
	
	$.fn.autolink = function () {
		var re = /((http|https|ftp):\/\/[\w?=&.\/-;#~%-]+(?![\w\s?&.\/;#~%"=-]*>))/g;
		return $(this).html( $(this).html().replace(re, '<a href="$1" target="_blank">$1</a>') );
	}
	
	$('a.megjelenit').click(function() {
		if ($('ul.onlineuser').css('display') == 'none') {
			$('ul.onlineuser').fadeIn('fast');
			$(this).html($(this).html().replace('megjelenítése', 'elrejtése'));
		} else {
			$('ul.onlineuser').fadeOut('fast');
			$(this).html($(this).html().replace('elrejtése', 'megjelenítése'));
		}
	});
	
});


// CHAT

function pager(p) {
	$.get("/chat/read?" + Math.random(), { page: p },
	function(data) {
		$('#messages').html(data);
		$('div.chat div.messages ul li p span.msg').each(function() { $(this).csere(); $(this).autolink(); });
	});
	if (page < 1) page = 1;
	return false;
}


function submitChat() {
	$.post("/chat/write", { message: $('#chat_message').val() },
	function(data) {
		pager(0);
		$('div.chat input#chat_message').val('');
	});
}


function chatTimeRefresh() {
	if (timer > 0) {
		timer--;
	} else {
		pager(page);
		timer = 10;
	}
	periodical = setTimeout(function() {chatTimeRefresh()}, 1000);
}

var timer = 10;
page = 0;
$(document).ready(function() {
	pager(0);
	var periodical = null;
	if ($.cookie('chat_display') && $.cookie('chat_display') != 'none') {
		var periodical = setTimeout(function() {chatTimeRefresh()}, 1000);
	}
});
