$(function(){
	
	//A böngésző nyelvének lekérdezése
	var lng = window.navigator.userLanguage || window.navigator.language;
	//Csak az első 2 karakter kell a chrome miatt
	var lang=lng.substring(0,2);
	if (lang=="hu" && readCookie('ido')==null && false) {
		//Cookie beállítva 1óra időtartamra
		createCookie('ido',1,60);
		
            $.fancybox({
			 type: "iframe",
			 fitToView: false,
			 autoSize: false,
             width: 800,
             height: 500,
             autoScale: false,
             transitionIn: 'fade',
             transitionOut: 'fade',
	         wrapCSS    : 'catch_fancy',
			 closeBtn : false,
             href: '/teszt/felugro.php'
			});
	}
            
		
	function createCookie(name, value, mins/*days*/) {
		var expires;
	
		if (mins) {
			var date = new Date();
			//date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
			date.setTime(date.getTime() + (mins * 60 * 1000));
			expires = "; expires=" + date.toGMTString();
		} else {
			expires = "";
		}
		document.cookie = encodeURIComponent(name) + "=" + encodeURIComponent(value) + expires + "; path=/";
	}
	
	function readCookie(name) {
		var nameEQ = encodeURIComponent(name) + "=";
		var ca = document.cookie.split(';');
		for (var i = 0; i < ca.length; i++) {
			var c = ca[i];
			while (c.charAt(0) === ' ') c = c.substring(1, c.length);
			if (c.indexOf(nameEQ) === 0) return decodeURIComponent(c.substring(nameEQ.length, c.length));
		}
		return null;
	}
	

	// bottom banner
	setInterval(function(){change_box_one()},10000);
	$("#bottom .box").hide();
	$("#bottom .box").slice(0,3).show();

	// changer
	$('#changer .img').tilesSlider({
		x: 6,
		y: 4,
		effect: "simple",
		random: true ,
		slideSpeed:4000,
		auto: true,
		timer: false,
		loop: true,
		nav: false,
	});

	/* video box position */
	console.log( parseInt($("#right #content.product .product_cont .descr").css("height")) );
	if (parseInt($("#right #content.product .product_cont .descr").css("height")) > 200){
		$("#right #content.product .product_cont .video").addClass("h").css("width");
	}

	// gif anim
	$(".ani").hover(function(){
		$(this).find(".athumb").hide();
	},function(){
		$(this).find(".athumb").show();
	});

	// last item
	$("#menu .item:last").addClass("last");
	$("#menu .item .subitem .cat_container .cat:last").addClass("last");
	$("#right #content.product .product_cont .data1 table tr:last").addClass("last");
	$("#right #content.product .product_cont .data2 table tr:last").addClass("last");
	$("#right #content.product .product_cont .v_images .images .imgs:last").addClass("last");
	$("#right #content.product .product_cont .h_images .images .imgs:last").addClass("last");
	$("#left #ajanlok .catches .img:nth-child(3n)").addClass("last");
	$("#right #content.catches .cat:nth-child(5n)").addClass("last");
	$("#right #cat_selector .cat:last").addClass("last");
	$("#right #content.nyito .body .news .title:last").addClass("last");
	$("#right #content.nyito .body .news .intro:last").addClass("last");
	$("#right #content.videos_cat .body .left .cat:nth-child(3n)").after('<div class="clear"></div><div class="dotted"></div>');

	$("#right #content .body .cont p img").parents("p").before("<p></p>");

	// table strip
	$("#right #content.product .product_cont .data1 table tr:even").addClass("even");
	$("#right #content.product .product_cont .data2 table tr:even").addClass("even");

	// submenu nyil pozicio a szulo item fele
	$("#menu .item").each(function(k,v){
		$(v).find(".subitem .arrow").css("margin-left",(parseInt($(v).css("width"))/2)+"px");
	});

	// submenu
	$("#menu .item").hover(function(){
		$(this).children(".subitem").stop(true,true).slideDown(function(){
			if ($(this).find(".cat").length > 0){ // termek almenu
				setEqualHeight($("#menu .item .subitem .cat_container .cat"));
			}
		}).parent().addClass("selected");
	},function(){
		$(this).children(".subitem").slideUp().parent().removeClass("selected");
	});

	// bottom menu hover
	$("#right #cat_selector .cat").hover(function(){
		$(this).find(".logo_szurke").hide();
		$(this).find(".logo_szines").show();
		$(this).find(".termek_szurke").hide();
		$(this).find(".termek_szines").show();
		$(this).find(".rm").css("background-image","url(/images/red_more.png)");
	},function(){
		$(this).find(".logo_szurke").show();
		$(this).find(".logo_szines").hide();
		$(this).find(".termek_szurke").show();
		$(this).find(".termek_szines").hide();
		$(this).find(".rm").css("background-image","url(/images/grey_more.png)");
	});

	// news lapozas
	$("#right #content.nyito .foot_a .pag").disableSelection();
	$("#right #content.nyito .foot_a .pag .np").click(function(){
		$("#right #content.nyito .body").html("");
		$("#right #content.nyito .foot_a .pag .np").removeClass("akt");
		$(this).addClass("akt");
		var pathArray = window.location.pathname.split( '/' );
		if (!pathArray[1]){
			var lang = "en";
		}
		else{
			var lang = pathArray[1];
		}
		$.get("/"+lang+"/news/ajax/"+$(this).attr("data-page"),function(data){
			$("#right #content.nyito .body").html(data).fadeIn();
		});
	});
	$("#right #content.nyito .foot_a .pag .left").click(function(){
		$("#right #content.nyito .foot_a .np.akt").prev(".np").click();
	});
	$("#right #content.nyito .foot_a .pag .right").click(function(){
		$("#right #content.nyito .foot_a .np.akt").next(".np").click();
	});

	// news archives lapozas
	$("#right #content.archives .foot_a .pag").disableSelection();
	$("#right #content.archives .foot_a .pag .np").click(function(){
		$("#right #content.archives .body").html("");
		$("#right #content.archives .foot_a .pag .np").removeClass("akt");
		$(this).addClass("akt");
		var pathArray = window.location.pathname.split( '/' );
		if (!pathArray[1]){
			var lang = "en";
		}
		else{
			var lang = pathArray[1];
		}
		$.get("/"+lang+"/news/ajax_archives/"+$(this).attr("data-page"),function(data){
			$("#right #content.archives .body").html(data);
		});
	});
	$("#right #content.archives .foot_a .pag .left").click(function(){
		$("#right #content.archives .foot_a .np.akt").prev(".np").click();
	});
	$("#right #content.archives .foot_a .pag .right").click(function(){
		$("#right #content.archives .foot_a .np.akt").next(".np").click();
	});

	// global fancy
	$(".thickbox").fancybox({minWidth:50});

	// catches fancy
	$(".catchbox").fancybox({
		afterLoad   : function() {
	        this.inner.append( '<div class="catches_data_content">'+this.element.find(".catch_data").html()+'</div>' );
	        this.inner.append( '<div class="catches_share_content">'+$(".catch_share").html()+'</div>' );
    	},
    	wrapCSS : 'catch_fancy',
    	padding : [25,25,50,25],
    	helpers : {
	        overlay : {
	            css : {
	                /*'background' : 'none'*/
	            }
	        }
    	},
    	tpl 	: {
    		closeBtn : '<a title="Close" class="fancybox-item fancybox-close" href="javascript:;">close</a>',
    	}
	});

	// video fancy
	$(".video_fancy").click(function() {
		$.fancybox({
				'padding'		: 0,
				'autoScale'		: false,
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				'title'			: this.title,
				'width'			: 680,
				'height'		: 495,
				'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
				'type'			: 'swf',
				'swf'			: {
				   	 'wmode'		: 'transparent',
					'allowfullscreen'	: 'true'
				}
			});

		return false;
	});

});

