

/* hash / back button */
var StateManager = EXANIMO.managers.StateManager;

window.onload = function(){
	
	//check to see if this page is archives, if not archives initialize statemanager for hash and backbutton support
	var isArchives = window.location.href.indexOf( "archives" );
	if( isArchives == - 1 ){
		StateManager.initialize();
	}
}


window.addEvent('domready', function(){

	_smealInitialized = false;
	_blogExists = $chk( $('the-smeal-blog-internal') );
	initializeConfig();
});


/* PHP File location */
var _phpLoc = "includes/";

var _initDisplayIndice;
var _deanInitHeight;
var _blogInitHeight;
var _blogExists;
var _sideBarOpen = false;

var _configArray = new Array(); //Houses all item nodes from the config as a json object */
var _featureArray = new Array();  // _featureArray = [ { element:element, hasLoaded:true } ]
var _deanArray = new Array();

//once a section has loaded within the main page state we add it to the loaded array, we can possibly store this somewhere if they leave and come back?
var _loadedArray = new Array(); 


 


/////////////////////////////////////////////////////////////////////////////////////////
// INITIALIZE
/////////////////////////////////////////////////////////////////////////////////////////

function initializeConfig(){

	var jsonString = Json.toString({method:'initializeConfig', params:''});
	new Ajax( _phpLoc + 'JSONWrapper.php', {postBody: jsonString, onComplete: onInitializeClient}).request();
}

function onInitializeClient( response ){

	_configArray = Json.evaluate( response );
	checkHash();
	//$('log').setText( response );
	//alert("Response String : " + response );
}

function checkHash(){

	var currentHash = document.location.href.split('#')[1];

	//if there is a hash and it has length
	if( currentHash != undefined && currentHash.length > 0 ){
	
		var str = "archives.php";
		
		var vars = currentHash.split( "&" );
		var section = vars[0].split( "=" )[1];
		var id = vars[1].split( "=" )[1];
		//check to see if the section / id within the hash exists in the current config, if so draw the page and open that article
		var exists = existsInConfig( section, id );  

		//if the seciton & id in the hash match that of the display list for the config, set a flag for the ID so we can display the proper article after we initialize the page
		if( exists ){
			_initDisplayIndice = id;
			initInterface();
		
		//else build a query string and show the archived article or archive page depending on the location string
		}else{
			if( section != undefined ){
				str += "?section=" + section;
			}
			if( id != undefined ){
				str += "&id="+id;
			}
			window.location = str;
		}
	}else{
		initInterface();
	}
}

//check to see if the article in question exists within the main index page configuration
function existsInConfig( section, id ){

	var total = _configArray[section].length;
	for( var i=0; i < total; i++ ){
		var item = _configArray[section][i][0];
		var atts = item['@attributes'];
		var itemID = atts.id;
		if( itemID == id ){
			return true;
		}
	}
	return false;
}

//initialize the interface
function initInterface(){
	
	setDean();
	initializeSidebar();
	var totalFeatures = setFeatures();
	_smealInitialized = true;	

}

/////////////////////////////////////////////////////////////////////////////////////////
// END INITIALIZE
/////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////
// STATEMANAGER, HASH / BACK BUTTON SUPPORT
/////////////////////////////////////////////////////////////////////////////////////////

// Define what happens when the state changes.
StateManager.onstaterevisit = function(e){
//StateManager.onstatechange = function(e){
	
	
    //do some stuff to reset the current state back to where we were based on ID
	if( _smealInitialized ){
		if( e.id == 'defaultState' ){
			//alert("DEFAULT");
		}else{
			var vars = e.id.split( "&" );
			var section = vars[0].split( "=" )[1];
			var id = vars[1].split( "=" )[1];
			switch( section ){
				case "feature":
					var featureItem = getFeatureByID( id );
					display( featureItem.toggler, featureItem.indice );
				break;
			}
		}
	}
} 


function updateState( title, hash ){
	
	//alert("Update State Title : " + title + " : HASH : " + hash );
	//if not archives set state
	var isArchives = window.location.href.indexOf( "archives" );
	if( isArchives == - 1 ){
		if( _smealInitialized ){
			//edit hash and add a marker to history
			StateManager.setState( hash, 'PSU Smeal Report : ' + title );  
		}
	}
}


