// ---------------------------------------------------------//
// GET/SET THE URL HASH
// ---------------------------------------------------------//
 
function hashString(key)
{ 
	var value = null;
	if(hashStringArr[key])
	{
		value=hashStringArr[key];
	}
	return value;
}

hashStringArr = new Array();

function hashString_Parse()
{
	var query = window.location.hash.substring(1);
	var pairs = query.split("&");
	var mainQuery =  window.location.search.substring(1);
	
	/* from # */
	for (var i=0;i<pairs.length;i++)
	{
		var pos = pairs[i].indexOf('=');
		if (pos >= 0)
		{
			var argname = pairs[i].substring(0,pos);
			var value = pairs[i].substring(pos+1);
			hashStringArr[argname]=value;
		}
	}
	/* from ? */
	var mPos = mainQuery.indexOf("=");
	if(mPos >= 0){
		var argname = mainQuery.substring(0,mPos);
		var value = mainQuery.substring(mPos+1);
		hashStringArr[argname]=value;
	}

}

/* Get the current query string */
hashString_Parse(); // Start the query string gathering
var urlStory = hashString("story"); // Get the storyID.
var urlContentId = hashString("contentId");

/* Set and keep the root url */
var topURL = window.location.href; // get the current url
if(topURL.indexOf("#") > -1){ // check to see if there is a ? with data
	var topURL = topURL.substring(0, topURL.indexOf("#")) //reset the url to the page url only
}

// ---------------------------------------------------------//
// BANNER REFRESHER (728X90) //
// ---------------------------------------------------------//

var bannerCheck = 0;  // set the initial variable for counting
function bannerRefresh(){
	if(bannerCheck != 0){
		var ban = document.getElementById('ad728x90');
		ban.src = ban.src;		
	}
	bannerCheck++;	
}



// ---------------------------------------------------------//
// SET PERMALINK AND ADDTHIS INFORMATION//
// ---------------------------------------------------------//
var fsAddthis_url; // add this url holder
var fsAddthis_title; // add this title holder

function setHash(loc, rowInfo){
	window.location.hash= loc; // set the new url
	document.title = rowInfo['story/title']+" : FoxSports.com Water Cooler"; // reset the HTML title bar	
	hashString_Parse(); // reset the parse...	
	
	if(document.location.port){ // check if there's a location port
		var locPort = ":"+document.location.port; // set the location port
	}
	
	fsAddthis_url = document.location.protocol+"//"+document.location.hostname+locPort+document.location.pathname+"?contentId="+urlContentId; // create the "Add This" url
	
	fsAddthis_title  = rowInfo['story/title']+" : FoxSports.com Water Cooler"; // set the title for the add this
	
}

// ---------------------------------------------------------//
// GET XML DATA //
// ---------------------------------------------------------//
var dsWc = new Spry.Data.XMLDataSet("/watercooler/xml?contentId="+urlContentId+"&type=body", "watercooler/content", {subPaths: ["story", "nextStory", "category" ], filterDataFunc:escapeMe});
var dsStoryExtras = new Spry.Data.XMLDataSet("wc_extras.xml", "storyExtras");
var dsHeadlines = new Spry.Data.NestedXMLDataSet(dsStoryExtras, "headlines", {useCache:true}); // Get the XML Data
var dsExclusives = new Spry.Data.NestedXMLDataSet(dsStoryExtras, "exclusives", {useCache:true}); // Get the XML Data
var dsPhotos = new Spry.Data.NestedXMLDataSet(dsStoryExtras, "photos", {useCache:true}); // Get the XML Data
var dsVideos = new Spry.Data.NestedXMLDataSet(dsStoryExtras, "videos", {useCache:true}); // Get the XML Data
fsHbxAutoRun = false; // bypass auto hbx call...  controlled by developer

/* set specail column types */
dsWc.setColumnType("story/storyText", "html");
dsHeadlines.setColumnType("copy", "html");


function escapeMe(ds, row, rowIndex){ // escape the url for titles with special characters
	row['story/url'] = escape(row['story/url']);// run the escape
	return row; // return the escaped value
}

// ---------------------------------------------------------//
// INITIAL PAGE LOAD SETUP //
// ---------------------------------------------------------//

if(urlStory){ // load up the requested image
	wcFlashLinkId = urlStory;
	dsWc.addObserver({ onPostLoad: function() {
		dsWc.removeObserver(this);
		getStory('ini',{goToStory:urlStory});
		
	}});
}


