/* Naked Control Panels*/

/**
  * @name  controlPanel
  * @type  jQuery
  * @param String  url                     URL to open for all matched elements
  * @param Hash    options                 additional options 
  * @param String  options[select_element] option element within a select element. Selecing this option will open the controlPanel
  * @param String  options[title]          title of the control panel
  *             
  */
jQuery.fn.controlPanel = function(url){
	var options=$.extend(arguments[1], {'url': url}) //we need this to prevent tests for option properties failing when no properties exist

	var show = function(url){
		if(url==undefined) return false;
		var t = options.title || $(this).attr('title') || $(this).attr('name') || null;
	  	var g = this.rel || false;

		//add a ? or & to the end of the URL 
	  	if (url.match(/\?/)==null) {
			url += "?"
		} else {
			url += "&"
		}
		url+="height=450&width=495"
	  	tb_show(t, url,g);// in thickbox.js
	  	return false;
	}

	this.each(function(i, element){
	
		
		
		$(element).filter(':not(form)').filter(":not(select)").click(function(event){
			var my_url = url;
			if (my_url==undefined || null){
				my_url = $(element).attr('href') || debug('Could not auto-detect target address')
			}
			show.call($(element), my_url)
			event.stopPropagation();
			return false;
		})
		
		if (options.select_element){
			$(element).filter('select').change(function(event){
				var my_url = url;
				if ($(this).val()==options.select_element){
					if (my_url==undefined||null){
						my_url = $(element).attr('href') || debug('Could not auto-detect target address')
					}
					show(my_url)
					event.stopPropagation();
					return false;
				}
			})
		}
		
		$(element).filter('form').submit(function(event){
			var my_url=this.action+"?"+$(this).formSerialize();
			show(my_url);
			event.stopPropagation();
			return false;
		})
	})
}







