/***************************************
 *   reuters spotlight script library
 **************************************/

// ******* top level namespace and "static" utility functions *******
var spotlight = {};
// ensure a namespace exists
spotlight.ensureNamespace = function( ns ) {
	if (ns != null && ns.length>0) {
		var levels = ns.split(".");
		var currentNS = spotlight;
		var start = (levels[0] == 'spotlight') ? 1 : 0;
		for (var i=start; i<levels.length; i++) {
			currentNS[levels[i]] = currentNS[levels[i]] || {};
			currentNS = currentNS[levels[i]];
		}
		return currentNS;
	} else {
		return null;
	}
}
// manages a script block in the HEAD of the doc
spotlight.ensureScript = function(id, url) {
	if ( id != null && url != null && url.length > 0 ) {
		var head = document.getElementsByTagName("head")[0];
		var script = document.getElementById(id);
		if (script != null) {
			head.removeChild(script);
		}
		script = document.createElement('script');
		if ( id != null ) {
			script.id = id;
		}
		script.type = 'text/javascript';
		script.src = url;
		head.appendChild(script)
	}
}
// the spotlight namespaces
spotlight.ensureNamespace('spotlight');
spotlight.ensureNamespace('spotlight.data');
spotlight.ensureNamespace('spotlight.dhtml');
spotlight.ensureNamespace('spotlight.dhtml.renderers');


// ******* spotlight base data list manager *******
spotlight.data.listManagerBase = function(id, apiKey, ulClass, liClass, showDescription, descriptionLength) {
	if ( arguments.length > 0 ) {
		this.init(id, apiKey, ulClass, liClass, showDescription, descriptionLength);
	}
}
spotlight.data.listManagerBase.prototype.init = function(id, apiKey, ulClass, liClass, showDescription, descriptionLength) {
	this.id = id;
	this.apiKey = apiKey;
	this.listClass = ulClass;
	this.itemClass = liClass;
	this.showDescription = (showDescription==null) ? false : showDescription;
	this.descriptionLength = (descriptionLength==null) ? -1 : descriptionLength;
	this.baseUrl = 'http://spotlight.reuters.com/api/feed';
	this.channel = null;
	this.edition = null;
	this.feedType = null;
	this.items = null;  // data items
}
// get data from the spotlight services
spotlight.data.listManagerBase.prototype.getData = function(edition, channel) {
	if ( edition != null ) this.edition = edition;
	if ( channel != null ) this.channel = channel;
	spotlight.ensureScript(this.id + '_script', this.buildUrl());
}
// build the spotlight url
spotlight.data.listManagerBase.prototype.buildUrl = function() {
	var url = '';
	if ( this.edition != null && this.channel != null && this.feedType != null && this.apiKey != null && this.apiKey.length > 0) {
		url += this.baseUrl + '/' + this.edition + '/' + this.feedType;
		url += '/' + this.channel + '/json?apikey=' + this.apiKey;
		url += '&callback=' + escape(this.id + '.writeData');
	}
	return url;
}
// process returned data
spotlight.data.listManagerBase.prototype.writeData = function(data) {
	var elem = document.getElementById(this.id);
	if ( elem != null ) {
		var rend = this.getListRenderer(data);
		if ( rend != null ) {
			rend.render(elem);
		} else {
			alert('Error: spotlight.data.listManagerBase.getListRenderer returned null, please provide an implementation');
		}
	}
	this.items = (data!=null) ? data.items : null;
	this.postProcess();
}
// get renderer
spotlight.data.listManagerBase.prototype.getListRenderer = function(data) {
	return new spotlight.dhtml.renderers.baseListRenderer(this.id, data, this.listClass, this.itemClass, this.showDescription, this.descriptionLength);
}
spotlight.data.listManagerBase.prototype.postProcess = function() {}


// ******* channel news headline list manager *******
spotlight.data.newsManager = function(id, apiKey, ulClass, liClass, showDescription, descriptionLength) {
	if ( arguments.length > 0 ) {
		this.init(id, apiKey, ulClass, liClass, showDescription, descriptionLength);
	}
}
spotlight.data.newsManager.prototype = new spotlight.data.listManagerBase();
spotlight.data.newsManager.prototype.superClass = spotlight.data.listManagerBase.prototype;
spotlight.data.newsManager.prototype.init = function(id, apiKey, ulClass, liClass, showDescription, descriptionLength) {
	this.superClass.init.call(this, id, apiKey, ulClass, liClass, showDescription, descriptionLength);
	this.feedType = 'channelnews';
}


