(function($) {
	var imgList = [];
	$.extend({
		preload: function(imgArr, option) {
			var setting = $.extend({
				init: function(loaded, total) {},
				loaded: function(img, currentloaded, loaded, total) {},
				loaded_all: function(loaded, total) {}
			}, option);
			var total = imgArr.length;
			var loaded = 0;
			
			setting.init(0, total);
			for(i = 0; i < imgArr.length; i++){
				imgList.push($('<img id="temp'+i+'" />')
					.attr("src", imgArr[i])
					.load(function() {
						loaded++;
						var id = this.id.replace("temp", "");
						
						setting.loaded(this, id, loaded, total);
						if(loaded == total) {
							setting.loaded_all(loaded, total);
						}
					})
				);
			}
			
		}
	});
})(jQuery);


function startSlideshow(i){
	
	if (window.carousellength == 1) 
	{
		$("#carousel a").eq(0).css("display", "block").css("visibility", "visible").css("zIndex", "202");		
	} 
	else if (window.carousellength == 2) 
	{
		$("#carousel a").eq(i).css("display", "block").css("visibility", "visible").css("zIndex", "202");
		$("#carousel a").eq(i+1).css("display", "block").css("visibility", "visible").css("zIndex", "201");
	}
	else if (window.carousellength > 2) 
	{
		var i = i;
		if (i == window.carousellength) {i = i; f = 0; s = 1;}
		else if ((i + 1) == window.carousellength) {i = i; f = 1; s = 2;}
		else if ((i + 2) == window.carousellength) {i = i; f = (i + 1); s = 1;}
		else {i = i; f = (i + 1); s = (i + 2); t = (i + 3);}
		$("#carousel a").eq(i).show().css("zIndex", "203");
		$("#carousel a").eq(f).show().css("zIndex", "202");
		$("#carousel a").eq(i).fadeOut(2000, function(){
			setTimeout(function(){		
				$("#carousel a").eq(i).hide().css("zIndex", "200");
				$("#carousel a").eq(s).hide().css("zIndex", "200");
				if (i == (window.carousellength-1))
				{ 
					startSlideshow(1);
				}
				else 
				{
					startSlideshow(f);
				}
			}, 2000);
		});
	}
}

function showFastCarousel() {
	window.carousellength = carousel[0].length;
	var $c = $('<div id="carousel" class="clearfix"><a name="preloader" id="preindicator"><h1>'+carouseltitle+'</h1><h3>'+carouselslogan+'</h3><div id="crossfader"></div></a></div>');
	$c.appendTo("#full");
	$("preindicator").click(function(e){
		e.preventDefault();
	});
	setTimeout(function(){
		$.preload(carousel[0], {
			init: function(loaded, total) {},
			loaded: function(img, currentloaded, loaded, total) {
				if (loaded == 2) {startSlideshow(0);}
				img.setAttribute('width', '700');
				img.setAttribute('height', '100');
				img.setAttribute('alt', carousel[1][currentloaded]["keywords"]);
				$('<a></a>')
				.append('<h2>'+carousel[1][currentloaded]["title"]+'</h2>')
				.append(img)
				.appendTo("#carousel");
			},
			loaded_all: function(loaded, total) {
				carousel = undefined;
			}
		});	
	}, 6000);
}


