﻿
/**
*	@class 				DefaultScreen
*
*	@usage 				Used to display the defualt screen (index.php)
*
*	@author 			Daniel Ivanovic dan@anti-blanks.co.uk
*/

var DefaultScreen = AbstractSiteScreen.extend(
	{
		/**
		*   init
		*
		*   Constructs the screen
		*
		*	@param	m - manager instance
		*	@param	h - view holder instance 
		*/
		init: function(m, h)
		{
			this._super( "DefaultScreen", m, h );
			
			var obj = this;
			
			// define all props
			this.compAncmts = null;
			this.compTicker = "div#ticker";
			
			// listen to applicable events
			$( this.manager.getCore() ).bind( 'onAnnouncementsDataReceived', function( evt, data ) { obj.updateComponents( evt, data ); } );
			$( this.manager.getCore() ).bind( 'onAnnouncementPostSuccess', function( evt, data ) { obj.updateComponents( evt, data ); } );
			$( this.manager.getCore() ).bind( 'onAnnouncementPostFailed', function( evt, data ) { obj.updateComponents( evt, data ); } );
			$( this.manager.getCore() ).bind( 'onGetTickerNewsFailed', function( evt, data ) { obj.updateComponents( evt, data ); } );
			$( this.manager.getCore() ).bind( 'onGetTickerNewsSuccess', function( evt, data ) { obj.updateComponents( evt, data ); } );
		},
		
		/**
		*   setup
		*
		*   Sets up the view instance
		*
		*	@param	o - forum owner id
		*/
		setup: function()
		{
			//alert( this.getType() + " setting up" );
			
			// call super last once all setup is complete
			// this will init the screen & build all the views
			this._super();
		},
		
		/**
		*   build
		*
		*   Builds the view
		*/
		build: function()
		{
			//alert( this.getType() + " building" );
			
			// default ticker to hidden
			$( this.compTicker ).hide();
			
			// get the news stories
			this.getTickerNews();
			
			// load all abstract page elements
			this._super();
		},
		
		/**
		*   buildComponents
		*
		*   Builds the screen components
		*/
		buildComponents: function()
		{
			//alert( "building components" );
			
			// setup page components
			
			// @@ announcements
		 	this.compAncmts = new Announcements( this.manager, "div#comp_ancmnts" );
			this.compAncmts.setup();
			
			// build abstract components
			this._super();
		},
		
		/**
		*   getTickerNews
		*
		*   Gets a list of ticker news stories
		*/
		getTickerNews: function()
		{
			this.manager.getControl( "NewsControl" ).getTickerNews();
		},
		
		/**
		*   loadTickerNews
		*
		*   Loads the ticker news ont the news ticker
		*
		*	@param	d - array of ticker news stories
		*/
		loadTickerNews: function(d)
		{
			//alert( "loading ticker news " + d.length );
			
			// show the ticker
			$( this.compTicker ).show();
			
			// add the news stories
			for ( var i=0; i<d.length; i++ )
			{
				//alert( $( this.compTicker + " > ul" ) );
				$( this.compTicker + " > ul#hp_ticker" ).append( '<li><span>' + d[i].date + '</span><a href="' + d[i].url + '">' + d[i].title + '</a></li>' );	
			}
			
			// start the scrolling
			$( this.compTicker + " > ul#hp_ticker" ).liScroll();
		},
		
		/**
		*   updateComponents
		*
		*   Updates the screen components
		*
		*	@param	evt - event
		*	@param	data - data object
		*/
		updateComponents: function(evt, data)
		{
			//alert( "updating components : " + evt.type + " : " + data );
			
			switch ( evt.type )
			{
				// @@ case - on get ticker news story success
				case "onGetTickerNewsSuccess":
				
					this.loadTickerNews( data.stories );
				
					break;
					
				// @@ onErrorDetected - error has been detected
				case "onErrorDetected":
				
					switch ( data.getErrorType() )
					{
						// @@ err - swear word detected
						case "ErrorSwearWordDetected":
						
							// post a message
							var msge = new MessageData();
							msge.setMessageType( "bad" );
							msge.setMessage( "We have detected the use of bad language in your text submission. Please try again using appropriate language." );
							this.manager.getControl( "GeneralControl" ).showMessage( msge );
						
							break;
							
						// @@ err - time restricted
						case "ErrorTimeRestriction":
						
							// post a message
							var msge = new MessageData();
							msge.setMessageType( "bad" );
							msge.setMessage( "You cannot add another announcement for " + data.getErrorData().timeout + " seconds." );
							this.manager.getControl( "GeneralControl" ).showMessage( msge );
						
							break;
					}
				
					break;
			}
			
			// update any components
			this.compAncmts.update( evt.type, data );
			
			// update abstract components
			this._super(evt, data);
		}
	}
);

/*******************************************************************************************************************

UI Classes

********************************************************/

