/**
* HGPageWhatsNewComponent
*
* A popup which contains information on the latest updates pulled via a HGMessagePCommand command
*
* @param hg_page The associated hg_page
**/
function HGPageWhatsNewComponent( hg_page ) { 

	this.style_set = "hg_page_whats_new";

	this.hg_page = hg_page;

	this.whats_new_popup = new SimplePopupWindow( 
		"whats_new_popup_window" ,
		"What's New" ,
		"hg_popup" , 
		400 , 
		600 , 
		15 ,
		true , 
		false ,
		false
	);

	this.whats_new_widget = new SimpleWidgetFactory( "hg_page_whats_new" ).build();

	this.message_count = 0;

	this.message_area = new SimpleWidgetFactory( "hg_page_whats_new_message_area" ).build();
	
	this.refresh_button = new SimpleButtonWidgetFactory( this.style_set + "_controls", new HGPageWhatsNewRefreshCommand( this ) ).build();
	this.refresh_button.addContentNode( textToDOM( 'span', 'refresh', this.style_set + "_controls" ) );

	this.older_message_button = new SimpleButtonWidgetFactory( this.style_set + "_controls", null ).build();
	this.older_message_button.addContentNode( textToDOM( 'span', 'get older messages', this.style_set + "_controls" ) );

	this.whats_new_popup.addControlButton( this.refresh_button );
	this.whats_new_popup.addControlButton( this.older_message_button );

	this.whats_new_widget.addContentNode( this.message_area.getWidgetDOM() );

	this.whats_new_popup.body.addContentNode( this.whats_new_widget.getWidgetDOM() );
	this.whats_new_popup.header.addContentNode( textToDOM( "span", " ** New Stuff ** " ) );
	
}

new HGPageWhatsNewComponent();

/**
* HGPageWhatsNewComponent.setStatusToWorking
**/
HGPageWhatsNewComponent.prototype.setStatusToWorking = function( msg ) { 
	this.whats_new_popup.setStatusToWorking( "working", msg );
}

/**
* HGPageWhatsNewComponent.clearStatus
**/
HGPageWhatsNewComponent.prototype.clearStatus = function() { 

	this.whats_new_popup.clearStatus();

}

/**
* HGPageWhatsNewComponent.clearMessages
**/
HGPageWhatsNewComponent.prototype.clearMessages = function() { 
	this.message_area.clearContents();
	this.message_count = 0;
}

/**
* HGPageWhatsNewComponent.addNewMessage
*
* Renders a message into a DOM and adds it to the message list
*
* @param msg_date
* @param msg_type
* @param msg_from
* @param msg_text
* @param msg_link_xml
**/
HGPageWhatsNewComponent.prototype.addNewMessage = function( msg_date, msg_type, msg_from, msg_text, msg_link_xml ) { 

	var even_odd = "even";

	if ( this.message_count % 2 != 0 ) 
		even_odd = "odd";

	var new_command = null;

	if ( this.hg_page ) { 

		switch( msg_type ) {
			case "HGContent": 
			
				var content_type_id = msg_link_xml.getElementsByTagName( 'content_type_id' )[0].childNodes[0].nodeValue;
				var content_id_xml = msg_link_xml.getElementsByTagName( 'content_id' )[0];
				if ( content_id_xml ) 
					var content_id = content_id_xml.childNodes[0].nodeValue;
				else
					var content_id = 0;

				//new_command = new HGContentPCommand( content_type_id, content_id, this.hg_page.content_context );
				new_command = new ClosePopupHGContentPCommand( content_type_id, content_id, this.hg_page.content_context, this.whats_new_popup )

				break;
			case "HGContentReply": 
			
				var content_type_id = msg_link_xml.getElementsByTagName( 'content_type_id' )[0].childNodes[0].nodeValue;
				var content_id_xml = msg_link_xml.getElementsByTagName( 'content_id' )[0];
				if ( content_id_xml ) 
					var content_id = content_id_xml.childNodes[0].nodeValue;
				else
					var content_id = 0;

				//new_command = new HGContentPCommand( content_type_id, content_id, this.hg_page.content_context );
				new_command = new ClosePopupHGContentPCommand( content_type_id, content_id, this.hg_page.content_context, this.whats_new_popup )

				break;
			case "HGComic":
		
				var comic_type_id = msg_link_xml.getElementsByTagName( 'comic_type_id' )[0].childNodes[0].nodeValue;
				var comic_id_xml = msg_link_xml.getElementsByTagName( 'comic_id' )[0];
				if ( comic_id_xml ) 
					var comic_id = comic_id_xml.childNodes[0].nodeValue;
				else
					var comic_id = 0;
	
				//new_command = new HGComicPCommand( comic_type_id, comic_id, this.hg_page.comic_context );
				new_command = new ClosePopupHGComicPCommand( comic_type_id, comic_id, this.hg_page.comic_context, this.whats_new_popup )

				break;
		}

	}

	//each message is a button IF the component has an associated HGPage
	var new_message = new SimpleButtonWidgetFactory( this.style_set + "_message_" + even_odd, new_command ).build();
	
	var msg_date = PHPTimestampToDate( msg_date );

	var date_span = textToDOM( 'span', "DATE: " + msg_date.getFullYear() + "-" + msg_date.getMonth() + "-" + msg_date.getDate() + " ", this.style_set + "_msg_date" );
	date_span.alt = msg_date;

	new_message.addContentNode( date_span );
	new_message.addContentNode( textToDOM( 'span', "USER: " + msg_from + " ", this.style_set + "_msg_from" ) );
	new_message.addContentNode( textToDOM( 'span', "<br/>" + msg_text + " ", this.style_set + "_msg_text" ) );

	this.message_area.addContentNode( new_message.getWidgetDOM() );

	this.message_count ++;

}

