/*********************************************
  PMAF Web Controls Javascript Library
 
  Has dependency on config_pmg.js
 
 *********************************************/

/************************************************************
 * Button States
 ***********************************************************/
var PmafButtonState = {
 Normal : 0,
 Rollover : 1,
 Focus : 2
}

/************************************************************
 * Key Codes
 ***********************************************************/
var PmafKeyCode = {
 Enter : 13,
 Space : 32,
 Zero : 0
}

/************************************************************
 * Key Event
 ***********************************************************/
function PmafKeyEvent(ev) {
	this.event = ev || window.event;
	this.keyCode = this.event.keyCode;
}

/************************************************************
 * Button Part
 ***********************************************************/
function PmafButtonPart(obj) {
 this.obj = obj;

 this.setBackgroundImage = function(image) {
	if(gBrowser.vendor == "IE"){	
		obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + image + "', sizingMethod='scale')";
	}else{
		obj.style.backgroundImage = "url('" + image + "')";
	} 
 } 
}

/************************************************************
 * Button Model
 ***********************************************************/
function PmafButtonModel(id) {
 this.container = document.getElementById(id);
 this.left = new PmafButtonPart(this.container.childNodes[0]);
 this.tile = new PmafButtonPart(this.container.childNodes[1]);
 this.right = new PmafButtonPart(this.container.childNodes[2]);
}

/************************************************************
 * Button Image Set
 ***********************************************************/
function PmafButtonImageSet(leftImageUrl,tileImageUrl,rightImageUrl) {
 this.leftImageUrl = leftImageUrl;
 this.tileImageUrl = tileImageUrl;
 this.rightImageUrl = rightImageUrl;
}

/************************************************************
 * Button View
 ***********************************************************/
function PmafButtonView(
	id,
	normalStateImageSet,
	rolloverStateImageSet,
	focusedStateImageSet
) {

 this.id = id;
 this.state = PmafButtonState.Normal;
 this.imageStates = new Array();
 this.imageStates[PmafButtonState.Normal] = normalStateImageSet;
 this.imageStates[PmafButtonState.Rollover] = rolloverStateImageSet || focusedStateImageSet || normalStateImageSet;
 this.imageStates[PmafButtonState.Focus] = focusedStateImageSet || normalStateImageSet;

 this.setState = function(state) {
	this.state = state;
	this.render();
 }

 this.render = function() {
	var button = new PmafButtonModel(this.id);
	button.left.setBackgroundImage(this.imageStates[this.state].leftImageUrl);
	button.tile.setBackgroundImage(this.imageStates[this.state].tileImageUrl);
	button.right.setBackgroundImage(this.imageStates[this.state].rightImageUrl);
 }
 
}
/************************************************************
 * Button Controller
 ***********************************************************/
function PmafButtonController() {
 this.buttons = new Array();

 this.add = function(button) {
	this.buttons[button.id] = button;
 }

 this.handleOnFocus = function(button) {
	this.buttons[button.id].setState(PmafButtonState.Focus);
 }

 this.handleOnBlur = function(button) {
	this.buttons[button.id].setState(PmafButtonState.Normal);
 }

 this.handleOnMouseOver = function(button) {
	if (this.buttons[button.id].state == PmafButtonState.Normal)
		this.buttons[button.id].setState(PmafButtonState.Rollover);
 }

 this.handleOnMouseOut = function(button) {
	if (this.buttons[button.id].state == PmafButtonState.Rollover)
		this.buttons[button.id].setState(PmafButtonState.Normal);
 }

 this.handleKeyPress = function(ev,button) {
		var myEvent = new PmafKeyEvent(ev);
		var lKeys = [PmafKeyCode.Enter,PmafKeyCode.Space,PmafKeyCode.Zero]

		for(var i = 0; i<lKeys.length; i++){
			if(myEvent.keyCode == lKeys[i]){
				button.onclick(ev);
                //ev.cancelBubble = true;
                //if (ev.stopPropagation) ev.stopPropagation();					
				return false;
			}
		}
		return true;

 }

}

var PmafBC = new PmafButtonController();
