/*
 * Jax (jQuery activity extend) 0.2
 *
 * Copyright (c) 2008 Hoya Kim
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 * 
 * http://jax.ixcore.co.kr
 *
 * Depends:
 *	jQuery.js
 *	jax.js
 *
 * version: 0.1
 */
if(Jax.ImageButton){
	throw new Error('Module "Jax.ImageButton" already exists!');
}

Jax.ImageButton = function(options){
	this.options = $.extend({}, Jax.ImageButton.defaults, options || {});
	this.init();
}
Jax.ImageButton.jax = '0.1';
Jax.ImageButton.State = {
	Disabled: 'disabled',
	Out: 'out',
	Over: 'over',
	Selected: 'selected',
	Unselected: 'unselected'};

Jax.ImageButton.defaults = {
	res: {path: '/jax3/res/images/imagebutton/',
		  disabled: 'disabled.gif',
		  selected: 'selected.gif',
		  unselected: 'unselected.gif'},
	alt: '',
	css: {},
	checkOnClick: false,
	checked: false,
	unit: 'em',
	size: {width: 1.1, height: 1.1},
	state: Jax.ImageButton.State.Unselected,
	act: {click: function(){}, stateChanged: function(newState, oldState, caller){}}
};

$.extend(Jax.ImageButton.prototype, {
	init: function(){
		var _self = this;
		var _tabindex = (this.options.tabindex)? ' tabindex="' + this.options.tabindex + '"' : '' ;
		this.$element = $('<img src="" class="jax-imgbtn"' + _tabindex + ' alt="' + this.options.alt + '" title="' + this.options.alt + '" />');

		this.setState(this.getState(), 'init');

		if(!!_self.options.checkOnClick && !!!this.isDisabled()){
			this.setState(!!this.isChecked() ? Jax.ImageButton.State.Selected : Jax.ImageButton.State.Unselected, 'init');
		}
		this.$element.data('jax', this);
		this.$element.css({width: this.options.size.width + this.options.unit});
		this.$element.css({height: this.options.size.height + this.options.unit});
		this.$element.css(this.options.css);
		this.$element.bind('click', function(e){
			if(!!!_self.isDisabled()){
				_self.options.act.click(e);
			}
		});
		this.$element.bind('mousedown', function(e){
			if(!!!_self.isDisabled()){
				_self.setState(Jax.ImageButton.State.Selected, 'mousedown');
			}
		});
		this.$element.bind('mouseup', function(e){
			if(!!!_self.isDisabled()){
				if(!!_self.options.checkOnClick){
					_self.toggleChecked();
				}else{
					_self.setState(Jax.ImageButton.State.Unselected, 'mouseup');
				}
			}
		});
		this.$element.bind('mouseover', function(e){
			if(!!!_self.isDisabled()){
				_self.oldState = _self.getState();
				_self.setState(Jax.ImageButton.State.Over, 'mouseover');
			}
		});
		this.$element.bind('mouseout', function(e){
			if(!!!_self.isDisabled()){
				_self.setState(_self.oldState, 'mouseout');
				//if(!!_self.options.checkOnClick){
				//	_self.setState(!!_self.isChecked() ? Jax.ImageButton.State.Selected : Jax.ImageButton.State.Unselected, 'mouseout');
				//}else{
				//	_self.setState(Jax.ImageButton.State.Unselected, 'mouseout');
				//}
			}
		});
		this.$element.bind('keydown', function(e){
			if(!!!_self.isDisabled()){
				if(e.keyCode == 13 || e.which == 13){
					_self.options.act.click(e);
					e.preventDefault();
					e.stopPropagation();
				}
			}
		});
	},
	toggleChecked: function(){
		this.options.checked = !!!this.options.checked;
		this.setState(!!this.isChecked() ? Jax.ImageButton.State.Selected : Jax.ImageButton.State.Unselected, 'toggleChecked');
	},
	setChecked: function(flag){
		this.options.checked = flag;
		this.setState(this.isChecked() ? Jax.ImageButton.State.Selected : Jax.ImageButton.State.Unselected, 'setChecked');
	},
	isChecked: function(){
		return !!this.options.checked;
	},
	setDisable: function(flag){
		if(flag == true && this.isDisabled() == false){
			this.setState(Jax.ImageButton.State.Disabled, 'setdisable');
		}else if(flag == false && this.isDisabled() == true){
			this.setState(this.isChecked() ? Jax.ImageButton.State.Selected : Jax.ImageButton.State.Unselected, 'setdisable');
		}
	},
	isDisabled: function(){
		return this.getState() == Jax.ImageButton.State.Disabled;
	},
	setState: function(state, caller){
		if(!state) throw new Error('state is required.');
		if(typeof state != 'string') throw new Error('state must be string');
		var _check = false;
		for(var _prop in Jax.ImageButton.State){
			if(Jax.ImageButton.State[_prop] == state){
				_check = true;
				break;
			}
		}
		if(!!!_check)
			throw new Error('state[' + state + '] is invalid.');
		this.options.oldState = this.options.state;
		this.options.state = state;
		
		this.$element.attr('src', this.getFileName(state)).css({cursor: (state == Jax.ImageButton.State.Disabled) ? 'default' : 'pointer'});
		this.options.act.stateChanged(state, this.options.oldState, caller);

	},
	getState: function(){
		return this.options.state;
	},
	getFileExtension: function(){
		return '';
		//return '.' + Jax.getImageExtension();
	},
	getFileName: function(state){
		if(!state) throw new Error('state is required.');
		if(typeof state != 'string') throw new Error('state must be string');
		var _fname = '';
		switch(state){
			case Jax.ImageButton.State.Over:
				_fname = this.options.res[state] || this.options.res['selected'];
				break;
			case Jax.ImageButton.State.Out:
				_fname = this.options.res[state] || this.options.res['unselected'];
				break;
			default:
				_fname = this.options.res[state];
				break;
		}
		return this.options.res.path + _fname + this.getFileExtension();
	},
	show: function(container){
		if(!container) throw new Error('container is required.');
		if(typeof container != 'string') throw new Error('container must be string');
		$(Jax.toId(container)).append(this.$element);
		return this;
	},
	appendTo: function(o){
		if(!o) throw new Error('arguments[0] is required.');
		if(typeof o == 'string') o = $(Jax.toId(o));
		if(o.jquery){
			this.$element.appendTo(o);
		}else if(o.jax){
			this.$element.appendTo(o.$element);
		}
	}
});