/*
	Synthetic Page and Event Reporting Framework (SPERF)
	----------------------------------------------------
	for use when reporting is desired for things that don't exist as/in a real web 
	page (e.g. HTML). this code makes use of Omniture's Site Catalyst reporting 
	product and creates 'page' or 'link' events for the requested event_id.

	use as follows:
		REPORTING.makeCall('simple_event');
		REPORTING.makeCall('complex_event', { key1 : 'value1', key2 : 'value2' });
*/
var REPORTING = {
	events : { },
	makeCall : function() { }
};

// called by the app
REPORTING.makeCall = function (event_id, data_object) {
	// bail out if event_id is unknown
	if (typeof(this.events[event_id]) == "undefined") {alert("event id not found");return false;}
	this.events[event_id].populateData(data_object);
	this.events[event_id].setPageVariables();
	//alert("populatedData: "+this.events[event_id].populateData(data_object));
	//alert("setPageVariables: "+this.events[event_id].setPageVariables());

	dispatcher.setAttribute("pageName",this.events[event_id].generatePageName());

	// send page event
	if (this.events[event_id].event_type == "PAGE") {
		dispatcher.setAttribute("linkType","");
		dispatcher.setAttribute("lnk","");

		var pageURL = this.events[event_id].generatePageURL();
		dispatcher.setAttribute("pageURL",pageURL);

		var hier2 = (pageURL.charAt(0) == "/") ? pageURL.substring(1, pageURL.length) : pageURL;
		hier2 = (hier2.charAt(hier2.length) == "/") ? hier2.substring(0, hier2.length - 1) : hier2;
		if(hier2.indexOf("/") == -1 && hier2.indexOf("index.jhtml") != -1 && hier2.indexOf("FLIP-FeaturedPhoto_1536414") != -1){
			dispatcher.setAttribute("hier2","/artists/"+hier2);
			dispatcher.setAttribute("prop22","artists");
		} 
		else{
			dispatcher.setAttribute("hier2",hier2);
		}
/*
		s_hier2 = (s_pageURL.charAt(0) == "/") ? s_pageURL.substring(1, s_pageURL.length) : s_pageURL;
		var heir2_1 = "before: "+s_hier2;
		s_hier2 = (s_hier2.charAt(s_hier2.length) == "/") ? s_hier2.substring(0, s_hier2.length - 1) : s_hier2;
		var heir2_2 = "\nafter: "+s_hier2;
		if(top.location.search.indexOf("debug")!=-1) alert("event id: "+event_id+"\n\n"+heir2_1+heir2_2+"\n\ns_prop22: "+s_prop22);
*/
		
		dispatcher.sendCall();

	// send link event
	} else if (this.events[event_id].event_type == "LINK") {
		dispatcher.setAttribute("linkType","o");
		dispatcher.setAttribute("hier2","");
		//s_lnk = s_co(s_wd); // this is no good
		dispatcher.sendCall();
	// unknown event type
	} else {
		return false;
	}

	// reset data for next call
	this.events[event_id].clearPageVariables();

	return true;
} // END: makeCall()


function ReportingEvent(event_type, page_name_function, page_url_function, page_variable_function) {
	this.data = {}; // holds a copy of the data passed in (for use by methods of this object)
	this.event_type = event_type; // "PAGE" or "LINK"

	this.generatePageName = page_name_function;
	this.generatePageURL = page_url_function;
	this.setPageVariables = page_variable_function;

} // END: ReportingEventType()


ReportingEvent.prototype.populateData = function(data) {
	try{
		for (var i in data){
			this.data[i] = data[i];
		};
		return true;
	}
	catch(e){
		return false;
	}
}; // END: populateData()


ReportingEvent.prototype.clearPageVariables = function() {
	this.data = { }; // reset data passed in by app
	dispatcher.clearProps();

	dispatcher.setAttribute("pageName","");
	dispatcher.setAttribute("pageURL",document.location.href);
	dispatcher.setAttribute("events","");
	dispatcher.setAttribute("hier2","");
	dispatcher.setAttribute("linkTrackVars","None");
}; // END: clearPageVariables()

// =============================================================================

