/*
	*************************
	SNY homepage JS
	*************************
	Last Modified: 02/30/2011
	Author: Joe Kanakaraj
	Desc: Script handles loading of all modules on SNY homepage.
*/


bam.snyhome = (function($){
	var _log=function(msg){
		if (_self.debug && typeof console!=="undefined") {
			if (typeof msg === "string") console.log("bam.mlbhome: "+msg);
			else console.log(msg);
		}
	},
	
	_clearSearchBox =  function(){
		$("#sbi").val("");
	},
	_restoreSearchBox =  function(){
		$("#sbi").val("Search");
	},
	
	//SHOP MODULE
	_shopModule = {
				init: function(){
					var defaultID = $("#teamshop ol li.active").attr("id");
					_shopModule.categoryList();
					
				},
				showTabs : function(){
					$('.tabbed-module#teamshop li').removeClass('active');
					$("#teamshop .tabbed-container").hide();
				},
				
				categoryList: function(x){
					$.ajax({
						dataType: 'json',
						url:      '/gen/sny/shop.json',
						success:  function(obj){		
							//alert('success!')
							var url = '/components/homepage/y2011/shop_module.tpl',
								elem = $.template(url,obj);
							$("#teamshop ol").append(elem);
							$(".tabbed-module#teamshop li:first").addClass("active");
						},
						error:  function(err){
							console.dir(err);
						}
					});
					
					
				}
			},
			
	//PROGRAM MODULE
	_programModule = {
				init: function(){
					var defaultID = $("#program ol li.active").attr("id");
					_programModule.categoryList();
					
				},
				showTabs : function(){
					$('.tabbed-module#program li').removeClass('active');
					$("#program .tabbed-container").hide();
				},
				
				categoryList: function(x){
					$.ajax({
						dataType: 'json',
						url:      '/gen/sny/programs.json',
						success:  function(obj){		
							var url = '/components/homepage/y2011/program_module.tpl',
								elem = $.template(url,obj);
							$("#program ol").append(elem);
							$(".tabbed-module#program li:first").addClass("active");
						},
						error:  function(err){
							console.dir(err);
						}
					});
					
					
				}
			},		
	
	//FANZONE MODULE
	_fanzoneModule = {
				init: function(){
					_fanzoneModule.categoryList();
					
				},
				categoryList: function(x){
					$.ajax({
						dataType: 'json',
						url:      '/gen/fanzone/spotlight.json',
						success:  function(obj){
							var url = '/components/homepage/y2011/fanzone_module.tpl',
								elem = $.template(url,obj);
							$("#fanzone ol").append(elem);
							$("#fanzone ol li").last().parent().parent().addClass("last");
						},
						error:  function(err){
							console.dir(err);
						}
					});
					
					
				}
			},
			
	//STATS MODULE
	_headlinesModule = {
				init: function(){
					this.loadTeams();
				},
				showTabs : function(){
					$('.tabbed-module#teamheadlines li').removeClass('active');
					$("#teamheadlines .tabbed-container").hide();
				},
				getTeamAndSport : function(id){
					var teamSport  = id;
					    this.dash  = teamSport.search(/-/i),
					    this.team  = teamSport.slice(this.dash+1),
					    this.sport = teamSport.slice(0,this.dash);
				},
				
				loadTeams : function(){
					$.ajax({
						dataType: 'json',
						url:      '/gen/nyteams/teams.json',
						error: function(xhr){_log('loadTeams():'+xhr)},
						success: function(obj){
							var url = '/components/homepage/y2011/headlines_module.tpl',
								elem = $.template(url,obj);
							$("#teamheadlines ol").append(elem);
							$(".tabbed-module#teamheadlines li:first").addClass("active");
						},
						complete: function(){
							var defaultID = $(".tabbed-module#teamheadlines ol li.active").attr("id");
							_headlinesModule.getTeamAndSport(defaultID);
							_headlinesModule.moduleAjaxRequest();
						}
					});
				
				},
				
				moduleAjaxRequest :  function(){
					var team  = this.team,
						sport = this.sport;
					$.ajax({
						dataType: 'xml',
						url     : '/components/homepage/y2011/sny.proxy.jsp?sport='+sport+'&team='+team+'',
						success : function(obj){
							bam.loadSync(bam.homePath + "bam.xml.js"); 
							var json = bam.xml.xmlToJSON(obj),
								$moduleSelector = $("#teamheadlines #"+sport+"-"+team+" .tabbed-container ol");							
								$moduleSelector.empty();
							for (var x=0; x<4;x++){
								$moduleSelector.append('<li><a href='+json.item[x].link[0].Text+'>'+json.item[x].title[0].Text+'</a></li>');
							}
						},
							error : function(xhr){_log('moduleAjaxRequest():'+xhr);
							}
						});
					}
				},
		//CONNECT WITH SNY
		_connectModule = {
				init : function(){
					this.twModule.init();
				},
				
				twModule: {
					init: function(){
						this.moduleAjaxRequest();				
					},
					
					moduleAjaxRequest: function(){
						$.ajax({
							url:        "/gdcross/components/game/mlb/twitter/snyInsiderTweets.json",
							async:      true,
							timeout:    5000,
							dataType:   "json",
							error:      function(xhr) {},
							success:    function(json) {
								var $container = $("#tw ul.twitter_blurb"), $lit = $("<li>"), $lic;
								$container.hide().empty();
								$.each(json,function(n){
									if(n<10) {
										var htmlStr = "";
										if(!!this.user.profile_image_url){
											htmlStr += "<div class='image_container'><img src='" + this.user.profile_image_url + "' width='48px' heigh='48px' alt=''/></div>";
										}
										if(!!this.user.screen_name){
											htmlStr += "<span><a class='tw_user' href='http://twitter.com/"+this.user.screen_name+"' target='_blank'>@"+this.user.screen_name+":</a> </span>";
										}
										if(!!this.text){
											htmlStr += "<span class='tw_text'>"+this.text+"</span>";
										}
										$lic = $lit.clone(false).html(htmlStr);
										if(n===0)$lic.addClass("first");
											$container.append($lic);
									}
								});
								$container.fadeIn();
							}                              
						});                           
	
					}
				},
				fbModule: {
					init: function(){
						$("#connect-with-sny.tabbed-module ol li div").hide();
						$("#fb div").empty().append('<iframe src="http://www.facebook.com/plugins/likebox.php?href=http%3A%2F%2Fwww.facebook.com%2Fsny&amp;width=298&amp;colorscheme=light&amp;show_faces=true&amp;stream=false&amp;header=false&amp;height=300" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:298px; height:300px;" allowTransparency="true"></iframe>').fadeIn();
					}
				},
				mbModule: {
					init: function(){
						this.moduleAjaxRequest();
					},
					moduleAjaxRequest :  function(){
						$.ajax({
							dataType: 'xml',
							url     : '/components/homepage/y2011/sny.mb.proxy.jsp',
							success : function(obj){
								bam.loadSync(bam.homePath + "bam.xml.js"); 
								var json = bam.xml.xmlToJSON(obj), $moduleSelector = $("#mb ul");
									$moduleSelector.empty();
								for (var x=0; x<4;x++){
									$moduleSelector.append('<li><a href="http://sny.prospero.com/n/pfx/forum.aspx?nav=messages&webtag=sny">'+json.entry[x].subject[0].Text+' -- '+json.entry[x].name[0].Text+'</a></li>');
								}
							},
								error : function(error){
									_log('error!');
								}
							});
					}
				}
			
			},
	
	_showNavMenu = function (){
		$(this).next().show();
	},
	
	_emailAFriend = function(id){
		popWin('/news/email/send_blog.jsp?blog_id='+id+'','email',500,470,'location=no,menubar=no,scrollbars=yes,status=yes,toolbar=no,resizable=yes');
	}
	
	_properties = {},
	$doc = $(document),
	
	_self = {
		init: function(){
			var page = bam.getParam()['page'];
			if (page === "undefined") page = 1;
			
			if (page === 1){
				$doc.bind("ready", function(e) {
					bam.tracking.track({
						async:{
							isDynamic:true,
							compName:'SNY Homepage',
							compActivity:'Homepage 1: index.jsp',
							actionGen:true
						}
					}, $(this)[0]);
				});	
			} else if (page === 2){
			
				$doc.bind("ready", function(e) {
					bam.tracking.track({
						async:{
							isDynamic:true,
							compName:'SNY Homepage',
							compActivity:'Homepage 2: index.jsp',
							actionGen:true
						}
					}, $(this)[0]);
				});				
			
			} else if (page === 3){
				$doc.bind("ready", function(e) {
					bam.tracking.track({
						async:{
							isDynamic:true,
							compName:'SNY Homepage',
							compActivity:'Homepage 3: index.jsp',
							actionGen:true
						}
					}, $(this)[0]);
				});
			}
			
			
			_headlinesModule.init();
			_shopModule.init();
			_programModule.init();
			_fanzoneModule.init();
			_connectModule.init();
			var $connectLi = $("#connect-with-sny ol li");
			
			//Shop module event
			$("#teamshop ol li").live('click', function() { 
				_shopModule.showTabs();
				$(this).addClass('active');
				$(".tabbed-container", this).show();
				
			});
	
			//Program module event
			$("#program ol li").live('click', function() { 
				_programModule.showTabs();
				$(this).addClass('active');
				$(".tabbed-container", this).show();
				
			});
			
			//HEADLINES MODULE EVENT
			$("#teamheadlines.tabbed-module ol li").live('click', function() { 
				var whichID = $(this).attr("id");
				_headlinesModule.getTeamAndSport(whichID);
				_headlinesModule.moduleAjaxRequest();
				_headlinesModule.showTabs();
				$(this).addClass('active');
				$(".tabbed-container", this).show();
			});
			
			//CONNECT EVENTS
			
			$("#mb h4").bind('click', function() { 
				/*
				$connectLi.removeClass('active');
				$("#connect-with-sny.tabbed-module.tabbed-module ol li div").hide();
				_connectModule.mbModule.moduleAjaxRequest();
				$(this).parent().addClass('active');
				$("#see-more a").attr("href","http://sny.prospero.com/n/pfx/forum.aspx?webtag=snytv");
				$("#mb div").show();
				*/
			});
			
			$("#tw h4").bind('click', function() { 
				$connectLi.removeClass('active');
				$("#connect-with-sny.tabbed-module ol li div").hide();
				_connectModule.twModule.init();
				$(this).parent().addClass('active');
				$("#see-more a").attr("href","http://www.twitter.com/snytv");
				$("#tw div").show();
			});
			
			$("#fb h4").bind('click', function() {
				$connectLi.removeClass('active');
				_connectModule.fbModule.init();
				$(this).parent().addClass('active');
				$("#see-more a").attr("href","http://www.facebook.com/SNY");
			});
			
			$(".email").bind('click', function(){
				var id = $(this).attr("id");
				_emailAFriend(id);
			});
			
			
			//SEARCHBOX FOCUSIN EVENT
			$("#sbi").focusin(function(){
				_clearSearchBox();	
			});
			
			//SEARCHBOX FOCUSOUT EVENT
			$("#sbi").focusout(function(){
				//_restoreSearchBox();	
			});
			
			//NAV HOVER EVENT
			$(".nav li").hover(
			  function () {
			  	$(".nav li").removeClass("hover")
			    $(this).addClass("hover");
				$('ul', this).show();
			  }, 
			  function () {
			    $(this).removeClass("hover");
				$('ul', this).hide();
			  }
			);

		}
	}
	
	return _self;
})(jQuery);

bam.getParam = function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}



$(function(){
	bam.snyhome.init();
});