$(document).ready(function(){

	if (typeof carousel !== "undefined") {
		showFastCarousel();
	}
	$("#gallery a.childdrawning").fancybox({titlePosition: 'over', cyclic: true});
	
	
	$("#loading").fadeOut();
	
	var replacement = $("#titlereplacement").val();
	$("title").text(strip(replacement));
	
	var ismail_regexp = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	
function ismail (s) {
	return String(s).search (ismail_regexp) != -1;
}
function strip(html) {
	var tmp = document.createElement("DIV");
	tmp.innerHTML = html;
	return tmp.textContent||tmp.innerText;
}

	window.defaultsearch = $("#f_search").val();


	$("#f_cellphone").mask("06 (99) 999-99-99", {placeholder: "_"});
	$("#f_phone").mask("06 99999999", {placeholder: "_"});
	
	$('.autoclear').each(function(index) {
	    $(this).data("original", $(this).attr("value"));
	});
	
	$('.autoclear').focus(function(){
		if ($(this).val() == $(this).data("original")) {
			$(this).val("");
		}
	});
	$('.autoclear').blur(function(){
		if ($(this).val() == "") {
			$(this).val($(this).data("original"));
		}
	});	

	$("#form-finder #form-finder-submit").click(function(e){
		$("#form-finder").submit();
	});
	$("#form-finder").submit(function(){
		if (($("#f_search").val() == $("#f_search").data("original")) || ($("#f_search").val().length < 3)) {
			$("#form-finder-error").text("A keresendő kifejezésnek legalább 3 karakter hosszúnak kell lennie.");
			$("#form-finder-error").show();
			return false;
		}
		else {
			window.location = "/Keresés/"+$("#f_search").val()+"/";
			return false;
		}
	});
	
	if ($("#wrap aside nav ul li.active").size() > 0)
	{
		$("#wrap #flagcarrier").css("top", $("#wrap aside nav ul li.active").offset().top+20);
	}
	else 
	{
		$("#wrap #flagcarrier").hide();
	}
	
	

	
	
$(".numonly").keydown(function (event) { 
	if ((!event.shiftKey && !event.ctrlKey && !event.altKey) && ((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105)))
	{ 
	} 
	else if (event.keyCode != 8 && event.keyCode != 46 && event.keyCode != 37 && event.keyCode != 39 && event.keyCode != 9)
	{ 
	event.preventDefault(); 
	} 
	}); 
	
$("form#ajanlat a.button-recall").click(function() 
	{
		$(this).parent().submit();
	}
);

















jQuery.fn.validateThis = function() {
	
	function addErrormsg(e, status) 
	{
		if (e.next("span.msg").size() != 0) 
		{
			e.next("span.msg").text(status);
		} 
		else 
		{
			var c = $("<span />");
			c.addClass("msg error").text(status);
			e.after(c);
//			e.css("background", "red");
		}
	}
	
	var theform = $(this);
	
	theform.submit(function()
	{
		var ok = true;
		var gok = false;
		var formArr = new Array();
		
		theform.find("input, textarea").each(function(index, value)
		{
			
			// ha a cuccba érkezik egy keydown akkor kiszedi a mögöttes tartalmat pl hibaüzenetet.
			$(this).keydown(function(){
				if ($(this).attr("rel")) {
					$(this).removeClass("bah").next("span.msg").remove();
					$(this).siblings('[rel="'+$(this).attr("rel")+'"]').removeClass("bah").next("span.msg").remove();
				}
				$(this).removeClass("bah").next("span.msg").remove();
			});
			
			// ha needy
			if ($(this).hasClass("needy"))
			{
				if ($(this).val() == $(this).data("original")) 
				{
					formArr.push([1, index, 1]);
				}
				else if ($(this).val() == "")
				{
					formArr.push([1, index, 2]);
				}
				else if ($(this).val().length < 3)
				{
					formArr.push([2, index, 1]);
				}
				else 
				{

				}
			} 
			
			if ($(this).hasClass("email"))
			{
				if (!(ismail($(this).val())))
				{
					formArr.push([2, index, 2]);
				}
			}
			
			if ($(this).hasClass("groupy")) 
			{
				console.log("HASCLASS GROUPY::::")
				console.log($(this));
				var bok = false;
				if (!(($(this).val() == $(this).data("original")) || ($(this).val() == "") || ($(this).val().length < 3)))
				{
					bok = true;
				}
				$(this).siblings('.groupy[rel="'+$(this).attr("rel")+'"]').each(function() {
					console.log($(this));
					console.log($(this).val());
					if (!(($(this).val() == $(this).data("original")) || ($(this).val() == "") || ($(this).val().length < 3)))
					{
						bok = true;
					}
				});
				if (bok == false) {
					formArr.push([3, $(this).attr("rel"), 1]);
				}
			}
	});
	
	/*
	
	ARRAY
	0: 	1 egy egyedülálló needy elem hibás
	1: 	index, mindig az aktuális elem indexe, ami mögé a hibaüzenet kerül
	2: 	1 volt eredetileg értéke a doboznak és ez az érték nem változott meg
	    2 a doboz értéke null
	
	0:  2 egy egyedülálló needy elem tartalma hibás (pl: nem e-mail cím, stb.)
    1:  index, ami mindig az aktuális elem indexe
	2:  1 doboz értéke kevesebb mint 3 karakter
		2 a doboznak e-mail címnek kellene lennie, de nem az.
		
	0:  3 egy groupy elemcsomag hibás
	1:  a group azonosítója
	2:  1 a groupban nincsen szöveg
	*/
	
	console.log(formArr);
	
	
	if (formArr.length > 0) 
	{
		$.each(formArr, function(index, value) {
			if (value[0] == 1) 
			{
				if (value[2] == 1) {
					addErrormsg(theform.find("input, textarea").eq(value[1]), "Kérem töltse ki ezt a mezőt.");
				}
				if (value[2] == 2) {
					addErrormsg(theform.find("input, textarea").eq(value[1]), "Kérem töltse ki ezt a mezőt.");
				}
			}
			if (value[0] == 2) 
			{
				if (value[2] == 1) {
					addErrormsg(theform.find("input, textarea").eq(value[1]), "A megadott szöveg kevesebb mint 3 karakter hosszú.");
				}
				if (value[2] == 2) {
					addErrormsg(theform.find("input, textarea").eq(value[1]), "Ez nem tűnik valós e-mail címnek.");
				}
			}
			if (value[0] == 3) 
			{
				$.each($('input[rel="'+value[1]+'"], textarea[rel="'+value[1]+'"]'), function(){
//					console.log($(this));
					if (value[2] == 1) {
						addErrormsg($(this), "Legyen szíves legalább egy telefonszámot adjon meg!");
					}
				});
				
			}
		});
		return false;
	}
	else
	{
		return true;
	}
	
	/*
	
	console.log($(this));
	if ($(this).val() == $(this).data("original")) 
	{
		addErrormsg($(this), "Hiba, ebben a dobozban az eredeti szöveg van.");
		ok = false;
	}
	else if ($(this).val() == "")
	{
		addErrormsg($(this), "Hiba, ide nincs írva semmi.");
		ok = false;
	}
	else if ($(this).val().length < 3)
	{
		addErrormsg($(this), "Hiba, ez meg túl rövid.");
		ok = false;
	}
	else 
	{
		
	}
	
	*/
});		

}








$("form#ajanlat").validateThis();

});