REPORTING.events['fbml_pv'] = new ReportingEvent(
	"PAGE",

	function() { // generatePageName()
		var fbmlParams = new Object();
		var fbmlVid = "";
		var pth = new Array();
		for (i in this.data){
			if (typeof this.data[i] != "object" && this.data[i].toString()!="[object Object]"){
				pth.push(this.data[i]);
				//alert(i+": "+this.data[i]+": type="+typeof this.data[i]);
			}
			else fbmlParams = this.data[i];
		}
		if(typeof pth[pth.length-1] == "undefined") pth.pop();
		if(typeof fbmlParams["videoVals"] != "undefined")
			fbmlVid = fbmlParams["videoVals"];
		if(typeof fbmlParams["pageUrl"] != "undefined") {return fbmlParams["pageUrl"];}
		else {return "/" + pth.join('/') + fbmlVid;}
	},

	function() { // generatePageURL()
		var fbmlParams = new Object();
		var pth = new Array();
		var tmp="";
		for (i in this.data){
			//tmp+=("i: "+data[i]+": "+(typeof this.data[i])+"\n");
			if (typeof this.data[i] != "object" && this.data[i].toString()!="[object Object]")
				pth.push(this.data[i]);
			else fbmlParams = this.data[i];
		}
		if(typeof pth[pth.length-1] == "undefined") pth.pop();
		if(top.location.search.indexOf("debug")!=-1)alert((typeof fbmlParams["pageUrl"])+": "+fbmlParams["pageUrl"]);
		if(typeof fbmlParams["pageUrl"] != "undefined") return fbmlParams["pageUrl"];
		else return pth.join('/');
	},

	function() { // setPageVariables()
		dispatcher.setAttribute("channel",this.data['section_0']);
		for (i in this.data){
			if(typeof this.data[i] == "object" || this.data[i].toString()=="[object Object]"){
				dispatcher.setAttribute("prop1",typeof this.data[i]['artist']!='undefined'?this.data[i]['artist']:"");
				dispatcher.setAttribute("prop4",typeof this.data[i]['login']!='undefined'?this.data[i]['login']:"");
				dispatcher.setAttribute("prop5",typeof this.data[i]['zyg']!='undefined'?this.data[i]['zyg']:"");
				dispatcher.setAttribute("prop6",typeof this.data[i]['span']!='undefined'?this.data[i]['span']:"");
				dispatcher.setAttribute("prop7",typeof this.data[i]['demo']!='undefined'?this.data[i]['demo']:"");
				dispatcher.setAttribute("prop9",typeof this.data[i]['flipbookTitle']!='undefined'?this.data[i]['flipbookTitle']:"");
				dispatcher.setAttribute("prop12",typeof this.data[i]['vidOrigin']!='undefined'?this.data[i]['vidOrigin']:"");
				dispatcher.setAttribute("prop17",typeof this.data[i]['source']!='undefined'?this.data[i]['source']:"");
				dispatcher.setAttribute("prop22",typeof this.data[i]['rollUp']!='undefined'?this.data[i]['rollUp']:"");
				dispatcher.setAttribute("prop24",typeof this.data[i]['bps']!='undefined'?this.data[i]['bps']:"");
				dispatcher.setAttribute("prop25",typeof this.data[i]['fla']!='undefined'?this.data[i]['fla']:"");

				if(typeof this.data[i]['votes']!= 'undefined'){
				   if(typeof this.data[i]['gamename']!= 'undefined'){
						dispatcher.setAttribute("prop11",this.data[i]['gamename'] + " : " + this.data[i]['votes']);
				   } else {
						dispatcher.setAttribute("prop11",'unknown gamename' + " : " + this.data[i]['votes']);
				   }
				} 
	
				if(typeof this.data[i]['gamename']!= 'undefined'){
					dispatcher.setAttribute("prop22",this.data[i]['gamename']);
				}
			}
		}
		return true;
	}
); // END: fbml_pv


REPORTING.events['fbml_grid'] = new ReportingEvent(
	"LINK",

	function() { // generatePageName()
		var clkThru = typeof this.data['clickthru']!='undefined'?this.data['clickthru']:"";
		return clkThru;
	},

	function() { // generatePageURL()
		return;
	},

	function() { // setPageVariables()
		var paneSz = typeof this.data['sz']!='undefined'?this.data['sz']:"SIZE_UNSPECIFIED";
		var panePos = typeof this.data['pos']!='undefined'?this.data['pos']:"POSITION_UNSPECIFIED";
		var paneSec = typeof this.data['area']!='undefined'?this.data['area']:"AREA_UNSPECIFIED";
		dispatcher.setAttribute("linkName",paneSec + " : " + paneSz + " : " + panePos);
		return;
	}
); // END: fbml grid