/////////////////////////////////////////////////////////////////////////////////////////
// MODULES
/////////////////////////////////////////////////////////////////////////////////////////

//retreive any and all modules that exist for a specific article so we can write them to the sidebar
function getModulesFomConfig( id, section ){

	var moduleArray = new Array();
	var item = getItem( id, section );

	/* tricky, if there is a 'modules' object, it will be an array if there is more than 1 or just a plain object if there is only 1, we need to check for both */
	if( item['modules'] ){
		var totalModules = item['modules']['module'].length;
		//if there is only 1 module for this article
		if( totalModules == undefined ){
			var module = item['modules']['module'];
			var oModule = new Object();
			oModule.id = module['@attributes'].id;
			oModule.title = module['@attributes'].title;
			oModule.url = module['@attributes'].url;
			moduleArray.push( oModule );
		}else{
			for( var j=0; j< totalModules; j++ ){
				var module = item['modules']['module'][j];
				var oModule = new Object();
				oModule.id = module['@attributes'].id;
				oModule.title = module['@attributes'].title;
				oModule.url = module['@attributes'].url;
				moduleArray.push( oModule );
			}
		}
	}
	return moduleArray;
}

function modulesLoaded( response ){
	//alert("Modules Loaded");
}

//remove modules if any exist
function removeModules(){

	var mods = $$( "#moduleContainer .moduleInner" );
	mods.each( function( e ){
		checkVideo( e, 0 );
		$(e).remove(); });
}

/////////////////////////////////////////////////////////////////////////////////////////
// END MODULES
/////////////////////////////////////////////////////////////////////////////////////////




/////////////////////////////////////////////////////////////////////////////////////////
//  FEATURES AND ACCORDION
/////////////////////////////////////////////////////////////////////////////////////////


/*
	anytime we close a feature we need to see if that feature element has a video in it, then we need to get that id based on the 'videoContent' class so we can attempt to stop it
*/

/** Adjust Display Of Pseudo Accordion */
function display( toggler, index ){

	//each time we adjust the display we remove any modules that may be present in the sidebar
	removeModules();
	
	//alert(" Display : " + toggler + "   :   Index : " + index );
	for( var i=0; i < _featureArray.length; i++ ){
		var t = _featureArray[i].toggler;
	
		//check to see if the toggler that was clicked is the one that was passed, if not we need to close this article
		if( t != toggler ){
		
			var p = _featureArray[i].page;
			if( p != undefined ){
				t.removeClass('active');  //toggle active class, off
				var isOpen = p.getIsOpen();
				//prior to closing a tab we need to see if there is a video element within this tab and attempt to stop it if so
				if( isOpen ){
					checkVideo( _featureArray[i].elementID, 0 );
				}
				p.close();
			}
		}else{
			//else open this article
			var p = _featureArray[i].page;
			if( p != undefined ){
				t.addClass('active');  //toggle active class, on
				p.open();
				checkVideo( _featureArray[i].elementID, 1 );
				var id = _featureArray[i].id;
				var section = _featureArray[i].section;
				var hasLoaded = getHasLoaded( id, section );
				if( hasLoaded ){
					var hasModules = getModulesFomConfig( id, section );
					//if article has been loaded and has modules, minimize dean / blog and add modules below
					if( hasModules.length > 0 ){
						minimizeAll();
						var modules = new ModuleClass( hasModules, id  );
						//modules.addEvent( 'onModulesLoaded', modulesLoaded );
					}else{
						//else reset blog / dean back to initial load state / size
						resetSideBar();
					}
				}else{
					//if the article has not been loaded reset blog / dean back to initial open load state / size
					var isOpen = getSideBarOpen();
					if( !isOpen ){
						resetSideBar();
					}
				}
				
				//update the hash based on the opened article
				var item = getItem( id, section );
				var title = item['@attributes'].title;
				var hash = "section=" + section + "&id=" + id;
				updateState( title, hash );
			}
		}
	}
}
	
