/* QuickLinks customization UI classes and onDomLoad registration */

// Requires QuickLinksPersistance defined in quick_links.js 

// QuickLinksManager
var QuickLinksManager = Class.create(QuickLinksPersistance,{

	// Constructor
	initialize: function($super, sourceId, destinationId, addId, removeId, clearId, cookieDomain) 
	{
		$super(cookieDomain);
		this.sourceEl = $(sourceId);
		this.destEl = $(destinationId);	
		this.sourceLinks = null;
		
		Event.observe(addId, 'click', this.addSelected.bindAsEventListener(this),false);
		Event.observe(removeId, 'click', this.removeSelected.bindAsEventListener(this),false);
		Event.observe(clearId, 'click', this.removeAll.bindAsEventListener(this),false);
	},

	// Add link from source column
	addSelected: function()
	{	
		//alert("test");
		// backup options and user selections
		oldSource = $A(this.sourceEl.options);
		oldDest = $A(this.destEl.options);
		oldLinks = $A(this.quickLinkData.links);
		
		// add links
		this.swapOptions(this.sourceEl, this.destEl);
		
		this.rebuildUserLinks();
		
		if (!this.saveQuickLinkData())
		{
			this.notifyAddtionFailure();
			this.quickLinkData.links = oldLinks;
			this.saveQuickLinkData();
			this.loadUI();
		} 
		
		this.syncQuickLinks();
		
	},
	
	// 
	rebuildUserLinks: function()
	{	
		this.quickLinkData.links = new Array(this.destEl.options.length);
		
		qLinkData = this.quickLinkData;
		$A(this.destEl.options).each(function(o,i)
		{ 
			qLinkData.links[i]={name: o.text, url: o.value};
		});
	},
	
	// rollback a select element
	rollbackSelect: function(data, element)
	{	
		element.options.length=0;
		data.each(function(o,i){ element.options[i] = new Option(o.text, o.value);});	
	},
	
	// Remove link from destination column
	removeSelected: function()
	{
		this.swapOptions(this.destEl, this.sourceEl);
		this.rebuildUserLinks();	
		this.saveQuickLinkData();
		this.syncQuickLinks();
	},
	
	//Sync Quicklinks menu
	syncQuickLinks: function()
	{
		quickLinks.quickLinkData.links = $A(this.quickLinkData.links);
		quickLinks.populateQuicklinks();	
	},

	swapOptions: function(sourceEl, destEl) 
	{
		
  		var selectOptions = sourceEl.getElementsByTagName('option');
  		
		for (var i = 0; i < selectOptions.length; i++) 
		{
			var opt = selectOptions[i];
			if (opt.selected) 
			{
				sourceEl.removeChild(opt);
				destEl.appendChild(opt);
				i--;
			}
		}
		// Avoid problems with IE6 Dom methods on Select elements
		if(window.XMLHttpRequest) {$A(destEl.options).sort(function(a,b){return (a.text.toLowerCase() < b.text.toLowerCase() ) ? -1 : 1; }).each(function(o,i){$A(destEl.options)[i] = new Option(o.text, o.value);});}
	},
	
	// Clear all custom links
	removeAll: function()
	{	
		var selectOptions = this.destEl.getElementsByTagName('option');
  		for (var i = 0; i < selectOptions.length; i++) 
  		{
     		var opt = selectOptions[i].selected = true;
		}
		this.removeSelected();
	},
	
	// Populate the UI with avaialble links and current links
	loadUI: function()
	{	
		
		// filter out links already selected
		allLinks = $A(this.getAvailableLinks().links);
		userLinks = $A(this.getQuickLinkData().links);
		
		displayLinks = allLinks.select(function(sLink) 
		{ 
		      return !userLinks.any( function(uLink)
		      {
		      	return uLink.url == sLink.url;
		      }); 
	    }); 
		
		// source links
		this.populateSelectLinks(displayLinks,this.sourceEl);
		
		// current links
		this.populateSelectLinks(this.getQuickLinkData().links,this.destEl);

	},
	
	// populate select with list
	populateSelectLinks: function(data, selectEl)
	{			
		selectEl.options.length = 0;
		if (data.length > 0 )
		{
			data.each( function(link, index){
				selectEl.options[index] = new Option(link.name , link.url);
			});
		}
	},

	// Returns array of available links
	getAvailableLinks: function()
	{	
		if (this.sourceLinks==null)
		{
			// extract from soure select list
			linkElements = $$("#"+this.sourceEl.id+" option");
			parsedLinks = {links: []};
			
			linkElements.each(function(sLink, index)
			{
				parsedLinks.links[index] = {name: sLink.text, url: sLink.value };
			});
			
			this.sourceLinks = parsedLinks;
			this.sourceLinks.links.sort( function(a,b) { return a.name.toLowerCase() > b.name.toLowerCase() });
		}
					
		return this.sourceLinks;
	},

	
	// Update the customization UI
	updateUI: function()
	{	
		// nada
	}

});

var quickLinksMgr = null;

function initQuickLinksManager()
{
	quickLinksMgr = new QuickLinksManager("sourceLinks", "destinationLinks", "addLinks", "removeLinks", "clearLinks", "ucf.edu");
	quickLinks.setAddLinkDeligate(quickLinksMgr);
	quickLinksMgr.loadUI();
	return false;
}


