/**
* HGPage
*
* The HGPage is the organization of the web page as a whole.  The page contains the following sections:
*
* -Header
* -Page Level Navigation Controls
* -Page Level Controls
* -Comic Context
* -Content Context
* -List Context
* -Footer
*
* @param user_id The user_id of the logged on user if one is logged on
* @param access_level The access_level of the logged on user if one is logged on
**/
function HGPage( user_id, access_level ) { 

	this.user_id = user_id;
	this.access_level = access_level;

	this.style_set = "HGPage";

	//create the container widget
	this.container = new SimpleWidgetFactory( "HGPage_container" ).build();

	//create the header widget
	this.header = new SimpleWidgetFactory( "HGPage_header" ).build();

	//add the header image to the header
	var header_img = document.createElement('img');
	//header_img.src = "img/page/headerfg1.png";
	header_img.src = "img/page/headerfg1.gif";
	this.header.addContentNode( header_img );

	//create the page level navigation controls widget
	this.page_level_nav_controls_container = new SimpleWidgetFactory( "HGPage_nav_controls" ).build();
	//this.page_level_nav_controls = new TabularWidgetLayoutFactory( "HGPage_nav_controls", 5 ).build();
	this.page_level_nav_controls = new VerticalComponentLayout( "HGPage_nav_controls", 2 );
	this.page_level_nav_controls_container.addContentNode( this.page_level_nav_controls.getDOM() );

	//create the page level controls widget
	this.page_level_controls_container = new SimpleWidgetFactory( "HGPage_controls" ).build();
	this.page_level_controls = new TabularWidgetLayoutFactory( "HGPage_controls", 5 ).build();
	this.page_level_controls_container.addContentNode( this.page_level_controls.getDOM() );
	
	//create the footer widget
	this.footer = new SimpleWidgetFactory( "HGPage_footer" ).build();

	//generate the list context
	this.list_context = new HGListContext( "HGPage" );
	this.list_context.hideContext();

	//generate the comic context
	this.comic_context = new HGComicContext( "hg_comic", this.user_id, this.access_level, this );

	//generate the content context
	this.content_context = new HGContentContext( "hg_content", this.user_id, this.access_level, this );

	//generate the navigation buttons
	this.content_button = contentTypeListButtonFactory( this );
	this.comic_button = comicTypeListButtonFactory( this );

	/*
	this.page_level_nav_controls.addWidget( this.content_button );
	this.page_level_nav_controls.addWidget( this.comic_button );
	*/
	this.page_level_nav_controls.addComponent( this.content_button );
	this.page_level_nav_controls.addComponent( this.comic_button );
	

	//generate page control buttons
	this.logon_button = logonPopupButtonFactory( this );
	this.logoff_button = logoffButtonFactory( this );
	this.new_user_button = newUserPopupButtonFactory( this );
	this.whats_new_button = whatsNewPopupButtonFactory( this );
	this.update_user_button = updateUserPopupButtonFactory( this );

	this.page_level_controls.addWidget( this.logon_button );
	this.page_level_controls.addWidget( this.logoff_button );
	this.page_level_controls.addWidget( this.whats_new_button );
	this.page_level_controls.addWidget( this.new_user_button );
	this.page_level_controls.addWidget( this.update_user_button );

	//generate the page link box
	this.page_link_form = new HGForm();
	this.page_link_field = new HGFormElement ( 
		"text" ,
		"" , 
		"page_link" ,
		"20" ,
		"1" ,
		"500" ,
		null ,
		null ,
		" " ,
		null
	);

	this.page_link_form.addHGFormElement( this.page_link_field );

	this.repopulate_link_box_button = repopulateLinkBoxButtonFactory( this );

	this.link_box_layout = new TabularWidgetLayoutFactory( "HGPage_link_box", 3 ).build();

	this.link_box_container = new SimpleWidgetFactory( "HGPage_link_box_container" ).build();
	this.link_box_container.addContentNode( this.page_link_form.form );

	this.link_box_layout.addWidget( this.repopulate_link_box_button );
	this.link_box_layout.addWidget( this.link_box_container );

	this.page_level_controls_container.addContentNode( this.link_box_layout.getDOM() );
	
	//breaker div
	var b_div = document.createElement('div');
	b_div.style.height = "1px";
	b_div.style.overflow = "hidden";
	b_div.style.clear = "both";
		
	this.page_level_controls_container.addContentNode( b_div );

	//modify the button config based on whether or not anyone is logged on
	this.buttonConfigForUser();

	//place everything into the container
	this.container.addContentNode( this.header.getWidgetDOM() );
	this.container.addContentNode( this.page_level_nav_controls_container.getWidgetDOM() );
	this.container.addContentNode( this.page_level_controls_container.getWidgetDOM() );
	this.container.addContentNode( this.list_context.context_container.getWidgetDOM() );
	this.container.addContentNode( this.comic_context.context_container.getWidgetDOM() );
	this.container.addContentNode( this.content_context.context_container.getWidgetDOM() );
	this.container.addContentNode( this.footer.getWidgetDOM() );

}