/** Rip out the href on feature togglers, create an object for this feature item, turn on it's display, create an instance of the pagination class for each item and add an onclick event to call AJAX methods */
function setFeatures(){

	var features = new Array();
	features = $$("#features .feature");  //feature elements
	var t = $$('#features h1.toggler');  //toggle elements
	var totalFeatures = features.length;

	var i = 0;
	
	features.each(function(feature){

		var elementID = feature.getProperty( 'id' ); //feature.id;

		var section = elementID.split("_")[0];
		var articleID = elementID.split("_")[1];

		var tmpObject = new Object();
		tmpObject.element = feature;  //element : 'feature_7'
		tmpObject.id = articleID;  //id of article: '7'
		tmpObject.elementID = elementID;
		tmpObject.toggler = t[i];  //toggler for this element
		tmpObject.section = section;  //section name: 'feature'
		tmpObject.indice = i;

		//initially we set each feature display to none, if user has js enabled we need to set all feature elements to block
		//if( totalFeatures > 1 ) {
		feature.setStyle('display', 'block');
		//}
		
		var p = new Pagination( feature );
		tmpObject.page = p;  //page instance
		
		//if this article does not have a teaser we load in the actual article in features.php, here we check to see if the feature element has pages, if so set paging
		var pages = feature.getElementsBySelector('.page');
		if( pages.length > 0 ){
			if( totalFeatures > 1 ){
				p.setPaging( false );
			}else{
				//alert("open");
				p.setPaging( true );
			}
			tmpObject.hasLoaded = true;  //initially set to true as there is no teaser
			setHasLoaded( articleID, 'feature' );
		}else{
			tmpObject.hasLoaded = false;  //initially set to false
		}
		
		_featureArray.push( tmpObject );
		
		//if there is a toggler element
		if( t[i] != null ){
			addTogglerEvent( t[i], i );
		
			$$("#" + elementID + " a.feature-more").each(function(link){
				link.setProperty('href', '#');
				addMoreEvent( link, articleID, section, feature )
			});
		}
		
		//if there is only 1 feature and 0 pages within this feature open it fully
		if( totalFeatures == 1 && pages.length == 0 ){
			p.open();  
		}
		i++;
		
	});
	
	//if there is only 1 feature we're on the archives page, check to see if there are any modules as they will be built by the archives.php page
	//if there are modules retract the dean / blog, else reset them to their open state
	if( totalFeatures == 1 ){
		var hasModules = new Array();
		var hasModules = $('moduleContainer').getElementsByClassName('moduleInner');
		var totalModules = hasModules.length;
		if( hasModules.length > 0 ){
			minimizeAll();
		}else{
			resetSideBar();
		}	
	}
	
	//if there are no features at all we are on the archives list, rest the sidebar to open states
	if( totalFeatures == 0 ){
		resetSideBar();
	}
	
	/* if there is more than 1 feature we have an accordion and need to display the first one or the _initDisplayIndice set when we retrieved the hash up top  */
	if( _featureArray.length > 1 ){
		var fItem;
		//if there is an ID in the hash we need to set that article to be displayed initially
		if( _initDisplayIndice != null ){
			fItem = getFeatureByID( _initDisplayIndice).indice;
		}else{
			//if there is no id in the hash we display item 0 or the most recent
			fItem = 0;
		}
		display( t[fItem], fItem );
	}else{
		//if there is only 1 feature
		fItem = 0;
	}
	return totalFeatures;
}


// add a click event and remove the href for each of the togglers in the accordion
function addTogglerEvent( toggler, index ){

	//strip the feature toggler href
	$$(toggler , ' a.toggle' ).each(function(link){
				link.setProperty('href', '#');
	});

	toggler.addEvent('click', function( e ){
				e = new Event(e).stop();
				display( toggler, index );
	});
}

//add the 'read-more' click event
function addMoreEvent( clickElement, articleID, section, elementID ){

	clickElement.addEvent('click', function( e ){
				e = new Event(e).stop();
				getData( articleID, section, elementID ); 
	});
}

/////////////////////////////////////////////////////////////////////////////////////////
// END FEATURE INITIALIZATION
/////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////
//  DEAN AND SIDEBAR
/////////////////////////////////////////////////////////////////////////////////////////

