/**
 * These functions control the browser history handling. 
 * Any new link added that the user clicks should call dhtmlHistory.add( tagName, data ) 
 * tagName should be a unique identifier and shoudl NOT match any id in the HTML code or
 * it will work as a traditional anchor and focus that ID item. 
 */
 
var lastHistClicked = null;
var lastClickedClass = Class.create({
		initialize: function(){
			this.lastHistClicked = null;
			this.lastAnchor = null;
		},
		compare: function( func, anchorName ){
			if (this.lastAnchor == anchorName)
				return false;
			this.lastAnchor = anchorName;
			this.lastHistClicked = func;
			return true;
		}
		});
var lastClicked = new lastClickedClass();


function hist_CheckAdd( func, anchorName, dataObj){
	return; // disable this for now

	if (!lastClicked.compare( func, anchorName )){
		return;
	}
	lastHistClicked = func;
	dhtmlHistory.add( anchorName, dataObj );
}
 

/** 
 * Call this function and return out of the handler if the user is trying to browse back
 * or forward to a login required page (ie., manage their profile, read their mail, etc.
 */
function history_LoginOK( ){
	if (!curUser.isLoggedIn()){
		alert("You must be logged in to view this page. You are being redirected.");
		aj_ChangePg('top15'); // default home page
		return false;
	}
	return true;
}


/**
 * This is the event handler called by window.load events, which happen anytime that
 * someone clicks the back/forward button
 */
