
/* QuickLinks Persistance and Rendering Classes */


// QuickLinksPersistance
// -----------------------------------------------
// Provides persistance implementation for Quicklinks
// Classes

var QuickLinksPersistance = Class.create({

	// Constructor
	initialize: function(cookieDomain) 
	{
		this.domain = cookieDomain;
		this.jar = new CookieJar({  
			expires:630720000,   // seconds   
			domain: this.domain,
			path: '/'  
		});  
	this.quickLinkData = null;  
	},
	
	// Load Quicklinks data from cookie
	getQuickLinkData: function()
	{
		if (!this.quickLinkData){ this.quickLinkData = this.jar.get('quickLinkData');}
		if (!this.quickLinkData){this.quickLinkData = {links:[]};}
		this.quickLinkData.links.sort( function(a,b) { return a.name.toUpperCase() > b.name.toUpperCase(); });
		return this.quickLinkData;
	},
	
	// Save quicklinks data from cookie
	saveQuickLinkData: function()
	{
		return this.jar.put('quickLinkData', this.quickLinkData);
	},
	
	// Notify user of addtion failure
	notifyAddtionFailure: function()
	{
		alert("Maximum number of links exceeded.");
	}
});

// QuickLinksRenderer
// -----------------------------------------------
// Renders contents of Quicklinks menu. 

var QuickLinksRenderer = Class.create(QuickLinksPersistance,{

	// Constructor
	initialize: function($super, linksSelectId, linksDividerId, customizationURL, cookieDomain) 
	{
		$super(cookieDomain);
		this.selectElement = $(linksSelectId);
		this.listDividerElement = $(linksDividerId);
		this.customizeURL = customizationURL;
		this.addLinkDelegate = null;
		Event.observe(linksSelectId, 'change', this.quickLinksChanged.bindAsEventListener(this),false);
	},
	
	// onChanged handler for QuickLinks menu
	setAddLinkDeligate: function(delegate)
	{
		this.addLinkDelegate = delegate
	},
	
	// onChanged handler for QuickLinks menu
	quickLinksChanged: function()
	{	
		var linkURL = this.selectElement.getValue();
		if (linkURL ==">") {document.location = this.customizeURL;return;}
		if (linkURL == "+") {this.addCurrentPage();return;}
		if (linkURL != ""){document.location = linkURL;}
		else { this.selectElement.selectedIndex=0;}
	},
  
	// Handler to add current page to the users custom list
	addCurrentPage: function()
	{
		userTitle = prompt ("Save this page as:",document.title);
		if (userTitle != null && userTitle != "")
		{
			// backup quicklinks
			oldQuickLinks = $A(this.quickLinkData.links);
			
			newTitle = userTitle;
			this.quickLinkData.links.push({url: document.URL, name: newTitle});
			
			if (this.saveQuickLinkData())
			{
				this.populateQuicklinks();
				
				// sync Quick Links Manager if present
				if (this.addLinkDelegate)
				{
					this.addLinkDelegate.quickLinkData.links = $A(this.quickLinkData.links);
					this.addLinkDelegate.loadUI();
				}
			}
			else
			{
				// rollback
				this.quickLinkData.links = oldQuickLinks.copy();
				this.notifyAddtionFailure();		
			}
		}

		// reset menu
		this.selectElement.selectedIndex=0;
		return false;
	},
	
	// append custom links to the select list
	populateQuicklinks: function()
	{	
		qlinkData = this.getQuickLinkData();
		
		// remove all subsequent siblings after the divider element
		this.listDividerElement.nextSiblings().invoke("remove");
		
		selElement = this.selectElement;
		
		if (qlinkData.links.length > 0 )
		{
			// append custom links
			qlinkData.links.each( function(link){
				selElement.insert(new Element('option', { value: link.url}).update(link.name),{bottom: selElement});
			});
			// append divider and Add This Page option
			selElement.insert(new Element('option', { value: ""}).update("- - - - - - - - - -"),{bottom: selElement});	
		}
		selElement.insert(new Element('option', { value: "+"}).update("+ Add This Page"),{bottom: selElement});
		
		// append divider and customization options
		selElement.insert(new Element('option', { value: ""}).update("- - - - - - - - - -"),{bottom: selElement});
		selElement.insert(new Element('option', { value: ">"}).update("&gt; Customize This List"),{bottom: selElement});
	}
	
});

var quickLinks = null;

function initQuickLinks()
{
	quickLinks = new QuickLinksRenderer("UCFHeaderLinks", "UCFHeaderLinksStaticDivider","http://ww2.ucf.edu/quicklinks/customizer.html","ucf.edu");
	quickLinks.populateQuicklinks();
	return false;
}