//initialize the dean section, set the height of the container to the amount viewable
function setDean(){
	
	var stories = $$("#from-the-dean-internal .story");  //feature elements
	var totalStories = stories.length;
	
	var amount = this._configArray['deanView'];
	var deanHeight = 0;
	var count = 0;
	
	stories.each(function(story){
		var storyID = story.id;
		var section = storyID.split("_")[0];
		var storyNum = storyID.split("_")[1];
		
		if( count < amount ){
			var storyObj = $(storyID).getSize();
			deanHeight += storyObj.scrollSize.y;
		}
		
		//removed, no longer loading in dean articles into the dean module
		//var tmpObject = new Object();
		//tmpObject.element = storyID;
		//tmpObject.hasLoaded = false;
		
		//_deanArray.push( tmpObject );
		
		/*
		$$("#" + storyID + " a.fromTheDean").each(function(link){
		
				link.setProperty('href', '#');
				link.addEvent('click', function( e ){
					e = new Event(e).stop();
					getData( storyNum, section, "from-the-dean-internal" ); 
				});
		});
		*/
		count++;
	});	
	
	_deanInitHeight = deanHeight;
}

//set the click events for the maximize / minimize buttons within the dean / blog if blog exists
function initializeSidebar() {
	
	$('from-the-dean-minimize').onclick = function ( e ) {
		e = new Event(e).stop();
		showDean( false );
	};
	
	$('from-the-dean-maximize').onclick = function ( e ) {	
		e = new Event(e).stop();
		showDean( true );
	};
	
	if( _blogExists ){
	
		var scrollHeightBlog = $('the-smeal-blog-internal').getSize();
		_blogInitHeight = $('the-smeal-blog-internal').getStyle('height');
		$('the-smeal-blog-minimize').onclick = function ( e ) {	
			e = new Event(e).stop();
			showBlog( false );
		};
		
		$('the-smeal-blog-maximize').onclick = function ( e ) {	
			e = new Event(e).stop();
			showBlog( true );
		};
	}
}


function showDean( value ){

	var h = checkDeanSidebarNav( 'from-the-dean', 'from-the-dean-internal', value );
	minMax( 'from-the-dean-internal', h );
}


function showBlog( value ){
	
	var h = value == true ? $('the-smeal-blog-internal').getSize().scrollSize.y : 0;
	minMax( 'the-smeal-blog-internal', h );
}

function resetSideBar(){
	
	setSideBarOpen( true );
	var dh = checkDeanSidebarNav( 'from-the-dean', 'from-the-dean-internal', true );
	minMax( 'from-the-dean-internal', dh );
	if( _blogExists ){
		minMax( 'the-smeal-blog-internal', _blogInitHeight );
	}
}


function minimizeAll(){

	setSideBarOpen( false );
	var dh = checkDeanSidebarNav( 'from-the-dean', 'from-the-dean-internal', false );
	minMax( 'from-the-dean-internal', dh );
	if( _blogExists ){
		minMax( 'the-smeal-blog-internal', 0 );
	}
}


function checkDeanSidebarNav( navelement, innerElement, isOpen ) {

	var h;
	//check to see if the dean has a navigation element
	var elementHasNav = $(navelement).getElement('.navigation');
	//alert("HN : " + elementHasNav);
	//if dean has nav element
	if( elementHasNav != null ){
		//toggle navigation on / off
		var navOpacity = isOpen == true ? "block" : "none"; 
		elementHasNav.setStyle( "display", navOpacity );
		
		//if closing set the new _deanInitHeight...
		//*** NOTE : this could be called back to back so we need to check if it's closed first so we don't set the new _deanInitHeight to 0
		if( ! isOpen ){	
			
			var checkHeight = $(innerElement).getStyle('height').toInt();
			//if it's already closed, do nothing
			if( checkHeight == 0 ){
				//alert("It's Less");
			}else{
				//alert("it's not less, set dean ");
				//else get and the the height of the dean module for when it needs to open back up again
				_deanInitHeight = $(innerElement).getStyle('height').toInt();
			}
			//close it
			h = 0;
		}else{
			//open it with the old _deanInitHeight
			h = _deanInitHeight;
		}
		//else treat as normal, expand and contract fully
	}else{
		h = isOpen == true ? $(innerElement).getSize().scrollSize.y : 0;
	}
	return h;
}


