/*
 *
 *  stories javascript
 *
 *  REQUIRES:
 *
 *   JQuery
 *   Template library (and helper)
 *   
 */
 
 
var LandingPage = {};
LandingPage = {	
	divScrollable: '#districtStories',
	divScrollableItems: '#districtStories :div.items',
	apiScrollable: '',
	
	cachedTemplate: '',
	cachedTemplateFull: '',
	
	serviceCache: {},
	
	StoryTemplateFile: "/gerrysite/templates/landingstories.jst",
	StoryTemplateFileFull: "/gerrysite/templates/landingstories_full.jst",	
	
	StoryDataFile: "/gerrysite/js/districtstories.js",
	OnlyFeatured: true,
	
	endKey: ''
};
 
 
$(document).ready(function(){

	toggleLoading(true);
	//loadScrollable();

	//preload and cache the 'partial story' template.
	getTemplateAsync(LandingPage.StoryTemplateFile, function(template) {
		LandingPage.cachedTemplate = template;
	});

	//preload and cache the 'full story' template.
	getTemplateAsync(LandingPage.StoryTemplateFileFull, function(template) {
		LandingPage.cachedTemplateFull = template;
	});
	
	$.getJSON(LandingPage.StoryDataFile, function(stories) {
		//stream in the stories
		chainLoader(0, stories);
	});
	
	/* precache the mapping libraries */
	setTimeout("preCacheMapping();", 1250);
});

	function preCacheMapping()
	{
		$.getScript("http://cdn.avencia.com/lib/openlayers/OpenLayers_9492_RTN.min.js", function() {});	
		$.getScript("http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.1", function() {});	
		$.getScript("/gerrysite/js/SimplePolygon.js", function() {});
	}


	function loadScrollable()
	{
		$(LandingPage.divScrollable).scrollable({
			clickable: false,
			size: 1 
		}).mousewheel().navigator();
	}
	
	function toggleLoading(shown)
	{
		var loadingLogo = "images/loading1.gif";
		var normalLogo = "/gerrysite/img/cicero_logo.png";
		var shownLogo = (shown) ? loadingLogo : normalLogo;
		$("#cicerologo").children('img').attr('src', shownLogo);
	}

	//retrieves all story data, in order, and appends them to the page as they are loaded.
	function chainLoader(idx, data)
	{	
		if ((data == null) || (idx >= data.stories.length))
		{
			loadScrollable();
			//LandingPage.apiScrollable.reload().end();
			
			toggleLoading(false);
			return;
		}
	
		var story = data.stories[idx];
		story.index = idx + 1;
		
		var isFeatured = (story.Featured || (!LandingPage.OnlyFeatured));
		var cacheKey = story.DistrictID + story.DistrictType + story.State;
		var cachedInfo = LandingPage.serviceCache[cacheKey];		
		if (cachedInfo != null)
		{
			if (isFeatured)
				renderStory(LandingPage.cachedTemplate, story, cachedInfo);
		}
		else if (data.stories.length > idx)
		{
			var options = {
				DistrictID: story.DistrictID,
				DistrictType: story.DistrictType,
				City: story.City,
				State: story.State,
				Width: story.FWidth,
				Height: story.FHeight
			};
		
			if (!isFeatured)
			{	
				chainLoader(idx + 1, data);
				return;
			}			
			
			callGetDistrictSummary(options, function(response) {
				LandingPage.serviceCache[cacheKey] = response;
				renderStory(LandingPage.cachedTemplate, story, response);
				
				//start on the next one.
				chainLoader(idx + 1, data);
			});
			return;
		}
	}
	
	function renderStory(template, story, districtinfo)
	{
		if ((story == null) || (story == undefined))
		{			
			return;
		}
		
		story.Officials = districtinfo.Officials;
		story.ImageUrl = districtinfo.ImageUrl;
		story.ImageRatio = districtinfo.ImageRatio;
	
	
		var singleContainer = { stories: [ story ] };
		var newhtml = template.process(singleContainer);
		$(LandingPage.divScrollableItems).append(newhtml);

		var controlShowID = '#' + story.DistrictID.replace(/ /g, '') + story.State + "show";		
		$(controlShowID).overlay({   
			onBeforeLoad: function() { 
				newhtml = LandingPage.cachedTemplateFull.process(singleContainer);				
				$("#overlay #wrap").html(newhtml);
			}
		});
		
		
	}

	function makeURL(story)
	{
		theURL = "search.aspx?type="+ escape(story.DistrictType) + "&id=" + escape(story.DistrictID) + "&state=" + escape(story.State);
		if (story.City != undefined)
		{
			theURL += "&city=" + escape(story.City);
		}
		return theURL;
	}

	function left(str, n){
		if (n <= 0)
			return "";
		else if (n > String(str).length)
			return str;
		else
			return String(str).substring(0,n);
	}
