$(window).load(function() {
	var width = window.innerWidth ? window.innerWidth : 0;
	var resizeinit = false;
    var owidth, ofejlec, osite190, osite810, olablec;
    var awidth, afejlec, asite190, asite810, alablec;
	function optimize1024()
	{
		width = window.innerWidth ? window.innerWidth : 0;
		if (!width || (width > 1024 && !resizeinit)) {
			return;
		}
		if (!resizeinit) {
			owidth = $('#main').css('width');
			osite190 = $('#site_190').css('margin');
			osite810 = $('#site_810').css('margin');
			olablec = $('#site_lablec').css('width');
			resizeinit = true;
		}
		if (width <= 1024) {
			awidth = alablec = '1000px';
			afejlec = asite190 = asite810 = '0';
		} else {
			awidth = alablec = owidth;
			afejlec = ofejlec;
			asite190 = osite190;
			asite810 = osite810;
		}
		$('#main').css('width', awidth);
		$('#site_190').css('margin', asite190);
		$('#site_810').css('margin', asite810);
		$('#site_lablec').css('width', awidth);
	}
	optimize1024();
	$(window).bind('resize', function() {
		optimize1024();
	});

	if (screen.width<=1024) {
		$("#wp-szponzoracio").hide();
	}

	$("div.advert").show();

	
	/* ajanlo layer-ek */

	var $topicLinks = $('a.help');
	if (0 < $topicLinks.length) {
		$('body').append(
			'<div id="recommend-layer">'+
			'<p><a href="" target="_blank"><img src="http://img.hazipatika.com/t.gif" alt="" /><br /><span></span></a></p>'+
			'</div>'
		);

		var $recommendLayer = $('#recommend-layer');
		var recommendLayerTimer = null;
		var recommendLayerHide = function() {
			$recommendLayer.hide();
		}

		$recommendLayer.mouseover(function() {
			clearTimeout(recommendLayerTimer);
		}).mouseout(function() {
			recommendLayerTimer = setTimeout(recommendLayerHide, 300);
		});

		$topicLinks.attr({
			title: '',
			target: '_blank'
		}).mouseover(function() {
			clearTimeout(recommendLayerTimer);
			$this = $(this);

			if ($this.is('.topics')) {
				var className1 = 'topics';
				var imgSRC = IMGURL + '/topics/158x87/' + $this.attr('rel');
				var text = 'Kattintson ide ï¿½s tudjon meg<br/>tï¿½bbet <strong>kiemelt tï¿½mï¿½nkbï¿½l</strong>!';
			} else if ($this.is('.denc')) {
				var className1 = 'denc';
				var imgSRC = IMGURL + '/boxes/recommend/denc_image.gif';
				var text = $this.attr('alt').replace(/:\s+/, ': <strong>') + '</strong>';
			} else if ($this.is('.exam')) {
				var className1 = 'exam';
				var imgSRC = IMGURL + '/boxes/recommend/exam_image.gif';
				var text = $this.attr('alt').replace(/:\s+/, ': <strong>') + '</strong>';
			}

			$recommendLayer.attr('class', className1 + 'down').css({
				left: '-1000px',
				top: 0
			}).show();

			if (text) {
				$recommendLayer.find('p a').attr('href', $this.attr('href')).find('span').html(text);
			}

			if (imgSRC) {
				$recommendLayer.find('p a img').attr('src', imgSRC);
			}

			var offset = $this.offset();
			var topPos = (offset.top + $this.height());
			if ((topPos + $recommendLayer.height()) < $(window).scrollTop() + $(window).height()) {
				var className2 = 'down';
			} else {
				var className2 = 'up';
				topPos = offset.top - $recommendLayer.height() + 5;
			}

			var leftPos = offset.left + ($this.width() / 2) - ($recommendLayer.width() / 2);
			$recommendLayer.attr('class', className1 + className2).css({
				left: Math.ceil(leftPos) + 'px',
				top:  Math.ceil(topPos) + 'px'
			});
		}).mouseout(function() {
			recommendLayerTimer = setTimeout(recommendLayerHide, 300);
		});
	}

/* :hover script (jQuery) - .onMouse class-t kell adni az adott elemnek */

	if ($.browser.msie && jQuery.browser.version.substr(0,1) == "6") {
		$('.onMouse').bind("mouseenter mouseleave", function() {
			$(this).toggleClass('hover');
		});
	}

/* deafultValue trukk - .defaultValue class kell a text inputnak */
	$("input.defaultValue, textarea.defaultValue").focus(function() {
		if (this.value == this.defaultValue) {
			this.value = "";
		}
	});
	$("input.defaultValue, textarea.defaultValue").blur(function() {
		if (this.value == "") {
			this.value = this.defaultValue;
		}
	});

/* form reset - kell a formon belul egy link .reset osztallyal - lasd lent */
	$("a.reset").click(function() {
		$(this).parents("form").clearForm();
		return false;
	});

/* adatmodositas layer */
	$('#adatmod_gomb').mouseover(function() {
		$('#adatmod_layer').show();
	});
	$('#adatmod_menu_1').mouseover(function() {
		$('#adatmod_layer').show();
		$("#adatmod_layer").removeClass().addClass("adatmod_menu_1");
	});
	$('#adatmod_menu_1').mouseout(function() {
		$("#adatmod_layer").removeClass();
	});
	$('#adatmod_menu_2').mouseover(function() {
		$('#adatmod_layer').show();
		$("#adatmod_layer").removeClass().addClass("adatmod_menu_2");
	});
	$('#adatmod_menu_2').mouseout(function() {
		$("#adatmod_layer").removeClass();
	});
	$('#adatmod_layer').mouseout(function() {
		$('#adatmod_layer').hide();
	});

/* bookmark layer */
	$('#konyvjelzoim_gomb').click(function() {
		// nyitas / zaras
		if ($('#hp_kj_belso').css('display') == 'block') {
			bookmark.clicktrack('Zárás');
		} else {
			bookmark.clicktrack('Nyitás');
		}

		bookmark.load();

		$('.menu_top li.konyvjelzoim').toggleClass("konyvjelzoim_be");
		$('#hp_kj_belso').toggle();
		this.blur();
		return false;
	});
	$('#hp_kj_bezar').click(function() {
		bookmark.clicktrack('Zárás');
		$('#menu_bar_1 ul li.konyvjelzoim').removeClass('konyvjelzoim_be');
		$('#hp_kj_belso').hide();
		return false;
	});

/* login layer */
	$('#belepes_gomb').click(function() {
		$('.menu_top li.belepes').toggleClass('belepes_be');
		$('#belepes_layer').toggle();
		$('#user').focus();
		this.blur();
		return false;
	});
	$('form#belepes_layer a.reset').click(function() {
		$('#menu_bar_1 ul li.belepes').removeClass('belepes_be');
		$(this).parents('form').hide();
		return false;
	});

/* dashboard layer */
	$('#menu_bar_1 a.username').click(function() {
		$('#dashboard').show();
		return false;
	});
	$("#dashboard a.close").click(function() {
		$("#dashboard").hide();
		return false;
	});

/* korvalaszto menu */
	$("#site_korvalaszto li.eletkor").mouseover(function() {
		$this = $(this);
		$("#site_korvalaszto_vonal").removeClass().addClass($this.attr("class"));
	}).mouseleave(function() {
		$("#site_korvalaszto_vonal").removeClass();
	});

/* layer */
	var timer;
	var layer;

	$(".layerMutato").mouseover(function() {
		clearTimeout(timer);
		layer = $(this).parent(".layerBefoglalo").find(".layer");
		if ($('.layer:visible').html() != layer.html()) {
			$('.layer:visible').hide();
		}
		timer = setTimeout(layerShow, 300);
	}).mouseout(function() {
		clearTimeout(timer);
		timer = setTimeout(layerHide, 200);
	});
	$(".layer").mouseover(function() {
		clearTimeout(timer);
		layer.show();
	}).mouseout(function() {
		clearTimeout(timer);
		timer = setTimeout(layerHide, 200);
	});

	function layerShow() {
		layer.fadeIn();
	}

	function layerHide() {
		layer.hide();
	}

/* user infoLayer */

	var $infoLayer;
	var $posClass;
	var $tobbiHsz;

	$('a.infoLink img').mouseover(function() {  // info box, pl. a profiloknal

		clearTimeout(timer);

		$this = $(this);
		$infoLayer = $this.parent().parent().find('.userInfo');

		if ($('.userInfo:visible').html() != $infoLayer.html()) {
			$('.userInfo:visible').hide();
		} else {
			$('.userInfo:visible').show();
		}

		var $offset = $this.offset();
		var $height = $infoLayer.height();

		$nullaPont = (($(window).scrollTop() + $(window).height()) - ($offset.top + 90 + $height));

		if ($nullaPont > 0) {
			$posClass = "info_le";
		} else {
			$posClass = "info_fel";
		}

		$infoLayer.addClass($posClass);

		$infoLayer.css("height", $height).css("left", $offset.left-26);
		if ($posClass == "info_le") {
			$infoLayer.css("overflow", "hidden").css("top", $offset.top+70);;
		} else {
			$infoLayer.css("top", $offset.top-$height);
		}

		timer = setTimeout(infoLayerMutat, 300);

	}).mouseout(function() {
		clearTimeout(timer);
		$infoLayer.find('div').removeClass();
		timer = setTimeout(infoLayerHide, 200);
	});

	$('a.infoLinkFull').mouseover(function() {  // teljes adatlap

		clearTimeout(timer);

		$this = $(this);
		$infoLayer = $this.parent().parent().find('.userInfo');

		if ($('.userInfo:visible').html() != $infoLayer.html()) {
			$('.userInfo:visible').hide();
		} else {
			$('.userInfo:visible').show();
		}

		var $offset = $this.offset();
		var $height = $infoLayer.height();

		$nullaPont = (($(window).scrollTop() + $(window).height()) - ($offset.top + 90 + $height));
		$posClass = "info_jobbra";

		$infoLayer.addClass($posClass);

		$infoLayer.css("height", $height).css("right", $offset.right);
		if ($posClass == "info_le") {
			$infoLayer.css("overflow", "hidden").css("top", $offset.top+70);;
		} else {
			$infoLayer.css("top", $offset.top-$height);
		}

		timer = setTimeout(infoLayerMutat, 300);

	}).mouseleave(function() {
		clearTimeout(timer);
		$infoLayer.find('div').removeClass();
		timer = setTimeout(infoLayerHide, 200);
	});

	$('.userInfo').mouseover(function() {
		clearTimeout(timer);
		$infoLayer.addClass($posClass).show();
	}).mouseleave(function() {
		clearTimeout(timer);
		infoLayerHide();
		timer = setTimeout(infoLayerHide, 200);
	});

	function infoLayerMutat() {
		$infoLayer.fadeIn();
	}

	function infoLayerHide(){
		$infoLayer.hide();
		$infoLayer.removeClass($posClass).css("overflow", "visible");
	}

var $title = document.title;
var $url = top.location.href;

/* kedvencek koze */
	$("a.kedvencek_koze").click(function() {

		// Internet Explorer
		if (document.all) {
		   window.external.AddFavorite($url, $title);
		}
		// Mozilla Firefox
		else if (window.sidebar) {
		   window.sidebar.addPanel($title, $url, '');
		}
		else {
		   alert("Sajnos, a te böngészõd nem támogatott.\nA CTRL+D billentyû kombinációval elmentheted ezt az oldalt könyvjelzõként.");
		}
		return false;
	});

/* Kezdolapnak */
	$("a.kezdolapnak").click(function() {
		document.body.style.behavior='url(#default#homepage)';
		document.body.setHomePage($url);
		return false;
	});

/* smiley doboz */

	$(".smiley a.gomb").click(function() {
		var $this = $(this);
		$this.parent(".smiley").toggleClass("nyitva");
		var $title = $this.attr("title");
		if ($title == "továbbiak") {
			$this.attr("title", "visszazár").html("visszazár");
		} else if ($title == "visszazár") {
			$this.attr("title", "továbbiak").html("továbbiak");
		}
		return false;
	});

/* leGordulo */

	var $nyitva = false;

	if (!($.browser.msie && jQuery.browser.version.substr(0, 1) == "6")) {
		$(".szuro select").hide();
		$(".szuro a.leGordulo").show();
	}

	$(".szuro a.leGordulo").click(function(){
		$this = $(this);
		$this.toggleClass("nyitva");
		$setter = $(".szuro div." + $this.attr("id") + " ul li a");
		$toset = $(".szuro select." + $this.attr("id")).get(0);
		$setter.click(function() {
			$this = $(this);
			$toset.value = $this.attr('href').replace('#', '');
			$($toset).trigger("change");
		});
		$lista = $(".szuro div." + $this.attr("id"));
		$lista.toggle();
		$lista.mousemove(function() {
			$nyitva = true;
		}).mouseout(function() {
			$nyitva = false;
		});
		return false;
	}).blur(function(){
		if (!$nyitva) {
			$this.removeClass("nyitva");
			$lista.hide();
		}
	});

	$(".szuro ul li a").click(function() {
		$this = $(this);
		$(".szuro a#" + $this.parents("div[class^='gordulo_']").attr("class")).find("em").text($this.html()).end().removeClass("nyitva");
		$this.parents("div[class^='gordulo_']").hide();
		return false;
	});

/* toolbar betumeret novelo */

	var $toolbar = $("ul.toolbar");
	if (0 < $toolbar.length) {
		var $tartalom = $("#" + $toolbar.attr("class").substr(8));
		var $betuMeret = $tartalom.attr("class").substr(6,1);
	}

	$(".toolbar li#gomb_betu_minusz a").click(function() {
		$(".toolbar li#gomb_betu_plusz a").removeClass();
		if ($betuMeret != 1) {
			$betuMeret--;
			if ($betuMeret == 1) {
				$(this).addClass("inaktiv");
			}
			$tartalom.removeClass().addClass("meret_" + $betuMeret);
		}
		return false;
	});

	$(".toolbar li#gomb_betu_plusz a").click(function() {
		$(".toolbar li#gomb_betu_minusz a").removeClass();
		if ($betuMeret != 4) {
			$betuMeret++;
			if ($betuMeret == 4) {
				$(this).addClass("inaktiv");
			}
			$tartalom.removeClass().addClass("meret_" + $betuMeret);
		}
		return false;
	});


/* toolbar gomb kiiro */
	$("#toolbar_2 li a").mouseover(function() {
		$this = $(this);
		$("#toolbar_2 li#gombszoveg").text($this.attr("title"));
	}).mouseout(function() {
		$("#toolbar_2 li#gombszoveg").text("");
	});

/* legordulo select utanzat */
	var $legorduloNyitva = false;

	$("a.legordulo").click(function() {
		$this = $(this);
		$this.toggleClass("nyitva");
		$lista = $("." + $this.attr("id"));
		$lista.toggle();
		$lista.mousemove(function() {
			$legorduloNyitva = true;
		}).mouseout(function() {
			$legorduloNyitva = false;
		});
		return false;
	}).blur(function(){
		if (!$legorduloNyitva) {
			$this.removeClass("nyitva");
			$lista.hide();
		}
	});

// login link varazslo
	if (USER_LOGGED_IN) {
		$("a.login_link").each(function(){
			var $this = $(this);
			var tmp = $this.attr('href').match(";fwd=([^;$]+)");
			var decoded = URL.decode(tmp[1]);
			if (decoded != tmp[1]) {
				tmp[1] = decoded;
			}
			$this.attr('href', tmp[1]);
		});
	}

// textarea / input maxlength cropper
	$(".counter").keyup(function(){
		$this = $(this);
		$text = $this.get(0);
		$counter = $("#" + $this.attr("id") + "_cnt").get(0);
		if (!$counter) {
			return;
		}
		$cnt = parseInt($counter.innerHTML);
		if (!$counter.orig) {
			$counter.orig = $cnt;
		}
		$db = $counter.orig - $text.value.length;
		if ($db >= 0) {
			$counter.innerHTML = $db;
		} else {
			$text.value = $text.value.substr(0, $counter.orig);
		}
	});
	$(".counter").blur(function(){ $(this).trigger("keyup"); });
	$(".counter").change(function(){ $(this).trigger("keyup"); });
	$(".counter").trigger("keyup");

/* form ellenorzes */
	$("form.chkLogin").submit(function(){
		$form = $(this);
		if ($form.find("input[name='username']").val() == $form.find("input[name='username']").defaultValue || $form.find("input[name='username']").val() == '') {
			alert('Kérjük töltsd ki a felhasználónév mezõt!');
			return false;
		} else if ($form.find("input[name='password']").val() == $form.find("input[name='password']").defaultValue || $form.find("input[name='password']").val() == '') {
			alert('Kérjük töltsd ki a jelszó mezõt is!');
			return false;
		}
		else return true;
	});

/* alert box */
	$("#bsz_alert a").click(function(){
		return animate("#bsz_alert");
	});

/* warning box */
	$("#bsz_warning a.bezar").click(function(){
		return animate("#bsz_warning");
	});

/* A regisztracio es profil szerkesztese oldalakon kinyithato formok */

	$(".adat_csoport h5 a").click(function(){
		$(this).parents(".adat_csoport").toggleClass("nyitva");
		return false;
	});
	
	$(document).bind("ajaxStart", function () {
		var active = document.activeElement;
		if ($(active).attr('nodeName') == 'INPUT' && $(active).attr('type') == 'text') {
			$('#menu_bar_1 .ajaxloading_icon').show();
		}
	});
	$(document).bind("ajaxComplete ajaxSuccess ajaxError", function () {
		var active = document.activeElement;
		if ($(active).attr('nodeName') == 'INPUT' && $(active).attr('type') == 'text') {
			$('#menu_bar_1 .ajaxloading_icon').hide();
		}
	});
	// altalanos kereso
	$('input[name=sitesearch_k]').autocomplete('/ajax/util/sitesearch/suggest_ajax', {
		minChars: 2,
		sendOnEnter : true,
		scroll: false,
		selectFirst: false,
		max: 5,
		cacheLength: -1,
		extraParams: {
			returnType: 'list'
		},
		queryParam: 'k'
	});

	/* jelszo mutatasa */
	$('#passmask').click(function() {
		if ( $('#password').val() == "" ) {
			if ($(this).attr('checked')) {
				$('#password').replaceWith("<input id='password' class='text' name='password' type='password' value=''/>");				
			} else {
				$('#password').replaceWith("<input id='password' class='text' name='password' type='text' value=''/>");
			}
		} else {
			if ($(this).attr('checked')) {
				$('#password').replaceWith("<input id='password' class='text' name='password' type='password' value=" + $('#password').val() + " />");				
			} else {
				$('#password').replaceWith("<input id='password' class='text' name='password' type='text' value=" + $('#password').val() + " />");
			}
		}
	});
	$('#passmask-layer').click(function() {
		if ( $('#password-layer').val() == "" ) {
			if ($(this).attr('checked')) {
				$('#password-layer').replaceWith("<input id='password-layer' name='password' type='password' tabindex='2' value=''/>");				
			} else {
				$('#password-layer').replaceWith("<input id='password-layer' name='password' type='text' tabindex='2' value=''/>");
			}
		} else {
			if ($(this).attr('checked')) {
				$('#password-layer').replaceWith("<input id='password-layer' name='password' type='password' tabindex='2' value=" + $('#password-layer').val() + " />");				
			} else {
				$('#password-layer').replaceWith("<input id='password-layer' name='password' type='text' tabindex='2' value=" + $('#password-layer').val() + " />");
			}
		}
	});
});