function minMax( element, size ) {

	/* anytime we minimize a sidebar element we need to check for a video element, if there is a video element we need to stop it */
	if( size == 0 ){
		checkVideo( element, 0 );
	}
	
	var transition = new Fx.Style(element, 'height', {transition: Fx.Transitions.Quad.easeOut}).start(size);
}

/////////////////////////////////////////////////////////////////////////////////////////
// END INITIALIZE DEAN AND SIDEBAR
/////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////
// ARTICLES
/////////////////////////////////////////////////////////////////////////////////////////


//remove modules if section == dean, instantiate a new article class which will load an external .html or .php file and replace the current contents within the accordion tab
function getData( id, section, element ){
	
	if( section != 'dean' ){
		removeModules();
	}
	$(element).setHTML( "<h2>LOADING...</h2>" );
	//alert(" ID : " + id + "    :  Section : " + section + " ::  Element :  " + element );
	var articles = new ArticleClass( id, section, $(element) );
	articles.addEvent( 'onArticleLoaded', articleLoaded);
}


/* Article loading completed, set browser state, add hasLoaded = true to the _laodedArray so we know whether or not to show modules in the sidebar, get and load modules if any exist for this article */
/**
	@param 	response	Object ( id, section, element )	
*/
function articleLoaded( response ){
	
	//alert("id : " + response.id );
	//alert("section : " + response.section );
	//alert("element : " + response.element );
	var id = response.id;
	var section = response.section;
	var element = response.element;
	
	setHasLoaded( id, section, true );  //once an element has loaded it's loaded state get set to true
	
	var item = getItem( id, section );
	var title = item['@attributes'].title;
	var hash = "section=" + section + "&id=" + id;
	
				
	/* check to see if this article has modules */
	var hasModules = getModulesFomConfig( id, section );
	//if article has modules, minimize dean / blog, draw modules
	if( hasModules.length > 0 ){
		minimizeAll();
		var modules = new ModuleClass( hasModules, id );
		modules.addEvent( 'onModulesLoaded', modulesLoaded );
	}
		

	//create and inject a "view teaser" element so we can leave the loaded article and reload / view the teaser again	
	var teaserP = new Element('p', {
		'class': 'view-teaser'
	});
	
	var teaser = new Element( 'a', {
		'events': {
		       'click': function(e){
				e = new Event(e).stop();
				reloadTeaser( id, section, element );
		       }
		},
		//'class': '',
		'href': '#'
	});
	teaser.setText( "Home" );
	teaser.injectInside( teaserP );

	/* check if loaded article has pages, if has pages then paginate, else animate height to height of loaded content */
	var hasPages = element.getElementsBySelector('.page');
	if( hasPages.length > 0 ){
		if( section == 'feature' ){
			teaserP.injectTop( hasPages[0] );
			setPagination( element );
		}else{
			/* set a paging element if it's not a feature */
			setPagingOnArticle( element, section ); 	
		}
	}else{
	
		teaserP.injectTop( element );
		var newHeight = setHeight( element );
		var pe = getPagingElement( element );
		pe.setCurrentHeight( newHeight );
	}	
	//update page title and hash
	updateState( title, hash );
}

// reload the teaser
function reloadTeaser( id, section, element ){	

	var pe = getPagingElement( element );
	pe.removeNavigation();
	var teaser = getTeaser( id, section );
	teaser = teaser + "?date=" + new Date().getTime();
	removeModules();
	$(element).setHTML( "<h2>LOADING...</h2>" );
	
	new Ajax(teaser, {
		evalScripts: true,
		autoCancel: true,
		method: 'get',
		update: $(element),
		//onFailure: showError(),  
		onSuccess: function(){ teaserLoaded( pe, id, section, element );}
		//onSuccess: teaserLoaded
	}).request();
}