// ******* channel photo list manager *******
spotlight.data.photoManager = function(id, apiKey, ulClass, liClass, showDescription, descriptionLength) {
	if ( arguments.length > 0 ) {
		this.init(id, apiKey, ulClass, liClass, showDescription, descriptionLength);
	}
}
spotlight.data.photoManager.prototype = new spotlight.data.listManagerBase();
spotlight.data.photoManager.prototype.superClass = spotlight.data.listManagerBase.prototype;
spotlight.data.photoManager.prototype.init = function(id, apiKey, ulClass, liClass, showDescription, descriptionLength) {
	this.superClass.init.call(this, id, apiKey, ulClass, liClass, showDescription, descriptionLength);
	this.feedType = 'channelphotos';
	this.currentItemIndex = 0; // tracking of a currently selected/displayed item
}
// get renderer
spotlight.data.photoManager.prototype.getListRenderer = function(data) {
	return new spotlight.dhtml.renderers.photoListRenderer(this.id, data, this.listClass, this.itemClass, this.showDescription, this.descriptionLength);
}
spotlight.data.photoManager.prototype.postProcess = function() {
	this.currentItemIndex = 0;
	this.showCurrentItem();
}
// image display control
spotlight.data.photoManager.prototype.next = function() {
	if ( this.items != null ) {
		if ( this.currentItemIndex<(this.items.length-1) ) {
			this.currentItemIndex++;
		} else {
			this.currentItemIndex = this.items.length-1;
		}
		this.showCurrentItem();
	}
}
spotlight.data.photoManager.prototype.previous = function() {
	if ( this.items != null ) {
		if ( this.currentItemIndex>0 ) {
			this.currentItemIndex--;
		} else {
			this.currentItemIndex = 0;
		}
		this.showCurrentItem();
	}
}
spotlight.data.photoManager.prototype.begin = function() {
	if ( this.items != null ) {
		this.currentItemIndex = 0;
		this.showCurrentItem();
	}
}
spotlight.data.photoManager.prototype.end = function() {
	if ( this.items != null ) {
		this.currentItemIndex = this.items.length-1
		this.showCurrentItem();
	}
}
spotlight.data.photoManager.prototype.showCurrentItem = function() {
	for(var i=0; i<this.items.length; i++ ) {
		var elem = document.getElementById(this.id + '_' + i);
		if ( elem != null ) {
			if ( i == this.currentItemIndex ) {
				elem.style.display = '';
			} else {
				elem.style.display = 'none';
			}
		}
	}
}


// ******* base dhtml widget *******
spotlight.dhtml.widgetBase = function(id) {
	if ( arguments.length > 0 ) {
		this.init(id);
	}
}
// initialise base widget
spotlight.dhtml.widgetBase.prototype.init = function(id) {
	this.id = id;
}
// swap a css class - takes class attrib string, returns new class attrib string
spotlight.dhtml.widgetBase.prototype.swapCssClass = function(classAttrib, srcClass, destClass) {
	var retClass = '';
	if ( classAttrib != null && classAttrib.length>0 ) {
		if ( srcClass != null && srcClass.length>0 && classAttrib.indexOf(srcClass) > -1 ) {
			retClass = classAttrib.replace( srcClass, destClass );
		} else {
			retClass = classAttrib  + ' ' + destClass;
		}
	} else {
		retClass = destClass;
	}
	return retClass;
}


// ******* tab pane control widget - generic tab/pane view management *******
spotlight.dhtml.pane = function(id, tabCssClass, tabCssClassSelected, contentCssClass, contentCssClassSelected) {
	if ( arguments.length > 0 ) {
		this.init(id, tabCssClass, tabCssClassSelected, contentCssClass, contentCssClassSelected);
	}
}
spotlight.dhtml.pane.prototype = new spotlight.dhtml.widgetBase();
spotlight.dhtml.pane.prototype.superClass = spotlight.dhtml.widgetBase.prototype;
spotlight.dhtml.pane.prototype.init = function(id, tabCssClass, tabCssClassSelected, contentCssClass, contentCssClassSelected) {
	this.superClass.init.call(this, id);
	this.tabCssClass = tabCssClass;
	this.tabCssClassSelected = tabCssClassSelected;
	this.contentCssClass = contentCssClass;
	this.contentCssClassSelected = contentCssClassSelected;
	this.defaultTabIndex = 0;
}
// add a "tab/pane" to the collection
spotlight.dhtml.pane.prototype.addTab = function(tabId, contentId) {
	if ( this.tabs == null ) this.tabs = new Array();
	var tabObj = new Object();
	tabObj.id = tabId;
	tabObj.contentId = contentId;
	this.tabs[this.tabs.length] = this.tabs[tabId] = tabObj;
}
// change the current tab
spotlight.dhtml.pane.prototype.changeTab = function(tabId) {
	if ( this.tabs != null && this.tabs[tabId] != null) {
		var newTab = this.tabs[tabId];
		for ( var i=0; i < this.tabs.length; i++ ) {
			var elem = document.getElementById(this.tabs[i].id);
			elem.className = this.swapCssClass(elem.className, this.tabCssClassSelected, this.tabCssClass);
			var cElem = document.getElementById(this.tabs[i].contentId);
			if ( this.contentCssClass != null ) {
				cElem.className = this.swapCssClass(cElem.className, this.contentCssClassSelected, this.contentCssClass);
			} else {
				cElem.style.display = 'none';
			}
		}
		var newElem = document.getElementById(newTab.id);
		newElem.className = this.swapCssClass(newElem.className, this.tabCssClass, this.tabCssClassSelected);
		var ncElem = document.getElementById(newTab.contentId);
		if ( this.contentCssClassSelected != null ) {
			ncElem.className = this.swapCssClass(cElem.className, this.contentCssClass, this.contentCssClassSelected);
		} else {
			ncElem.style.display = '';
		}
	}
}
// show the default tab
spotlight.dhtml.pane.prototype.showDefaultTab = function() {
	if (this.tabs != null && this.tabs[this.defaultTabIndex] != null) {
		this.changeTab(this.tabs[this.defaultTabIndex].id);
	}
}