/**
* HGPageWhatsNewComponent.pullWhatsNew
*
* @param n The number of items to pull
**/
HGPageWhatsNewComponent.prototype.pullWhatsNew = function( n ) { 

	var pull_command = new HGMessagePCommand( this, n );
	pull_command.execute();

}

/**
* HGPageWhatsNewComponent.place
**/
HGPageWhatsNewComponent.prototype.place = function() { 
	document.body.windowing_environment.addPopupWindow( this.whats_new_popup );
}

/**
* HGPageWhatsNewComponent.setMoreDate
*
* Set which date should be used in the PULL MORE command
*
* @param d The date to set
**/
HGPageWhatsNewComponent.prototype.setMoreDate = function( d ) { 

	this.older_message_button.setCommand( new HGPageWhatsNewPullOlderCommand( this, d ) );

}

/**
* HGPageWhatsNewComponent.getDOM
**/
HGPageWhatsNewComponent.prototype.getDOM = function() { 
	
	return thiswhats_new_popup.getDOM();

}

/**
* HGMessagePCommand
*
* Pull a number of messages
*
* @param target The HGPageWhatsNewComponent 
* @param n The number of messages to pull
* @param from_date A timestamp which the user wants to pull messages from
**/
function HGMessagePCommand( target, n, from_date ) { 

	this.target = target;
	this.n = n;
	this.from_date = from_date;

}

new HGMessagePCommand();

HGMessagePCommand.prototype.execute = function() { 

	//set up the HGRequest
	var nreq = new HGRequestObject( "HGMessagePCommand" );
	nreq.addRequestElement( 'n', this.n );

	if ( this.from_date && this.from_date != null ) { 
		nreq.addRequestElement( 'date', this.from_date );
	}
	//if we are not pulling from a date we should wipe out any messages currently in the component
	else { 
		this.target.clearMessages();
	}
		

	var af = AJAXRequestFunctionFactory( this.target, handleHGMessageRequest );
	XMLHTTPRequestCoordinator( "php/HGContentGetRequestC.php", af, "GET", nreq );

	this.target.setStatusToWorking( "Pulling updates" );

}

