/***********************************************************
NOTES:
	MyMenu Application created by Blake Kidney for Western Seminary.
	
	This application is used in conjunction with the MyMenuEditor Application.
	
	The application requires the following:
		- JQuery 1.4.4 or greater	(http://jquery.com/)
		- JQuery Cookie Plugin  	(http://plugins.jquery.com/project/Cookie)
			
	The application uses the following files:
		- mymenu-app.js
		- mymenu.css

***********************************************************/

//create a new my menu object
var mm = new MyMenu({
	key: "custommenu",				//name of the cookie	
	myMenuListId: "mymenu",		//id of the menu button
	addBtnId: "addBtn",				//id of the add button
	editBtnId: "editBtn",				//id of the edit button
	editPageUrl: "http://www.westernseminary.edu/scripts/mymenu/mymenuedit.htm" //absolute url of the page that has the editor
});
mm.create();
//mm.clear();

/****************************************
MyMenu Class
****************************************/
function MyMenu(config) {
	
	/****************************************
	Private Variables
	****************************************/				
	var key = config.key;
	var myMenuListId = config.myMenuListId;
	var addBtnId = config.addBtnId;
	var editBtnId = config.editBtnId;
	var editPageUrl = config.editPageUrl;
	var pageUrl = encodeURI(document.location.href);
	
	/****************************************
	Public Shared Variables
	****************************************/	
	this.key = key;
	this.setCookie = setCookie;
	this.getCookie = getCookie;
	this.clearCookie = clearCookie;
	
	/****************************************
	Creates the menu on a btn
	****************************************/
	this.create = function() {		
		
		//initialize once the document is ready
		$(document).ready(function() {
			
			//pull the cookie data into a variable
			var data = getCookie(key);
			if(!data) data = "";	
	
			//add the menu to the page
			$('#'+myMenuListId).html(makeMenuHtml(data));	
			
			//setup actions for an addBtn click
			$("#"+addBtnId).click(function() {
				//check to see if cookies are enabled
				if(!navigator.cookieEnabled) {
					alert("You cannot add a page because your browser does not accept cookies."
						  + "\nEnable cookies in your browser if you would like to use this custom menu.");
				} else {										   
					//prompt for user to give the page a name
					var title=prompt("Please enter a title for this menu button.", document.title);
					if (title!=null && title!="") {
						//remove click handler
						$(this).unbind("click");
						//remove id
						$(this).removeAttr("id");
						//change href to new page
						$(this).attr("href", pageUrl);
						//change text to new title
						$(this).text(title);
						//save the new html to the cookie without the edit button
						var newhtml = data + "<li><a href='"+pageUrl+"'>"+title+"</a></li>";
						setCookie(key,newhtml);
					}
				}
				return false;
			});	
		});			
	}
	/****************************************
	Creates the menu html from the cookie data
	****************************************/
	function makeMenuHtml(data) {
		//create the menu html where the links are each listed: <li><a>Link</a></li>
		var menuhtml = data;
		//if this page has not already been added & this is not the edit page, then create addbtn
		if(data.indexOf(pageUrl) < 0 && editPageUrl != pageUrl) {
			menuhtml += "<li><a href='#' id='"+addBtnId+"'>Add This Page</a></li>";
		}
		//create editbtn
		menuhtml += "<li><a href='"+editPageUrl+"' id='"+editBtnId+"'>Edit This Menu</a></li>";	
		return menuhtml;
	}	
	/****************************************
	Sets the Cookie
	****************************************/
	function setCookie(name, value) {
		var expdate = new Date(2100, 0, 1);
		var str = name + "=" + encodeURIComponent(value);
		str += ";path=/;domain=westernseminary.edu";
		str += ";expires="+expdate.toUTCString();
		document.cookie = str;
	}
	/****************************************
	Gets the Cookie
	****************************************/
	function getCookie(name) {
		var results = document.cookie.match( '(^|;) ?' + name + '=([^;]*)(;|$)' );
		if(results) {
			return ( decodeURIComponent(results[2]) );
		} else {
			return null;
		}
	}
	/****************************************
	Clears the Cookie
	****************************************/
	function clearCookie(name) {
		var expdate = new Date(2000, 0, 1);
		var str = name + "=";
		str += ";path=/;domain=westernseminary.edu";
		str += ";expires="+expdate.toUTCString();
		document.cookie = str;
		alert("Cookie Cleared!\n"+document.cookie);
	}
}