function change_box_one(){
	var now = $("#bottom .box:visible:first");
	var next = $("#bottom .box:visible:first").next().next().next();
	if (next.hasClass("box")){
		now.css("overflow", "hidden");
		now.animate({
				width:'0',
			},600,function(){
			now.animate({
					marginLeft:'0',
				},600,function(){
					now.hide();
					now.css("width",next.css("width"));
					now.css("margin-left",next.css("margin-left"));
					now.css("overflow", "visible");
					next.fadeIn("slow");
			});
		});
	}
	else{
		$("#bottom .box").hide();
		$("#bottom .box").slice(0,3).show();
	}
}

function setEqualHeight(columns){
	var tallestcolumn = 0;
	columns.each(
		function(){
			currentHeight = $(this).height();
			if(currentHeight > tallestcolumn){
				tallestcolumn  = currentHeight;
			}
		}
	);
	columns.height(tallestcolumn);
}

(function($){
    $.fn.disableSelection = function() {
        return this
                 .attr('unselectable', 'on')
                 .css('user-select', 'none')
                 .on('selectstart', false);
    };
})(jQuery);

window.onload = function(){ // kepek merete miatt kell
	if ($("#right").height() > $("#left").height()) {
		var ld = Math.floor( ( $("#right").height() - $("#left").height()  ) / 157);
		//console.log( $("#right").height(),  $("#left").height());
		$("#left .dinvaltozo .valtozo").slice(0,ld).show();
	}
}
