/**
 *
 */
billboard.modules = new (function($)
{
	var me = this;

	billboard.broadcaster.addListener( "pageLoaded", function() { me.onPageLoaded(); } );

	me.init = function()
	{
	};

	/**
	 * check content of the page to determine which modules need to be initialized
	 */
	me.onPageLoaded = function()
	{
		if ( billboard.section == "home" ) {
			me.initBuzz();
		}
		if ( $(".hot-news-features").length>0 ) {
			me.initHotNews();
		}
		if ( $("#hot-container").length > 0 ) { 
			me.initHotModule();
		}		
		if ( $(".hot-conversations").length > 0 ) {
			me.initHotConversations();
		}
	};

	/**
	 * pull all the most popular conversations with the most replies
	 */
	me.initHotConversations = function()
	{
		billboard.log("Module.initHotConversations()");
		var elem;
		var contentId = billboard.entityId; // page level unique id 
		var board;
		billboard.social.getMessageBoards( contentId, {order:"MostCommented",start:0,count:15}, function(response) { 
			billboard.log( response );
			if ( response.numBoards > 0 ) { 
				billboard.log(" showing hot conversation module");
				$("#hot-conversations-container").show();
				for ( var i=0;i<response.boards.length;i++ ) {
					board = response.boards[i];
					elem = $("#hot-convo-template").clone();
					elem.removeAttr("id");
					elem.find(".num-replies").html( board.numReplies );
					elem.find(".convo-topic a").html( board.title );
					var url = updateQueryParams( $("a.view-all").attr("href"),{cid:board.id});
					elem.find(".convo-topic a").attr( "href", url );
					$("#hot-conversation-list").append( elem.show() );
					billboard.hijackLinks( elem );
				}
			}
		});

		/*
		billboard.social.getComments( contentId, {start:0,count:15,sort:"MostRecentFirst"}, function( data ) {
			billboard.log( data );
			if ( data && data.comments ) { 
				billboard.log(" showing hot conversation module");
				$("#hot-conversations-container").show();
				for ( var i=0;i<data.comments.length;i++ ) {
					elem = $("#hot-convo-template").clone();
					elem.removeAttr("id");
					elem.find(".num-replies").html( data.comments[i].num_child_comments );
					elem.find(".convo-topic a").html( data.comments[i].subject );
					var url = updateQueryParams( $("a.view-all").attr("href"),{cid:data.comments[i].id});
					elem.find(".convo-topic a").attr( "href", url );
					elem.insertAfter( $("#hot-convo-template") ).show();
					billboard.hijackLinks( elem );
				}
			}
		});
		*/
	};

	me.initBuzz = function()
	{
		billboard.log("Modules.initBuzz");
		billboard.social.getHotItems( "song", {count:10,numDays:7}, function( song ) { 
			//billboard.log( "initBuzz result" );
			//billboard.log( song  );
			var id = song.id.split("-")[1];
			billboard.getItemDetails( id, "song", function( result ) {
				if ( !result || typeof(result.response) == "undefined" ) { return; }
				var result = result.response.body.FavoriteItemView;
				result.name = result.title;
				
				// determine url to image
				var photo = "/images/defaults/artist-94.gif";
				if ( result.photoUrl && result.photoUrl.length>0) { 
					photo = result.photoUrl;
				}
				
				for ( var p in result ) {
					song[p] = result[p];
				}
				
				// create dom object
				elem = $("#noise-maker-template").clone();
				elem.removeAttr("id");
				elem.find(".rank").text( $(".noise-maker-list li").length );
				elem.find(".pic img").attr("src",photo);
			//	elem.find(".info .song").text( song.name );
				elem.find(".info .song").append("<a href='"+ result.url + "'> "+ result.title + "</a>");
				//elem.find(".info .artist").text( song.artist );
				//elem.find(".info").attr("href",song.url );
				var links = new Array();
				if(result.artists){
					for(i=0; i < result.artists.length; i++){
						links.push("<a href='"+  	result.artists[i].link + "'> "+ result.artists[i].name + "</a>");
					}
					elem.find(".info .artist").append( links.join(", ") );
				}

				if ( result.lalaId && result.lalaId.length > 0 ) {
					elem.find(".play-btn a").click( function() { 
						javascript:billboard.player.play(result.lalaId);
					});
				}
				else {
					elem.find(".play-btn a").hide();
				}

				billboard.hijackLinks( elem );
				
				// add the element to the module
				$(".noise-maker-list").append( elem.show() );
			});
		});
	};

	me.initHotNews = function()
	{
		billboard.log("Modules.initHotNews");
		billboard.social.getHotItems( "article", {numDays:1}, function(item){ 
			var elem;
			var item;
			item.id = item.id.split("-")[1];
			
			billboard.getItemDetails( item.id, item.type, function(result){
				if ( result && result.response ) { 
					var result = result.response.body.FavoriteItemView;
					result.name = result.title;
					
					// determine url to image
					var photo = "";
					if ( result.photoUrl && result.photoUrl.length>0) { 
						photo = result.photoUrl;
					}

					for ( var p in result ) {
						item[p] = result[p];
					}
					billboard.log( item );
					
					// create dom object
					elem = $("#hot-news-template").clone();
					elem.removeAttr("id");
					if(photo!="") elem.find(".pic img").attr("src",photo);
					else elem.find(".pic").hide();
					elem.find(".news-name").text( item.title );
					elem.find(".percent-like").text( item.loveHatePercentage+"%" );
					elem.find(".total-comments").text( item.numComments );
					elem.find(".total-comments").attr( "href", item.url+"#comment-container" );
					elem.click( function() {
						if(item.url!="") billboard.navigateToUrl( item.url );
						return false;
					});
					elem.mouseover( function(){
						$(this).addClass("tout-highlight");
						if(item.url!="") $(this).addClass('clickable');
					});
					elem.mouseout( function(){
						$(this).removeClass("tout-highlight");
					});
					billboard.hijackLinks( elem );
					
					if ( result.lalaId && result.lalaId.length > 0 ) {
						elem.find(".play-btn a").click( function() { 
							javascript:billboard.player.play(result.lalaId);
						});
					}
					else {
						elem.find(".play-btn a").hide();
					}
					// add the element to the module
					$(".hot-news-features").append( elem.show() );
				}
			});
		});
	};

	/**
	 *
	 */
	me.initHotModule = function()
	{
		billboard.log("Billboard.initHotModule()");

		// show the proper container, based on random type
		var types = ["artist","album","song"];
		var type = types[Math.floor(Math.random()*3)];
		var count= 0;
		var module = $("#hot-"+type+"-container");
		module.show();
		
		billboard.social.getHotItems( type, {numDays:1}, function(item){ 
			var item;
			var link;

			item.id = item.id.split("-")[1];

			billboard.getItemDetails( item.id, item.type, function(result){
				count++; 

				var result = typeof(result.response) != 'undefined' ? result.response.body.FavoriteItemView : '';
				result.name = result.title;
				
				// determine url to image
				var photo;
				if(type == "song") {
					photo = "/images/defaults/"+type+"-69.gif";
				}	else {
					photo = "/images/defaults/"+type+"-94.gif";
				}

				if ( result.photoUrl && result.photoUrl.length>0) { 
					photo = result.photoUrl;
				}

				for ( var p in result ) {
					item[p] = result[p];
				}
				
				// create dom object
				var elem = $("#hot-"+type+"-template").clone();
				elem.removeAttr("id");
				if(type== "song" && count==3) elem.addClass("last-song");
				elem.find(".pic img").attr("src",photo);
				elem.find(".pic a").attr("href",item.url);
				if( type == "song" ){
					var links = new Array();
					if(result.artists && result.artists.length > 0){
						for(i=0; i < result.artists.length; i++){
							links.push("<a href='"+  	result.artists[i].link + "'> "+ result.artists[i].name + "</a> &nbsp;");
						}
						elem.find(".artist-name a").append(links.join(", "));
					}
				}
				else{
					elem.find(".artist-name a").text( (item.artist)?(item.artist):(item.title) );
					elem.find(".artist-name a").attr( "href", item.url );
				}				


				
				elem.find(".name a").text( item.name );
				elem.find(".name a").attr( "href", item.url );
				elem.find(".percent-like").text( item.loveHatePercentage+"%" );
				elem.find(".total-comments").text( item.numComments );
				elem.find(".total-comments").attr( "href", item.url+"#comment-container" );
				elem.mouseover( function(){$(this).addClass("tout-highlight");});
				elem.mouseout( function(){	$(this).removeClass("tout-highlight");});
				elem.append( $("<div class='lala-id' style='display:none'>"+result.lalaId+"</div>") );
				elem.append( $("<div class='lala-title' style='display:none'>"+result.name+"</div>") );

				// activate the play button
				if ( type == "song" && result.lalaId && result.lalaId.length>0 ) {
					billboard.log( "<--song -->" );
					billboard.log( item );
					elem.find(".action").show();
					elem.find(".play-btn a").click( function() { 
						javascript:billboard.player.play(result.lalaId);
					});
				}
				else {
					elem.find(".play-btn a").hide(); 
				}
				elem.find(".action a").click( function() { 
					billboard.log("click");
					var id = $(this).parent().parent().find(".lala-id").text();
					var title = $(this).parent().parent().find(".lala-title").text();
					if ( id && title ) { 
						billboard.player.setQueue( [ {id:id,title:title} ] );
					}
				});

				elem.show();
				
				// add the element to the module
				$(".hot-"+type+"s").append( elem );
				billboard.hijackLinks( elem );
			});
		});
	};
})(jQuery);
