var ArticleLightbox = Class.create(KeyBox.Base,
{
	currentCategoryId: undefined,
	employeeLinkSelector: new Array(),
	currentCategoryTitle: '',
	
	initialize: function(employeeLinkSelector, categoryMenuObject, lightBoxObj, optionsObj)
	{
		
		this.initKeyBoxBase(optionsObj); // Lightbox init
		if(categoryMenuObject)
			if(categoryMenuObject.current != 0)
				this.currentCategoryId = categoryMenuObject.current;

		if($$(employeeLinkSelector).length > 0 && typeof this.currentCategoryId == 'number')
		{
			var self = this;
			this.employeeLinkSelector = employeeLinkSelector;
			$$(employeeLinkSelector).each(function(elem)
			{
				self.setObserver(elem);
			});		
		}
		if(this.currentCategoryTitle == '')
			if($($$('.ajax-menu li.active a')[0]))
				this.currentCategoryTitle = $($$('.ajax-menu li.active a')[0]).getAttribute('rel');
	},
	
	setObserver: function(employeeLinkElement)
	{
		//console.log(employeeLinkElement);
		var self = this;
		var newHash = $(employeeLinkElement).getAttribute('href').replace(window.location.pathname,'').replace('/','');
		$(employeeLinkElement).observe('click', function()
		{
			var last = $(this).getAttribute('href').lastIndexOf('/');
			var length = $(this).getAttribute('href').length;
			var newString = $(this).getAttribute('href').substr(0, last);
			var newLength = newString.length;
			last = newString.lastIndexOf('/');
			newHash = newString.substr(last, newLength).replace('/','');
			window.location.hash = newHash;
			
			
		//	var extId = $(this).getAttribute('rel');
			//self.initEmployeeBox(extId);
		});
		
		$Hash.addObserver(newHash, function(hashObj)
		{
			var extId = this.uid;
			self.initEmployeeBox(extId);
		},$(employeeLinkElement).getAttribute('rel'));
	},
	
	setObservers: function()
	{
		var self = this;
		$$(self.employeeLinkSelector).each(function(elem)
		{
			self.setObserver(elem);
		});
	},
	
	setCurrentCategoryId: function(cid)
	{
		this.currentCategoryId = cid;
	},
	
	setCurrentCategoryTitle: function(title)
	{
		this.currentCategoryTitle = title;
	},
	
	initEmployeeBox: function(extId)
	{
		var self = this;
		var container = this.container;
		var newContent = '';
		var employeeObj = this.getEmployeeObj(extId, function(transport)
		{
			var JSONObj = transport.responseText.evalJSON();
			newContent = JSONObj.templateContent;
			if(JSONObj.templateContent != '')
			{
				self.activateBox();
				self._trace(JSONObj.articleUrls.current);
				$(container).update(JSONObj.templateContent);
				$('lb-seq-current').update(JSONObj.currentIndex);
				$('lb-seq-total').update(JSONObj.count);
				$('lb-cat').update(self.currentCategoryTitle);
				$('lb-prev').observe('click', function()
				{
					window.location.hash = JSONObj.articleUrls.prev;
					self._trace(JSONObj.articleUrls.prev);
					if((JSONObj.currentIndex) > 1)
						self.updateEmployeeBox(JSONObj.articles[JSONObj.currentIndex-2]);
					else
						self.updateEmployeeBox(JSONObj.articles[JSONObj.count-1]);
				});
				$('lb-next').observe('click', function()
				{
					window.location.hash = JSONObj.articleUrls.next;
					self._trace(JSONObj.articleUrls.next);		
					if(JSONObj.currentIndex < JSONObj.count)
						self.updateEmployeeBox(JSONObj.articles[JSONObj.currentIndex]);
					else
						self.updateEmployeeBox(JSONObj.articles[0]);
				});
				$('close-employee').observe('click', function(ev)
				{
					self.removeBox();
				});
				
				var t= setTimeout(function()
				{
					if(window.lightboxsIFRes)
						window.lightboxsIFRes.activate();
					clearTimeout(t);
					
				},500);
				
			}
		}, function()
		{
			self.showResult();
		});				
	},
	
	updateEmployeeBox: function(extId)
	{
		var self = this;
		var container = this.container;
		var employeeObj = this.getEmployeeObj(extId, function(transport)
		{
			var JSONObj = transport.responseText.evalJSON();
			if(JSONObj.templateContent != '')
			{
				$(container).update(JSONObj.templateContent);
				$('lb-seq-current').update(JSONObj.currentIndex);
				$('lb-seq-total').update(JSONObj.count);
				$('lb-cat').update(self.currentCategoryTitle);
				$('lb-prev').observe('click', function()
				{
					window.location.hash = JSONObj.articleUrls.prev;
					self._trace(JSONObj.articleUrls.prev);
					if((JSONObj.currentIndex) > 1)
						self.updateEmployeeBox(JSONObj.articles[JSONObj.currentIndex-2]);
					else
						self.updateEmployeeBox(JSONObj.articles[JSONObj.count-1]);
				});
				$('lb-next').observe('click', function()
				{
					window.location.hash = JSONObj.articleUrls.next;
					self._trace(JSONObj.articleUrls.next);
					if(JSONObj.currentIndex < JSONObj.count)
						self.updateEmployeeBox(JSONObj.articles[JSONObj.currentIndex]);
					else
						self.updateEmployeeBox(JSONObj.articles[0]);
				});
				$('close-employee').observe('click', function()
				{
					self.removeBox();
				});

				var t= setTimeout(function()
				{
					if(window.lightboxsIFRes)
						window.lightboxsIFRes.activate();
					clearTimeout(t);
					
				},500);
			}
		}, function() { 
			
		});
	},
	
	getEmployeeObj: function(extId, afterFinish, isComplete)
	{
		var self = this;
		if(window.EmployeeRequest)
			window.EmployeeRequest.transport.abort();
		window.EmployeeRequest = new Ajax.Request('/index.php',
		{
			method: 'get',
			parameters:
			{
				'c_' : 'HalogenAJAXEmployee',
				'm_' : 'getEmployee',
				'cid' : self.currentCategoryId,
				'extid' : extId
			},
			onSuccess: afterFinish,
			onComplete: isComplete
		});
	},
	
	activateBox: function()
	{
		this.showBaseContainer();
	},
	
	_trace: function(articleUrl)
	{
		if(typeof articleUrl == 'string')
		{
			if(window.pageTracker)
				window.pageTracker._trackPageview('/'+articleUrl);
		}
	}
});