/**
*   Class : AnnouncementData
*
*   Class holds all data necessary to establish an announcement element
*
*   @usage    Used by all forum elements
*   @author   Daniel Ivanovic dan@substance001.com
*/
var AnnouncementData = AbstractObject.extend(
	{
		/**
		*   init
		*
		*   Constructs the data object
		*/
		init: function()
		{
			this._super( "AnnouncementData" );
			
			// announcement id
			this.announcementId = 0;
			// announcement
			this.announcement = '';
			// author
			this.author = 0;
			// author name
			this.authorName = '';
			// author profile
			this.authorProfile = null;
			
		},
		
		/**
		*   setAnnouncementId / getAnnouncementId
		*
		*   Sets / Returns the announcement id
		*/
		setAnnouncementId: function(s) { this.announcementId = s; },
		getAnnouncementId: function() { return this.announcementId; },
		
		/**
		*   setAnnouncement / getAnnouncement
		*
		*   Sets / Returns the announcement string
		*/
		setAnnouncement: function(s) { this.announcement = s; },
		getAnnouncement: function() { return this.announcement; },
		
		/**
		*   setAuthor / getAuthor 
		*
		*   Sets / Returns the post author
		*/
		setAnnouncementAuthor: function(s) { this.author = s; },
		getAnnouncementAuthor: function() { return this.author; },
		
		/**
		*   setAuthorName / getAuthorNmae 
		*
		*   Sets / Returns the post author name
		*/
		setAnnouncementAuthorName: function(s) { this.authorName = s; },
		getAnnouncementAuthorName: function() { return this.authorName; },
		
		/**
		*   setAnnouncementAuthorProfile / getAnnouncementAuthorProfile 
		*
		*   Sets / Returns the post author profile data object
		*/
		setAnnouncementAuthorProfile: function(s) { this.authorProfile = s; },
		getAnnouncementAuthorProfile: function() { return this.authorProfile; }
	}
);

/**
*   Class : AnnouncementsData
*
*   Class holds all data necessary to esatblish an announcements element
*
*   @usage    Used by all forum elements
*   @author   Daniel Ivanovic dan@substance001.com
*/
var AnnouncementsData = AbstractObject.extend(
	{
		/**
		*   init
		*
		*   Constructs the data object
		*/
		init: function()
		{
			this._super( "AnnouncementsData" );
			
			// announcements
			this.announcements = [];
		},
		
		/**
		*   setAnnouncements / getAnnouncements
		*
		*   Sets / Returns the announcements array
		*/
		setAnnouncements: function(s) { this.announcements = s; },
		getAnnouncements: function() { return this.announcements; }
	}
);

/**
*	@class 				Announcements
*
*	@usage 				Used to display the announcements element
*
*	@author 			Daniel Ivanovic dan@anti-blanks.co.uk
*/

