var SeriesKey = 608;
var BaseSite = "http://" + document.location.host;
var BaseSiteDir = "vic";

$(document).ready(function() {
	$("#content-feature-boxes>br").remove(); /* remove line breaks added between CMS-driven feature boxes */
	$("#content-feature-boxes .feature-box:nth-child(2n)").addClass("alt");
	$("#feature-boxes>br").remove();  /* remove line breaks added between CMS-driven feature boxes */
	$(".news-list:nth-child(2n)").addClass("alt");
	
	if(PageIncludesFeatureNews())
		PopulateFeatureNews();
		
	if(PageIncludesAssocNews())
		PopulateAssocNews();
});

function PageIncludesFeatureNews()
{
	$news_features_feed = $("#news-list-feature");
	if($news_features_feed.length > 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function PopulateFeatureNews()
{
	$.ajax({
		url: BaseSite + '/site/baseball/vic/include/featurephotoflash.xml',
		dataType: 'xml', // 'xml' passes it through the browser's xml parser
		complete: function( xhr, status )
		{
		  //alert( "COMPLETE.  You got:\n\n" + xhr.responseText ) ;
		  if( status == 'parsererror' )
		  {
			xmlDoc = null;
	
			// Create the xml document from the responseText string.
			// This uses the w3schools method.
			// see also
			if( window.DOMParser )
			{
			  parser=new DOMParser();
			  xmlDoc=parser.parseFromString( xhr.responseText,"text/xml" ) ;
			}
			else // Internet Explorer
			{
			  xmlDoc=new ActiveXObject( "Microsoft.XMLDOM" ) ;
			  xmlDoc.async = "false" ;
			  xmlDoc.loadXML( xhr.responseText ) ;
			}
	
			$( '#response' ).append( '<p>complete event/xmlDoc: ' + xmlDoc + '</p>' ) ;
			$( '#response' ).append( '<p>complete event/status: ' + status + '</p>' ) ;
	
			processFeatureNewsXMLDoc( xmlDoc ) ;
		  }
		},
		success: function(data) {		
		
			// The SUCCESS EVENT means that the xml document
			// came down from the server AND got parsed successfully
			// using the browser's own xml parsing caps.
			
			processFeatureNewsXMLDoc( data );
			
			// IE gets very upset when
			// the mime-type of the document that
			// gets passed down isn't text/xml.
			
			// If you are missing the text/xml header
			// apparently the xml parse fails,
			// and in IE you don't get to execute this function AT ALL.
		}
	});
}

function processFeatureNewsXMLDoc(data)
{
	// headline article
	$headlineArticle1 = $(data).find("item").eq(0);
	$main_news_1 = $("#news-list-feature-main");
	
	// first headline
	if($headlineArticle1.length > 0 && $main_news_1.length > 0)
	{
		$headlineArticle1URL = $headlineArticle1.find("link").text();
		$main_news_1.find("h2").html("<a href='" + $headlineArticle1URL + "' title=''>" + $headlineArticle1.find("title").text() + "</a>");
		$abstractSection = $main_news_1.find(".abstract");
		$abstractText = $headlineArticle1.find("description").text();
		$abstractSection.text($abstractText.substr(0, 200) + ($abstractText.length > 200 ? "..." : ""));
		$abstractSection.append(' <a class="readmore" href="' + $headlineArticle1URL + '">Read more</a>');

		$articleImage = $main_news_1.find(".image");	
		if($headlineArticle1.find("enclosure[type='image/jpeg']").length > 0)
			$articleImage.append('<img src="' + $headlineArticle1.find("enclosure[type='image/jpeg']").first().attr("url").replace("/fullsize", "/thumbnail") + '" alt="" />'); /* try to grab thumbnail if it's a fullsize image from library */

		$main_news_1.show();
	}
	
	// populate other news items
	$other_news_items = $("#news-list-feature-others li");
	if($other_news_items.length > 0)
	{
		var numNewsToShow = 4; /* including main article(s) */
		var numNewsShown = 0;
		var count = 0;
		$otherArticles = $(data).find("item").each(function() {
			if(numNewsShown >= numNewsToShow)
				return false; /* break the loop */
	
			numNewsShown++;
			
			/* skip main article(s) */
			if(numNewsShown <= 1)
				return; /* continue */

			$other_news_item = $other_news_items.eq(count);
	
			$title = $(this).find("title").text();
			$link = $(this).find("link").text();
			$desc = $(this).find("description").text();
			
			$other_news_item.find("h2").html('<a href="' + $link + '">' + $title + "</a>");
			
			$abstractSection = $other_news_item.find(".abstract");
			$abstractSection.text($desc.substr(0, 200) + ($desc.length > 200 ? "..." : ""));
			$abstractSection.append(' <a class="readmore" href="' + $link + '">Read more</a>');
			
			$articleImage = $other_news_item.find(".image");	
			if($(this).find("enclosure[type='image/jpeg']").length > 0)
				$articleImage.append('<img src="' + $(this).find("enclosure[type='image/jpeg']").first().attr("url").replace("/fullsize", "/thumbnail") + '" alt="" />'); /* try to grab thumbnail if it's a fullsize image from library */

			count++;
		});
		if(numNewsShown >= 1)
			$("#other_news_items").show();
		else
			$("#other_news_items").hide();
	}
}


function PageIncludesAssocNews()
{
	$news_association = $("#news-association");
	if($news_association.length > 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

/* PopulateAssocNews
   -----------------
   Manually calls on the latest news XML from each league
   and association, and for each one will grab 1 or 2 articles (depending on configuration)
*/
function PopulateAssocNews()
{
	GetAssocNews('/site/baseball/vicsummerleague/include/latestnewsRSS.xml', 'BVSL', 2);
	GetAssocNews('/site/baseball/ballarat/include/latestnewsRSS.xml', 'Ballarat', 1);
	GetAssocNews('/site/baseball/bendigo/include/latestnewsRSS.xml', 'Bendigo', 1);
	GetAssocNews('/site/baseball/geelongassociation/include/latestnewsRSS.xml', 'GBA', 2);
	GetAssocNews('/site/baseball/latrobevalley/include/latestnewsRSS.xml', 'LVBA', 1);
	GetAssocNews('/site/baseball/vwbl/include/latestnewsRSS.xml', 'MWBL', 2);
	GetAssocNews('/site/baseball/sunraysia/include/latestnewsRSS.xml', 'SBL', 1);
}

function GetAssocNews(URL, AssocAbbreviation, NumberArticles)
{
	$.ajax({
		url: BaseSite + URL,
		dataType: 'xml', // 'xml' passes it through the browser's xml parser
		complete: function( xhr, status )
		{
		  //alert( "COMPLETE.  You got:\n\n" + xhr.responseText ) ;
		  if( status == 'parsererror' )
		  {
			xmlDoc = null;
	
			// Create the xml document from the responseText string.
			// This uses the w3schools method.
			// see also
			if( window.DOMParser )
			{
			  parser=new DOMParser();
			  xmlDoc=parser.parseFromString( xhr.responseText,"text/xml" ) ;
			}
			else // Internet Explorer
			{
			  xmlDoc=new ActiveXObject( "Microsoft.XMLDOM" ) ;
			  xmlDoc.async = "false" ;
			  xmlDoc.loadXML( xhr.responseText ) ;
			}
	
			$( '#response' ).append( '<p>complete event/xmlDoc: ' + xmlDoc + '</p>' ) ;
			$( '#response' ).append( '<p>complete event/status: ' + status + '</p>' ) ;
	
			processAssocXMLDoc( xmlDoc, AssocAbbreviation, NumberArticles ) ;
		  }
		},
		success: function(data) {		
		
			// The SUCCESS EVENT means that the xml document
			// came down from the server AND got parsed successfully
			// using the browser's own xml parsing caps.
			
			processAssocXMLDoc( data, AssocAbbreviation, NumberArticles );
			
			// IE gets very upset when
			// the mime-type of the document that
			// gets passed down isn't text/xml.
			
			// If you are missing the text/xml header
			// apparently the xml parse fails,
			// and in IE you don't get to execute this function AT ALL.
		}
	});
}

function processAssocXMLDoc(data, AssocAbbreviation, NumberArticles)
{
	for(i = 0; i < NumberArticles; i++)
	{
		$article = $(data).find("item").eq(i);

		if($article.length == 0)
			return;	/* on more articles */

		// get the div and it's contained list.  If there is no list yet, create it
		$assoc_news_div = $("#news-association");
		if($assoc_news_div.length == 0)
			return;	// no div to populate
		$assoc_news_list = $("#news-association>ul");
		if($assoc_news_list.length == 0)
		{
			$assoc_news_div.append("<ul />");
			$assoc_news_list = $("#news-association>ul");
		}
		
		// add an item for this article
		$articleURL = $article.find("link").text();
		$assoc_news_list.append("<li class='newslist'><a class='newslist' href='" + $articleURL + "' title=''>(" + AssocAbbreviation + ") " + $article.find("title").text() + "</a></li>");		
	}
}