// ******* base list renderer *******
spotlight.dhtml.renderers.baseListRenderer = function(id, data, listClass, itemClass, showDescription, descriptionLength) {
	if ( arguments.length > 0 ) {
		this.init(id, data, listClass, itemClass, showDescription, descriptionLength);
	}
}
// initialise base widget
spotlight.dhtml.renderers.baseListRenderer.prototype.init = function(id, data, listClass, itemClass, showDescription, descriptionLength) {
	this.id = id;
	this.data = data;
	this.listClass = listClass;
	this.itemClass = itemClass;
	this.listElem = 'ul';
	this.itemElem = 'li';
	this.showDescription = (showDescription==null) ? false : showDescription;
	this.descriptionLength = (descriptionLength==null) ? -1 : descriptionLength;
}
// render the list into the supplied elem
spotlight.dhtml.renderers.baseListRenderer.prototype.render = function(elem) {
	var html = '';
	if ( this.data.items != null ) {
		html += '<' + this.listElem + ' class=\"' + this.listClass + '\">';
		for( var i=0; i<this.data.items.length; i++ ) {
			var item = this.data.items[i];
			html += '<' + this.itemElem + ' id=\"' + this.id + "_" + i + '\" class=\"' + this.itemClass + '\">';
			html += this.renderItem(item);
			html += '</' + this.itemElem + '>';
		}
		html += '</' + this.listElem + '>';
	}
	elem.innerHTML = html;
}
// render a single item in the list
spotlight.dhtml.renderers.baseListRenderer.prototype.renderItem = function(item) {
	var html = '';
	html += '<a href=\"' + item.link + '\">';
	html += item.title;
	html += '</a><br/>';
	if ( this.showDescription ) {
		html += (this.descriptionLength>0 && item.description!=null) ? item.description.substring(0, this.descriptionLength) + '...' : item.description;
	}
	return html;
}


// ******* photo list renderer *******
spotlight.dhtml.renderers.photoListRenderer = function(id, data, listClass, itemClass, showDescription, descriptionLength) {
	if ( arguments.length > 0 ) {
		this.init(id, data, listClass, itemClass, showDescription, descriptionLength);
	}
}
spotlight.dhtml.renderers.photoListRenderer.prototype = new spotlight.dhtml.renderers.baseListRenderer();
spotlight.dhtml.renderers.photoListRenderer.prototype.superClass = spotlight.dhtml.renderers.baseListRenderer.prototype;
spotlight.dhtml.renderers.photoListRenderer.prototype.init = function(id, data, listClass, itemClass, showDescription, descriptionLength) {
	this.superClass.init.call(this, id, data, listClass, itemClass, showDescription, descriptionLength);
	this.listElem = 'div';
	this.itemElem = 'div';
}
// render a single item in the list
spotlight.dhtml.renderers.photoListRenderer.prototype.renderItem = function(item) {
	var html = '';
	html += '<a href=\"' + item.link + '\">';
	html += '<span>' + item.title + '</span>';
	html += '<img src=\"' + item.link + '\"';
	if ( this.showDescription ) {
		html += ' alt=\"' + ((this.descriptionLength>0 && item.description!=null) ? item.description.substring(0, this.descriptionLength) + '...' : item.description) + '\"';
	}
	html += ' />';
	html += '</a>';
	return html;
}