function animate(selector, remove)
{
	if ($.browser.msie && jQuery.browser.version.substr(0, 1) == "6") {
		$(selector).slideUp("slow");
	} else {
		$(selector).animate({
			height: "0px",
			paddingTop: "0px",
			opacity: 0
		}, "1000", "linear", function(){$(selector).hide()});
	}
	if (remove) {
		setTimeout("$('" + selector + "').remove()", 1500);
	}
	return false;
}

/* ezt erdemes lenne akar a jquery.js-be is beletenni: http://www.learningjquery.com/2007/08/clearing-form-data */

$.fn.clearForm = function() {
  return this.each(function() {
	var type = this.type, tag = this.tagName.toLowerCase();
	if (tag == 'form')
	  return $(':input',this).clearForm();
	if (type == 'text' || type == 'password' || type == 'file' || tag == 'textarea')
	  this.value = this.defaultValue; /* Lacko: a defaultValue-t adja vissza; ha nincs akkor semmi - remelem gondot nem okoz */
	else if (type == 'checkbox' || type == 'radio')
	  this.checked = false;
	else if (tag == 'select')
	  this.selectedIndex = 0; /* ezt Lacko irta at -1 -rol */
  });
};

/* google analytics click tracking */
_uacct = "UA-4369473-1";

/* webaudit new */
function median_webaudit() {
	var d = document, s = screen ? screen.width + 'x' + screen.height : "";
	var u = d.URL ? new String(d.URL) : "";
	var r = d.referrer ? new String(d.referrer) : "";
	return "@s=" + s + "@u=" + escape(u.substring(0, 183)) + "@r=" + escape(r.substring(0, 127));
}
var same = Math.floor(Math.random() * 100000000) + median_webaudit();
/* webaudit new end */

