$(function() {

	// szallitasi koltseg
	$("#fizetesi_mod").bind("change",function(){
		var fizmod = $(this).val();
		var atmod = $("#atveteli_mod").val();
		$.ajax({
			url: "/webshop/szallitas",
			method: "POST",
			data:  { "fizmod":fizmod, "atmod":atmod },
			dataType: 'JSON',
			success: function( result ) {								
				$("#adatok_szall").html(numberFormat(result.szallitas));
				$("#adatok_netto").html(numberFormat(result.netto));
				$("#adatok_brutto").html(numberFormat(result.brutto));
			}
		});
	});
	$("#atveteli_mod").bind("change",function(){
		$("#fizetesi_mod").trigger("change");
	});

	// scroll
	$(window).bind('scroll', function() {
		var pos = $(window).scrollTop();
		var win_h = $(window).height();
		var start = $(".newtime-container").offset().top;		
		console.log(pos, win_h, start);
		if ( (pos + win_h) >= start ){
			console.log("nwo");
			$("#newtime_logo").addClass("start");
		}
	});

	//slider
	var owl = $(".ban1").owlCarousel({
		loop: true,
		margin: 0,
		items: 1,
		lazyLoad: true,
		navText: ['left', 'right'],
		autoplay: true,
		autoplayTimeout: 12000,
		autoplayHoverPause: true,
		pagination: true,
	});		

	$("a.inactive").bind("click",function(event){
		event.preventDefault();
	});
	
	// lapozas, rendezes
	$(".talalatok-select").bind("change",function(){
		window.location = $(this).val();
	});
	
	$(".rendezes-select").bind("change",function(){
		window.location = $(this).val();
	});
	
	$(".oldal-select").bind("change",function(){
		window.location = $(this).val();
	});
	// /lapozas, rendezes

	// nezet-valto	
	if (getCookie("nezet") == "kompakt"){
		$("#nezet").prop("checked","");
		$("#termek-lista .termekek").addClass("kompakt");
	}
	else{
		$("#nezet").prop("checked","checked");
		$("#termek-lista .termekek").removeClass("kompakt");
	}
	$("#nezet").bind("change",function(){
		if( $(this).is(':checked') == false ){ // kompakt
			$("#termek-lista .termekek").addClass("kompakt");
			setCookie("nezet", "kompakt", 100);
		}
		else{ // lista
			$("#termek-lista .termekek").removeClass("kompakt");
			setCookie("nezet", "lista", 100);
		}
	});
	// /nezet-valto

	// keszlet-valto	
	if (getCookie("keszlet") == "1"){		
		$("#keszlet").prop("checked","checked");
	}
	else{
		$("#keszlet").prop("checked","");
	}
	$("#keszlet").bind("change",function(){
		if( $(this).is(':checked') == false ){ // kompakt
			setCookie("keszlet", "0", 100);
		}
		else{ // lista
			setCookie("keszlet", "1", 100);
		}
		window.location.assign($(this).val());
	});
	// /keszlet-valto

	// kosar torles mobil es desktop
	$("#basket-big .remove").bind("click",function(){
		var did = $(this).data("id");
		$("#basket-big input[name=db_"+did+"]").val(0);
		$("#kosar-form").submit();
	});
	// kosar termek valtoztatas mobil es desktop kozott szinkronban legyen
	$("#basket-big input.number").bind("change",function(){		
		var did = $(this).data("id");
		var db = $(this).val();
		$("#basket-big input[name=db_"+did+"]").val(db);
		$("#kosar-form").submit();
	});
	
		
	// login
	$("#user .user-inner").bind("click",function(){
		$("#login-window").toggleClass("hidden");
	});
	$("#login-window .close").bind("click",function(){
		$("#login-window").toggleClass("hidden");
	});
	// /login

	// menu
	$("#menu").bind("click",function(){
		$("#hamb-menu").addClass("open");
	});
	$("#hamb-menu .bezar").bind("click",function(){
		$("#hamb-menu").removeClass("open");
	});

	// === termek menu ===
	
	var max_menu_items = 9; // n elemet jelenít meg
	
	// hover
	$("#categories li.level-1 > a").mouseover(function(){
		$(this).children(".level1-hover").addClass("active");
		$("#categories ul.level-2").removeClass("active");
		$(this).next("ul.level-2").addClass("active");
	});
	
	// leave
	$("#categories li.level-1").mouseleave(function(){
		$(".level1-hover").removeClass("active");
		$("#categories ul.level-2").removeClass("active");
	});	
	
	// 10. elemtol hide
	$("#categories ul.level-3 li:nth-child(n+"+(max_menu_items+1)+")").addClass("more hidden");
	
	// tovabbi menuk
	$("#categories ul.level-3").each(function(index,value){
		var men = $(this).find("li.level-3").length;
		if (men > max_menu_items){
			$(this).append('<li><a href="" class="showmore">több...</a></li><li><a href="" class="showless hidden">...kevesebb</a></li>');
		}
	});
	
	// tovabbi menuk mutatasa
	$("#categories .showmore").click(function(event){
		event.preventDefault();
		$(this).parent().parent().find("li.more").removeClass("hidden");
		$(this).parent().parent().find("li a.showmore").addClass("hidden");
		$(this).parent().parent().find("li a.showless").removeClass("hidden");
	});
	
	// tovabbi menuk elterjtese
	$("#categories .showless").click(function(event){
		event.preventDefault();
		$(this).parent().parent().find("li.more").addClass("hidden");
		$(this).parent().parent().find("li a.showmore").removeClass("hidden");
		$(this).parent().parent().find("li a.showless").addClass("hidden");
	});

	// csak a leaf kattintható
	$("#categories li.level-1 > a").bind("click",function(e){
		e.preventDefault();
	});
	$("#categories li.level-2 > a").bind("click",function(e){
		if ($(this).parent().find(".level-3").length != 0){
			e.preventDefault();
		}		
	});

	// === /termek menu ===

	// === mobile termek menu ===
	// open - close mobil termek menu
	$("#categories-m ul.level-2").addClass("hidden");
	$("#categories-m li.level-1").prepend('<div class="nyil"><img src="/assets/images/nyil.svg"></div>');
	$("#categories-m .kategoriak").bind("click",function(){
		$("#categories-m .level-0").toggleClass("hidden");
		$("#categories-m").toggleClass("open");
	});
	// open-close level-1
	$("#categories-m li.level-1 > a").bind("click",function(e){
		e.preventDefault();
		$(this).next("ul.level-2").toggleClass("hidden");		
		$(this).parent().toggleClass("open");
	});
	// === /mobile termek menu ===
	
	// kosarba
	$(".to-basket").bind("click",function(){
		if( $(this).hasClass("no") ) {
			modal_alert("Üzenet","A termék jelenleg nem elérhető!");
		}
		else{
			$.ajax({
				url: "/webshop/add_basket",
				method: "POST",
				data:  {
					"id":$(this).data("id"),
					"db":$(this).parent().parent().find("input[name=db]").val(),
					"raktar":$(this).parent().parent().find("input[name=raktar]").val()
				},
				dataType: 'JSON',
				success: function( result ) {
					if (result){
						$("#basket .label2").html(result.osszes_str);
						$("#basket .numbask").html(result.ossz_db);					
						modal_alert("Üzenet","Termék sikeresen hozzáadva a kosárhoz!");
					}				
				}
			});
		}
	});
	
	// szallitasi cimek
	$(".szallitasi_cimek").bind("change",function(){
		$.ajax({
			url: "/webshop/ajax_szallitasi_cim",
			method: "POST",
			data:  {"id":$(this).val()},
			dataType: 'JSON',
			success: function( result ) {								
				$(".szallitas input[name=sz_nev]").val(result.nev);
				$(".szallitas select[name=sz_orszag]").val(result.orszag);
				$(".szallitas input[name=sz_isz]").val(result.isz);
				$(".szallitas input[name=sz_telepules]").val(result.telepules);
				$(".szallitas input[name=sz_utca]").val(result.utca);
				$(".szallitas input[name=sz_hsz]").val(result.hsz);
				$(".szallitas input[name=sz_egyeb]").val(result.egyeb);
			}
		});
	});

	$(".show_rendeles").bind("click",function(){
		$(".basket-row.spacer").html("");
		var gomb = $(this);
		$.ajax({
			url: "/webshop/ajax_show_rendeles",
			method: "POST",
			data:  {"id":gomb.data("id")},
			success: function( result ) {								
				//gomb.parent().parent().next().html(result);
				$("#xtModal .modal-title").html("Rendelés részletei");
				$("#xtModal .modal-body").html(result);
				//$("#xtModal .modal-footer").html("");
				$("#xtModal").modal("show");
			}
		});
	});

	$(".show_garugy").bind("click",function(){
		$(".basket-row.spacer").html("");
		var gomb = $(this);
		$.ajax({
			url: "/webshop/ajax_show_garancia_ugy",
			method: "POST",
			data:  {"id":gomb.data("id")},
			success: function( result ) {								
				$("#xtModal .modal-title").html("Garanciális ügy részletei");
				$("#xtModal .modal-body").html(result);
				$("#xtModal").modal("show");
			}
		});
	});

	//Fancybox.bind("[data-fancybox]", {});

	// $("#termek-adatlap .product-image").click(function(e){
	// 	e.preventDefault();
	// 	$("#xtModal .modal-title").html($(this).parent().parent().find(".right h1").text());
	// 	$("#xtModal .modal-body").html("<center><img src='"+$(this).find("img").attr("src")+"' style='max-width:100%'></center>");
	// 	$("#xtModal").modal("show");
	// });

	// raktár választás termék adatlapon
	$("#termek-adatlap .right .sor .value .kesz").click(function(e){
		e.preventDefault();
		var valasztott = $(this);
		// ár
		$("#termek-adatlap .right .price .val").html(valasztott.data("ar"));
		// raktar
		$("#raktar").val(valasztott.data("raktar-id"));
		// keret
		$("#termek-adatlap .right .sor .value .kesz").removeClass("selected");
		valasztott.addClass("selected");
		// kosárba rakható-e?
		if (valasztott.data("raktaron")){
			$(".to-basket").removeClass("no");
		}
		else{
			$(".to-basket").addClass("no"); // nincs raktaron
		}
	});
	
	$(".adatlap .kesz[data-raktar-id=" + $("#auto-raktar").data("auto-raktar") + "]").trigger("click");

	// headline banner
	$(".headline:nth-child(2)").css("top",$(".headline:nth-child(1)").outerHeight(),"px");
	$("body").css("padding-top",$(".headline:nth-child(1)").outerHeight()+$(".headline:nth-child(2)").outerHeight()+"px");

	$(".headline .bezar").bind("click",function(){
		$.ajax({
			url: "/webshop/ajax_banner_bezar",
			method: "POST",
			data:  {},
			success: function( result ) {								
				location.reload();
			}
		});
	});

	// /headline banner

	// uj jelszo check
	$(".passchange").bind("submit",function(){
		if ( $(".passchange input[name='pass1']").val() !=  $(".passchange input[name='pass2']").val()){
			modal_alert("Hiba!","A két jelszó nem egyezik!");
			return false;
		}
	});

	// vonalkodolvaso ne kuldje be
	$(".garancia-sn-form input[name='gyariszam']").bind("keydown",function(e){
		if(e.which == 13){ // enter
			e.preventDefault();
		}
	});

	// garancia sn check
	$(".garancia-sn-form input[name='garancia1']").bind("click",function(e){
		e.preventDefault();
		var form = $(".garancia-sn-form");
		var kuld = false;
		$.ajax({
			url: "/webshop/garancia_check",
			method: "POST",
			data:  {"gyariszam":$(".garancia-sn-form input[name='gyariszam']").val()},
			success: function( result ) {								
				if(result){
					if (confirm("A gyáriszám már szerepelt garanciális ügyintézésben. Biztos benne, hogy beküldi?")){
						form.submit();	
					}
				}
				else{
					form.submit();
				}
			}
		});
	});

	// gszuro form
	$(".gszuro-form select").change(function(){
		$(".gszuro-form").submit();
	});

});

// form data to object
(function($){
  $.fn.getFormData = function(){
    var data = {};
    var dataArray = $(this).serializeArray();
    for(var i=0;i<dataArray.length;i++){
      data[dataArray[i].name] = dataArray[i].value;
    }
    return data;
  }
})(jQuery);

function setCookie(cname, cvalue, exdays) {
	var d = new Date();
	d.setTime(d.getTime() + (exdays*24*60*60*1000));
	var expires = "expires="+ d.toUTCString();
	document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

function getCookie(cname) {
	var name = cname + "=";
	var decodedCookie = decodeURIComponent(document.cookie);
	var ca = decodedCookie.split(';');
	for(var i = 0; i <ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') {
			c = c.substring(1);
		}
		if (c.indexOf(name) == 0) {
			return c.substring(name.length, c.length);
		}
	}
	return "";
}

// univerzalis modal alert
function modal_alert(title,message){
	$('#xtModalAlert .modal-title').html(title);
	$('#xtModalAlert .modal-body').html(message);
	$('#xtModalAlert').modal('show');		
}

function numberFormat(num) {
	return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1 ');
}