/**
 * jquery.comboBox.js
 * 
 * Based on the original comboBox.js functionality.
 */

(function($) {
	
	$.fn.comboBox = function(settings)
	{
		var config = {
			'foo' : 'bar'
		};
		
		if (settings)
		{
			$.extend(config, settings);
		}
		
		this.each(function() {
			initTextField( $(this) );
		});
		
		return this;
	};
	
	function initTextField(select)
	{
		if($("#txt" + select.attr("name")).length === 0)
		{
			// Create the text field to sit on top of the select parent
			var current_pos = select.position();
			var right_width = parseInt(select.css("borderRightWidth"), 10);
			if(right_width < 10)
			{
				// Safety net if we get too small a value
				right_width = 20;
			}
			var select_width = select.outerWidth() - right_width;
			
			$(document.createElement('input'))
				.attr("id",	"txt" + select.attr("name"))
				.attr("name",	"txt" + select.attr("name"))
				.attr("autocomplete", "off")
				.attr("title", _VARS.comboBox.default_value)
				.attr("mptrans", "value,title")
				.addClass("comboText")
				.css({
					"z-index":	"99999",
					"color":		"#000",
					"padding":	"0",
					"position":	"absolute",
					"border":		"none",
					"top":		current_pos.top,
					"left":		current_pos.left
				})
				.width( select_width )
				.height(select.innerHeight())
				.val(_VARS.comboBox.default_value)
				.focus(function(){
					if($.trim($(this).val()) === _VARS.comboBox.default_value)
					{
						$(this).val("");
					}
				})
				.blur(function() {
					if( $.trim($(this).val()) === "" )
					{
						$(this).val(_VARS.comboBox.default_value);
					}
				})
				.insertAfter(select);
			
			select
				.css("z-index", "99998")
				.change(function() {
					var textfield = $("#txt" + $(this).attr("name"));
					var textval = textfield.val();
					
					if(textval.indexOf("@") > 0)
					{
						textfield.val(textval + ", " + $(this).val());
					}
					else
					{
						textfield.val($(this).val());
					}
					
					textfield.focus();
				})
				.focus(function() {
					$("txt" + $(this).attr("name")).css("color", "#000")
				});
			
			// If it's IE6 we need the background iframe
			if( $.browser.msie && /msie 6\.0/i.test(navigator.userAgent) )
			{
				$("#txt" + select.attr("id"))
					.css({
						"top":		current_pos.top + 3,
						"left":		current_pos.left + 3
					});
				initIframe(select, select_width, select.innerHeight(), current_pos.top, current_pos.left);
			}
		}
		
		return true;
	}
	
	
	function initIframe(select, textWidth, textHeight, nTop, nLeft)
	{
		var ctrl = document.getElementById(select.attr("id"));
		var textfield = document.getElementById("txt" + select.attr("id"));
		
		iFrame = document.createElement("iframe");
		iFrame.setAttribute("src", "javascript:void(0);");
		iFrame.setAttribute("scrolling", "0");
		iFrame.setAttribute("tabindex", "-1");
		iFrame.setAttribute("frameborder", "0");
		iFrame.id = "frame" + ctrl.name;
		iFrame.style.position = "absolute";
		iFrame.style.width = textWidth + "px";
		iFrame.style.height = textHeight + "px";
		iFrame.style.top = nTop + "px";
		iFrame.style.left = nLeft + "px";
		iFrame.style.marginTop = "3px";
		iFrame.style.marginLeft = "3px";
		ctrl.parentNode.insertBefore(iFrame, textfield);
	}
	
})(jQuery);