/* setup for the initial photos to load */
Spry.Data.Region.addObserver("wc_copy", { onPostUpdate: function(){
	Spry.Data.Region.removeObserver("wc_copy", this); // remove the observer so it only runs once
		loadAds(); // initialize the advertisements	
		if(fsHbxAutoRun == false){ // only run when auto run is off.			
			fsHbxPageName = dsWc.getCurrentRow()['category/categoryName']+" : "+dsWc.getCurrentRow()['story/title']+" : FoxSports.com Watercoolers"; // set hitbox page name
			fsHbxTrackingPath = _hbxStrip("/"+dsWc.getCurrentRow()['category/categoryName'].toLowerCase()+"/"+pageType+"/"+wcTitle+"/"+dsWc.getCurrentRow()['@contentId']+" - "+dsWc.getCurrentRow()['story/title']); // set hitbox tracking path
			launchFsHbx(); // launch hitbox
		}	
}});

// ---------------------------------------------------------//
// LOAD STORY //
// ---------------------------------------------------------//
function getStory(me, options){
	var options = options || {};
	var goToStory  = options.goToStory || 0;	
	var rowMea;
	var media;
		
	rowMea = dsWc.findRowsWithColumnValues({"@contentId": goToStory}, true, true); // search for the requested 
	
	if(rowMea){		
		dsWc.setCurrentRow(rowMea.ds_RowID); // go to the selected story		
	}else{
		dsWc.setCurrentRow(0); // go to the selected story	
		rowMea = dsWc.getCurrentRow();
	}
	
	if(me != 'ini'){ // check for initial load
		Spry.Utils.loadURL("GET", "/fe/xml/photo_gallery_pageview_candidate.xml?comscorekw=pageview_candidate&title="+dsWc.getCurrentRow()['story/title']+"&ran="+Math.round((Math.random() * 10000) + 1), true); // call comscore				
		Spry.Utils.loadURL("GET", "/fe/html/photo_gallery/random.html", true); // call neilson	
		_hbPageView(dsWc.getCurrentRow()['category/categoryName']+" : "+dsWc.getCurrentRow()['story/title']+" : FoxSports.com Watercoolers","/"+_hbxStrip(dsWc.getCurrentRow()['category/categoryName'].toLowerCase()+"/"+pageType+"/"+wcTitle+"/"+dsWc.getCurrentRow()['@contentId']+" - "+dsWc.getCurrentRow()['story/title'])); // hitbox call 
		serveAdRefreshViaMSFT(); // refresh the ads...
	}
	setHash("story="+rowMea['@contentId'], rowMea); // set the url information
	
	/* set flash */
	wcFlashLinkId = rowMea['@contentId'];
	wcFlashTitle = rowMea['story/title'];	
	
}

// ---------------------------------------------------------//
// ADVERTISEMENT REFRESH CODE //
// ---------------------------------------------------------//
function serveAdRefreshViaMSFT() {	
		dapMgr.trackEvent(eventType.click);	
}

function loadAds(){ /* Load Adds */	
	dapMgr.enableACB('ad300x250box',false); // enable advertisement
	dapMgr.renderAd('ad300x250box', adCode300x250, 300, 250); // render advertisement
	dapMgr.enableACB('ad728x90box',false); // enable advertisement
	dapMgr.renderAd('ad728x90box', adCode728x90, 728, 90);	 // render advertisement	
}



// ---------------------------------------------------------//
// THUMBNAIL NAVIGATION //
// ---------------------------------------------------------//
function thumbNav(me, options){	
	var options = options || {};
	var direction = options.direction || null; // capture the selected direction
	var media_slider = options.media || null;
	
	var currentPanelIndex = media_slider.getContentPanelIndex(media_slider.getCurrentPanel()); // get the index of the current panel
		
	switch(direction){ // for directions
	case 'previous':  // previous	
		if(currentPanelIndex == 0){
			media_slider.showLastPanel();	
		}else{
			media_slider.showPreviousPanel();	
		}		
		break;
	case 'next': // next		
		if((currentPanelIndex+1) == media_slider.getContentPanelsCount()){
			media_slider.showFirstPanel();	
		}else{
			media_slider.showNextPanel();
		}	
		
		break;		
	}
	
}
