var v1TopStories = {
	//location of HTML files to be loaded into the content div
	contentUrls: {
		topStories: "/assets/includes/topstories_videos/topstories.php",
		mostPopular: "/assets/includes/topstories_videos/mostpopular.php",
		latestVideos: "/assets/includes/topstories_videos/latestvideos.php"
	},
	//location of RSS files associated with each view (used to update href in the RSS link)
	//if there is no RSS feed for a view, set the value to an empty string
	rssUrls: {
		topStories: "/index-pics-{region}.xml",
		mostPopular: "",
		latestVideos: "/video/videos.xml"
	},
	oldHtml: '', //container for the current content - reverts to this on error
	loadingHtml: '<div class="v1-ajax-loading" style="height:{height}px;"></div>',
	errorMessage: 'Sorry...\nWe\'re experiencing technical difficulties. Please try again later.',
	
	//for video player/thumbnail behaviours
	curThumb: -1,
	
	loadContent: function(contentId,bricCategory,videoString,storyType) {
		v1TopStories.newContentId = contentId; //store this for RSS update
		
		//valid contentId's are the property names of contentUrls
		v1TopStories.showLoading();
		$.ajax({
			url: v1TopStories.contentUrls[contentId]+"?cat_uri="+bricCategory+"&vid_path="+videoString+"&story_type="+storyType,
			cache: false,
			success: v1TopStories.onLoad,
			error: v1TopStories.onLoadError
		});
	},
	
	showLoading: function(){
		var curContentDiv = $("div.v1TopStoriesWidget div.contentHolder");
		v1TopStories.oldHtml = curContentDiv.html();
		
		var contentHeight = curContentDiv.height();
		curContentDiv.attr("style", "height:"+contentHeight+"px");
		var curLoadingHtml = v1TopStories.loadingHtml.replace("{height}", contentHeight);
		curContentDiv.html(curLoadingHtml);
	},
	
	onLoad: function(responseHtml){
		v1TopStories.curThumb = -1;
		
		var curContentDiv = $("div.v1TopStoriesWidget div.contentHolder");
		curContentDiv.html(responseHtml);
		curContentDiv.attr("style", "");
		
		v1TopStories.updateRssLink();
	},
	
	insertRegion: function(_str){
		var userRegion = $.cookies.get("sn_region");
		//alert('userRegion: ' + userRegion);
		userRegion = (userRegion != null) ? userRegion.toLowerCase() : "ontario";
		
		_str = _str.replace("{region}", userRegion);
		return _str;
	},
	
	updateRssLink: function(){
		var rssLink = $("div.v1TopStoriesWidget a.rssLink");
		var newRssHref = v1TopStories.rssUrls[v1TopStories.newContentId];
		
		if (newRssHref == "" || typeof(newRssHref) == "undefined") {
			rssLink.hide();
		} else {
			newRssHref = v1TopStories.insertRegion(newRssHref);
			rssLink.attr('href', newRssHref);
			rssLink.show();
		}
	},
	
	onLoadError: function(xhr, status, exception){
		//alert("error; status: " + status + "; exception: " + exception);
		alert(v1TopStories.errorMessage);
		$("div.v1TopStoriesWidget div.contentHolder").html(v1TopStories.oldHtml);
	},
	
	updateCurThumb: function(thumbnail){
		if (v1TopStories.curThumb == -1) {
			v1TopStories.curThumb = $($("ul.v1-video-thumbnails a")[0]); //first() is in v1.4
		}
		
		v1TopStories.curThumb.find("div.available").show();
		v1TopStories.curThumb.find("div.current").hide();
		
		v1TopStories.curThumb = $(thumbnail);
		v1TopStories.curThumb.find("div.available").hide();
		v1TopStories.curThumb.find("div.current").show();
	},
	
	onThumbnailClick: function(thumbnail, playerId) {
		//have to use a try/catch since IE's typeof returns "object" instead of "undefined"
		try {
			var player = window["bc-" + playerId];
			var playerModule = player.getModule(APIModules.VIDEO_PLAYER);
			
			var curVideoId = playerModule.getCurrentVideo().id;
			
			if (curVideoId != thumbnail.id) {
				v1TopStories.updateCurThumb(thumbnail);
				playerModule.loadVideo(thumbnail.id);
			}
		} catch (e) {
			alert("Sorry - the video player is still loading. Please try again in a few seconds.");
		}
	}
};

