function bal_felso_kosar_frissitese(){
	$.post("components/rendeles/bal_felso_kosar.php", {esemenyid : 1}, function(data){
		if (data.length>0){
			$(".kiskosar").html(data).fadeIn("slow");
		}
	});
};

function kosar_termek_osszesen_frissitese(sor,kosarid_ertek){
	$.post("components/rendeles/kosar_termek_osszesen.php", {kosarid : kosarid_ertek}, function(data){
		// Eredmény kiértékelése JSON formátumból
		var obj = jQuery.parseJSON(data);

		sor.parent().parent().find("#termek_netto").html(obj.html_kod_netto).fadeIn("slow");
		sor.parent().parent().find("#termek_brutto").html(obj.html_kod_brutto).fadeIn("slow");
	});
};

function kosar_osszesen_frissitese(){
	$.post("components/rendeles/kosar_osszesen.php", {esemeny : 1}, function(data){
		// Eredmény kiértékelése JSON formátumból
		var obj = jQuery.parseJSON(data);

		$(".nagykosar_osszesen_fejlec").html(obj.html_kod).fadeIn("slow");
	});
};

$(document).ready(function(){

	$('input.darab').numeric({
		minValue	: 1,
		maxValue	: 999,
		increment	: 1
	});

	$('#slider').nivoSlider({
		directionNav:false,
		controlNav:false,
		keyboardNav:false,
		pauseOnHover:false
	});
	$("ul#news_scroller").liScroll({travelocity: 0.08});

	$("a[rel=galeria]").fancybox({
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? '&nbsp;-&nbsp; ' + title : '') + '</span>';
		}
	});

	$("[title]").mbTooltip({ // also $([domElement]).mbTooltip  >>  in this case only children element are involved
		opacity : .97,       //opacity
		wait:200,           //before show
		cssClass:"default",  // default = default
		timePerWord:1000,      //time to show in milliseconds per word
		hasArrow:true,			// if you whant a little arrow on the corner
		hasShadow:true,
		imgPath:"images/common/",
		ancor:"mouse", //"parent"  you can ancor the tooltip to the mouse position or at the bottom of the element
		shadowColor:"black", //the color of the shadow
		mb_fade:200 //the time to fade-in
	});


	$("#tabs").tabs();

	// Bal felső kiskosár betöltése
	bal_felso_kosar_frissitese();

	/***** VASARLAS *****/

	$(".lepjenbe").click(function(){ // ha nincs bejelentkezve
		$('#dialog-message')
		.attr('title', "Figyelem!")
		.html('<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 40px 0;"></span>A vásárláshoz felhasználó név és jelszó szükséges!</p><p>Kérjük jelentkezzen be az oldal tetején, vagy regisztráljon.</p>')
		.dialog({
			autoOpen: false,
			modal: true,
			buttons: {
				'Bejelentkezek': function() {
					document.location.href+='#top';
					$(this).dialog('close');
				},
				'Regisztrálok': function() {
					document.location.href='index.php?fomenu=registration&menu=registration';
				}
			}
		})
		.dialog('open');
		return false;
	});

	$(".raktaron").click(function(){
		var termekid = $(this).parent().find("input[name='termekid']").val();
		var darab = $(this).parent().parent().find("input[name='darab']").val();
		var termektulajdonsagok = $(this).parent().parent().find("select[name='termektulajdonsagok']").val();
		$.post("components/termek/kosarba.php", {termekid: termekid, darab: darab, tulajdonsag: termektulajdonsagok}, function(data){
			$('#dialog-message')
			.attr('title', "Vásárlás")
			.html(data)
			.dialog({
				autoOpen: false,
				modal: true,
				buttons: {
					'Rendben': function() {
						bal_felso_kosar_frissitese();
						$(this).dialog('close');
					},
					'Kosár tartalma': function() {
						document.location.href='index.php?fomenu=webshop&menu=rendeles';
					}
				}
			})
			.dialog('open');
			return false;
		});
	});

	$(".nincsraktaron").click(function(){
		var termekid = $(this).parent().find("input[name='termekid']").val();
		var darab = $(this).parent().parent().find("input[name='darab']").val();
		var termektulajdonsagok = $(this).parent().parent().find("select[name='termektulajdonsagok']").val();
		if(!/^[1-9]\d*$/.test(darab)){
			$('#dialog-message')
			.attr('title', "Hiba!")
			.html('<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 40px 0;"></span>Érvénytelen mennyiség</p>')
			.dialog({
				autoOpen: false,
			    modal: true,
			    buttons: {
			      'Vissza': function() {
			        $(this).dialog('close');
			      }
			    }
			})
			.dialog('open');
		}
		else {
			$('#dialog-order')
			.attr('title', "Figyelem!")
			.html('<p><span class="ui-icon ui-icon-info" style="float:left; margin:0 7px 40px 0;"></span>Jelenleg a termék nincs raktáron, a szállításra várni kell. Így is megrendeli a terméket?</p>')
			.dialog({
				autoOpen: false,
			    modal: true,
			    buttons: {
			      'Nem': function() {
			        $(this).dialog('close');
			      },
				  'Igen': function() {
			        $.post("components/termek/kosarba.php", {termekid: termekid, darab: darab, tulajdonsag: termektulajdonsagok}, function(data){
			        	bal_felso_kosar_frissitese();
			        });
					$(this).dialog('close');
			      }
			    }
			})
			.dialog('open');
		}
		return false;
	});

	/***** VASARLAS VEGE *****/

	// Kosárban lévő mennyiség nővelése
	$(".gomb_kek.kosar_plusz").click(function(){
		var kosar_id_ertek = $(this).jSonComments().kosarid;
		var sor = $(this);

		$.post("components/rendeles/rendeles_modositas.php", {esemenyid : 2, id : kosar_id_ertek}, function(data){
			// Eredmény kiértékelése JSON formátumból
			var obj = jQuery.parseJSON(data);

			if (obj.sikerult=="1"){
				sor.prev().prev().html(obj.mennyiseg).fadeIn("slow");
				kosar_termek_osszesen_frissitese(sor,kosar_id_ertek);
				kosar_osszesen_frissitese();
				bal_felso_kosar_frissitese();
			}
		});

	});

	// Kosárban lévő mennyiség csökkentése
	$(".gomb_barna.kosar_minusz").click(function(){
		var kosar_id_ertek = $(this).jSonComments().kosarid;
		var sor = $(this);

		$.post("components/rendeles/rendeles_modositas.php", {esemenyid : 3, id : kosar_id_ertek}, function(data){
			// Eredmény kiértékelése JSON formátumból
			var obj = jQuery.parseJSON(data);

			if (obj.sikerult=="1"){
				if (obj.mennyiseg!=0){
					sor.prev().html(obj.mennyiseg).fadeIn("slow");
				} else {
					sor.parent().parent().parent().effect('explode', {}, 1000, function(){});
				}

				kosar_termek_osszesen_frissitese(sor,kosar_id_ertek);
				kosar_osszesen_frissitese();
				bal_felso_kosar_frissitese();
			}
		});

	});

	// Kosárban lévő termék törlése
	$(".kosar_torles").click(function(){
		var itemid = $(this).parent().parent().attr('id').substring(4); // levagom a <li> id-je elöl a sallangot
		$.post("components/rendeles/rendeles_modositas.php", {esemenyid : 1, id : itemid}, function(data){
			if (data.length>0){
			}
		});
		$(this).parent().parent().effect('explode', {}, 1000, function(){
//			$(this).parent().hide();
		});

		kosar_osszesen_frissitese();
		bal_felso_kosar_frissitese();
	});

});
