﻿var AjaxImageButton = new Class(
{
	initialize : function()
	{
		this.states = ["n", "h", "a", "i"];
		this.activeItems = { };
	},

	collectViewState : function(control)
	{
		return { };
	},

	activate : function(section, control)
	{
		var currentControl = $(imgBtn.activeItems[section]);

		if (currentControl == control)
		{
			return;
		}

		if (currentControl != null)
		{
			imgBtn.setStatus(section, currentControl, 0, true);
		}

		imgBtn.activeItems[section] = control.id;
	},

	isActivated : function(imgButton)
	{
		if (imgButton.section == "")
		{
			return false;
		}

		return imgBtn.activeItems[imgButton.section] == imgButton;
	},

	getImageUrl : function(imgButton, status)
	{
		var extension = imgButton.imageUrl.substr(imgButton.imageUrl.length - 4, 4);
		var currentStatus = imgBtn.states.indexOf(imgButton.imageUrl.substr(imgButton.imageUrl.length - 5, 1));

		return imgButton.imageUrl.substr(0, imgButton.imageUrl.length - 5) + imgBtn.states[status] + extension;
	},

	setStatus : function(imgButton, status)
	{
		var newImageUrl = null;
		
		if (status == 0)
		{
			newImageUrl = imgButton.imageUrl;
		}
		else
		{		
			newImageUrl = imgBtn.getImageUrl(imgButton, status);
		}
		
		if (imgButton.src != newImageUrl)
		{
			imgButton.src = newImageUrl;
		}
	},

	onMouseEnter : function()
	{
		if (imgBtn.isActivated(this) || !this.hasHoverState)
		{
			return;
		}

		imgBtn.setStatus(this, 1);
	},

	onMouseLeave : function()
	{
		if (imgBtn.isActivated(this) || !this.hasHoverState)
		{
			return;
		}

		imgBtn.setStatus(this, 0);
	},

	onMouseDown : function()
	{
		if (!this.hasActiveState)
		{
			return;
		}

		imgBtn.setStatus(this, 2);
	},

	onMouseUp : function()
	{
		if (!this.hasActiveState)
		{
			return;
		}

		imgBtn.setStatus(this, 0);
	},

	onClick : function()
	{
	
	},

	setImageUrl : function(dialogID, controlID, imageUrl)
	{
		var control = ui.getNestedControl(dialogID, controlID);

		if (control == null)
		{
			return;
		}

		control["imageUrl"] = imageUrl;
		control.src = imageUrl;
	},

	initControl : function(dialogID, controlID, imageUrl, section, hasHoverState, hasActiveState)
	{
		var control = ui.getNestedControl(dialogID, controlID);

		if (control == null)
		{
			return;
		}

		control["section"] = section;
		control["imageUrl"] = imageUrl;
		control["hasHoverState"] = hasHoverState;
		control["hasActiveState"] = hasActiveState;

		control.addEvent('mouseenter', imgBtn.onMouseEnter);
		control.addEvent('mouseleave', imgBtn.onMouseLeave);
		control.addEvent('mousedown', imgBtn.onMouseDown);
		control.addEvent('mouseup', imgBtn.onMouseUp);
		control.addEvent('click', imgBtn.onClick);
	}
});

var imgBtn = new AjaxImageButton();