
var MostPopular_Module = function () { };

MostPopular_Module.prototype.BUFFER_PATH = buildAbsolutePath('/modules/mostPopular/mostPopular.data.asp');

MostPopular_Module.prototype.MAX_NEWS_STORIES = 5;

MostPopular_Module.prototype.init = function () {
	
	this._module    	  = Element.get("markets_mostPopular");
	
	if(!this._module) {
		return;	
	}
	
	this._menuItems 	  = Element.parseSelector("a", "mostPopularMenu");
	this._descriptionArea = Element.get("mostPopularDescription");
	this._moduleBody 	  = Element.parseSelector(".moduleBody", "markets_mostPopular", "first");
	this._body			  = Element.get("cloud");
	this._loader		  = Element.get("markets_mostPopularLoader");
	this._loadingGraphic	  = Element.get("markets_mostPopularLoadingGraphic");
	
	this.addEvents();
};

MostPopular_Module.prototype.addEvents = function () {
	
	Events.add({
		 element:this._menuItems
		,type:"click"
		,handler:this.retrieveData
		,context:this
	});
};

MostPopular_Module.prototype.retrieveData = function ( ev, el ) {
	
	if(Element.hasClass(el, "active")) {
		
		return;	
	}
	
	Element.removeClass(this._menuItems, "active");
	Element.addClass(el, "active");
	
	this.dataType = el.getAttribute("item");
	
	this.showLoading();
	
	Common.loadContentBuffer({
		 page:this.BUFFER_PATH
		,prevenEval:true
		,onload:this.handleData
		,context:this
		,data:{
			 dataType:this.dataType || 'quotes'
			,action:'retrieveData'
		}
	});
};

MostPopular_Module.prototype.handleData = function ( oBuffer ) {
	
	var data = DataFunctions
				.serializer
					.deserialize( oBuffer.getResult() );

	
	Element.setHTML( this._descriptionArea, data.description );
	
	var i  		 = 0,
		len 	 = data.data.length,
		tagArray = new Array();

	for(; i < len; i++) {
		
		if(this.dataType == "mostViewed") {
		
			if(i == this.MAX_NEWS_STORIES) {
			
				break;
			}
		
			tagArray.push(this.createNewsLink(i, len, data.data[i]));
			continue;
		}
		
		tagArray.push(this.createTagCloudLink( i, len, data.data[i] ));
	}
	
	
	if(this.dataType == "mostViewed") {
		
		var ol = Element.create("ul", {className:"mostViewedNews"}, tagArray);
		
		tagArray.splice(0,tagArray.length);
		tagArray.push(ol, '<br class="clear" />');
	}

	while(this._body.lastChild) {
		
		Element.remove(this._body.lastChild);
	}
	
	Element.addChild(this._body, tagArray);
	
	this.hideLoading();
};


MostPopular_Module.prototype.createTagCloudLink = function( i, total, data ) {

	return Element.create("a", {href:data.path,className:"tag"+(i+1)}, [
			data.text + ( i+1 == total? '':', ' )
	]);
};


MostPopular_Module.prototype.createNewsLink = function( i, total, data ) {

	var elem = Element.create("li", {}, [
			Element.create("div", {className:"headline hasLayout"}, [
			  Element.create("span", {className:"olHack"}, i+1)
			 ,Element.create("div", {className:"link fleft"}, [
			 	Element.create("a", {href:data.path}, [
			 	 	 data.text
			 		,data.altText? Element.create("span", {className:"moduleHasIcon smallArrow"}, "&nbsp;&nbsp;") : ""
			 		,"<br />"
			 	])
			 	,Element.create("span", {className:"subtext"}, data.subtext)
			 ])
		])
	]);
	return elem;
};

MostPopular_Module.prototype.showLoading = function () {
	
	var w = this._moduleBody.offsetWidth;
	var h = this._moduleBody.offsetHeight;
	
	Element.setStyle(this._loader, "width:"+w+"px");
	Element.setStyle(this._loader, "height:"+h+"px");
	Element.removeClass(this._loader, "none");
	
	Element.setStyle(this._loadingGraphic, "margin-top:"+((h / 2)-10)+"px");
	Element.setStyle(this._loadingGraphic, "margin-left:"+((w / 2)-10)+"px");
};

MostPopular_Module.prototype.hideLoading = function () {
	
	Element.addClass(this._loader, "none");
};

loadBuffer.add(function(){
	
	var MostPopular = new MostPopular_Module();
		MostPopular.init();
});