var GUI = {
	prefer_this_alone : function(this_alone, class_name){
		class_name = class_name || 'preferred'
		this_alone = $(this_alone);
		var was = this_alone.checked;
		this_alone.up('div.row,tbody').select('input[type=checkbox],input[type=radio]').each(function(i){
			i.checked = false;
			i.up('div,tr').removeClassName(class_name);
		});
		if(!was){
			this_alone.checked = true;
			this_alone.up('div,tr').addClassName(class_name);
		}
	},
	select_location : function(location, return_item, callback){
		var id = location.id.match(/\d+$/).pop();
		var val = $F(location);
		if(val.blank()){
			GUI.populate_ils({'responseJSON':null}, id);
		} else {
			new Ajax.Request('ajax/', {
				parameters : {'table':'inventory_items','location_id':val},
				onSuccess : function(ret){
					GUI.populate_ils(ret, id, return_item, callback);
				}
			});
		}
		return false;
	},
	populate_ils : function(ret, list, return_item, callback){
		var data = ret.responseJSON, select = new Element('select');
		list = $('il_'+list);
		if(data){
			list.enable();
			select.insert('<option value="">--Select One--</option>');
			for(i=0; i < data.length; i++){
				select.insert('<option rel="'
					+ (return_item ? data[i].il_id : data[i].item_id)
					+ '" value="'
					+ (return_item ? data[i].item_id : data[i].il_id)
					+ '">'+data[i].name+'</option>');
			}
			select.selectedIndex = 0;
			if(callback)
				list.update(callback(select).innerHTML);
			else
				list.update(select.innerHTML);
		} else {
			select.insert('<option value="">No items found</option>');
			list.disable();
			if(callback) list.update(callback(select).innerHTML);
		}
	},
	select_item : function(il, is_item_id, onSuccess, onFailure){
		onSuccess = onSuccess || populate_item_data;
		onFailure = onFailure || clear_item_data;
		var params = {'table':'inventory_data'};
		params[is_item_id?'item_id':'il_id'] = $F(il);
		il = il.id.match(/\d+$/).pop();
		if(count(params) > 1){
			new Ajax.Request('ajax/', {
				parameters : params,
				onSuccess : function(ret) {
					onSuccess(ret.responseJSON, il);
				},
				onFailure : function(ret) {
					onFailure(il);
				}
			});
		} else onFailure(il);
	},
	enumerate : function(template, where, rep, callback){
		if(template){
			template = ((GUI.templates&&GUI.templates[template])?GUI.templates[template]:template);
			if(!rep){
				var num = new Date();
				num = Date.parse(num) + Math.floor(Math.random()*1000);
				rep = [[new RegExp(/~id~/gi), num]];
			} else if('number'==typeof(rep)){
				rep = [[new RegExp(/~id~/gi), rep]];
			}
			for(i = 0; i<rep.length; i++){
				template = template.replace(rep[i][0], rep[i][1]);
			}
			var children = $(where).childElements();
			if(children.length){
				children.last().insert({ after : template });
			} else {
				$(where).insert( template );
			}
		}
		if(callback) callback($(where));
		return false;
	},
	enumerated_fill : function(template, where, rep, callback){
		$(where).innerHTML = '';
		return GUI.enumerate(template, $(where), rep, callback);
	},
	fill_with : function(template, where){
		if(!where) return;
		if(typeof(template)=='object' && template.tagName && template.tagName.toLowerCase() == 'select')
			template = Number($(template).childElements()[$(template).selectedIndex].getAttribute('rel'));
		if(typeof(template)=='string')
			template = ((GUI.templates&&GUI.templates[template])?GUI.templates[template]:template);
		if(typeof(template)=='number') template = GUI.templates[GUI.togglers[where][template%GUI.togglers[where].length]];
		if(template) $(where).update(template); else $(where).update('');
	},
	templatize : function(id){
		if(!$(id)){
			throw('cannot templatize "'+id+'"');
			return;
		}
		if(!GUI.templates) GUI.templates = {};
		GUI.templates[id] = '<'+$(id).tagName+($(id).className?' class="'+$(id).className+'"':'')+'>'+$(id).innerHTML+'</'+$(id).tagName+'>';
		$(id).remove();
	},
	toggler : function(block, alternatives, initialize, callback){
		initialize = initialize===undefined?true:initialize;
		if(!GUI.togglers) GUI.togglers = {};
		if(!GUI.templates) GUI.templates = {};
		Event.observe(document,'dom:loaded',function(){
			GUI.togglers[block] = [null].concat(alternatives);
			alternatives.each(function(alt,i){
				var next = alternatives[(i+1)%alternatives.length];
				if(initialize===true && $(alt).down('.inlinebutton'))
					$(alt).down('.inlinebutton').setAttribute('onclick','$("'+block+'").update(GUI.templates["'+next+'"]);'+(callback?' '+callback+'("'+next+'");':'')+' return false;');
				GUI.templates[alt] = $(alt).innerHTML;
				$(alt).remove();
			});
			if(initialize) $(block).update(GUI.templates[GUI.togglers[block][Number(initialize)]]);
		});
	},
	// blinds up or down element_to_toggle depending on if causing_element has the class open or 
	// close and adds it to a queue scoped to element_to_toggle,
	// that way someone can click the button as many times as they want and it wont go retarded.
	// If you omit causing_element it will be assumed to be the same as element_to_toggle.
	// options get passed directly to the Effect.  
	blind_toggle : function(element_to_toggle, causing_element, options){
		element_to_toggle = $(element_to_toggle);
		causing_element = $(causing_element) || element_to_toggle;
		options = Object.extend( {duration: 0.2, queue: { position: 'end', scope: element_to_toggle.identify() } }, options);
		if(causing_element.hasClassName('open')){
			causing_element.removeClassName('open').addClassName('close');
			Effect.BlindDown(element_to_toggle, options);
			//element_to_toggle.show()
		} else {
			causing_element.removeClassName('close').addClassName('open');
			Effect.BlindUp(element_to_toggle, options);
			//element_to_toggle.hide();
		}
	},
	toggle : function(bool, block){
		bool ? $(block).show() : $(block).hide();
	},
	sync_dynamic_to_checkbox : function(container, block_match, checkbox){
		checkbox = $(checkbox);
		document.observe("dom:loaded", function(){
			if(!checkbox.checked)
				$(container).select(block_match).invoke('hide');
		});
		checkbox.observe('click', function(ev){
			$(container).select(block_match).invoke(checkbox.checked?'show':'hide')
		});
	},
	sync_to_checkbox : function(blocks, checkbox){
		checkbox = $(checkbox);
		if(typeof(blocks) == 'object'){
			document.observe("dom:loaded", function(){
				if(!checkbox.checked)
					blocks.invoke('hide');
			});
			checkbox.observe('click', function(ev){
				blocks.invoke(checkbox.checked?'show':'hide')
			});
		} else {
			document.observe("dom:loaded", function(){
				if(!checkbox.checked)
					$(blocks).hide();
			});
			checkbox.observe('click', function(ev){
				GUI.toggle(ev.element().checked, blocks);
			});
		}
	},
	sync_to_checkboxes : function(block, checkboxes){
		checkboxes.each(function(cb,i){ checkboxes[i] = $(cb); })
		document.observe("dom:loaded", function() {
			if( !checkboxes.pluck('checked').any() )
				$(block).hide().addClassName('hidden');
		});
		checkboxes.each(function(checkbox){
			checkbox.observe('click', function(){
				var queue = Effect.Queues.get(block);
				if( checkboxes.pluck('checked').any() ){ //if any of the checkboxes are checked.
					if( $(block).hasClassName('hidden') ){
						$(block).removeClassName('hidden').blindDown({ 
							queue: { 
								position: 'end', 
								scope: block
							} 
						});
					}
				} else {
					if( !$(block).hasClassName('hidden') ){
						$(block).addClassName('hidden').blindUp({ 
							queue: { 
								position: 'end', 
								scope: block
							} 
						});
					}
				}
			});	
		});
	},
	zebrify : function(tbody){
		var bool = true;
		$(tbody).childElements().each(function(tr){
			if(bool)
				tr.removeClassName('even');
			else
				tr.addClassName('even');
			bool = !bool;
		});
	},
	remove_div : function(elem){
		elem.remove();
	}
};

