/* Created by sydo - dreamscape.hu */
var user_error = 0; 
$(document).ready(function() {
	
	$(".button").button();
	
	
	/* MAIN IMAGE CYCLE */
	
	$('#main_image').cycle({ 
		fx:     'fade', 
		speed:  'slow', 
		timeout: 7000,
		next:   '#next', 
		prev:   '#prev' 
	});
	
	/* MENU */
	
	$("#left_menu li ul").css("display", "none"); // hide all menu items
	
	$("#left_menu li:has('ul')").addClass("parent");
	
	$("#left_menu li").hover(function(ev){
		
	$(this).addClass("hovered").parent().parent().removeClass("hovered");
		ev.stopPropagation();
	}, function() {
		$(this).removeClass("hovered");
	});
	
	$("#left_menu li:has('ul')").click(function(event) {
		var $target = $(event.target);
		if ( $target.is(":has('ul')") ) {
			$(this).find("ul:first").slideToggle("fast").parent().toggleClass("opened");
			return false; 
		}
		else {
			$target.find("a").trigger("click");
		}
	});
	
	$("#left_menu a").each(function() {
		var link = $(this).attr("href");
		var list_id = link.replace("index.php?action=list&id=", "");		
		var opened = $("#opened_menu").val();
		if (list_id == opened) {
			$(this).addClass("active").parent().parent().addClass("opened").show().parent("li").addClass("opened").parent().show().parent("li").addClass("opened");
		}
	});
	
	

	/* TOP NAV */
	if (document.location.href.search("elerhetosegek") != -1) { var pos = "393.5px"; $("#highlight").css("left", pos); }
	else if (document.location.href.search("akcios") != -1) { var pos = "623px"; $("#highlight").css("left", pos); }
	else if (document.location.href.search("telepitoknek") != -1) { var pos = "859px"; $("#highlight").css("left", pos); }
	else { var pos = "196px"; }
	
	$("#menu li img").hover(function() {
		$("#highlight").stop().animate({ left: $(this).position().left + $(this).width() / 2 - 150 / 2 + 179}, 500, "easeOutBack");
	}, function() {
		//$("#highlight").stop().animate({ left: pos }, 500, "swing" );
	});
	
	$("#menu").hover(function() {}, function() {
		$("#highlight").stop().animate({ left: pos }, 500, "easeOutBack" );
	});
	
	
	
	
	/* CONTENT */
	
	$("#content-right .thumb").hoverIntent(function(e) { 
		var image = $(this).attr("id");
		$("#big_" + image).css("top", e.pageY + 10 + "px");
		$("#big_" + image).css("left", e.pageX + 10 + "px");
		$().mousemove(function(e){
			$("#big_" + image).css("top", e.pageY + 10 + "px");
			$("#big_" + image).css("left", e.pageX + 10 + "px");
   		}); 
		$("#big_" + image).css({ width: "400px" } );
		$("#big_" + image).slideDown("fast");
	}, 
	function() { 
		var image = $(this).attr("id");
		$("#big_" + image).stop().hide();
		$("#big_" + image).css("width", "100px");
	});
	
	var row = 0;
	
	$("table tr").each(function() { // this function makes the color of the td's
		if (row == 0) {
			$(this).find("td").addClass("second");
			row = 1;
		}
		else {
			row = 0;
		}
	});
	
	$("#telepitoknek input[type=text]").addClass("mytext");
	$("#telepitoknek input[type=password]").addClass("mytext");
	
	$("#telepitoknek input[type=checkbox]").click(function() {
		if ($(this).is(':checked')) {
			$(this).parent().next().find("input[type=text]").each(function() {
				var name = $(this).attr("name");
				var name = name.substr(2);
				var content = $("input[name=" + name + "]").val();
				$(this).val(content);
			});
		}
		else {
			$(this).parent().next().find("input[type=text]").val("");
		}
	});
	
	$("#telepitoknek input.required").parent().prev("td").append("<span style='color: red'>*</span>");
	
	$("#telepitoknek td").hover(function() {
		$(this).parent().find("td").addClass("silver");
	}, function() {
		$(this).parent().find("td").removeClass("silver");
	}
	);
	
	$("#telepitoknek input[name=user]").blur(function() {
		$.ajax({
   		type: "POST",
   		url: "inc/checkuser_reg.php",
   		data: "user=" + $("input[name=user]").val(),
   		success: function(msg){
    			if (msg == "ok") {
    				user_error = 0;
    			}
    			else {
    				user_error = 1;
    			}
    	}
    			
		});
	
	});
	
	$("#main_image img").each(function() {
		var id = $(this).attr("rel");
		//alert(id);
	});
	

});
function check_reg_form() {
	var error = 0;
	$("#telepitoknek .required").each(function() {
		if ( $(this).val()=="" ) error = 1;
	});
	
	if (error == 1) { 
		alert("A csillaggal jelölt mezőket kötelező kitölteni!");
		return false;
	}
	
	else if (user_error == 1) { 
		alert("A felhasználónév már használatban van!");
		return false;
	}
	return true;
	

}