var actage = 1; // alapból ezzel a korcsoporttal indítunk

$(window).load(function() {
	// receptek eletkor szerinti fule
	$('#ful_4').click(function() {
		$("#ful_4").removeClass().addClass('inactive');
		$("#ful_6").removeClass();
		$("#ful_8").removeClass();
		$("#ful_12").removeClass();
		$("#honapos_4").show();
		$("#honapos_6").hide();
		$("#honapos_8").hide();
		$("#honapos_12").hide();
		actage = 1;
	});
	$('#ful_6').click(function() {
		$("#ful_4").removeClass();
		$("#ful_6").removeClass().addClass('inactive');
		$("#ful_8").removeClass();
		$("#ful_12").removeClass();
		$("#honapos_4").hide();
		$("#honapos_6").show();
		$("#honapos_8").hide();
		$("#honapos_12").hide();
		actage = 2;
	});
	$('#ful_8').click(function() {
		$("#ful_4").removeClass();
		$("#ful_6").removeClass();
		$("#ful_8").removeClass().addClass('inactive');
		$("#ful_12").removeClass();
		$("#honapos_4").hide();
		$("#honapos_6").hide();
		$("#honapos_8").show();
		$("#honapos_12").hide();
		actage = 3;
	});
	$('#ful_12').click(function() {
		$("#ful_4").removeClass();
		$("#ful_6").removeClass();
		$("#ful_8").removeClass();
		$("#ful_12").removeClass().addClass('inactive');
		$("#honapos_4").hide();
		$("#honapos_6").hide();
		$("#honapos_8").hide();
		$("#honapos_12").show();
		actage = 4;
	});

	// receptfigyelo tipusok
	$("#figyelo").click(function() {
		// If checked
		if ($("#figyelo").is(":checked")) {
			$("#figyelo_tipusok").show();
			$("#hirlevel_adatok").show();
		}
		else {
	    $("#figyelo_tipusok").hide();
			if ($("#hirlevel").is("input:checked")) {
				$("#hirlevel_adatok").show();
			}
			else {
				$("#hirlevel_adatok").hide();
			}
		}
	});

	$("#hirlevel").click(function() {
		// If checked
		if ($("#hirlevel").is(":checked")) {
			$("#hirlevel_adatok").show();
		}
		else {
			if ($("#figyelo").is("input:checked")) {
				$("#hirlevel_adatok").show();
			}
			else {
				$("#hirlevel_adatok").hide();
			}
	}
	});

	$('#reszletes_kereses').submit(function() {
		if (document.getElementById('kulcsszo').value == '' &&
			document.getElementById('f_tipus').value == 0 &&
			document.getElementById('f_dieta').value == 0 &&
			document.getElementById('f_ido').value == 0 &&
			document.getElementById('f_nehezseg').value == 0 &&
			document.getElementById('kor_4').checked == false &&
			document.getElementById('kor_6').checked == false &&
			document.getElementById('kor_8').checked == false &&
			document.getElementById('kor_12').checked == false &&
			document.getElementById('iv_1_1').value == 0 && document.getElementById('iv_1_2').value == 0 &&
			document.getElementById('iv_2_1').value == 0 && document.getElementById('iv_2_2').value == 0 &&
			document.getElementById('iv_3_1').value == 0 && document.getElementById('iv_3_2').value == 0 &&
			document.getElementById('iv_4_1').value == 0 && document.getElementById('iv_4_2').value == 0 &&
			document.getElementById('iv_5_1').value == 0 && document.getElementById('iv_5_2').value == 0 &&
			document.getElementById('iv_6_1').value == 0 && document.getElementById('iv_6_2').value == 0 &&
			document.getElementById('iv_7_1').value == 0 && document.getElementById('iv_7_2').value == 0 &&
			document.getElementById('iv_8_1').value == 0 && document.getElementById('iv_8_2').value == 0 &&
			document.getElementById('iv_9_1').value == 0 && document.getElementById('iv_9_2').value == 0 &&
			document.getElementById('iv_10_1').value == 0 && document.getElementById('iv_10_2').value == 0 &&
			document.getElementById('iv_11_1').value == 0 && document.getElementById('iv_11_2').value == 0
		) {
			$('#growl_message_err').jGrowl('Válassz ki, vagy írj be legalább egy keresési feltételt!', { header: 'Üres keresés', life: 3000, theme: 'babaszoba_err', speed: 'slow'});
			return false;
		} else {
			return true;
		}
	});

	
	// maganszemely v. intezmeny
	$('#maganszemely').click(function() {
		document.getElementById('cimsor').style.display="block";
	});

	$('#intezmeny').click(function() {
		document.getElementById('cimsor').style.display="none";
	});

	// filter a kategoria valasztashoz
	$('#filter_kinyit').click(function() {
		$("#gombok").hide();
		$("#szuro_form_1").show();
	});
	$('#filter_bezar').click(function() {
		$("#gombok").show();
		$("#szuro_form_1").hide();
	});
	
	$('#btn_index_ujotlet').click(function() {
		$.ajax({
			type: "POST",
			url: "/ajax/services/babakonyha/ajaxreq_ajax",
			data: "cmd=newrecipe&age=" + actage,
			dataType: "json",
			async: false,
			success: function(ma){
				if (ma.status == 'ok') {
					document.getElementById(actage + '_fejlec').innerHTML = ages[ma.age];
					document.getElementById(actage + '_namelink').innerHTML = ma.name;
					document.getElementById(actage + '_namelink').setAttribute('href', '/services/babakonyha/recept' + ma.nameenc + '/' + ma.id);
					document.getElementById(actage + '_bovebben').setAttribute('href', '/services/babakonyha/recept' + ma.nameenc + '/' + ma.id);
					var times = mkt[ma.mktime].split(' ');
					document.getElementById(actage + '_timelink').innerHTML = times[0] + ' <span>' + times[1] + '</span>';
					document.getElementById(actage + '_timelink').setAttribute('href', '/services/babakonyha/lista?f_ido=' + ma.mktime);
					document.getElementById(actage + '_difficulty').innerHTML = har[ma.hardness];
					document.getElementById(actage + '_difficulty').setAttribute('href', '/services/babakonyha/lista?f_nehezseg=' + ma.hardness);
					document.getElementById(actage + '_diet').innerHTML = ill[ma.ill];
					document.getElementById(actage + '_diet').setAttribute('href', '/services/babakonyha/lista?f_dieta=' + ma.ill);
					document.getElementById(actage + '_cat').innerHTML = cats[ma.cat];
					document.getElementById(actage + '_cat').setAttribute('href', '/services/babakonyha/lista?f_tipus=' + ma.cat);
					var aages = ages[ma.age].split(' ');
					document.getElementById(actage + '_age').setAttribute('href', '/services/babakonyha/lista?f_kor=' + ma.age);
					document.getElementById(actage + '_agespan').setAttribute('class', 'kor_' + aages[0]);
					document.getElementById(actage + '_opinion').innerHTML = ma.opinion;
					document.getElementById('srbox' + actage).style.width = ma.stars + 'px';
				} else {
					$('#growl_message_err').jGrowl('Nem tudok új receptötletet adni! Próbáld újratölteni az oldalt!', { header: 'Új receptötlet:', life: 3000, theme: 'babaszoba_err', speed: 'slow'});
				}
			}
		});
	});
	
	$("#newcaptcha").click(function(){
		$.ajax({
			type: "POST",
			url: "/ajax/services/babakonyha/ajaxreq_ajax",
			data: "cmd=newcaptcha",
			dataType: "json",
			async: false,
			success: function(ma){
				if (ma.status == 'ok') {
					$("#img_captcha").attr('src', '/services/babakonyha/captcha.png?rnd=' + new Date().getTime());
				} else {
					$('#growl_message_err').jGrowl('Nem tudok új kulcsszót adni! Próbáld újratölteni az oldalt!', { header: 'Új kulcsszó:', life: 3000, theme: 'babaszoba_err', speed: 'slow'});
				}
			}
		});
	});
	
	STARVOTE.init({
		'w' : '19',
		'h' : '18',
		'wfix' : '36',
		'action' : '/ajax/services/babakonyha/vote_ajax',
		'enabled' : true,
		'votecount' : '.count'
	});
});