function aj_HistoryChange( newLocation, historyData ){
	var historyMsg;
	var isObj = false;
	var skipDirectURLcheck = false;
	
	if (typeof historyData == "object" && historyData != null){
		historyMsg = historyStorage.toJSON(historyData);
		isObj = true;
	} else {
		historyMsg = historyData;
	}
	/*
	var historyMsg = (typeof historyData == "object" && historyData != null
			? historyStorage.toJSON(historyData)
			: historyData
	);
	*/

	if (!isObj){
  	  switch ( historyMsg ){
		case 'hist_showcontent':
			aj_showcontent( newLocation );
			break;
		case 'hist_ChangeTopic':
			aj_ChangeTopic( newLocation );
			break;
		case 'hist_ChangePg':
			aj_ChangePg('top15');
			break;
		case 'dpu_ManageAccount':
			if (!history_LoginOK()){
				return;
			}
			dpu_DisplayUserHomePage();
			break;
		case 'dpu_inviteFriendForm':
			if (!history_LoginOK()){
				return;
			}
			dpu_inviteFriendForm();
			formatLoadPg('displayuserhomepage');
			break;
		case 'dpu_EditMyInfo':
			if (!history_LoginOK()){
				return;
			}
			dpu_EditMyInfo();
			formatLoadPg('displayuserhomepage');			
			break;
		case 'dpu_EditPrivacy':
			if (!history_LoginOK()){
				return;
			}
			dpu_EditPrivacy();
			formatLoadPg('displayuserhomepage');			
			break;
		case 'vlog_UserVlog':
			if (!history_LoginOK()){
				return;
			}
			vlog_UserVlog();
			formatLoadPg('displayuserhomepage');			
			break;
		case 'hist_LoadSent':
			if (!history_LoginOK()){
				return;
			}
			mess_LoadSent();
			formatLoadPg('displayuserhomepage');			
			break;
		case 'hist_LoadInbox':
			if (!history_LoginOK()){
				return;
			}
			mess_LoadInbox();
			formatLoadPg('displayuserhomepage');			
			break;
		default:
			/** 
			 * These are very simple and straight forward links that can also
			 * work as bookmarks and direct URLs typed in.
			 */		
			// default to our 'home page '
			switch (newLocation){
				case 'topic_conversation':
					if ( historyMsg != sCurTopic ){
						// topic changed since we were here... change it back.
						aj_BackToTopic( historyMsg );
					}
					aj_TopicConversation();
					break;
				case 'survey':
					if ( historyMsg != sCurTopic ){
						// topic changed since we were here... change it back.
						aj_BackToTopic( historyMsg );
					}				
					aj_TakeSurvey();
					break;
				case 'critique':
					if ( historyMsg != sCurTopic ){
						// topic changed since we were here... change it back.
						aj_BackToTopic( historyMsg );
					}				
					aj_WriteAReview();
					break;
				case 'forgot_password':
					dpu_PasswordForm();
					break;
				case 'new_user':
					aj_displayReviewerForm();
					break;
				default:
					//alert('defaulting out to top 15 page from simple history');			
					if ( loadedDirectURL == false ){
						aj_ChangePg('top15');
					} else {
						// load direct url again..				
						skipDirectURLcheck = true;
					}
					break;
			}
//			aj_ChangePg('top15');
			break;
	  }
	} else {
		// we're dealing with an object
		// grab the first three digits of the newLocation to determine what type it is
//		newLocation = String( newLocation );
//		var toLoc = newLocation.substr(0,3);
		
		var detailedInfo = historyStorage.get( newLocation );
		var toLoc = detailedInfo.link_clicked;
		switch( detailedInfo.link_clicked ){
			case 'vlog_reply_inbox':
				if (!history_LoginOK()){
					return;
				}
				vlog_ReplyInboxMsg( detailedInfo.mid );
				break;
			case 'text_reply_inbox':
				if (!history_LoginOK()){
					return;
				}
				mess_textReplyInboxMsg( detailedInfo.mid );
				break;
			case 'load_question':
				if ( detailedInfo.topic != sCurTopic ){
						// topic changed since we were here... change it back.
					aj_BackToTopic( detailedInfo.topic );
				}
				aj_LoadOldQuestion( detailedInfo.qid  );
				// make sure all the right divs are cuase loadoldq doesn't do it.
				formatLoadPg('topicconversation');				
				break;
			case 'show_crit_review':
				if (detailedInfo.topic != sCurTopic ){
					aj_BackToTopic( detailedInfo.topic );
				}
				dpv_showCritReview( detailedInfo.critid );
				formatLoadPg('writeareview');
				break;
			case 'dp_play_film':
				/* Don't need to check this because dp_PlayFilm does already
				if (detailedInfo.topic != sCurTopic ){
					aj_BackToTopic( detailedInfo.topic );
				}	
				*/
				dp_PlayFilm( detailedInfo.filmSrc, detailedInfo.filmId, detailedInfo.restrict, detailedInfo.topic );
				break;
			case 'display_message':
				if (!history_LoginOK()){
					return;
				}
				mess_displayMsg( detailedInfo.mid );
				formatLoadPg('displayuserhomepage');				
				break;
			case 'load_folder':
				if (!history_LoginOK()){
					return;
				}
				mess_LoadFolder( detailedInfo.folderName  );
				formatLoadPg('displayuserhomepage');				
				break;				
			default:
				//alert('defaulting out to top 15 page from object history');
				if (loadedDirectURL == false){
					aj_ChangePg('top15'); // default to the home page.
				}  else {
					skipDirectURLcheck = true;
				}
				break;
		}
	}
	
	if ( !skipDirectURLcheck && loadedDirectURL == true ){
		if (loadedDispAwrap == true){
			//dispAwrap.togTo('bodyHeaderTableDiv');
			loadedDirectURL = false;
		}
	}
}


