function isNumber(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}	


$(document).ready(function() {
	
	$("#loginbtn").click(function() {
		var form = $(this).parents("form:first");		
		form.submit();
	});
	
	$("#subscribebtn").click(function() {
		
		var club_id = $("#club_id").val();
		var btn = $(this);
		
		if($(this).hasClass("doreg")) {
			window.location = "/register?subscribe";
		} else {		
			
			$.ajax({
				  url: "/clubs/subscribe/",		  
				  type: "POST",
			      data: ({club_id : club_id}),
			      dataType: "html",
				  success: function(msg){ 
					
					msg = $.trim(msg); //remove any spaces 
					
					if(parseInt(msg) == 1) {
						$("#subscribecontainer").html("<span class='nowsubscribed'>You are now subscribed to updates from this club.</span><br /><br />");
					} else {
						alert("Unable to subscribe to club updates, please try again later");
					}
					
				    
				  }
				});
		}
		
		return false;
	});
	
	$("#search-btn").click(function() {
		$(this).submit();
	});
	
	$(".stdbutton").live("click", function() {
		
		if($(this).attr("rel") != "") {
			
			var txt = $(this).attr("rel");
			
			$(this).find("span").html(txt);
		}
		
		if(!$(this).hasClass('stdlink')) {
			var form = $(this).parents("form:first");		
			form.submit();
			return false;  //stop the view from jumping to the top
		}		

	});

	
	/**
	 * 	Site search
	 */
	$("#searchbox").click(function() {
		if($(this).val() == "Search fishr.net") {
			$(this).val("");
		}
		
		$("#whereissearch").fadeOut("slow");
	});
	
	$("#whereissearchclose").click(function() {
		
		$("#whereissearch").fadeOut("slow");
		
	});	
	
	$("#searchbox").autocomplete("/clubs/autocompleteall",
			{ 
			minChars: 1,
			cacheLength: 0,
			onItemSelect: selectItem,
			onFindValue: findValue,
			formatItem: formatItem,
			autoFill: false
		});
	
	$("#usersearchbox").autocomplete("/users/autocomplete",
			{ 
			minChars: 1,
			cacheLength: 0,
			onItemSelect: selectItem,
			onFindValue: findValue,
			formatItem: formatItem,
			autoFill: false
		});	
	
	$("#clubsearchbox").autocomplete("/clubs/autocomplete",
			{ 
			minChars: 1,
			cacheLength: 0,
			onItemSelect: selectItem,
			onFindValue: findValue,
			formatItem: formatItem,
			autoFill: false
		});		
	
	
	function selectItem(li) {
		
		console.log($(li).find("a").attr("href"));
		
		window.location = $(li).find("a").attr("href");

	}
	
	function findValue(li) {		
		if( li == null ) return alert("No match!");

		// if coming from an AJAX call, let's use the product id as the value
		if( !!li.extra ) var sValue = li.extra[0];

		// otherwise, let's just display the value in the text box
		else var sValue = li.selectValue;
	
		//alert("The value you selected was: " + sValue);
	}
	
	function formatItem(row) {
		
		if(row[1] == undefined) {
			return row[0];
		}
		else {
			if(row[1].indexOf("#CLUB#") === 0) {			
				return "<a href='http://localhost" + row[3] + "' style='background:#ecf3c0;display:block;margin:2px;font-size:9px;'>" + row[0] + "</a>";
			} else {
				return "<a href='http://localhost" + row[3] + "' style='background:#b6d6e7;display:block;margin:2px;font-size:9px;'>" + row[0] + "</a>";
			}
			
			
		}
	}
	
	

	
	
	$(".usesearch").click(function() {
		
		$("#whereissearch").fadeIn("fast");
		
		$('html, body').animate({ scrollTop: 0 }, 'slow');
		
		return false;
		
	});
	
	

	$("#loginbtn").click(function () {

	      $("#loginpopup").fadeIn('slow', function() {
	          // Animation complete
	      });
	});	

	$("#header").click(function (e) {
	    var x = parseInt(e.pageX - this.offsetLeft);
	  	var y = parseInt(e.pageY - this.offsetTop);

	  	//alert("x " +  x +  " y " +  y);

	  	if(x < 475 || x > 750) {
	  		//alert("header : x " +  x +  " y " +  y);
			$("#loginpopup").fadeOut('fast', function() {
		          // Animation complete
		    });
		}
	});				
	
	$("#container-inner").click(function (e) {

	      var x = parseInt(e.pageX - this.offsetLeft);
	  	  var y = parseInt(e.pageY - this.offsetTop);

	  		
	  	  if(x < 460 || y > 98) {   

			$("#loginpopup").fadeOut('fast', function() {
		          // Animation complete
		    });
	  	  }
	});
	
	$("#venue-feature").textup(100);
	$("#venue-feature").textup(100);
	$("#venue-feature").textup(100);
	
	//$("#user-feature").textup(100);
	
	/*
	$("#venue-feature").hover( function() {		
		  $("#venue-feature-details").animate({
			    height: "170px",			    			    
			  }, 300, function() {
				    // Animation complete.
				  $("#venue-feature-summary").fadeIn(100);
			  }
		  	);		  		
	 },	function() {		
		 
		 //$("#venue-feature-summary").fadeOut(100);
		 
		  $("#venue-feature-summary").hide(); 
		 
		  $("#venue-feature-details").animate({
			    height: "60px",			    			    
			  }, 300, function() {
				    // Animation complete.			
				  $("#venue-feature-summary").hide();
			  }
		  	);		
	 }	
	);
	*/
	
	//$("#container-inner").hover(function (e) {
	
	$(".cms-control").live("click", function() {
		//alert("control click");		
		
		var href = $(this).attr("href");
		var hrefonly = href.substring(1, href.length)
		
		console.log(hrefonly);
		
		$(".pane").hide();
		$("#pane-" + hrefonly).show();
	});
	
	
	
	

	
	$("#btn-cms-news-add").live("click", function() {
		
		var form = $(this).parents("form:first");
		
		console.log($(form));
		console.log($(form).serialize());
		
		return false;
		
		//alert(form.serialize());
		
		/*
		$.ajax({
			  url: "/clubs/ajax_cms_news_add/",	  
			  type: "POST",
		      //data: ({id : storyid}),
			  data: .serialize()
		      dataType: "html",
			  success: function(msg){			    
			    $("#cms-news-editor-inner").html(msg)
			  }
			});
		*/
		
	});
	
	
	$("#cms-news-add").click(function() {
		
		$.ajax({
			  url: "/cms/news-edit/",			  
			  type: "POST",
		      data: ({id : objectid}),
		      dataType: "html",
			  success: function(msg){ 
			    $("#cms-bar-editor-contents").html(msg);
			    loader.hide();
			  }
			});
		
	});
	
	
	
	
	$("#cms-news-cancel").live("click", function() {		
		//$("#cms-news-editor").hide();
		//$("#cms-news-editor").remove();
		
	});	
	
	$("#pane-manage .action-edit a").live("click", function() {
		
		var newsid = $(this).attr("href");
		
		$.ajax({
			  url: "/cms/news_edit/",			  
			  type: "POST",
		      data: ({news_id : newsid}),
		      dataType: "html",
			  success: function(msg){
				$(".pane").hide();				
			    $("#pane-other").html(msg);
			    $("#pane-other").show();
			  }
			});		
		
		
		return false;
	
	});
	
	$("#NewsNewsEditForm a.button").live("click", function() {
		var form = $("#NewsNewsEditForm");
		var formdata = $("#NewsNewsEditForm").serialize();		
		var pane = $("#cms-news");
		
		$.ajax({
			  url: "/news/edit/",			  
			  type: "POST",
		      data: formdata,
		      dataType: "html",
			  success: function(msg){			    
				
				msg = $.trim(msg); //remove any spaces 
				
				$(".cms-msg").remove(); //Remove any existing messages
				
				if(parseInt(msg) == 1) {
					
					var successmsg = "The news story has been added successfully";
					
					$(pane).prepend("<div class='cms-msg-success cms-msg'>" + successmsg + "</div>");
					
					//$(form).prepend("<div class='cms-msg-success'>" + successmsg + "</div>");
				} else {
					
					var failmsg = "Unable to add news story, please try again later";
					
					$(pane).prepend("<div class='cms-msg-failure cms-msg'>" + failmsg + "</div>");					
					
				}
				
				$(".pane").hide();							    
			    $("#pane-manage").show();
				
			  }
		});		
	});
	

	$(".thread-reply").click("live", function() {
		
		$("#comment-li").remove();
		
		var commentdiv = $(this).parents(".comment").filter(":first");
		
		var id = $(this).parents(".comment").filter(":first").attr("id");
	
		var parts = id.split("-");
		
		var comment_id = parts[1];
		
		//var comment-box
		$("#CommentParentId").val(comment_id);
		
		$(commentdiv).after("<li id='comment-li'></li>");
		$("#comment-li").append($("#comment-box"));
		
		/*var comment_id = $(this).parents(".comment :first").attr("id");
		
		alert("comment id " + comment_id);
		
		$("#CommentParentId").val($(this).val());
		*/
		return false;
		
	});
	
	
	
	$(".searchresultitem").live("click", function() {
		
		var uid = $(this).find(".uid").filter(":first").val();
		
		window.location = uid;
		
		return false;
	})
	
	
	$(".shinybtn").live("click", function() {		
		$(this).val("Please wait...");		
	})
	
	$(".call-to-action").click(function() {
		
		var url = $(this).attr("rel");
		
		if(url != "") {
			window.location = url;
		}
	});
	

	
	$(".galleryphoto").hover( function() {
		$(this).find(".del").css("display", "block");
	 },	function() {		
		$(this).find(".del").hide();		 
	 }	
    );
	
	$(".del").click(function() {
		
		var answer = confirm("Are you sure you want to delete this image?")
		var control = $(this);
		var container = $(this).parents(".galleryphoto");
		var id = $(this).attr("rel");
		
		if (answer){

			$.ajax({
				  url: "/users/photodelete/",			  
				  type: "POST",
			      data: ({photo_id : id}),
			      dataType: "html",
				  success: function(msg){
					
					  if(parseInt(msg) == 1) {
						  window.location.reload();
					  } else {
						  alert("Failed to delete photo, please try again later");
					  }
					  
				  },
				  error: function(jqXHR, textStatus, errorThrown) {
					  alert("err " + errorThrown + " " + textStatus	);
				  }
				  
				  
				});				
		} 
		
		return false;
		
		
	});
	
	
});