STARVOTE = {
	inited: false,

	w: null,
	h: null,
	wfix: null,

	action: null,

	enabled: false,

	// init
	init: function(conf) {
		this.w = conf.w;
		this.h = conf.h;
		this.wfix = conf.wfix;

		this.action = conf.action;

		this.votecount = conf.votecount;

		$enabled = this.enabled = conf.enabled;

		if (this.enabled) {
			$(".star-rating li a").each(function() {
				$info = $(this).parents('.stars').find(".star-info");
				$(this).mouseover(function() {
					$info.html($(this).attr('rel'));
					$info.show();
					// ahanyadik * X (szelesseg) - Y (wtf?)
					$info.css('margin-left', ($(this).html() * STARVOTE.w) - STARVOTE.wfix-5);
				});
				$(this).mouseout(function() {
					$info.hide();
				});
				$(this).click(function() {
					// ajax mentes (tipus, id, szavazat)
					STARVOTE.vote($(this).parents('.stars').attr('name'), $(this).attr('href').replace('#', ''), $(this).html(), $(this).parents('.stars'));
				});
			});
		}
		this.inited = true;
	},

	// szavazas kuldese
	vote: function(type, id, vote, stars) {
		if (!this.inited || !this.enabled) {
			return;
		}
		post = {'type': type, 'id': id, 'vote': vote};
		$.ajax({
			type: 'POST',
			url: this.action,
			dataType: 'json',
			data: $.param(post),
			success: function (ret) {
				if (ret.status == 'ok') {
					stars.find(STARVOTE.votecount).html(ret.cnt);
					stars.find('.current-rating').width(ret.current + 'px');
				}
				STARVOTE.message(ret);
			}
		});
	},

	message: function(ret) {
		$('#growl_message_' + ret.status).jGrowl(ret.message, { header: ret.header, life: 3000, theme: 'babaszoba_' + ret.status, speed: 'slow' });
	}
}