//once teaser has loaded, re-create the 'read more' link, reset the paging instance to reflect the new content and reset / open the sidebar elements
function teaserLoaded( pe, id, section, element ){
	
	removeHasLoaded( id, section );
	
	var readMoreP = new Element('p', {
		'class': 'read-more'
	});
	var readMoreA = new Element('a', {
			'events': {
		       'click': function(e){
				e = new Event(e).stop();
				getData( id, section, element );
		    }
		},
		'class': 'feature-more',
		'href': '#'
	});
	
	readMoreA.setText( 'Read More' );
	readMoreA.injectInside( readMoreP );
	readMoreP.injectInside( element );	
	pe.reload();
	resetSideBar();
}

//archives button at bottom of features
function showArchives(){

	window.location = "archives.php";
}

//mainly used from the archives.php page if the display article has pages we need to instantiate the pagination class and set it's paging
function setPagingOnArticle( e, section ){

	var p = new Pagination( e );
	p.setPaging( true );
	if( section == 'dean' ){
		var currentHeight = p.getCurrentHeight();
		_deanInitHeight = currentHeight;
	}
}

// find the element within the _featureArray and tell it's Pagination instance to set paging
function setPagination( element ){
	
	for( var i=0; i < _featureArray.length; i++ ){
		var e = _featureArray[i].element;
		if( e == element ){
			var p = _featureArray[i].page;
			p.setPaging( true );
			return;
		}
	}
}

//retrieve the article url to load, called fromt he article class
function getArticle( id, section ){

	var total = _configArray[section].length;
	for( var i=0; i < total; i++ ){
		var item = _configArray[section][i][0];
		var atts = item['@attributes'];
		var itemID = atts.id;
		if( itemID == id ){
			return atts.url;
		}
	}
	return;
}

//retrieve the teaser url on teaser 'reload'
function getTeaser( id, section ){

	var total = _configArray[section].length;
	for( var i=0; i < total; i++ ){
		var item = _configArray[section][i][0];
		var atts = item['@attributes'];
		var itemID = atts.id;
		if( itemID == id ){
			return atts.teaser;
		}
	}
	return;
}
/////////////////////////////////////////////////////////////////////////////////////////
// END ARTICLES
/////////////////////////////////////////////////////////////////////////////////////////


//sets the height of an element based on it's true height
function setHeight( e ){

	var resize = new Fx.Style(e, 'height', {duration: 700, opacity:true});
	var elementObj = $(e).getSize();
	var height = elementObj.scrollSize.y;
	resize.start(height);
	return height;
}



/////////////////////////////////////////////////////////////////////////////////////////
// GETTERS /SETTERS / HELPERS
/////////////////////////////////////////////////////////////////////////////////////////

/* return a section item object based on id and section */
function getItem( id, section ){

	var total = _configArray[section].length;
	for( var i=0; i < total; i++ ){
		var item = _configArray[section][i][0];
		var itemAtts = item['@attributes'];
		var itemID = itemAtts.id;
		//alert("IID : " + itemID );
		if( itemID == id ){
			return item;
		}
	}
}


/* return a feature object based on id */
function getFeatureByID( id ) {

	var totalFeatures = _featureArray.length;
	for( var i=0; i < totalFeatures; i++ ){
		var feature = _featureArray[i];
		var fID = feature.id;
		if( fID == id ){
			return feature;
		}
	}
}

/* Get the "hasLoaded" boolean for a specific item */
function getHasLoaded( id, section ){
	
	var hasLoaded = itemExists( id, section );
	return hasLoaded;
}

/* Set the "hasLoaded" boolean for an item */
function setHasLoaded( id, section ){

	var to = new Object();
	to.id = id;
	to.section = section;
	_loadedArray.push( to );
}

/* if we reload a teaser back in we need to remove the article from the 'hasLoaded' array so when it's selected the modules will not show up in the sidebar */
function removeHasLoaded( id, section ){

	var loadedTotal = _loadedArray.length;
	
	for( var i = 0; i < loadedTotal; i++ ){
		var lid = _loadedArray[i].id;
		if( lid == id ){
			var lsection = _loadedArray[i].section;
			if( lsection == section ){
				_loadedArray.splice(i,1)
				return;
			}
		}
	}
	
}