/* "Mediatar" cikk doboz min-width script (jQuery) */
$(function() {
	if ($.browser.msie) {
		$this = $('.cikk_mediatar_belso');
		$szelesseg = $this.width();
		if ($szelesseg < 210) {
			$this.width(210);
		}
		$('.cikk_mediatar_belso .cikk_mediatar_gomb').width($szelesseg - 9);
	}
});

function _spam_fix(a)
{
	a.href = a.href.replace(/_NO_SP_AM_./g, "");
	return true;
}

function _spam_fix_status(a, what)
{
	if (what) {
		window.status = a.href.replace(/_NO_SP_AM_./g, "");
	} else {
		window.status = '';
	}
}

/* content switch */
var switchGroup = new Array();
function addSwitchGroupElement(group, element, display)
{
	switchGroup[switchGroup.length] = group;
	switchGroup[switchGroup.length] = element;
	if (!display) {
		display = 'none';
	}
	switchGroup[switchGroup.length] = display;
}

function resetSwitchGroup(group)
{
	for (i=0; i<switchGroup.length; i+=3) {
		if (switchGroup[i] == group) {
			x = document.getElementById(switchGroup[i+1]);
			x.style.display = switchGroup[i+2];
		}
	}
}

function clearSwitchGroup(group)
{
	for (i=0; i<switchGroup.length; i+=3) {
		if (switchGroup[i] == group) {
			x = document.getElementById(switchGroup[i+1]);
			x.style.display = 'none';
		}
	}
}

