/**
 * Apply history list widget on ul elements.
 * 
 * The `.presentationUiHistory()` method display the history items in a list element.
 * 
 * ### Examples
 * Make all ul elements to history list.
 * 
 * JavaScript:
 * 
 *     de_epages('ul')
 *         .presentationUiHistory();
 * 
 * 
 * @class jQuery.ui.presentationUiHistory
 * @extends jQuery.widget
 * 
 * @uses de_epages.presentation.history
 * @uses jQuery.ui.widget
 * @uses jQuery.tmpl
 * @uses jQuery.dict
 * @uses ep.alert
 * @uses ep.ajax
 * @since 6.13.0
 */

/**
 * See `jQuery.ui.presentationUiHistory` for details.
 * 
 * @method presentationUiHistory
 * @member jQuery
 * 
 * @since 6.13.0
 */

/*
 * @copyright		© Copyright 2006-2010, epages GmbH, All Rights Reserved.
 *
 * @module			de_epages.presentation.ui.history
 */

define( "de_epages/presentation/ui/history", [
	"jquery",
	"ep",
	"de_epages",
	"util/string",
	"$dict!../dictionary",
	"$tmpl!./history",

	"jquery/ui/widget",
	"ep/alert",
	"ep/ajax",
	"de_epages/presentation/history"
], function( $, ep, de_epages, str, presentationDict, tmplHistory ){

/*
 * @dictionary		de_epages.presentation.dictionary
 *
 * @translation		{NoHistoryItems}
 *					{ObjectNoLongerExists}
 *					{AddToFavorites}
 *					{ClearHistory}
 */

	var historyHandle			= de_epages.presentation.history,
		tNoHistoryItems			= presentationDict.translate('NoHistoryItems'),
		tObjectNoLongerExists	= presentationDict.translate('ObjectNoLongerExists'),
		tAddToFavorites			= presentationDict.translate('AddToFavorites'),
		tClearHistory			= presentationDict.translate('ClearHistory');

	// widget to display history
	$.widget( 'ui.presentationUiHistory', {

		_create: function(){
			this.element
				.addClass('de_epages-presentationUiHistory');
		},

		_init: function(){
			this._refreshList();
			if( window.dojo && window.$$ ){
				this._dojoFavorites = $$('favoritesWidget');
			}
		},

		_refreshList: function(){
			var tmpl = tmplHistory(
					{
						tNoHistoryItems:	tNoHistoryItems,
						tAddToFavorites:	tAddToFavorites,
						tClearHistory:		tClearHistory,
						history:			historyHandle.get()
					},
					{
						shrink: function( string ){
							return str.shrink( string, {ratio:0.75,length:30} );
						}
					}
				);

			this.element
				.empty()
				.append( tmpl )
				.find('li > a')
					.on( 'click', $.proxy(this, '_gotToHistoryPoint') );
		},

		_gotToHistoryPoint: function( event ){
			event.preventDefault();

			var self			= this,
				elem			= $(event.target),
				historyIndex	= elem.data('index'),
				historyItem		= ( historyHandle.get() )[ historyIndex ];

			if( elem.is('.de_epages-presentationUiHistory-clear *') ){
				historyHandle.remove();
				self._refreshList();
			}
			else if( elem.is('.addToFavorite') ){
				// add to favorites
				event.stopPropagation();
				if( this._dojoFavorites ){
					this._dojoFavorites.addHistoryToFavorites({
						Name:		historyItem.title,
						ObjectID:	historyItem.objectId,
						URI:		'?ViewAction='+historyItem.viewAction+'&ObjectID='+historyItem.objectId
					});
				}
			}
			else{
				// check object exists
				ep.ajax({
					dataType:	'json',
					data:		{
						'ViewAction':		'JSONObjectExists',
						'ObjectID':			ep.config.objectId,
						'CheckObjectID':	historyItem.objectId
					}
				})
				.done(function( check ){
					if( check.ObjectExists ){
						// got to history point
						location.href = '?ViewAction='+historyItem.viewAction+'&ObjectID='+historyItem.objectId;
					}
					else{
						// show error
						ep.alert({
							content:	tObjectNoLongerExists,
							ok:			function(){}
						});

						historyHandle.removeByObjectId(historyItem.objectId);
						self._refreshList();
					}
				});
			}
		},

		destroy: function(){
			this.element
				.removeClass('de_epages-presentationUiHistory')
				.empty();

			this._superApply(arguments);
		}

	});

	return de_epages;

});