/**
 * @class de_epages.presentation.history
 * 
 */

/*
 * @copyright		© Copyright 2006-2010, epages GmbH, All Rights Reserved.
 *
 * @module			de_epages.presentation.history
 *
 * @revision		$Revision: 1.7 $
 */

define("de_epages/presentation/history", [
	"jquery",
	"de_epages",
	"util/scope",
	"util/storage"
], function ($, de_epages, scope, storage) {

	var undef,

		historyData = storage.localStorage('history') || [],

		historySave = function(){
			storage.localStorage('history', historyData );
		},

		historyHandle = $.extend( scope('de_epages.presentation.history'), {

			/**
			 * Add a page to the history.
			 * 
			 * The `de_epages.presentation.history.add()` method adds a page action to the history storage.
			 * 
			 * ### Dependencies
			 * 
			 *  + `de_epages`
			 *  + `jQuery.storage`
			 *  + `jQuery.scope`
			 * 
			 * @param {Object} [options] Data of the history item to add, if options is not set then the current page added.
			 * @param {Integer} options.objectId An ObjectID associated to the ViewAction.
			 * @param {String} options.viewAction A name of a ViewAction.
			 * @param {String} options.title Title of the history item.
			 * 
			 * @method add
			 * @static 
			 * @member de_epages.presentation.history
			 * 
			 * @since 6.13.0
			 */
			add: function( add ){
				if( !add ){
					add = {
						title:			$('title').text(),
						objectId:		ep.config.objectId,
						viewAction:		ep.config.viewAction
					};
				}
				if( !add.title ){
					add.title = '';
				}
				add.id = add.objectId + ':' + add.viewAction;

				// remove duplicate
				$.each( historyData, function( i, data ){
					if( data.id == add.id ){
						historyData.splice(i,1);
						return false;
					}
				});

				historyData.unshift(add);
				historyData = historyData.splice(0,40);
				historySave();
			},

			/**
			 * Get the history or a history item.
			 * 
			 * The `de_epages.presentation.history.get()` method returns an array of history items or a single history item.
			 * 
			 * ### Dependencies
			 * 
			 *  + `de_epages`
			 *  + `jQuery.storage`
			 *  + `jQuery.scope`
			 * 
			 * @param {Integer} [index] A zero indexed position of a history item.
			 * 
			 * @method get
			 * @static 
			 * @member de_epages.presentation.history
			 * 
			 * @since 6.13.0
			 */
			get: function( i ){
				return i===undef ? historyData : historyData[i];
			},

			/**
			 * Remove the history or a history item.
			 * 
			 * The `de_epages.presentation.history.remove()` method removes all history items or a single history item.
			 * 
			 * ### Dependencies
			 * 
			 *  + `de_epages`
			 *  + `jQuery.storage`
			 *  + `jQuery.scope`
			 * 
			 * @param {Integer} [index] A zero indexed position of a history item.
			 * 
			 * @method remove
			 * @static 
			 * @member de_epages.presentation.history
			 * 
			 * @since 6.13.0
			 */
			remove: function( i ){
				historyData = i===undef ? [] : historyData.splice( i, 1 );
				historySave();
			},

			/**
			 * Remove all history items based on a specified ObjectID.
			 * 
			 * The `de_epages.presentation.history.removeByObjectId()` method removes all history items which are based
			 * on the given ObjectID.
			 * 
			 * ### Dependencies
			 * 
			 *  + `de_epages`
			 *  + `jQuery.storage`
			 *  + `jQuery.scope`
			 * 
			 * @param {Integer} objectId An ObjectID.
			 * 
			 * @method removeByObjectId
			 * @static 
			 * @member de_epages.presentation.history
			 * 
			 * @since 6.13.0
			 */
			removeByObjectId: function( objectId ){
				for( var i=0,iLength=historyData.length ; i<iLength ; i++ ){
					var historyItem = historyData[i];

					if( historyItem.objectId === objectId ){
						historyData.splice(i,1);
						i--;
						iLength--;
					}
				};
				historySave();
			}

		});

	historyHandle.add();

	return de_epages;

});