/**
* HGPage.startPage
*
* Attach the HGPage DOM to the dock and initialize the contexts
*
* @param dock Where the HGPage will be attached (this should be document.body since I don't think it will work with anything else)
**/
HGPage.prototype.startPage = function( dock ) {

	//attach the container to the dock
	dock.appendChild( this.container.getWidgetDOM() );

	//pull GET request values
	var get_request_object = parseLocationSearch();
	
	//higherarchy : 
	//  GET request
	//  cookies
	//  defaults
	
	var content_context_shown = get_request_object['content_context_shown'] || readCookie( "content_context_shown" ) || 1;
	var comic_context_shown = get_request_object['comic_context_shown'] || readCookie( "comic_context_shown" ) || 1;

	var last_content_type_id = get_request_object['content_type_id'] || readCookie( "last_content_type_id" ) || 1;
	var last_comic_type_id = get_request_object['comic_type_id'] || readCookie( "last_comic_type_id" ) || 1;

	var last_content_id = get_request_object['content_id'] || readCookie( "last_content_id" ) || 0;
	var last_comic_id = get_request_object['comic_id'] || readCookie( "last_comic_id" ) || 0;

	//start the contexts
	this.comic_context.loadComic( last_comic_type_id, last_comic_id );
	this.content_context.loadContent( last_content_type_id, last_content_id );

	if ( content_context_shown == 0 ) {
		this.content_context.hideContext();
	}

	if ( comic_context_shown == 0 ) { 
		this.comic_context.hideContext();
	}

}

/**
* HGPage.resetUserInfo
*
* Used when a user logs on or logs off.  Changes user_id and access_level information, alters page level controls, and reloads the contexts
**/
HGPage.prototype.resetUserInfo = function( user_id, access_level ) { 

	//set user_id and access_level
	this.user_id = user_id;
	this.access_level = access_level;

	//reload contexts
	this.comic_context.resetUserInfo( this.user_id, this.access_level );
	this.content_context.resetUserInfo( this.user_id, this.access_level );

	//alter the page level controls
	this.buttonConfigForUser();

}

/**
* HGPage.buttonConfigForUser
*
* Modify the button configuration based on the user_id field of the hg_page
**/
HGPage.prototype.buttonConfigForUser = function() { 

	if ( this.user_id > 0 ) 
		this.setPageControlButtonConfigurationToLoggedOn();
	else
		this.setPageControlButtonConfigurationToLoggedOff();

}

/**
* HGPage.setPageControlButtonConfigurationToLoggedOn
*
* Show
*   logoff button
*   modify account button
*
* Hide
*   logon button
**/
HGPage.prototype.setPageControlButtonConfigurationToLoggedOn = function() { 

	this.logoff_button.showWidget();
	this.update_user_button.showWidget();
	this.logon_button.hideWidget();
	this.new_user_button.hideWidget();

}

/**
* HGPage.setPageControlButtonConfigurationToLoggedOff
*
* Show
*   logon button
*
* Hide
*   logoff button
*   modify account button
**/
HGPage.prototype.setPageControlButtonConfigurationToLoggedOff = function() { 

	this.logoff_button.hideWidget();
	this.update_user_button.hideWidget();
	this.logon_button.showWidget();
	this.new_user_button.showWidget();

}

/**
* HGPage.repopulateLinkBox
*
* Repopulate the contents of the link box based on the current page state
**/
HGPage.prototype.repopulateLinkBox = function() { 

	var rc = new PopulateLinkBoxCommand( this );
	rc.execute();

}

/**
* HGPage.getListContextPosition
* 
* @returns a x, y pair denoting the current position of the ListContext
**/
HGPage.prototype.getListContextPosition = function() { 

	return ( getTruePosition( this.list_context.context_container.getWidgetDOM() ) );

}

/**
* HGPage.getContentContextPosition
* 
* @returns a x, y pair denoting the current position of the ContentContext
**/
HGPage.prototype.getContentContextPosition = function() { 

	return ( getTruePosition( this.content_context.context_container.getWidgetDOM() ) );

}

/**
* HGPage.getComicContextPosition
* 
* @returns a x, y pair denoting the current position of the ComicContext
**/
HGPage.prototype.getComicContextPosition = function() { 

	return ( getTruePosition( this.comic_context.context_container.getWidgetDOM() ) );

}

