//alert("invoked");
(function($){
	
var xmlPath_L1 =  '/'+ 'Global' + '/' +'xml' + '/' + 'API_config.xml';
var env ='';
var envapi ='';
var dateRE = /^(\d{2})[\/\- ](\d{2})[\/\- ](\d{4})/;
var Athletes = new Array();
var teams = new Array();
var AthletesTeams = new Array();
var AthletesTeamsst = new Array();
var url = '';
var region = jQuery('#hiddenlocale').html();

var currentURL = window.location.toString().split("/");
var add = currentURL[2];
webURL = window.location.href ;
x = webURL.split("/");
serverName = x[2];



		
$.ajaxSetup({
			type: 'GET',
			dataType: 'xml',
			async : false
		});//end $ajaxSetup
		
		

$.extend({
	gsm_news_api : {

		opts : {},
			default_paths : {
				base: 'http://10.36.140.148/HL.Sports.Service/Service.svc/GSM?'
				
				},
		
		/*=========== Utility Methods ==========*/				

			fetch : function(params){
				var _data;
				var path = '';
				
				if(params == 'Events')
				{
				path = '/'+ region +'/xml/'+'Events.xml';
				}
				if((params == 'Athlete') || (params == 'ATHLETE'))
				{
				path = '/'+ region +'/xml/'+'GSM_Athletes.xml';
				}

				if((params == 'team') || (params == 'TEAM'))
				{
				path = '/'+ region +'/xml/'+'GSM_Teams.xml';
				}
				
				
				
				$.ajax({
				  url: path,				  
				  type: 'GET',
				  async: false,
				  dataType: 'xml',
				
				  success: function(data, textStatus, xhr) { 
					 // alert("success");
				    _data = data;
				  },
				  error: function(xhr, textStatus, errorThrown) {
				    _data - errorThrown;
				  }
				});
				return _data;
			},
		
		
		
		
		
		explicit : function(params, context){ 
	
				var _ArrTmp = []; 				
				$.each(params, function(k,v){
					_ArrTmp.push(k+'='+v);
				});
				
				var _outTmp = {};
				var _outTmps = {};
				var name = params.name;
				var count = params.count;
				
				if (params.type === 'Events')
				{
					//alert("In If");
					params_str = params.type;
					url = '/'+ region +'/xml/'+'Events.xml';
					//alert(url);
						
						
				if(params.name === undefined)
					{


				$.ajax({
				  url: url,				  
				  type: 'GET',
				  async: false,
				  dataType: 'xml',
				
				  success: function(data) { 
					procXMLEN(data);
				    
				  },
				  error: function(xhr, textStatus, errorThrown) {
				    _data - errorThrown;
					 alert("errorThrown");
				  }
				});


					
					/*$($.gsm_news_api.fetch(params_str)).find('item').each(function(idx){

						if(idx<params.count)
						{
						_outTmp[idx] = {};
						_outTmp[idx].news_id	=	$(this).find('ID').text();
						_outTmp[idx].title		=	$(this).find('title').text();
						_outTmp[idx].excerpt	=	$(this).find('short_story').text();						
						_outTmp[idx].published  =	$(this).find('posteddate').text();
						_outTmp[idx].link		=	$(this).find('href').text();	
						
						}
					})//end each*/

					}
									

					if(name =="all")
					{
						$.ajax({
				  url: url,				  
				  type: 'GET',
				  async: false,
				  dataType: 'xml',
				
				  success: function(data) { 
					procXMLEA(data);
				    
				  },
				  error: function(xhr, textStatus, errorThrown) {
				    _data - errorThrown;
					 alert("errorThrown");
				  }
				});

					/*$($.gsm_news_api.fetch(params_str)).find('item').each(function(idx){

						
						_outTmp[idx] = {};
						var newsid = $(this).find('ID').text();
						_outTmp[idx].news_id	=	$(this).find('ID').text();
						_outTmp[idx].title		=	$(this).find('title').text();
						_outTmp[idx].excerpt	=	$(this).find('short_story').text();						
						_outTmp[idx].published  =	$(this).find('posteddate').text();
						_outTmp[idx].link		=	$(this).find('href').text();	
						
						
					})//end each*/

					}

					
	 $.gsm_news_api.template(context, params.type, _outTmp);

				}

				else
			{




				if ((params.type == 'ATHLETE') && (params.name!= 'all'))
					{
							params_str = params.type;
							gsmid = params.name;

					strPath = '/'+ region +'/Articles/athlete_';

					$($.gsm_news_api.fetch(params_str)).find('NewsArticle').each(function(idx){
							//_outTmps[idx] = {};

							var newsid = $(this).find('people_id').text();
							if(gsmid == newsid)
						{
								
						_outTmps[idx] = {};
						_outTmps[idx].news_id	=	$(this).find('news_id').text();
						_outTmps[idx].title		=	$(this).find('title').text();
						_outTmps[idx].published  =	$(this).find('pubDate').text();
						//alert(_outTmps[idx].published);
						_outTmps[idx].updated	= 	$(this).find('last_updated').text();
						_outTmps[idx].news_href	= 	$(this).find('news_href').text();
						
						}
						
						
					})//end each*/

				$.gsm_news_api.template(context, params.type, _outTmps);						

						
	

					
						
					}


				
					
					
				if ((params.type == 'Athlete') && (params.name == 'all'))
					{
					params_str = params.type;

					strPath = '/'+ region +'/Articles/athlete_';

					$($.gsm_news_api.fetch(params_str)).find('NewsArticle').each(function(idx){

						_outTmp[idx] = {};
						_outTmp[idx].news_id	=	$(this).find('news_id').text();
						_outTmp[idx].title		=	$(this).find('title').text();
						_outTmp[idx].published  =	$(this).find('pubDate').text();
						_outTmp[idx].updated	= 	$(this).find('last_updated').text();
						_outTmp[idx].news_href	= 	$(this).find('news_href').text();
						
						
					})//end each*/

				
			
						
	

					$.gsm_news_api.template(context, params.type, _outTmp);						
							
						
					}

					if ((params.type == 'ATHLETE') && (params.name == 'all'))
					{
					params_str = params.type;

					strPath = '/'+ region +'/Articles/athlete_';

					$($.gsm_news_api.fetch(params_str)).find('NewsArticle').each(function(idx){

						_outTmp[idx] = {};
						_outTmp[idx].news_id	=	$(this).find('news_id').text();
						_outTmp[idx].title		=	$(this).find('title').text();
						_outTmp[idx].published  =	$(this).find('pubDate').text();
						_outTmp[idx].updated	= 	$(this).find('last_updated').text();
						_outTmp[idx].news_href	= 	$(this).find('news_href').text();
						
						
					})//end each*/

				
			
						
	

					$.gsm_news_api.template(context, params.type, _outTmp);						
							
						
					}




					if ((params.type == 'TEAM') && (params.name!= 'all'))
					{
							params_str = params.type;
							gsmid = params.name;

					strPath = '/'+ region +'/Articles/team_';

					$($.gsm_news_api.fetch(params_str)).find('NewsArticle').each(function(idx){
							//_outTmps[idx] = {};

							var newsid = $(this).find('team_id').text();
							if(gsmid == newsid)
						{
								//alert(gsmid);
								
						_outTmps[idx] = {};
						_outTmps[idx].news_id	=	$(this).find('news_id').text();
						_outTmps[idx].title		=	$(this).find('title').text();
						_outTmps[idx].published  =	$(this).find('pubDate').text();
						//alert(_outTmps[idx].published);
						_outTmps[idx].updated	= 	$(this).find('last_updated').text();
						_outTmps[idx].news_href	= 	$(this).find('news_href').text();
						
						}
						
						
					})//end each*/

				$.gsm_news_api.template(context, params.type, _outTmps);						

						
	

					
						
					}
			


	if ((params.type == 'team') && (params.name == 'all'))
					{
					params_str = params.type;

					strPath = '/'+ region +'/Articles/team_';

					$($.gsm_news_api.fetch(params_str)).find('NewsArticle').each(function(idx){

						_outTmp[idx] = {};
						_outTmp[idx].news_id	=	$(this).find('news_id').text();
						_outTmp[idx].title		=	$(this).find('title').text();
						_outTmp[idx].published  =	$(this).find('pubDate').text();
						_outTmp[idx].updated	= 	$(this).find('last_updated').text();	
						_outTmp[idx].news_href	= 	$(this).find('news_href').text();
						
						
						
					})//end each*/

				
			
	

					$.gsm_news_api.template(context, params.type, _outTmp);						
							
						
					}


						if ((params.type == 'TEAM') && (params.name == 'all'))
					{
					params_str = params.type;

					strPath = '/'+ region +'/Articles/team_';

					$($.gsm_news_api.fetch(params_str)).find('NewsArticle').each(function(idx){

						_outTmp[idx] = {};
						_outTmp[idx].news_id	=	$(this).find('news_id').text();
						_outTmp[idx].title		=	$(this).find('title').text();
						_outTmp[idx].published  =	$(this).find('pubDate').text();
						_outTmp[idx].updated	= 	$(this).find('last_updated').text();	
						_outTmp[idx].news_href	= 	$(this).find('news_href').text();
						
						
						
					})//end each*/

				
			
	

					$.gsm_news_api.template(context, params.type, _outTmp);						
							
						
					}
					
					
			
				
			
			}

	

	function procXMLEA(data){
						$(data).find('item').each(function(idx){
						
					_outTmp[idx] = {};
						var newsid = $(this).find('ID').text();
						_outTmp[idx].news_id	=	$(this).find('ID').text();
						_outTmp[idx].title		=	$(this).find('title').text();
						_outTmp[idx].excerpt	=	$(this).find('short_story').text();						
						_outTmp[idx].published  =	$(this).find('posteddate').text();
						_outTmp[idx].link		=	$(this).find('href').text();	
					
						
						
					})//end each
					}

	function procXMEN(data){
						$(data).find('item').each(function(idx){
						if(idx<count)
						{
						_outTmp[idx] = {};
						_outTmp[idx].news_id	=	$(this).find('ID').text();
						_outTmp[idx].title		=	$(this).find('title').text();
						_outTmp[idx].excerpt	=	$(this).find('short_story').text();						
						_outTmp[idx].published  =	$(this).find('posteddate').text();
						_outTmp[idx].link		=	$(this).find('href').text();	
						
						}
					})//end each*/
					
						
						
				
					}



	

 	},

both : function(){
				// Yeap I know, I know - Good luck to you. 
				// Hopefully they've learn to manage Project THere in the future
				
				var team_params = 'feed=news&type=team&name=all&count=20';
				var athlete_params = 'feed=news&type=athlete&name=all&count=20';

				var _athlete_all_obj = {};
				var _team_all_obj = {};
				
				
						
							var team_params_url = '/'+ region +'/xml/'+'GSM_Teams.xml';
							var athlete_params_url = '/'+ region +'/xml/'+'GSM_Athletes.xml';
							
							

				$.ajax({
				  url: team_params_url,				  
				  type: 'GET',
				  async: false,
				  dataType: 'xml',
				
				  success: function(data, textStatus, xhr) { 
					procGSMTeamsXML(data);
				    
				  },
				  error: function(xhr, textStatus, errorThrown) {
				    _data - errorThrown;
					 alert("errorThrown");
				  }
				});

				$.ajax({
				  url: athlete_params_url,				  
				  type: 'GET',
				  async: false,
				  dataType: 'xml',
				
				  success: function(data, textStatus, xhr) { 
					 procGSMAthlXML(data);
				   
				  },
				  error: function(xhr, textStatus, errorThrown) {
				    _data - errorThrown;
					 alert("errorThrown");
				  }
				});
				

					function procGSMTeamsXML(data){

						

					$(data).find('NewsArticle').each(function(idx){
					_team_all_obj[idx] = {};
					_team_all_obj[idx].news_id		=	$(this).find('news_id').text();
					_team_all_obj[idx].title		=	$(this).find('title').text();
					_team_all_obj[idx].excerpt		=	$(this).find('short_story').text();
					_team_all_obj[idx].content		=	$(this).find('description').text();
					_team_all_obj[idx].published  	=	$(this).find('pubDate').text();
					_team_all_obj[idx].updated		= 	$(this).find('last_updated').text();
					_team_all_obj[idx].sport		=	$(this).find('sport_type').text();
					_team_all_obj[idx].team_id		= 	$(this).find('team_id').text();
					_team_all_obj[idx].team_name	= 	$(this).find('team_name').text();
					_team_all_obj[idx].news_href	= 	$(this).find('news_href').text();

					
				})

					}


					function  procGSMAthlXML(data){
						
				$(data).find('NewsArticle').each(function(idx){
					_athlete_all_obj[idx] = {};
					
					_athlete_all_obj[idx].news_id	=	$(this).find('news_id').text();
					_athlete_all_obj[idx].title		=	$(this).find('title').text();
					_athlete_all_obj[idx].excerpt	=	$(this).find('short_story').text();
					_athlete_all_obj[idx].content	=	$(this).find('description').text();
					_athlete_all_obj[idx].published =	$(this).find('pubDate').text();
					_athlete_all_obj[idx].updated	= 	$(this).find('last_updated').text();
					_athlete_all_obj[idx].sport		=	$(this).find('sport_type').text();
					_athlete_all_obj[idx].team_id	= 	$(this).find('people_id').text();
					_athlete_all_obj[idx].team_name	= 	$(this).find('people_name').text();
					_athlete_all_obj[idx].news_href	= 	$(this).find('news_href').text();

					
				})


					}

				
				
				var _both_obj = {
					'athlete' : _athlete_all_obj,
					'team'  : _team_all_obj
				};
				
				
				//send out to build the HTML
				//@param: context, type, parsedXMLData				
				$.gsm_news_api.template('both', type= null, _both_obj);
			},

/*===========  HTML template method ==========*/
			
			template : function(context, type, obj){
				var _team_ul='', 
					_athlete_ul='',
					_events_ul='',
					_output='', 
					_output_both = {}, 
					itr = 0,
					itr2 = 0;
					
				function both_output(obj){					
					
					for (var i in obj.team) {
						var datet = obj.team[i].published.split('-');
						//alert(datet);
						var mmddyyt = datet[1]+'/'+datet[2]+'/'+datet[0];
						teams.push(mmddyyt + "|" + obj.team[i].news_id + "|" + obj.team[i].title + "|" + obj.team[i].news_href);	
					
					};	
					//teams.sort(mdyOrdD);	
					_team_ul += '<ul id="teams">';
					for (i=0;i<teams.length;i++)
				{
					
                  iP = new Array();
				  if(i<20){

					  
					  iP = teams[i].split("|");	
					  //alert(iP);
					  _team_ul += '<li'; (itr == 0 ) ? _team_ul += ' class="first">' : _team_ul += '>' ;
						_team_ul += '<a href="'+iP[3]+'">';
						_team_ul += '<p>'+iP[2]+'<span><i>'; 
						_team_ul += 'Posted On: '+iP[0]+'</i></span></p></a></li>';
						itr++;

				  }
					
				}
				_team_ul += '</ul>';

					

					
					for (var i in obj.athlete) {
						var datea = obj.athlete[i].published.split('-');
						var mmddyya = datea[1]+'/'+datea[2]+'/'+datea[0];
						Athletes.push(mmddyya + "|" + obj.athlete[i].news_id + "|" + obj.athlete[i].title + "|" + obj.athlete[i].news_href);	
						
					};				
					
					//Athletes.sort(mdyOrdD);						
						function mdyOrdD(a, b){
							a = a.replace(dateRE,"$3$1$2");
							b = b.replace(dateRE,"$3$1$2");
							return parseInt((a < b) ? 1 : ((a > b) ? -1 : 0));

							 }
				_athlete_ul += '<ul id="athletes">';
					for (i=0;i<Athletes.length;i++)
				{
					
                  iP = new Array();
				  if(i<20){

					  
					  iP = Athletes[i].split("|");	
					  //alert(iP);
					  _athlete_ul += '<li'; (itr2 == 0 ) ? _athlete_ul += ' class="first">' : _athlete_ul += '>' ;
						_athlete_ul += '<a href="'+iP[3]+'">';
						_athlete_ul += '<p>'+iP[2]+'<span><i>'; 
						_athlete_ul += 'Posted On: '+iP[0]+'</i></span></p></a></li>';
						itr2++;

				  }
					
				}
				_athlete_ul += '</ul>';


					
					_output_both['athlete'] = _athlete_ul;
					_output_both['team'] = _team_ul;
				};


	
								
				function module_output(obj){ 
					//alert("inside function");
				for (var i in obj) {
					
					var dateo = obj[i].published.split('-');
					//alert(dateo);
					var mmddyyo = dateo[1]+'/'+dateo[2]+'/'+dateo[0];
					AthletesTeams.push(mmddyyo + "|" + obj[i].news_id + "|" + obj[i].title +  "|" + obj[i].news_href);	

					};
					//AthletesTeams.sort(mdyOrdD);						
						function mdyOrdD(a, b){
							a = a.replace(dateRE,"$3$1$2");
							b = b.replace(dateRE,"$3$1$2");
							return parseInt((a < b) ? 1 : ((a > b) ? -1 : 0));



							 }
							_output += '<ul>';
					for (i=0;i<AthletesTeams.length;i++)
					{
						
						iP = new Array();
						 if(i<20){
						

							   iP = AthletesTeams[i].split("|");
							   _output += '<li>';
							   _output += '<a href="'+iP[3]+'">';
							   _output += '<p>'+iP[2]+'<span>Posted on '+iP[0]+'</span></p>';
							   _output += '</a>';
							   _output += '</li>';
						 }
							
					}
					_output += '</ul>';
					


					
				};


								function modulest_output(obj){ 
					//alert("inside function");
				for (var i in obj) {
					
					var dateo = obj[i].published.split('-');
					//alert(dateo);
					var mmddyyo = dateo[1]+'/'+dateo[2]+'/'+dateo[0];
					AthletesTeamsst.push(mmddyyo + "|" + obj[i].news_id + "|" + obj[i].title +  "|" + obj[i].news_href);	

					};
					//AthletesTeamsst.sort(mdyOrdD);						
						function mdyOrdD(a, b){
							a = a.replace(dateRE,"$3$1$2");
							b = b.replace(dateRE,"$3$1$2");
							return parseInt((a < b) ? 1 : ((a > b) ? -1 : 0));



							 }
							_output += '<ul>';
					for (i=0;i<AthletesTeamsst.length;i++)
					{
						
						iP = new Array();
						 if(i<20){
						

							   iP = AthletesTeamsst[i].split("|");
							   _output += '<li>';
							   _output += '<a href="'+iP[3]+'">';
							   _output += '<p>'+iP[2]+'<span>Posted on '+iP[0]+'</span></p>';
							   _output += '</a>';
							   _output += '</li>';
						 }
							
					}
					_output += '</ul>';
					


					
				};
				
				//Added for events
				function events_output(obj){
					//alert('in events template 2');
					_events_ul += '<ul id="events">';
					for (var i in obj) {
						var date = obj[i].published.split('-');
					
						_events_ul += '<li'; (itr2 == 0 ) ? _events_ul += ' class="first">' : _events_ul += '>' ;
						_events_ul += '<a href="'+obj[i].link+'">';
						_events_ul += '<p>'+obj[i].title+'<span>'; 
						_events_ul += 'Posted On: '+obj[i].published+'</span></p></a></li>';
						itr2++;
					};	
					_events_ul += '</ul>';
					
					
					//_output_both['athlete'] = _athlete_ul;
					//alert('output events:\n' + _events_ul);
				};
				

/*=========== template controls ==========*/

				switch(context){
					
						case 'both' : 
						both_output(obj); 
						$("#teams-wrap").html(_output_both['team']);
						$("#athletes-wrap").html(_output_both['athlete']);
						//$("#athletes-wrap").html(_output_both['team']);
					break;
					
					case 'module' : 
						module_output(obj); 
						$('.module-SideGSM-news > dd').html(_output);
						//$('#sideModuleAllNews').html(_output);
						//$(_DOM_elm).html(_output);
						
						//REMOVE FOR PRODUTION
						//$('#dev ul li p').css({'color': 'blue'});
					break;
					
					case 'strict' : 
						modulest_output(obj); 
						$('.newsfeeds > div').html(_output);
						
						//REMOVE FOR PRODUTION
						//$('#dev ul li p').css({'color': 'red'});
					break;
					
					case 'events' : 
						events_output(obj); 
						$('.module-SideGSM-news > dd').html(_events_ul);
						$("#events-wrap").html(_events_ul);
					break;

					case 'eventsstrict' : 
						events_output(obj); 
						$('.newsfeeds > div').html(_events_ul);						
						break;

					default : break;
				};//context switch
				
				//$.gsm_news_api.scroll_init('both');
				$.gsm_news_api.scroll_init(context);

			},//template Method

	/*=========== Init jScrollPane Dumbness  ==========*/			

			scroll_init : function(context){
				switch(context){
					case 'both' : 
						/*$('#module-feeds #bd ul#teams').jScrollPane({
						 	'scrollbarWidth': 18,
						 	'dragMinHeight': 18,
						 	'dragMaxHeight': 18,
						 	'topCapHeight' : 10,
						 	'bottomCapHeight': 10
					 	});*/
					break;
					case 'module' : 
						/*$('.sidebarModule dd ul').jScrollPane({
					 	'scrollbarWidth': 18,
					 	'dragMinHeight': 18,
					 	'dragMaxHeight': 18,
					 	'topCapHeight' : 10,
					 	'bottomCapHeight': 10
					 });*/
					break;
					case 'events' : 
						
					break;
					case 'eventsstrict' : 
						
					break;


					default : break;

				}
			
			}
	
	} // #gsm_news_api
});// #extend

})(jQuery);
