/**
 * @author Oleg Korovin (http://olegrorovin.spb.ru/)
 * @include "./_Lib.js"
 */


var My_Button = function( oParams ){
	this.oState = {
		h : 0,
		f : 0,
		p : 0,
		d : 0
	};
	this.create(oParams || {});
	return this;
};

My_Button.add = function(elNode){
	var b = new My_Button(elNode);
}


My_Button.prototype = {

	create : function(oParams){
		var oThis = this;

		oThis.elNode = $(oParams);
		oThis.elBox = oThis.elNode.parentNode;
		/*alert(
			oThis.elNode.id + '\n' +
			'disabled - ' + oThis.elNode.disabled + ' ' + (oThis.elNode.disabled ? 'true' : 'false') + '\n' +
			'getAttribute - ' + oThis.elNode.getAttribute('disabled') + ' ' + (oThis.elNode.getAttribute('disabled') ? 'true' : 'false')
//			+ '\n' +
//			'hasAttribute - ' + oThis.elNode.hasAttribute('disabled') + ' ' + (oThis.elNode.hasAttribute('disabled') ? 'true' : 'false')
		)/**/

		if(
			oThis.elNode.disabled ||
			oThis.elNode.getAttribute('disabled') ||
			(oThis.elNode.hasAttribute && oThis.elNode.hasAttribute('disabled'))
		){
			oThis.setState({
				d : 1
			});
			oThis.elNode.disabled = false;
		}

		oThis.applyStyles();
		//alert( oThis.getState().d );

		//Events.add(oThis.elNode,'mouseover',oThis.event('mouseover'));
		//Events.add(oThis.elNode,'mousemove',oThis.event('mouseover'));
		//Events.add(oThis.elNode,'mouseout',oThis.event('mouseout'));

		oThis.addEvent( 'mouseover' , 'mouseover' );
		oThis.addEvent( 'mousemove' , 'mouseover' );
		oThis.addEvent( 'mouseout' , 'mouseout' );

		oThis.addEvent( 'focus' , 'focus' );
		oThis.addEvent( 'blur' , 'blur' );

		oThis.addEvent( 'mousedown' , 'mousedown' );
		oThis.addEvent( 'mouseup' , 'mouseup' );
		oThis.addEvent( 'click' , 'click' );

		//oThis.elBox.onfocus = function(e){alert('x')}
	},


	mouseover : function(){
		var oThis = this;
		oThis.setState({ h : 1 });

		Styles.setClass(oThis.elBox,'HTML_Button_Hover',1);
	},
	mouseout : function(){
		var oThis = this;
		oThis.setState({ h : 0 });
		Styles.setClass(oThis.elBox,'HTML_Button_Hover',0);
	},


	focus : function(event){
		var oThis = this;

		oThis.setState({ f : 1 });
		Styles.setClass(oThis.elBox,'HTML_Button_Focus',1);
		//return Events.stop(event);
	},
	blur : function(){
		var oThis = this;
		oThis.setState({ f : 0 });
		Styles.setClass(oThis.elBox,'HTML_Button_Focus',0);
	},


	mousedown : function(event){
		var oThis = this;

		oThis.setState({ p : 1 });

		//var fListener = oThis.event('mouseup');

		Events.add(document,'mouseup',_mup)

		function _mup(e){
			//oThis.mouseup(e);
			(oThis.event('mouseup'))(e);
			Events.remove(document,'mouseup',_mup);
		}

		Events.stop(event);
		oThis.elNode.focus();
	},

	mouseup : function(event){
		var oThis = this;
		oThis.setState({ p : 0 });
		if( oThis.getState().d ){
			return Events.stop(event);
		}
	},

	click : function(event){
		var oThis = this;
		if(oThis.getState().d){
			return Events.stop(event);
		}
	},
/**/

	addEvent : function(sEvent,sListener){
		var oThis = this;
		Events.add( oThis.elNode, sEvent, oThis.event(sListener) );
	},

	event : function(sListener){
		var oThis = this;

		return function(e){
			var r = oThis[sListener](e);
			oThis.applyStyles();
			oThis.debug();
			return r;
		}
	},



	applyStyles : function(){
		var oThis = this;

		oThis.clearStyles();

		var
			oState = oThis.getState(),
			h = oState.h,
			f = oState.f,
			p = oState.p,
			d = oState.d;

		if(d){
			Styles.setClass(oThis.elBox,'HTML_Button_Disabled',1);
		}
		else if (p && h){
			Styles.setClass(oThis.elBox,'HTML_Button_Press',1);
		}
		else if (f && h){
			Styles.setClass(oThis.elBox,'HTML_Button_Focus_Hover',1);
		}
		else if (f){
			Styles.setClass(oThis.elBox,'HTML_Button_Focus',1);
		}
		else if (h){
			Styles.setClass(oThis.elBox,'HTML_Button_Hover',1);
		}
	},

	clearStyles : function(){
		var oThis = this;

		Styles.setClass(oThis.elBox,'HTML_Button_Hover',0);
		Styles.setClass(oThis.elBox,'HTML_Button_Focus',0);
		Styles.setClass(oThis.elBox,'HTML_Button_Focus_Hover',0);
		Styles.setClass(oThis.elBox,'HTML_Button_Press',0);
		Styles.setClass(oThis.elBox,'HTML_Button_Disabled',0);
	},


	setState : function(oState){
		this.extend(this.oState,oState);
	},

	getState : function(){
		return this.oState;
	},

	debug : function(){
		var oThis = this;
		//document.title = oThis.elBox.className;
		//document.title = oThis.oState.h+','+oThis.oState.f+','+oThis.oState.p+','+oThis.oState.d;
	},

	extend	: function(destination, source){
		for (var property in source)
    		destination[property] = source[property];
  		return destination;
	}
};
