/*
 * JavaScript аналог HTML тэга INPUT[type=checkbox]
 * @author: Dmitry Kozloff aka MyK
 * @date: 28.03.2008
 * @url: www.mitht.ru
 */

var jsCheckbox = new Class ({
		initialize: function(id, name) {
			if(this.setName(id, name))
			{
				this.values = [];

				for(var j in this.icons)
					this.values.include(j);

				this.obj.style.width = "12px";
				this.obj.style.height = "12px";
				this.obj.style.display = "inline";

				this.obj.onmouseover = function(){
					this.style.cursor = 'pointer';
					this.style.cursor = 'hand';
				};

				this.obj.innerHTML = '<img title="' + this.icons[this.current][1] + '" id="jsCheckbox_image_' + this.id + '" name="' + this.id + '" src="' + this.icons[this.current][0] + '" />' +
									'<input id="jsCheckbox_input_' + this.id + '" type="hidden" name="' + this.name + '" value="' + this.current + '" />';

				var thisObj = this;

				$('jsCheckbox_image_' + this.id).onclick = function(){

					var start = ((thisObj.current == (thisObj.values.length - 1)) ? 0 : thisObj.current);
					
					for(var i = start; i < thisObj.values.length; i++)
					{
						if(thisObj.values[i] != thisObj.current)
						{
							thisObj.current = thisObj.values[i];
							$('jsCheckbox_input_' + this.name).setAttribute('value', thisObj.current);
										
							this.src = thisObj.icons[thisObj.current][0];
							this.title = thisObj.icons[thisObj.current][1];
							
							thisObj.userFunction();

							break;
						}
					}
				}


				// Эта часть работает только в IE 5.5+ и FF: возврат в предыдущее положение указателя по ПКМ.
				$('jsCheckbox_image_' + this.id).oncontextmenu = function(){

					var start = ((thisObj.current == 0) ? (thisObj.values.length - 1) : thisObj.current);
					
					for(var i = start; i < thisObj.values.length; i--)
					{
						if(thisObj.values[i] != thisObj.current)
						{
							thisObj.current = thisObj.values[i];
							$('jsCheckbox_input_' + this.name).setAttribute('value', thisObj.current);

							this.src = thisObj.icons[thisObj.values[i]][0];
							this.title = thisObj.icons[thisObj.current][1];

							thisObj.userFunction();

							break;
						}
					}
					return false;
				}
			}
		},

		icons: {
			0: ['images/unchecked_gray.gif', 'NONE'],
			1: ['images/checked_gray.gif', 'AND']
/*			2: ['images/filled_gray.gif', 'JOIN'],
//			2: ['images/unaviable_gray.gif', 'NOT'],
//			2: ['images/delemma_gray.gif', 'OR'],
//			2: ['images/range_gray.gif', 'RANGE'],
//			3: ['images/not_range_gray.gif', 'NOT RANGE']
			7: ['images/group_by_gray.gif', 'GROUP BY'],
			8: ['images/order_by_asc_gray.gif', 'ORDER BY ASC'],
			9: ['images/order_by_desc_gray.gif', 'ORDER BY DESC'] */
		},

		values: [],

		current: 0,

		id: null,

		name: null,

		value: null,

		obj: null,

		setName: function (id, name) {
			if($(id))
			{
				this.obj = $(id);
				this.name = name;
				this.id = id;
				return true;
			}
			else
				return false;
		},

		setIcons: function (icons) {
			this.icons = icons;
			this.rebuild();
		},

		rebuild: function () {
			this.initialize(this.id, this.name);
		},

		setCurrent: function(value) {
			this.current = value;
			this.rebuild();
		},
			
		// Если хотите что либо выполнить после отметки чекбокса, используйте эту функцию.
		userFunction: function () {}
	});