/*

UMC Feature Rotator - Source code
Version: beta

This contains the source code for the UMC Feature Rotator. Calendar can go
within any site. Pulling from Kintera Sphere RSS feeds

Requires:
	Google AJAX Feed API
	http://code.google.com/apis/ajaxfeeds/

*/

/*
 *	Object that gets feed and creates UMCFeatureRotator
 */
function UMCFeatureRotator (params) {
	
	this.loadFeedData = function() {
			//get calendar data feed
			var divcnt = 0;
			var obj = new AjaxRequest();
			obj.rss = this.params.feed;
			obj.count = divcnt;
			obj.init();
			
			/* check status of data retrieval */
			this.checkStatus();
	}
	
	this.checkStatus = function() {
		var self = this;
		var complete = true;
		if (statusobj[0] == "running") {
			setTimeout(function() {self.checkStatus()}, 200); /* check status every 2 seconds */
		} else {
			//store data 
			var featureItems = xhrs[0];	
			featureData = new Array;
			featureData.features = featureItems;
			this.externalInitialize(featureData);
		}
	}

	this.loadPlayer = function() {

		var so = new SWFObject(params.swfLocation, "umcfeaturerotatorflash", "740", "240", "9");
		so.addParam('wmode', 'transparent');
		so.addParam('allowScriptAccess', 'always');
		so.write("umc-feature-rotator-area");
	}
	
	this.externalInitialize = function(featureData) {
			//this.loadPlayer();
			
			this.features = this.orderFeatures(featureData.features);
			//id="umc-feature-rotator-flash" src="'+params.swfLocation+'" flashvars="" allowscriptaccess="always" height="240" width="740">
			if($('umcfeaturerotatorflash')) {
				this.flashPlayer = $('umcfeaturerotatorflash');
			} else {
				if(!document.all) {
				document.getElementById('umc-feature-rotator-area').innerHTML = 'Unable to load flash at location '+ 
						this.params.swfLocation+
						'.<div id="fma"> <img width="558" height="150" border="0" usemap="#Map" alt="Flash Player image" src="http://www.adobe.com/products/flashplayer/images/fma_flash9.png"/> <map id="Map" name="Map"><area alt="Download Flash Player 9 now." href="http://www.adobe.com/go/getflashplayer" target="getflash" coords="120,90,216,124" shape="rect"/><area target="flashfeatures" href="http://www.adobe.com/products/flashplayer/productinfo/features/" coords="401,45,537,116" shape="rect"/> </map> </div>'+
						'<p><a href="#" onclick="document.location=\'\'">Click here</a> to refresh page after you install flash.';
				}
			}

			if(this.flashPlayer) {
				var flashParams = new Array();
				flashParams['features'] = this.features;
				flashParams['params'] = this.params;
				var paramsJSON = toJsonString(flashParams);
				//this.flashPlayer.loadFeatures(true);
				this.flashPlayer.loadFeatures(paramsJSON);
			}
		}

	this.setupCategory = function(tab, items) {
			var t = new Array();
			var paramName = 'tab'+(1+tab);
			t['title'] = this.params[paramName].category;
			t['description'] = this.params[paramName].description;
			t['items'] = new Array();
			for(var i =0; i<items.length; i++) {
				if((tab == items[i].tab) && (this.params.articlesPerCategory != t['items'].length)) {
					t['items'].push(items[i]);
				}
			}
			return t;
		}

	this.orderFeatures = function(items) {
			items.sort(UMCFeatureDateSort);
			var features = new Array();
			for(var i=0; i<4; i++) {
				features[i] = this.setupCategory(i, items);
			}
			return features;
		}

	//
	//	Initialization function
	//
	this.init = function (params) {
				/* initializing params  */
				this.params = params;
				this.loadFeedData();
			}
	
	/* Run initialization function */
	this.init(params);
}

/* Event Comparing */
function UMCFeatureCategorySort(x,y) {
	if(x.category == y.category){ return 0; }
	if(x.category < y.category){ return -1; }
	return 1;
}


function UMCFeatureDateSort(x, y){
	var xS = x.date.getTime();
	var yS = y.date.getTime();
	if(xS == yS) { return 0; }
	if(xS>yS) { return -1; }
	return 1;
}


function toJsonString(o) {
	var UNDEFINED
	switch (typeof o) {
		case 'string': return '"' + encodeJS(o) + '"'
		case 'number': return String(o)
		case 'object': 
			if (o) {
				var a = []
				if (o.constructor == Array()) {
					for (var i = 0; i < o.length; i++) {
						var json = toJsonString(o[i])
						alert(kson);
						if (json != UNDEFINED) a[a.length] = json
					}
					return '[' + a.join(',') + ']'
				} else if (o.constructor == Date) {
					//return 'new Date(' + o.getTime() + ')'
					return '""';
				} else {
					for (var p in o) {
						var json = toJsonString(o[p])
						if (json != UNDEFINED) a[a.length] = (/^[A-Za-z_]\w*$/.test(p) ? ("\""+p+"\"" + ':') : ('"' + encodeJS(p) + '":')) + json
					}
					return '{' + a.join(',') + '}'
				}
			}
			return 'null'
		case 'boolean'  : return String(o)
		case 'function' : return
		case 'undefined': return 'null'
	}
}

function encodeJS(s) {
return (!/[\x00-\x19\"\\]/.test(s)) ? s : s.replace(/([\\"])/g, '\\$1').replace(/\r/g, '\\r').replace(/\n/g, '\\n').replace(/\t/g, '\\t').replace(/[\x00-\x19]/g, '')
}