/* return the paging instance for this element */
function getPagingElement( element  ){

	for( var i=0; i < _featureArray.length; i++ ){
		var e = _featureArray[i].element;
		if( e == element ){
			var p = _featureArray[i].page;
			return p;
		}
	}
	
}

/** 
	return 	boolean 
*/
function getSideBarOpen(){
	return _sideBarOpen;
}

/**
	value		boolean
*/
function setSideBarOpen( value ){
	_sideBarOpen = value;
}

/**
	id		string
	section	string
*/
function itemExists( id, section ){
	
	var total = _loadedArray.length;
	for( var i = 0; i < total; i++ ){
		var item = _loadedArray[i];
		var iID = item.id;
		var iSection = item.section;
		if( iID == id && iSection == section ){
			return true;
		}
	}
	return false;
}





/////////////////////////////////////////////////////////////////////////////////////////
// AJAX EVENT HANDLERS NOT IN USE ATM
/////////////////////////////////////////////////////////////////////////////////////////

//seems to fire no matter what 
function showError( e ) {
	//alert("ERROR : " + e );
}

/*
function dataLoaded(){
	alert("DONE LOADING");
}

function doOnRequest( id ){
	alert( "onrequest");
}

function doOnComplete( id ){
	alert( "oncomplete");
}
*/



/////////////////////////////////////////////////////////////////////////////////////////
// FLASH VIDEO
/////////////////////////////////////////////////////////////////////////////////////////


function loadFile(obj) { 

	var id = obj['id'];
	thisMovie(id).loadFile(obj); 
};

//hide / show and stop flash if visible = false
function checkVideo( element, visible ){

	//get all video elements within the 'element' passed
	var videoElements  = new Array();
	videoElements = $(element).getElementsByClassName('videoContent');
	var totalVideos = videoElements.length;
	if( totalVideos > 0 ){
		for( var i = 0; i < totalVideos; i++ ){
			var video = videoElements[i];
			//get the id value of the video element
			var vid = video.getProperty( ( 'id' ) );
			/*
			the swf id is similar to the div id that houses the flash element, we need to get the swf id to tell javascript to stop the video when a panel closes.
			the div id is prefixed with 'video' + uuid, while the swf id is prefixed with 'id' + uuid
			we need the swf id to tell javascript to tell the flash movie to stop playing if we are closing this pane and a video is playing
			*/
			var uniqueID = vid.split("_")[1];
			var swfID = uniqueID;
			if( !visible ){
				if( _smealInitialized ){
					//this fails miserably if the swf is not visible
					togglePlayPause( swfID );
				}
			}
		}
	}
}

// This is a javascript handler for the player and is always needed.
function thisMovie(movieName) {

	if(navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
}

/* Cannot find a way to simply tell the video to pause only, the only event that appears to be available is a play / pause toggle, so for now we just 'stop' the video */
/* NOTE: If the article has pages and the video is located on a page that is not visible or in view, sending an event to the flash video will not work and throw an error that "sendEvent"
	is not a function, seemingly because the video is not visible so it does not exist, this only happens in firefox, so we need to check for the existance of the method as type function
*/
function togglePlayPause( id, param ){

	var element = thisMovie(id);
	if( typeof( element.sendEvent ) == 'function' ){
		thisMovie(id).sendEvent('stop', param); 
	}
}
	
// these functions are caught by the JavascriptView object of the player.
function playlistVideo( e, playerID, type, param ) { 
	
	/*
	this is called from an 'a' onclick which is nested within a 'li' which is nested within a 'ul', we need to traverse up to the ul which is parent.parent
	get all list items, check to see if a list item has the class 'activeVideo' and remove it, then add the 'activeVideo' class to the clicked element
	*/
	if( e != undefined ){
		var unorderedList = $(e).getParent().getParent();
		var li = unorderedList.getElements( "li" );
		for( var i=0; i < li.length; i++ ){
			var isActive = li[i].hasClass( 'activeVideo' );
			if( isActive ){
				li.removeClass( 'activeVideo' );
			}
		}
		//add the 'activeVideo' class to the parent of the passed element 
		$(e).getParent().addClass( 'activeVideo' );
	}
	thisMovie(playerID).sendEvent(type,param); 
};












