/* START: /wp-srv/js/story/thisItem.js */

/* import statements */
// document.write('<s\cript src="http://www.washingtonpost.com/wp-srv/js/utilsStatic.js"></s\cript>')
// document.write('<s\cript src="http://www.washingtonpost.com/wp-srv/js/story/storyUtils.js"></s\cript>')

// legal nav style prefixes
var story_prefixes = ['story-navigation-','story-navigation-top-','story-navigation-vertical-','story-navigation-media-vertical-','story-navigation-bottom-','story-relatedlinks-','story-media350-'];
var stories = [] ;
var thisItem = {
	id : "",
	stories : [],
	time_of_creation : 0,
	story_id_that_created_this : "",
	story_active : "",
	story_default : "",
	story_override : "",
	allow_comments : "",
	init : function(id,stories,time_of_creation,story_id_that_created_this) {
		this.id = id ;
		this.stories = this.scrubStoriesList(stories) ;
		this.time_of_creation = time_of_creation ;
		this.story_id_that_created_this = story_id_that_created_this ;
		this.setStoryDefault() ;
		this.setStoryOverride() ;
		this.setStoryActive() ;
		this.setAllowComments() ;
		if ( this.story_active ) {
			this.hideComments() ;
			this.showActiveItem() ;
			this.showStoryNavigation() ;
		}
	},
	setAllowComments : function() {
		this.allow_comments = true ;
		if ( typeof wp_article != "undefined" && typeof wp_article.allow_comments != "undefined" && wp_article.allow_comments == "no" || typeof COMMENTS_ALLOWED == "undefined" || !COMMENTS_ALLOWED ) {
			this.allow_comments = false ;
		}
	},
	hideComments : function() {
		if (! this.allow_comments ) {
			replaceClassName( 'story-comments-wrapper-top-'+this.story_active.id,'show','hide' );
			replaceClassName( 'story-comments-wrapper-bottom-'+this.story_active.id,'show','hide' );
		} else {
			replaceClassName( 'story-comments-wrapper-top-'+this.story_active.id,'hide','show' );
			replaceClassName( 'story-comments-wrapper-bottom-'+this.story_active.id,'hide','show' );
		}
	},
	scrubStoriesList : function(stories) {
		// removes stories from the list that contain only one item
		var cleansed_stories = new Array() ;
		for (var i=0; i<stories.length; i++) {
			if ( stories[i].items.length > 1 ) {
				cleansed_stories[cleansed_stories.length] = stories[i] ;
			}
		}
		return cleansed_stories ;
	},
	getStoryFromStoriesListById : function(id) {
		if ( id ) {
			for ( var i=0; i < this.stories.length; i++ ) {
				if ( this.stories[i].id == id ) {
					return this.stories[i] ;
				}
			}
		}
		return '' ;
	},
	setStoryActive : function() {
		var story_id = '' ;
		// check the url in case it's a story url
		if ( location.pathname.match(/(?:\\|\/)(ST\d{4,4}\d{2,2}\d{2,2}\d+)(?:_.+)?\.html/) ) {
			story_id = location.pathname.match(/(?:\\|\/)(ST\d{4,4}\d{2,2}\d{2,2}\d+)(?:_.+)?\.html/)[1] ;
		}
		// check the URL parameter (for staging)
		else if ( getValue('URL').match(/(?:\\|\/)(ST\d{4,4}\d{2,2}\d{2,2}\d+).html/) ) {
			story_id = getValue('URL').match(/(?:\\|\/)(ST\d{4,4}\d{2,2}\d{2,2}\d+)\.html/)[1] ;
		}
		// check if this.id is a story
		else if (this.id.indexOf('ST') == 0 ) {
			story_id = this.id;
		// check parameter on url
		} else if ( this.story_override && this.story_override.id.indexOf('ST') == 0 ) {
			story_id = this.story_override.id ;
		}
		// confirm that this is a legal story id and that this item is within it.
		var story_provisional = this.getStoryFromStoriesListById(story_id) ;
		if ( this.isItemPartOfStory( story_provisional ) ) {
			this.story_active = story_provisional ;
		}
		// if no story emerged, use the default
		if ( !this.story_active ) {
			var story_provisional = this.story_default ;
			if ( this.isItemPartOfStory( story_provisional ) ) {
				this.story_active = story_provisional ;
			}
		}
		/* Note one of the results of this: if someone passes in a bogus story id, you'll see the story
		in the context of the default story not *no* story, which could be possible, too. */
	},
	setStoryDefault : function() {
		/* Note: This prioritizes the newest date. To prioritize the oldest:
				i) uncomment the two commented out lines; and
				ii) comment out the uncommented corresponding two lines.
		*/
		// var time = 9999999999999 ;
		var time = -1 ;
		for ( var i=0; i < this.stories.length; i++ ) {
			// if ( this.stories[i].time_of_moment < time && this.isItemPartOfStory( this.stories[i] ) ) {
			if ( this.stories[i].time_of_moment > time && this.isItemPartOfStory( this.stories[i] ) ) {
				time = this.stories[i].time_of_moment ;
				this.story_default = this.stories[i] ;
			}
		}
	},
	setStoryOverride : function() {
		this.story_override = this.getStoryFromStoriesListById( getValue('sid') ) ;
	},
	showActiveItem : function() {
		if ( this.story_active && this.story_active.id && this.story_active.items ) {
			if ( this.isItemPartOfStory( this.story_active ) ) {
				for (var i=0; i<story_prefixes.length; i++) {
					var prefix = story_prefixes[i];
					replaceClassName( prefix+this.story_active.id+'-'+this.id,'inactive','active' );
					// in the expando, replace the leading bullet with &raquo;
					// javascript cannot match the bullet, so must hard-code.
					if ( prefix == 'story-navigation-' ) {
						// replaceTheFirstCharacter(prefix+this.story_active.id+'-'+this.id,twiddle_on);
						// innerHTML breaks Pluck, so doing this long-hand
						var id = prefix+this.story_active.id+'-'+this.id ;
						if ( document.getElementById(id) ) {
							var e = document.getElementById(id);
							var twiddle = document.createTextNode(twiddle_on+' ') ;
							e.insertBefore(twiddle,e.childNodes[1]) ;
							e.removeChild(e.childNodes[0]) ;
						}
					}
				}
				setHref('story-comments-link-top-'+this.story_active.id,getUrlFromId(this.id,'Comments'));
				setHref('story-comments-link-bottom-'+this.story_active.id,getUrlFromId(this.id,'Comments'));
			}
		}
	},
	showStoryNavigation : function() {
		if ( this.story_active && this.story_active.id && this.story_active.items ) {
			if ( this.isItemPartOfStory( this.story_active ) ) {
				for (var i=0; i<story_prefixes.length; i++) {
					var prefix = story_prefixes[i];
					/* 
						An outside process (Otto - A/B Testing) can suppress certain story elements if
						<<prefix-with-underscores-instead-of-dashes>>+SHOW = false.
						But much of the time that variable will be undefined, hence undefined is like true.
					*/
					var prefix_show = prefix.replace(/-/g,"_")+"SHOW" ;
					eval("var show = (typeof "+prefix_show+" == 'undefined' || (typeof "+prefix_show+" != 'undefined' && "+prefix_show+"))" ) ;
					if ( show ) {
						replaceClassName( prefix+this.story_active.id,'hide','show' );
					}
				}
			}
		}
	},
	isItemPartOfStory : function(story) {
		if ( story && story.items ) {
			for ( var i=0; i < story.items.length; i++ ) {
				if ( story.items[i] == this.id ) {
					return true ;
				}
			}
		}
		return false ;
	}
}
/* END: /wp-srv/js/story/thisItem.js */
