//Imports Common.js
//Imports jquery-1.4.2.min.js


//3/23/2010 JDK - Created
//to use, add <script type="text/javascript">RegisterDynaDdl("<Selector>");</script>
//to the bottom of your page. replace <Selector> with a css type selector for which
//to attach to. This could be something like "#DdlClientId" where DdlClientId is the
//literal client if of a single DDL, or ".DynaDdl" to register all DDLs with the 
//DynaDdl class, or simply "select" to register every drop down on the page. If
//needed, RegisterDynaDdl() can be called multiple times.
//
//*Your DDL's parent should not contain any other elements besides the
//		DDL itself. If it does, wrap your ddl with a <span></span>
//*If the DDL is in a TD, make sure alignment is set to TOP LEFT

function RegisterDynaDdl(Selector) {

	//function to expand the selection box
	var expand = function(){
		if($(this).data('origWidth') == undefined) {
			//Do first contact initialization
			$(this).data('origWidth',$(this).css('width'));
			this.parentNode.height = this.parentNode.offsetHeight+'px';
		}
		//expand the select
		$(this).css("position","absolute").css("width","auto");
	}
	
	//function to contract the selection box
	var contract = function() {
	//no hide workaround for IE6 to stop the element contacting when clicked
		if(!$(this).data('noHide'))
			$(this).css("position","relative").css("width",$(this).data('origWidth'));
	}
	
	//set the noHide workaround on focus and blur
	var focus = function(){$(this).data('noHide',true)}
	var blur  = function(){$(this).data('noHide',false); contract.call(this)}
	
	//Register the Events for IE only
	if(document.uniqueID && window.createPopup) {
		if($.browser.version.substr(0,1)<7){
			//IE5 & 6
			$(Selector).hover(expand,contract).focus(focus).click(focus).blur(blur).change(blur);

		}else{
			//IE7 >
			//8/5/2010 JDK - Found an issue where the selection contraction events were causing double postbacks on AutoPostback drop down. If they have the AutoPostback style onchange then dont worry about contracting the ddl
			//$(Selector).mousedown(expand).change(contract).blur(contract);
			$(Selector).each(function(){
			    $(this).mousedown(expand);
			    if(!this.outerHTML.match(/onChange=.+doPostBack/i)){
			        $(this).change(contract).blur(contract);
			    }
			});
		}
	}
}
