/* created by jdg */
/******************/

(function($) {

	$('a.anchor').click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
		&& location.hostname == this.hostname) {
			var $target = $(this.hash);
			$target = $target.length && $target	|| $('[name=' + this.hash.slice(1) +']');
			if ($target.length) {
				var targetOffset = $target.offset().top;
				$('html,body').animate({scrollTop: targetOffset}, 300);
				return false;
			}
		}
	});
	
	$("#loginform-tml-page").validate({
		rules: {
			log: { required:true },
			pwd: { required:true }
		}
	});
	
	$("#registerform-tml-page").validate({
		rules: {
			user_login: { required:true },
			user_email: { email:true, required:true },
			legalnotice: { required:true }
	
		}
	});

	$("#tenderform").validate({
		rules: {
			comment_price: { number:true },
			comment: { required:true }
		}
	});
	
	/* New tender form */
	$("#newtender form").validate({
		rules: {
			newtender_title: { required:true },
			newtender_thetags: { required:true },
			newtender_content: { required:true },
			newtender_title: { required:true }
		}
	});
	
	$("#newtender-title, #searchform-keyword").focus(function() {
		$(this).val("");
	});
	
	$("#newtender_nextstep").live("click", function() {
		var ss = $("#newtender_title").val();
		getrelated(ss);
	});
	if(undefined===window.permtitle) permtitle = false;
	if(permtitle) {
		var ss = $("#newtender_title").val();
		getrelated(ss);
	}
	
	$("#newtender_nextstep_form").live("click", function() {
		$("#related").hide();
		$("#moreoptions").fadeIn("fast");
	});
	if($("#csatlakozz").length) {
		$('#csatlakozz').live('mouseover', function() {
			$('#csatlakozz').bubbletip('#csatlakozz-tip').trigger('mouseover');
		});
		
		$('#addfollower').live('click', function() {
			var post_id = $(this).attr("class");
			$('#csatlakozz').removeBubbletip();
			$('#csatlakozz-tip').remove();
			$.post("/content/themes/yabe/single-tendermenu.php?mode=add&post_id="+post_id, function(data) {
				$("#singlemenu").html(data);
				$.post("/content/themes/yabe/single-followerlist.php?post_id="+post_id, function(data) {
					$(".followers").html(data);
				});
			});
			return false;
		});		
		$("#removefollower").live('click', function() {
			var post_id = $(this).attr("class");
			$('#csatlakozz').removeBubbletip();
			$('#csatlakozz-tip').remove();
			$.post("/content/themes/yabe/single-tendermenu.php?mode=remove&post_id="+post_id, function(data) {
				$("#singlemenu").html(data);
				$.post("/content/themes/yabe/single-followerlist.php?post_id="+post_id, function(data) {
					$(".followers").html(data);
				});
			});
			return false;
		});
		$("#csatlakozz").bubbletip($("#csatlakozz-tip"));		
	}; // #csatlakozz

	if($("#followterm").length) {
		$('#followterm').live('mouseover', function() {
			$('#followterm').bubbletip('#followterm-tip').trigger('mouseover');
		});
		
		$('#addfollower').live('click', function() {
			var term_id = $(this).attr("class");
			$('#followterm').removeBubbletip();
			$('#followterm-tip').remove();
			$.post("/content/themes/yabe/tag-follow.php?mode=add&term_id="+term_id, function(data) {
				$("#followterm").html(data);
			});
			return false;
		});		
		$("#removefollower").live('click', function() {
			var term_id = $(this).attr("class");
			$('#followterm').removeBubbletip();
			$('#followterm-tip').remove();
			$.post("/content/themes/yabe/tag-follow.php?mode=remove&term_id="+term_id, function(data) {
				$("#followterm").html(data);
			});
			return false;
		});
		$("#followterm").bubbletip($("#followterm-tip"));		
	}; // #followterm

	if($("#isvalidphone").length) {
		$("#isvalidphone").bubbletip($("#isvalidphone-tip"));
	}; // #isvalidphone

	if($("#phone").length) {
		$("#phone").bubbletip($("#phone-tip"));
		$("#phone").bind("click", function() {
			$(this).removeBubbletip();
			$("phone-tip").remove();
			$("#phone").remove();
			$("#number").html("<img src='/wp-admin/images/loading.gif' alt=''/>");
			$.post($(this).attr("href"), function(data) {
				$("#number").html(data);
			});
			return false;
		});
	}; // #phone

	// Profil oldal tender follow remove
	$('#followedtenders .tender, #followedterms .tag').live('mouseover', function() {
		$(this).children("span.followdelete").show();
	});
	$('#followedtenders .tender, #followedterms .tag').live('mouseout', function() {
		$(this).children("span.followdelete").hide();
	});
	$("#followedtenders .tender span.followdelete a").live('click', function() {
		if(confirm("Biztos le akarsz iratkozni erről a tenderről?")) {
			var post_id = $(this).attr("class");
			$("#followedtenders h2:first-child").append("<span id=\"loading\"><img src='/wp-admin/images/loading.gif' alt=''/></span>");
			$.post("/content/themes/yabe/profile-followedtenders.php?mode=remove&post_id="+post_id, function(data) {
				$("#followedtenders").html(data);
				$("#followedtenders h2 #loading").remove();
			});
		}
		return false;
	});

	$("#followedterms .tag span.followdelete a").live('click', function() {
		if(confirm("Biztos le akarsz iratkozni erről a tag-ről?")) {
			var term_id = $(this).attr("class");
			$("#followedterms h2:first-child").append("<span id=\"loading\"><img src='/wp-admin/images/loading.gif' alt=''/></span>");
			$.post("/content/themes/yabe/profile-followedterms.php?mode=remove&term_id="+term_id, function(data) {
				$("#followedterms").html(data);
				$("#followedterms h2 #loading").remove();
			});
		}
		return false;
	});
	
	// Tags
	$("#tag-filter").val() == "";
	
	var delay = (function() {
	  var timer = 0;
	  return function(callback, ms) {
	    clearTimeout(timer);
	    timer = setTimeout(callback, ms);
	  };
	})();
	
	$("#tag-filter").keyup(function() {
		delay(function() {
			var search = $("#tag-filter").val(), count = 0;
			if(search.length > 0) {
				$("#taglist").show();
				$("#taglist-full").hide();
				$.ajax({ 
					type: "POST", 
					url: "/content/themes/yabe/tagsearch.php", 
					data: "search="+search,
					beforeSend: function() { 
						$('#taglist').html('<img src="/wp-admin/images/loading.gif" alt="" />'); 
					},
					success: function(tags) { 
						$("#taglist").empty().show();
						if(tags.length > 0) { 
							$("#taglist").append(tags); 
						} 
					} 
				});
			} else {
				$("#taglist-full").show();
				$("#taglist").hide();
			}
		}, 500 );
	});
	// /Tags
	
	// External links
	$("a[rel='external'], li.thumbnail a").click(function() {
		window.open($(this).attr("href"));
		return false;
	});
	// /External links
	
	function getrelated(ss) {
		if(ss) {
			$("#nextstep").html("<img src='/wp-admin/images/loading.gif' alt='' style='float:right;'/>");
			$.post("/content/themes/yabe/newtender-related.php", { ss: ss }, function(data) {
				$("#nextstep").hide().remove();
				$("#related").html(data).fadeIn("fast");
			});
			return false;
		}
	}
	
})(jQuery);