function formatLoadPg( display ){
	/*
	if (display == currentDisplay){
		return;
	}
	*/
	switch( display ){
		case 'userform':
			onCritique = false;		
			dispC2.togTo('loginDetails');
			dispC.togTo('tableBodyDiv');
			//dispAwrap.togTo('bodyHeaderTableDiv');
			dispA.togto('maintopic');
			break;
		case 'registration':
			onCritique = false;
			//dispAwrap.togTo('bodyHeaderTableDiv');
			dispA.togTo('maintopic');
			dispC2.togTo('loginDetails');
			dispC.togTo('tableBodyDiv');
			break;
		case 'doneregistration':
			checkHideUserInfo();
			dispA.togTo('maintopic');
			dispB.togTo('videocontent');
			dispC2.togTo('contentWrapper');	
			//dispAwrap.togTo('bodyHeaderTableDiv');
			break;
		case 'nocontent':
			dispC2.togTo('');
			break;
		case 'filmdetails':
			//aj_HideAllContent();
			//dispAwrap.togTo('bodyHeaderTableDiv');
			dispA.togTo('maintopic');
			dispB.togTo('videocontent');
			dispC2.togTo('');
			dispC3.togTo('');
			dispC1.togTo('filmDetails');
			dispC.togTo('fullBodyDiv');
			break;
		case 'writeareview':
			dispB.togTo('videocontent');
			//dispAwrap.togTo('bodyHeaderTableDiv');
			dispA.togTo('maintopic');
			if (sCurTopic == 'doorpostinfo' || sCurTopic == 'hope')
				dispC2.togTo('contentWrapper');
			else 
				dispC2.togTo('bannerDiv');
			break;
		case 'survey':
			dispA.togTo('maintopic');
			//dispAwrap.togTo('bodyHeaderTableDiv');
			dispB.togTo('surveyform');
			dispC.togTo('tableBodyDiv');
			if (sCurTopic == 'hope' || sCurTopic == 'doorpostinfo'){
				dispC2.togTo('contentWrapper');
			} else {
				dispC2.togTo('bannerDiv');
			}
			break;
		case 'topicconversation':
			//dispAwrap.togTo('bodyHeaderTableDiv');
			dispA.togTo('maintopic'); // just in case it's not currenntly visible...
			dispB.togTo('videocontent');
			dispC.togTo('tableBodyDiv');
			dispC2.togTo('contentWrapper');
			if (!Element.visible('mainquestion'))
				Element.show('mainquestion');
			if (!Element.visible('oldquestions'))
				Element.show('oldquestions');
			if (!Element.visible('comments'))
				Element.show('comments');
			if (!Element.visible('questioncomments'))
				Element.show('questioncomments');
			break;
		case 'changetopic2':
			checkHideUserInfo();
			//dispAwrap.togTo('bodyHeaderTableDiv');			
			dispA.togTo('maintopic');
			dispB.togTo('videocontent');
//			dispC2.togTo('filmDetails'); // this shouldn't be
			dispC.togTo('tableBodyDiv');
			dispC3.togTo('thumbnailwrapper');
			break;
		case 'changetopic':
			checkHideUserInfo();
			//dispAwrap.togTo('bodyHeaderTableDiv');			
			dispA.togTo('maintopic');
			dispB.togTo('videocontent');
			if (sCurTopic == 'hope' || sCurTopic == 'doorpostinfo')
				dispC2.togTo('contentWrapper');
			else 
				dispC2.togTo('bannerDiv');
			dispC3.togTo('thumbnailwrapper');
			dispC.togTo('tableBodyDiv');			
			break;
		case 'doorpostinfo':
			//dispAwrap.togTo('bodyHeaderTableDiv');		
			dispA.togTo('maintopic');
			dispB.togTo('videocontent');
			dispC2.togTo('contentWrapper');
			dispC.togTo('tableBodyDiv');
			break;
		case 'top15':
			//dispAwrap.togTo('bodyHeaderTableDiv');		
			checkHideUserInfo();
			dispA.togTo('maintopic');
			dispB.togTo('videocontent');
			dispC1.togTo('fullBody');
			dispC.togTo('fullBodyDiv');			
			break;
		case 'displayuserhomepage':
			//dispAwrap.togTo('bodyHeaderTableDiv');
			dispA.togTo('profileHead');
			dispB.togTo('profileAlerts');
			dispC2.togTo('loginDetails');			
			dispC3.togTo('favoritesDiv');
			$('userDetailsMenu').show();	
			dispC.togTo('tableBodyDiv');
			break;
		case 'default':
		default:
			//dispAwrap.togTo('bodyHeaderTableDiv');
			dispA.togTo('maintopic');
			dispB.togTo('videocontent');
			if (sCurTopic == 'hope' || sCurTopic == 'doorpostinfo')
				dispC2.togTo('contentWrapper');
			else
				dispC2.togTo('bannerDiv');
			dispC.togTo('tableBodyDiv');		
			dispC3.togTo('thumbnailwrapper');
			break;
	}
	
	// now that we've done all that, update the current display
	currentDisplay = display;
}


var PgDisp = Class.create( {
		initialize: function( toPg ){
			this.current = toPg;
		},
	
		togTo: function( toPg ){
			if ( this.current == toPg ){
				if (this.current != ''){
					if (!$(this.current).visible ){
						Element.show( this.current );
					}
				}
				return;
			}
			if (this.current != '')
				Element.hide( this.current );
			this.current = toPg;
			if (toPg != '')
				Element.show( this.current );
		}
	} );

