var mbx_pagegallery = new Class(
/**
 *	@class
 *	@requires Mootools 1.2
 *	@description provides a gallery including navigation
 *	@lends mbx_pagegallery
 *	@property {object} options implements Mootools.Options
 *	@property {array} m_data holds gallerydata
 *	@property {int} m_currentIndex represents current index in m_data
 *	@property {int} m_itemCount holds count of m_data
 */
{
	
	Implements : Options,

	options: {
		'displayCount': null,
		'prevSelector' : null,
		'nextSelector' : null,
		'itemSelector' : null,
		'tyTopic' : null,
		'dataUrl' : null
	},
	'm_data' : null,
	'm_currentIndex' : null,
	'm_itemCount' : null,

	/**
	 *	@constructs
	 *	@param {object} p_options 
	 */
	initialize : function(p_options)
	{
		this.setOptions(p_options);
		this.m_itemCount = $$(p_options['itemSelector']).length;
		this.findStartupIndex();
		this.loadData();
		this.addEvents();
	},
	
	/**
	 * determines the starting-index(m_current_index) in the startup-process by the dom-element that was set active
	 */
	findStartupIndex : function()
	{
		var i = 0;
		var index = 0;
		
		var lis = $$(this.options.itemSelector);
		
		for(i in lis)
		{
			if($type(lis[i]) == 'element')
			{
				var li = lis[i].getParent();
				if(li.get('class') == 'active')
				{
					this.setCurrentIndex(index);
				}
				
				++index;
			}
		}
	},
	
	/**
	 *	getter for member m_currentIndex
	 *	@return {int} m_currentIndex
	 */
	getCurrentIndex : function()
	{
		return this.m_currentIndex;
	},
	
	/**
	 *	setter for member m_currentIndex
	 *	@param {int} p_index
	 */
	setCurrentIndex : function(p_index)
	{
		this.m_currentIndex = p_index;
	},
	
	/**
	 *	getter for member m_data
	 *	@return {array} m_data
	 */
	getData : function()
	{
		return this.m_data;
	},

	/**
	 *	setter for member m_data
	 *	@param {array} m_data
	 */
	setData : function(p_data)
	{
		this.m_data = p_data;
	},
	
	/**
	 *	getter for member m_itemCount
	 *	@return {int} m_itemCount
	 */
	getItemCount : function()
	{
		return this.m_itemCount;
	},
	
	/**
	 *	setter for member m_itemCount
	 *	@param {int} m_itemCount
	 */
	setItemCount : function(p_index)
	{
		this.m_itemCount = p_index;
	},
	
	/**
	 *	loads the gallerydata via xml-request, sets up required member-variables and prepares the data
	 */
	loadData : function()
	{
		//load data
		request = new Request({
			'url' : this.options['dataUrl'],
			'method' : 'get'
		});
		
		request.addEvent('success', function(p_data) {
			//get the data and prepare it
			tmpData = p_data.split(',');
			var data = [];
			
			tmpData.each(function(ai)
			{
				var tmpDataDetails = ai.split('###');
				
				data.extend([{
					'imageId' : tmpDataDetails[0],
					'detailLink' : tmpDataDetails[1],
					'caption' : tmpDataDetails[2]
				}]);
			});
			this.setData(data);
			this.setItemCount(data.length);
			this.prepareData();
		}.bind(this));
		
		request.send('tyTopic=' + this.options['tyTopic']);
	},
	
	/**
	 *	adds all neccessary events to the respective dom-elements
	 */
	addEvents : function()
	{
		//"prev"-Element
		if($(this.options['prevSelector']))
		{
			$(this.options['prevSelector']).addEvent('click', function(p_ev) {
				this.stopHtmlEvent(p_ev);
				this.onPrev_click();
			}.bind(this));
		}
		
		//"next"-Element
		if($(this.options['nextSelector']))
		{
			$(this.options['nextSelector']).addEvent('click', function(p_ev) {
				this.stopHtmlEvent(p_ev);
				this.onNext_click();
			}.bind(this));
		}
		
		//"item"-Element
		$$(this.options['itemSelector']).each(function(p_item) {
			p_item.addEvent('click', function(p_ev) {
				this.stopHtmlEvent(p_ev);
				this.onItem_click(p_item);
			}.bind(this));
		}.bind(this));
	},
	
	/**
	 *	helper to stop basic events
	 *	@param {native.event} p_ev
	 *	@event
	 */
	stopHtmlEvent : function(p_ev)
	{
		new Event(p_ev).stop();
	},
	
	/**
	 *	eventhandler for click-event on "prev"-button
	 *	@event
	 */
	onPrev_click : function()
	{
		var currentIndex = this.getCurrentIndex();
		
		if((currentIndex - 1) >= 0)
		{
			this.setCurrentIndex(currentIndex - 1);
		}
		else
		{
			this.setCurrentIndex(this.getItemCount() - 1);
		}
		
		this.update(currentIndex);
	},
	
	/**
	 *	eventhandler for click-event on "next"-button
	 *	@event
	 */
	onNext_click : function()
	{
		var currentIndex = this.getCurrentIndex();

		if((currentIndex + 1) < this.getItemCount())
		{
			this.setCurrentIndex(currentIndex + 1);
		}
		else
		{
			this.setCurrentIndex(0);
		}
		
		this.update(currentIndex);
	},
	
	/**
	 *	eventhandler for click-event on "item"
	 *	@event
	 *	@param {element} p_item
	 */
	onItem_click : function(p_item)
	{
		var currentIndex = this.getCurrentIndex();
		this.setCurrentIndex(p_item.get('rel').toInt());
		this.update(currentIndex);
	},
	
	/**
	 *	prepares the data - not implemented here
	 *	@return {bool}
	 */
	prepareData : function()
	{
		//not implemented here
	},
	
	/**
	 *	updates the gallery-specific dom-elements - not implemented here
	 *	@param {int} p_oldIndex
	 */
	update : function(p_oldIndex)
	{
		//not implemented here
	}
});