function setSwitchElement(group, element, display)
{
	clearSwitchGroup(group);
	if (!display) display = 'block'
	for (i=0; i<switchGroup.length; i+=3) {
		if (switchGroup[i] == group && switchGroup[i+1] == element) {
			x = document.getElementById(element);
			x.style.display = display;
		}
	}
}

function printPage()
{
	if (window.print) {
		if (confirm('Indulhat a nyomtatás?')) {
			window.print();
		}
	}
}

function confirmAction(url, text)
{
	if (confirm(text)) {
		window.location.replace(url);
	}
	return false;
}

// bekapcsolja a divet, amiben van hirdetés
window.onGoa3Invocation = function( response, pageIID ) {
  var $ = this.getjQuery();

  $.each(response, function( i, r ) {
    $("#zone".concat(r.zone)).parent(".wrap").css("display", "block");
  });
};

/* google adsense */
var existsAdSense = 0;

function SetCookie(name, value) {
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = '/';
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	document.cookie = name + '=' + escape (value) +
	((expires == null) ? '' : ('; expires=' + expires.toGMTString())) +
	((path == null) ? '' : ('; path=' + path)) +
	((domain == null) ? '' : ('; domain=' + domain)) +
	((secure == true) ? '; secure' : '');
}

function getCookieVal(offset) {
	var endstr = document.cookie.indexOf (';', offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie(name) {
	var arg = name + '=';
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
		return getCookieVal (j);
		i = document.cookie.indexOf(' ', i) + 1;
		if (i == 0) break;
	}
	return null;
}