var modAwrap = Class.create( PgDisp, {
		initialize: function( $super, toPg ){
			$super(toPg);
			this.hideOthers();	
			if (toPg != '')
				Element.show( toPg );
			this.topic = sCurTopic;
		},
		
		hideOthers: function(){
			// called once when the page is first drawn			
			switch( this.current ){
				case 'bodyHeaderTableDiv':
					Element.hide('bodyHeaderFullDiv');
					break;
				case 'bodyHeaderFullDiv':
					Element.hide('bodyHeaderTableDiv');
					break;
				default:
					break;
			}
		}
	} );

var modA = Class.create( PgDisp, {
		initialize: function( $super, toPg ){
			$super(toPg);
			this.hideOthers();	
			if (toPg != '')
				Element.show( toPg );
			this.topic = sCurTopic;
		},
		
		hideOthers: function(){
			// called once when the page is first drawn			
			switch( this.current ){
				case 'maintopic':
					Element.hide('profileHead');
					break;
				case 'profileHead':
					Element.hide('maintopic');
					break;
				default:
					break;
			}
		},
		
		changeTopic: function( toTopic ){
			if (this.topic == toTopic){
				this.togTo('maintopic');
				return;
			}
			this.topic = toTopic;
			$('maintopic').innerHTML = "Loading...";
			this.togTo('maintopic');
			new Ajax.Updater( 'maintopic', curPath+'handler.php', {parameters: {'event':'buildmaintopic', 'fortopic':toTopic}});
		}
	} );

var modB = Class.create( PgDisp, {
		initialize: function( $super, toPg ){
			$super(toPg);
			this.hideOthers();
			if (toPg != '')
				Element.show( toPg );
		},
		
		hideOthers: function(){
			switch( this.current ){
				case 'videocontent':
					Element.hide('surveyform');
					Element.hide('profileAlerts');
					break;
				case 'surveyform':
					Element.hide('videocontent');
					Element.hide('profileAlerts');
					break;
				case 'profileAlerts':
					Element.hide('videocontent');
					Element.hide('surveyform');
					break;
				default:
					break;
			}
		}
	} );

var modC = Class.create( PgDisp, {
		initialize: function( $super, toPg){
			$super(toPg);
			this.hideOthers();
			if (toPg != '')
				Element.show( toPg );
		},
		
		hideOthers: function(){
			switch( this.current ){
				case 'fullBodyDiv':
					Element.hide('tableBodyDiv');
					break;
				case 'tableBodyDiv':
					Element.hide('fullBodyDiv');
					break;
				default:
					break;
			}
		}
	} );

var modC1 = Class.create( PgDisp, {
		initialize: function( $super, toPg){
			$super( toPg );
			this.hideOthers();
			if(toPg != '')
				Element.show( toPg );
		},
		
		hideOthers: function(){
			switch( this.current ){
				case 'filmDetails':
					Element.hide('fullBody');
					break;
				case 'fullBody':
					Element.hide('filmDetails');
					Element.hide('filmDetailsMsg');
					break;
				default:
					break;
			}
		}
	} );

var modC3 = Class.create( PgDisp, {
		initialize: function( $super, toPg){
			$super( toPg );
			this.hideOthers();
			if(toPg != '')
				Element.show( toPg );
		},
		
		hideOthers: function(){
			switch( this.current ){
				case 'favoritesDiv':
					Element.hide('thumbnailwrapper');
					break;
				case 'thumbnailwrapper':
					Element.hide('favoritesDiv');
					break;
				case '':
					Element.hide('thumbnailwrapper');
					Element.hide('favoritesDiv');
					break;
				default:
					break;
			}
		}
		
	} );

var modC2 = Class.create( PgDisp, {
			initialize: function( $super, toPg){
				$super( toPg );
				this.hideOthers();
				if (toPg != '')
					Element.show( toPg );
			}, 
			
			hideOthers: function(){
				switch( this.current ){
					case 'bannerDiv':
						Element.hide('loginDetails');
						Element.hide('contentWrapper');
						break;
					case 'loginDetails':
						Element.hide('bannerDiv');
						Element.hide('contentWrapper');					
						break;
					case 'contentWrapper':
						Element.hide('bannerDiv');
						Element.hide('loginDetails');					
						break;
					case '':
						Element.hide('bannerDiv');
						Element.hide('loginDetails');					
						Element.hide('contentWrapper');
						break;
					default:
						break;
				}
			}
	} );
