//loadManageContact();
var pagemodified = false;

$(".editlink").live("click" ,function() {		
		var rel = $(this).attr("rel");
		$("#cms-pane").html("");		
		$("#cmsoverlay").fadeIn();		
		
		var club_id = $("#club_id").val();
		
		if(club_id == "") {
			alert("An error occured, please try again later");
		}
		
		switch(rel) {
		
			case "clubinfo":
				loadManageContact(club_id);
				break;
				
			case "clubnews":
				loadManageNews(club_id);
				break;
				
			case "clubwaters":
				loadManageWaters(club_id);
				break;

			case "clubrecords":				
				loadManageRecords(club_id);
				break;			
					
			case "clubphoto":				
				loadManagePhoto(club_id);
				break;	
				
			case "clubprices":				
				loadManagePrices(club_id);
				break;					
				
			default :
				break;
				
		}
		
		
		return false;
	});

$("#cmsloadaddnews").live("click", function() {		
	closeOverlay();		
	return false;
});

function closeOverlay() {		
	$("#cmsoverlay").fadeOut();
	$("#cms-pane").html("");		
};

$("#cmsclose").live("click", function() {		
	
	if(pagemodified == true) {
		window.location.reload();	
	} else {
		closeOverlay();
	}
	
	return false;
});

/******************************************************
 * 	CMS Button action handlers
*******************************************************/
	$("#cms-menu-managenews").live("click", function() {
		loadManageNews();	
		return false;
	});
	
	$("#cms-menu-addnews").live("click", function() {
		var id = $("#club_id").val();
		//alert(id);
		loadAddNews(id);
		return false;
	});	
	
	$("#cms-menu-addrecord").live("click", function() {
		var id = $("#club_id").val();
		loadAddRecord(id);		
		return false;
	});
	
	$("#cms-menu-addprice").live("click", function() {
		loadAddPrice();
		return false;
	});			
	
	
	$("#cms-price-add").live("click", function() {

		var form = $(this).parents("form").filter(":first");		
		var formdata = $(form).serialize();
		var message = "";
		var clubid = $("#PriceClubId").val();
		
		if($("#PriceName").val() == "") {
			alert("Please enter a name (e.g. Adult, Junior, OAP)");
		}
	
		if($("#PricePrice").val() == "" || !isNumber($("#PricePrice").val())) {
			alert("Please enter a valid price. (e.g 10.00)");
		}
		
		
		$.ajax({
			  url: "/cms/prices_add/",			  
			  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) {					
					//window.location.reload();
				} else {
					alert("Unable to save the news story, please try again later");
				}
				
				pagemodified = true;
				
				loadManagePrices(clubid)
				
			  }
			  
		});
		
		return false;
	});	
	
	$(".cms-price-edit").live("click", function() {
		var rel = $(this).attr("rel");
		loadEditPrice(rel);
	});

	$(".cms-price-delete").live("click", function() {
		var rel = $(this).attr("rel");		
		deletePrice(rel);
	});
	
	$(".cms-record-delete").live("click", function() {
		var rel = $(this).attr("rel");
		deleteRecord(rel);
	});	
	
	$(".cms-news-delete").live("click", function() {
		var rel = $(this).attr("rel");		
		deleteNews(rel);		
	});
	
	$("#cms-menu-managewaters").live("click", function() {
		loadManageWaters();
		return false;
	});	

	$("#cms-menu-managecontact").live("click", function() {
		loadManageContact();
		return false;
		//loadManageWaters();	
	});	
	
	$(".cms-record-doedit").live("click", function() {
		var rel = $(this).attr("rel");
		loadEditRecord(rel);
	});
	
	
	$(".cms-news-doedit").live("click", function() {		
		var rel = $(this).attr("rel");
		loadEditNews(rel);
	});
		
	$("#cms-news-edit").live("click", function() {
	
		var form = $(this).parents("form").filter(":first");		
		var formdata = $(form).serialize();
		var message = "";
		
		$.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) {					
					window.location.reload();					
				} else {					
					alert("Unable to save, please try again later");					
				}
			
			  }, complete : function() {
				 				  
				  loadManageNews();
			  }
			  
			  
		});
		
		return false;
	});

	$("#cms-photo-add").live("click", function() {
		
		/*
		var form = $(this).parents("form").filter(":first");
		var formdata = $(form).serialize();
		
		var message = "";
		
		$.ajax({
			  url: "/cms/photo_manage/",			  
			  type: "POST",
		      data: formdata,
		      dataType: "html",
			  success: function(msg){	
				  
				alert(msg);
			      
				msg = $.trim(msg); //remove any spaces 
				
				$(".cms-msg").remove(); //Remove any existing messages
				
				if(parseInt(msg) == 1) {					
					window.location.reload();
				} else {
					alert("Unable to save the photo, please try again later");
				}
				
			  }
			  
		});
		
		return false;*/
	});	
	
	
	$("#cms-record-add").live("click", function() {
		
		var form = $(this).parents("form").filter(":first");		
		var formdata = $(form).serialize();
		var message = "";
		
		$.ajax({
			  url: "/cms/record_add/",			  
			  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) {					
					window.location.reload();
				} else {
					alert("Unable to save the news story, please try again later");
				}
				
			  }
			  
		});
		
		return false;
	});	
	
	$("#cms-news-add").live("click", function() {
		
		var form = $(this).parents("form").filter(":first");		
		var formdata = $(form).serialize();
		var message = "";
		
		$.ajax({
			  //url: "/news/add/",
			  url: "/cms/news_add/",				
			  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) {					
					window.location.reload();
					
					//var successmsg = "The news story has been added successfully";
					//message = "<div class='cms-msg-success cms-msg'>" + successmsg + "</div>";
					//$_SESSION['message'] = message;
					//$(form).prepend("<div class='cms-msg-success'>" + successmsg + "</div>");
				} else {
					alert("Unable to save the news story, please try again later");
				}
				
			  }
			  
		});
		
		return false;
	});
	
	
	$("#cms-contact-save").live("click", function() {
		
		var form = $(this).parents("form").filter(":first");		
		var formdata = $(form).serialize();
		var message = "";
		
		$.ajax({
			  url: "/cms/contact_edit/",			  
			  type: "POST",
		      data: formdata,
		      dataType: "html",
			  success: function(msg){
				  
				msg = $.trim(msg); //remove any spaces 
				
				if(parseInt(msg) == 1) {
					
					window.location.reload();
				
				} else {
					
					alert("Unable to save club details, please try again later");
									
				}
				
			  }, complete : function() {
				  
				  //loadManageContact();
			  }
			  
			  
		});
		
		return false;
	});
	
	$("#cms-bar-display a").live("click", function() {
		$("#cms-bar-editor").hide();
	});
	
	/**
	 * 	Click event on the top menu bar
	 */
	$("#cms-bar .cms-bar-options li a").live("click", function() {
		
		var editor = $("#cms-bar-editor");
		//var loader = $("#cms-loader");
		var objecttype = editor.find("#objecttype").val();
		var objectid = editor.find("#objectid").val();
		
		var rel = $(this).attr("rel");
		
		editor.slideDown(200);
		showLoader();
		
		
		
		if(!(objecttype == undefined || objectid == undefined || objecttype == "" || objectid == "" )) {
			
			$.ajax({
				  url: "/cms/section/",		  
				  type: "POST",
			      data: ({section : rel, id : objectid, type : objecttype}),
			      dataType: "html",
				  success: function(msg){
				    $("#cms-bar-editor-contents").html(msg);
				    hideLoader();
				    $(".cms-bar-option").removeClass("selected");
				    $("#cms-bar-" + rel).addClass("selected");
				  }
				});
			
			
		} else {
			//alert("..");
		}	
		
	});
	
	
	function loadManagePrices(clubid) {
		
		showLoader();
		
		$.ajax({
			  url: "/cms/prices_manage/",		  
			  type: "POST",
		      data: ({club_id : clubid}),
		      dataType: "html",
			  success: function(msg){					
			    $("#cms-pane").html(msg);
			    $("#cms-pane").show();
			  },
			  complete: function(msg){				
				  hideLoader();
			  }			  
			});
				
	}		
	
	function loadManagePhoto(clubid) {
		
		showLoader();
		
		$.ajax({
			  url: "/cms/photo_manage/",		  
			  type: "POST",
		      data: ({club_id : clubid}),
		      dataType: "html",
			  success: function(msg){					
			    $("#cms-pane").html(msg);
			    $("#cms-pane").show();
			  },
			  complete: function(msg){				
				  hideLoader();
			  }			  
			});			
		
	}		
	
	function loadAddPrice(clubid) {
		
		var clubid = $("#club_id").val();
		
		$.ajax({	
			  url: "/cms/prices_add/",		  
			  type: "POST",
		      data: ({club_id : clubid}),
		      dataType: "html",
			  success: function(msg){					
			    $("#cms-pane").html(msg);
			    $("#cms-pane").show();
			  },
			  complete: function(msg){				
				  hideLoader();
			  }			  
			});				
		
	}
	
	
	
	function loadManagePhoto(clubid) {
		
		showLoader();
		
		$.ajax({
			  url: "/cms/photo_manage/",		  
			  type: "POST",
		      data: ({club_id : clubid}),
		      dataType: "html",
			  success: function(msg){					
			    $("#cms-pane").html(msg);
			    $("#cms-pane").show();
			  },
			  complete: function(msg){				
				  hideLoader();
			  }			  
			});			
	}		
	
	function loadManageContact(clubid) {
		
		//var clubid = 1310;
		
		showLoader();
		
		$.ajax({
			  url: "/cms/contact_manage/",		  
			  type: "POST",
		      data: ({club_id : clubid}),
		      dataType: "html",
			  success: function(msg){				
			    $("#cms-pane").html(msg);
			    $("#cms-pane").show();
			  },
			  complete: function(msg){				
				  hideLoader();
			  }			  
			});			
	}	
	

	function loadManageWaters(clubid) {
		
		//var clubid = 1310;
		
		showLoader();
		
		$.ajax({
			  url: "/cms/waters_manage/",		  
			  type: "POST",
		      data: ({club_id : clubid}),
		      dataType: "html",
			  success: function(msg){				
			    $("#cms-pane").html(msg);
			    $("#cms-pane").show();
			  },
			  complete: function(msg){				
				  hideLoader();
			  }			  
			});			
	}	
	
	function loadEditRecord(id) {
		
		showLoader();
		
		$.ajax({
			  url: "/cms/record_edit/",		  
			  type: "POST",
		      data: ({record_id : id}),
		      dataType: "html",
			  success: function(msg){					  
			    $("#cms-pane").html(msg);
			    $("#cms-pane").show();
			  },
			  complete: function(msg){				
				  hideLoader();
			  }			  
			});			
	}
	
	function loadManageRecords(clubid) {
		
		//var clubid = 1310;
		
		showLoader();
		
		$.ajax({
			  url: "/cms/record_manage/",		  
			  type: "POST",
		      data: ({club_id : clubid}),
		      dataType: "html",
			  success: function(msg){	
			    $("#cms-pane").html(msg);
			    $("#cms-pane").show();
			  },
			  complete: function(msg){				
				  hideLoader();
			  }			  
			});			
	}		
	
	function loadAddNews(clubid) {
		
		//var clubid = 1310;		
		//alert(clubid);
		
		showLoader();
		
		$.ajax({
			  url: "/cms/news_add/",		  
			  type: "POST",
		      data: ({club_id : clubid}),
		      dataType: "html",
			  success: function(msg){					  
			    $("#cms-pane").html(msg);
			    $("#cms-pane").show();
			  },
			  complete: function(msg){				
				  hideLoader();
			  }			  
			});			
	}	
	
	function loadAddRecord(clubid) {
		
		showLoader();
		
		$.ajax({
			  url: "/cms/record_add/",		  
			  type: "POST",
		      data: ({club_id : clubid}),
		      dataType: "html",
			  success: function(msg){
			    $("#cms-pane").html(msg);
			    $("#cms-pane").show();
			  },
			  complete: function(msg){				
				  hideLoader();
			  }			  
			});			
	}		
	
	
	function deleteNews(newsid) {
		
		showLoader();
		
		$.ajax({
			  url: "/cms/news_delete/",		  
			  type: "POST",
		      data: ({news_id : newsid}),
		      dataType: "html",
			  success: function(msg){	
				  
				msg = $.trim(msg); //remove any spaces 
				
				if(parseInt(msg) == 1) {
					$(".row-" + newsid).slideUp();
					pagemodified = true;
				} else {
					alert("Unable to delete this record at this time, please try again later");
				}
				
			  },
			  complete: function(msg){				
				  hideLoader();
			  }			  
			});			
	}	
	
	function deletePrice(priceid) {
		
		showLoader();
		
		$.ajax({
			  url: "/cms/prices_delete/",		  
			  type: "POST",
		      data: ({price_id : priceid}),
		      dataType: "html",
			  success: function(msg){	
				  
				msg = $.trim(msg); //remove any spaces 
				
				if(parseInt(msg) == 1) {
					$("#row-" + priceid).slideUp();
					pagemodified = true;
				} else {
					alert("Unable to delete this record at this time, please try again later");
				}
				
			  },
			  complete: function(msg){				
				  hideLoader();
			  }			  
			});			
	}	
	
	
	function deleteRecord(id) {
		
		showLoader();
		
		$.ajax({
			  url: "/cms/record_delete/",		  
			  type: "POST",
		      data: ({record_id : id}),
		      dataType: "html",
			  success: function(msg){	
				  
				msg = $.trim(msg); //remove any spaces 
				
				if(parseInt(msg) == 1) {
					$("#row-" + id).slideUp();
					pagemodified = true;
				} else {
					alert("Unable to delete this record at this time, please try again later");
				}
				
			  },
			  complete: function(msg){				
				  hideLoader();
			  }			  
			});			
	}		
	
	function loadEditPrice(priceid) {
		
		showLoader();
		
		$.ajax({
			  url: "/cms/prices_edit/",		  
			  type: "POST",
		      data: ({price_id : priceid}),
		      dataType: "html",
			  success: function(msg){				
			    $("#cms-pane").html(msg);
			    $("#cms-pane").show();
			  },
			  complete: function(msg){				
				  hideLoader();
			  }			  
			});			
	}
	
	function loadEditNews(newsid) {
		
		showLoader();
		
		$.ajax({
			  url: "/cms/news_edit/",		  
			  type: "POST",
		      data: ({news_id : newsid}),
		      dataType: "html",
			  success: function(msg){				
			    $("#cms-pane").html(msg);
			    $("#cms-pane").show();
			  },
			  complete: function(msg){				
				  hideLoader();
			  }			  
			});			
	}	
	
	function loadManageNews(clubid) {
		
		//var clubid = 1310;
		
		showLoader();
		
		$.ajax({
			  url: "/cms/news_manage/",			  
			  type: "POST",
		      data: ({club_id : clubid}),
		      dataType: "html",
			  success: function(msg){
			    $("#cms-pane").html(msg);
			    $("#cms-pane").show();
			  },
			  complete: function(msg){				
				  hideLoader();
			  }
			});	
		
	}
	
	function showLoader() {						
		//disabled for the moment
		//$("#cms-loader").show();
	}
	
	function hideLoader() {
		$("#cms-loader").hide();
	}	