function handleHGMessageRequest( x, t ) { 

	//pull the HGResponse element
	var hgr = x.getElementsByTagName( "HGResponse" )[0];

	var hgm = hgr.getElementsByTagName( "HGMessage" );
	
	//t.clearMessages();

	for ( var i=0; i<hgm.length; i++ ) { 

		var msg_date = hgm[i].getElementsByTagName( "msg_date" )[0].childNodes[0].nodeValue;
		var msg_type = hgm[i].getElementsByTagName( "msg_type" )[0].childNodes[0].nodeValue;
		var msg_from = hgm[i].getElementsByTagName( "msg_from" )[0].childNodes[0].nodeValue;
		var msg_text = hgm[i].getElementsByTagName( "msg_text" )[0].childNodes[0].nodeValue;
		var msg_link_xml = hgm[i].getElementsByTagName( "msg_link_xml" )[0];

		t.addNewMessage( msg_date, msg_type, msg_from, msg_text, msg_link_xml );

	}

	if ( hgm.length ) { 

		var i = hgm.length - 1;
		t.setMoreDate( hgm[i].getElementsByTagName( "msg_date" )[0].childNodes[0].nodeValue );

	}

	t.clearStatus();

}

/**
* HGPageWhatsNewRefreshCommand
*
* @param t The HGPageWhatsNewComponent that we are refreshing
**/
function HGPageWhatsNewRefreshCommand( t ) { 
	this.target = t;
}

new HGPageWhatsNewRefreshCommand();

HGPageWhatsNewRefreshCommand.prototype.execute = function() { 

	var msg_count = this.target.message_count;
	//clear the messages
	//this.target.clearMessages();
	//execute a HGMessagePCommand
	var hmpc = new HGMessagePCommand( this.target, msg_count );
	hmpc.execute();

}

/**
* HGPageWhatsNewPullOlderCommand
*
* @param t The HGPageWhatsNewComponent that we are pulling messages for
* @param d The date to pull from
**/
function HGPageWhatsNewPullOlderCommand( t, d ) { 
	this.target = t;
	this.from_date = d;
}

new HGPageWhatsNewPullOlderCommand();

HGPageWhatsNewPullOlderCommand.prototype.execute = function() { 

	//execute a HGMessagePCommand
	var hmpc = new HGMessagePCommand( this.target, 5, this.from_date );
	hmpc.execute();

}
/**
* HGPageWhatsNewMakePopupCommand
*
* @param hg_page
**/
function HGPageWhatsNewMakePopupCommand( hg_page ) { 
	
	this.hg_page = hg_page;

}

new HGPageWhatsNewMakePopupCommand();

HGPageWhatsNewMakePopupCommand.prototype.execute = function() { 

	var p = new HGPageWhatsNewComponent( this.hg_page );
	p.place();
	p.pullWhatsNew( 7 );

}

function whatsNewPopupButtonFactory( hg_page ) { 

	var rbut = new SimpleButtonWidget( hg_page.style_set + "_control_button", new HGPageWhatsNewMakePopupCommand( hg_page ) );

	rbut.addContentNode( textToDOM( "span", "*What's New*" ) );

	return rbut;

}

/**
* ClosePopupHGContentPCommand
*
* Close the popup window and pull content
**/
function ClosePopupHGContentPCommand( content_type_id, content_id, content_context, popup_window ) {

	this.content_type_id = content_type_id;
	this.content_id = content_id;
	this.content_context = content_context;
	this.popup_window = popup_window;

}

new ClosePopupHGContentPCommand();

ClosePopupHGContentPCommand.prototype.execute = function() {

	var hcpc = new HGContentPCommand( this.content_type_id, this.content_id, this.content_context );
	hcpc.execute();

	//close the popup window
	document.body.windowing_environment.removePopupWindow( this.popup_window );

	//scroll to the content context
	this.content_context.assoc_hg_page.scrollTo( parseInt( this.content_context.assoc_hg_page.getContentContextPosition().top ) - 50 );

	
}

/**
* ClosePopupHGComicPCommand
*
* Close the popup window and pull comic
**/
function ClosePopupHGComicPCommand( comic_type_id, comic_id, comic_context, popup_window ) {

	this.comic_type_id = comic_type_id;
	this.comic_id = comic_id;
	this.comic_context = comic_context;
	this.popup_window = popup_window;

}

new ClosePopupHGComicPCommand();

ClosePopupHGComicPCommand.prototype.execute = function() {

	var hcpc = new HGComicPCommand( this.comic_type_id, this.comic_id, this.comic_context );
	hcpc.execute();

	//close the popup window
	document.body.windowing_environment.removePopupWindow( this.popup_window );

	//scroll to the content context
	this.comic_context.assoc_hg_page.scrollTo( parseInt( this.comic_context.assoc_hg_page.getComicContextPosition().top ) - 50 );

	
}


