// Trophy Room Javascript
// Dependencies: jQuery, bam, bam.ajaxHelper

bam.loadSync(bam.homePath + "bam.ajaxHelper.js",
			 bam.homePath + "bam.cookies.js");

var ftr = (function(){
	//Private					
	var _service = bam.ajaxHelper.createInstance({
		url: "/fantasylookup/named.trophyroom.bam",
		dataType: "xml",
		Load: function(ipid, cb) {
			var that = this;
			var _qs = {"ipid": ipid};
			that.makeRequest(_qs, function(data) {
				that.cache = data;
				if($.isFunction(cb)){cb.call();}
			});
		},
		getTrophies: function(type) {
			var _filtered = null;
			if(!!this.cache && !!this.cache.queryResults && !!this.cache.queryResults.length && !!this.cache.queryResults[0].row) {
				_filtered = this.cache.queryResults[0].row.getNodesByAttribute("game_id", String(type));				
			}
			return _filtered;
		}
	});
	

	//Public
	var _self = {
		ipid: bam.cookies.get('ipid'),
		rooms: [101, 1, 2, 3],
		init: function() {
			// Destroy currrent tooltip if present
         	if($(this).data("qtip")) $(this).qtip("destroy");
			
			//Load trophies display
			_service.Load(this.ipid, function() {
				var _room, _section, _trophies, _out, _multiplier, _teamname;
				$(_self.rooms).each(function() {
					_room = parseInt(this, 10);
					_section = "#gameid_" + _room;
					_trophies = _service.getTrophies(_room);
					_multiplier = ((_room === 101 && 100)||(_room === 1 && 70)||(_room === 2 && 70)||(_room === 3 && 135)) + 5;										
					if(!bam.validation.isEmpty(_trophies)) {						
						_out = new bam.string.StringBuffer("<ul class=\"trophies\" style=\"display:table; width:"+ ((_trophies.length * _multiplier)+10) + "px" +"\">");
						$(_trophies).each(function() {
							_teamname = (this.wsfb_team_name.length > 13)?this.wsfb_team_name.substr(0, 12) + "...":this.wsfb_team_name;
							_out.append("<li class=\"trophy " + this.image + " png\">")																
							if(_room === 101) {	_out.append("<div class=\"teamname\" title=\""+this.wsfb_team_name+"\">" + _teamname + "</div>"); }
							if(_room !== 101) { 
								_out.append("<div class=\"stats\">" + this.stats + "</div>")
								.append("<div class=\"awdperiod\">" + this.awarded_period + "</div>");
							}
							_out.append("</li>");
					    });
						_out.append("</ul>");
						$(_section).html(_out.toString());
					} else {
						_out = new bam.string.StringBuffer("<div class=\"playnow\">");
						_out.append("<a href=\"");
						switch(_room) {
							case 101: _out.append("/mlb/fantasy/wsfb_c/info/index.jsp")
									  .append("\">PLAY NOW</a> for your chance to win Fantasy Baseball trophies!");
									  break;
							case 1	: _out.append("/mlb/fantasy/bts/")
									  .append("\">PLAY NOW</a> for your chance to win Beat the Streak trophies!");
									  break;
							case 2	: _out.append("/mlb/fantasy/btshr/")
									  .append("\">PLAY NOW</a> for your chance to win Home Run Beat the Streak trophies!");
									  break;
							case 3	: _out.append("/mlb/fantasy/survivor/")
									  .append("\">PLAY NOW</a> for your chance to win Survivor  trophies!");
									  break;
						}
						_out.append("</div>");
						$(_section).html(_out.toString());
					}
				});
			});
		},
		showQTip: function(room) {
			var $popupContainer = $("#popup_" + room);
			
			$(this).qtip({
               content: $popupContainer.html(), // Set the tooltip content to the current corner
               position: {
                  corner: {
                     tooltip: "bottomRight", // Use the corner...
                     target: "topLeft" // ...and opposite corner
                  }
               },
               show: {
                  when: false, // Don't specify a show event
                  ready: true // Show the tooltip when ready
               },
               hide: { when: { event: 'unfocus' } }, // Don't specify a hide event
               style: {
                  border: {
                     width: 5,
                     radius: 10
                  },
				  width: 300,
                  padding: 10, 
                  textAlign: 'center',
                  tip: true, // Give it a speech bubble tip with automatic corner detection
                  name: 'cream' // Style it according to the preset 'cream' style
               }
            })	
		}
	};

	return _self;
})();

//Run
$(function() {
	var loc = bam.url.Location(window.location);
	var qipid = loc.getParam("ipid") || false;
	
	if(!isLoggedIn && !qipid) {
		location.replace("/enterworkflow.do?flowId=fantasy.bts.btsloginregister&forwardUrl=/mlb/fantasy/trophy_room/");
	}
	
	if(!!qipid) {
		ftr.ipid = qipid;
	}
	var permaLink = "<a href=\"/mlb/fantasy/trophy_room/index.jsp?ipid=" 
	+ ftr.ipid + "\">http://mlb.mlb.com/mlb/fantasy/trophy_room/index.jsp?ipid=" 
	+ ftr.ipid + "</a>";
	
	$("div#perma_link").html("To share this page, use this link: " + permaLink);
	ftr.init();	
});