var Announcements = AbstractView.extend(
	{
		/**
		*   init
		*
		*   Constructs the announcements
		*
		*	@param	m - manager instance
		*	@param	h - view holder instance
		*/
		init: function(m, h)
		{
			this._super( "Announcements", m, h );
			
			// define props
			var obj = this;
			this.ancmtMsge = null;
			this.ancmtForm = null;
			this.announcements = [];
			
			// @@ static
			this.STATE_OPEN = "stateOpen";
			this.STATE_CLOSED = "stateClosed";
			this.MAX_PAGE_RESULTS = 10;
			this.PAGE_DEFAULT = 1;
			this.MAX_INPUT_LENGTH = 125;
		},
		
		/**
		*   setup
		*
		*   Sets up the view instance
		*/
		setup: function()
		{
			//alert( "setting up" );
			
			// build the view
			this.build();
		},
		
		/**
		*   build
		*
		*   Builds the view
		*/
		build: function()
		{
			//alert( "building" );
			
			var obj = this;
			
			// flush the holder
			this.flush( this );
			
			// define the cta message
			this.ancmtMsge = $( "li#comp_ancmnts_msge" );
			
			// define the form and set up the button
			this.ancmtForm = $( "li#comp_ancmnts_frm" );
			$("a#btn_ancmnts_shout").click( function() {
				obj.postAnnouncement( $( "textarea[name=frm_ancmnts_ancmnt]" ).val() );
				return false;
			});
			
			// build the post form
			// based upon whether there is a logged in user or not
			var loggedInUser = this.manager.getCore().getUserProfile();
			//alert( "logged in user : " + loggedInUser );
			if ( loggedInUser == 0 || loggedInUser == null ) {
				// show closed forum
				this.setElementState( this.STATE_CLOSED );
			} else {
				// show open forum
				this.setElementState( this.STATE_OPEN );
			}/**/
			
			// get the announcements
			// from the control
			this.manager.getControl( "AnnouncementsControl" ).getAnnouncements();
		},
		
		/**
		*  	displayAnnouncements
		*
		*   Displays all the anouncements
		*
		*	@param	a - announcments array
		*/
		displayAnnouncements: function(a)
		{
			//alert( "displaying announcements " + a.length );
			
			var obj = this;
			
			// flush the holder
			this.flush( this );
			
			for ( var i=0; i<a.length; i++ )
			{
				// define the author
				var aAuthorProfile = a[i].getAnnouncementAuthorProfile();
				
				// create the announcement
				var ancmt = $(document.createElement( "li" )).addClass( "hp_ancmnt" ).append(
					'<div class="ancmnt_hdr">' +
					   ' <div class="left">' +
							'<span class="bc_9_ffffff_b">' + aAuthorProfile.getName() + ' Says...</span>' +
						'</div>' +
						'<div class="right"></div>' +
					'</div>' +
					'<div class="ancmnt_cnt">' +
						'<div class="left">' +
							'<a href="' + this.manager.core.getPrefix() + 'pages/user/profile.php?utype=' + aAuthorProfile.getUserType() + 
							'&pid=' + aAuthorProfile.getProfileId() + '" target="_self"><img src="' + this.manager.core.getPrefix() + aAuthorProfile.getUserDirectory() + 
							'images/thumbnail/' + aAuthorProfile.getThumbPhoto() + '" width="46px" height="46px" border="0px" /></a>' +
						'</div>' +
						'<div class="right">' +
							'<span class="bc_9_ffffff_b">' + a[i].getAnnouncement() + '</span>' +
						'</div>' +
					'</div>'	 
				);
				
				// register as an element
				// so that we can easily remove it
				this.addElement( "Announcement_"+i, ancmt );
				
				// and append
				$( this.getHolder() + " > ul" ).append( ancmt );
			}
		},
		
		/**
		*	postAnnouncement
		*
		*	Called upon request to post announcement ( make an announcement ), Checks data before actioning
		*
		*	@param	s - shout text
		*/
		postAnnouncement: function(s)
		{
			//alert( this.type + " posting announcement" );
			
			var er = new ErrorChecker();
			var chk1 = er.checkString( s );
			
			if ( !chk1.pass ) {
				// show message
				var msge = new MessageData();
				msge.setMessageType( "bad" );
				msge.setMessage( "Please enter a valid announcement" );
				this.manager.getControl( "GeneralControl" ).showMessage( msge );
			} else if ( s.length > this.MAX_INPUT_LENGTH ) {
				// show message
				var msge = new MessageData();
				msge.setMessageType( "bad" );
				msge.setMessage( "Your announcement must be less than " + this.MAX_INPUT_LENGTH + " characters" );
				this.manager.getControl( "GeneralControl" ).showMessage( msge );
			} else {
				// make the announcement
				this.manager.getControl( "AnnouncementsControl" ).postAnnouncement( s );
			}
		},
		
		/**
		*   setElementState
		*
		*   Sets the element state
		*
		*	@param	s - state string
		*/
		setElementState: function(s)
		{
			//alert( "setting state : " + s );
			
			switch (s)
			{
				// @@ case - open
				case this.STATE_OPEN:
				
					// show the form
					$( this.ancmtForm ).show();
					// hide the cta message
					$( this.ancmtMsge ).hide();
				
					break;
					
				// @@ case - closed
				case this.STATE_CLOSED:
				
					// hide the form
					$( this.ancmtForm ).hide();
					// show the cta message
					$( this.ancmtMsge ).show();
				
					break;
			}
		},
		
		/**
		*   update
		*
		*   Updates the view
		*
		*	@param	t - event type
		*	@param	d - data
		*/
		update: function(t, d)
		{
			//alert( this.type + " updating : " + t + " : " + d );
			
			switch ( t )
			{
				// @@ onAnnouncementsDataReceived - announcements data has been received
				case "onAnnouncementsDataReceived":
				
					// build the announcements
					this.displayAnnouncements( d.getAnnouncements() );
				
					break;
					
				// @@ onAnnouncementPostSuccess - announcement post has been a success
				case "onAnnouncementPostSuccess":
				
					// post a message
					var msge = new MessageData();
					msge.setMessageType( "good" );
					msge.setMessage( "Your announcement has been made." );
					this.manager.getControl( "GeneralControl" ).showMessage( msge );
					
					// get the announcements
					// from the control
					this.manager.getControl( "AnnouncementsControl" ).getAnnouncements();
				
					break;
					
				// @@ onAnnouncementPostFailed - announcement post has failed
				case "onAnnouncementPostFailed":
				
					// post a message
					var msge = new MessageData();
					msge.setMessageType( "bad" );
					msge.setMessage( "Sorry! There has been an error. Please try again later." );
					this.manager.getControl( "GeneralControl" ).showMessage( msge );
				
					break;
			}
			
			// update abstract components
			this._super(t, d);
		}
	}
);

/*******************************************************************************************************************

Ready...

********************************************************/

$(document).ready( function() 
	{
		// create class objects
		
		// @@ application
		
		app = new Application( PREFIX );
		
		// @@ page
		
		page = new DefaultScreen( app.generalManager );
	}
);