/**
* HGPage.scrollTo
*
* Sets up the animation elements necessary to scroll to a particular position denoted by Y
*
* @param y
**/
HGPage.prototype.scrollTo = function( y ) { 

	//alert ( "Scroll To " + y + " :: Currently at : " + getWindowTop() );
	//window.scrollTo( getWindowLeft(), y );
	var new_stepper = new AnimationStepper( 
		y ,
		function ( s ) { 
			if ( s.animation_state == s.object_to_animate ) return false;
			if ( getWindowTop() < ( s.object_to_animate - 20 ) ) { 
				window.scrollTo( getWindowLeft(), parseInt( getWindowTop() ) + 20 );
			}
			else if ( getWindowTop() < s.object_to_animate ) { 
				window.scrollTo( getWindowLeft(), s.object_to_animate );
			}
			else if ( getWindowTop() > ( s.object_to_animate + 20 ) ) { 
				window.scrollTo( getWindowLeft(), parseInt( getWindowTop() ) - 20 );
			}
			else if ( getWindowTop() > s.object_to_animate ) { 
				window.scrollTo( getWindowLeft(), s.object_to_animate );
			}
			
			//check to see if we havent moved
			if ( getWindowTop() == s.animation_state ) return false;

			s.setState( getWindowTop() );

			//alert ( getWindowTop() );

			return true;

		}
	);

	document.body.animation_engine.addAnimationStepper( new_stepper );				

}

/**
* CheckLogonAndStartPageCommand
*
* Goes out to the server to check if anyone is logged on.  If someone is, pull down the user information and use it in the generation of the HGPage
*
* @param dock The DOM element where the page will be generated
**/
function CheckLogonAndStartPageCommand( dock ) { 
	
	this.target = dock;

}

new CheckLogonAndStartPageCommand();

CheckLogonAndStartPageCommand.prototype.execute = function() { 

	//set up the HGRequest
	var nreq = new HGRequestObject( "HGCheckLogonCommand" );

	var af = AJAXRequestFunctionFactory( this.target, handleHGCheckLogonRequest );
	XMLHTTPRequestCoordinator( "php/HGContentGetRequestC.php", af, "GET", nreq );

}

/**
* handleHGCheckLogonRequest
*
* Starts the HGPage using the information obtained from the server concerning whether or not anyone is logged on
**/
function handleHGCheckLogonRequest( x, t ) { 

	var hgr = x.getElementsByTagName( "HGResponse" )[0];
	var user_id = hgr.getElementsByTagName( "user_id" )[0].childNodes[0].nodeValue;
	var access_level = hgr.getElementsByTagName( "access_level" )[0].childNodes[0].nodeValue;

	//create the HGPage
	var hgp = new HGPage( user_id, access_level );

	//start the page
	hgp.startPage( t );

	//create an animation engine
	var ae = new AnimationEngine();
	ae.startEngine( 100 );

	//create a fixed component manager
	var fcm = new FixedComponentManager();
	fcm.startManager();

	//create the what's new popup
	//var wnc = new HGPageWhatsNewComponent( hgp, ae );

	//wnc.pullWhatsNew( 7 );

	//wnc.place();
	

}

/**
* PopulateLinkBoxCommand
*
* Get the current state information about the HGPage and populate the link box appropriately
*
* @param hg_page
**/
function PopulateLinkBoxCommand( hg_page ) { 

	this.hg_page = hg_page;

}

new PopulateLinkBoxCommand();

PopulateLinkBoxCommand.prototype.execute = function() { 

	var content_type_id = this.hg_page.content_context.content_type_id;
	var content_id = this.hg_page.content_context.content_id;
	var comic_type_id = this.hg_page.comic_context.comic_type_id;
	var comic_id = this.hg_page.comic_context.comic_id;

	this.hg_page.page_link_field.form_element.value = "http://www.recursive-living.com/?content_type_id=" + content_type_id + "&content_id=" + content_id + "&comic_type_id=" + comic_type_id + "&comic_id=" + comic_id;

	return true;	
}

/**
* RepopulateLinkBoxCommand
*
* Calls repopulateLinkBox on the hg_page
*
* @param hg_page
**/
function RepopulateLinkBoxCommand( hg_page ) { 

	this.hg_page = hg_page;

}

new RepopulateLinkBoxCommand();

RepopulateLinkBoxCommand.prototype.execute = function() { 

	this.hg_page.repopulateLinkBox();

}

/**
* repopulateLinkBoxButtonFactory
*
* A factory for building repopulate link box buttons
*
* @param p The HGPage 
**/
function repopulateLinkBoxButtonFactory( p ) { 

	var rbut = new SimpleButtonWidget( p.style_set + "_control_button", new RepopulateLinkBoxCommand( p ) );

	var button_img = document.createElement( 'img' );
	button_img.src = 'img/page/navigation/link.gif';
	button_img.title = "Click to populate the box to the right with a direct link to your current page state."
	rbut.addContentNode( button_img );

	return rbut;

}

