/**
 * jQuery Custom ComboBox Plugin 0.7
 *
 * (c) 2009 Ruud Seberechts - UniWeb BVBA
 *
 * Example usage (convert all select elements in the document):
 *
 * $('select').customComboBox();
 *
 * IMPORTANT: When select elements are modified after the
 *            initialization (eg. after ajax request a select
 *            box is enabled and items are added), the custom
 *            combobox should be updated:
 *
 *            $('select').updateCustomComboBox();
 *
 */
;(function($){

	$.customComboBox = {
		comboboxNr: 0,
		needsIframe: ($.browser.msie && $.browser.version < 7),
		xSelect: new Object()
	};

	$.fn.extend({

		customComboBox: function() {
			if (!$(this).is('select')) return;
			return this.each(function(){createCustomComboBox($(this))});
		},

		updateCustomComboBox: function() {
			if (!$(this).is('select')) return;
			return this.each(function(){updateCustomComboBoxs($(this))});
		}

	});

	function createCustomComboBox(select) {
		if (!$.customComboBox.comboboxNr && $.customComboBox.needsIframe) {
			$('body').append(
				'<iframe id="comboboxhelper" src="" scrolling="no" frameborder="0" style="position: absolute; z-index: 1; visibility: hidden"></iframe>'
			);
		}
		var nID = 'generated_combobox_' + (++$.customComboBox.comboboxNr);
		var boxID = '#' + nID;
		$.customComboBox.xSelect[nID] = select;
		var replace = '<ul>';
		var title = select.find('option:first').text();
		select.find('option').each(function(index){
			var thisval = $(this).attr('value');
			if(thisval == 0 && index == 0){
				replace += '<li><a href="#" rel="0"></a></li>';
			}else{
				replace += '<li><a href="#" rel="' + thisval + '">' + $(this).text() + '</a></li>';
			}
			if ($(this).is(':selected')) {
				title = $(this).text();
			}
		});
		var nwidth = (select.css('width').indexOf('%') != -1) ?
						select.css('width') :
						(select.outerWidth() ?
							select.outerWidth()+'px' :
							parseInt(select.css('width'))+'px'
						);

		var nname =  select.attr('name');
		replace = '<div class="combobox" id="' + nID + '" style="width: ' + nwidth + '; float: ' + (select.css('float') == 'right' ? 'right' : 'left') + ';">'
				+ '<a href="#"><span>' + '</span></a>'
				+ replace
				+ '</ul></div>';
		disabled = select.attr('disabled');
		select.css({'position':'absolute','marginLeft':'-10000px','top':'0'});
		select.after(replace);
		select.attr('title', boxID);
		if (nwidth.indexOf('%') == -1 && nwidth.indexOf('NaN') == -1)
			$(boxID+' a span').css({'width' : (parseInt(nwidth) - 24) + 'px'});
		$(boxID+' a span').text(title);
		if (disabled) {
			$(boxID).attr('disabled', 'disabled');
		}
		$(boxID).find('ul').hide();
		enableDisableComboBox(boxID);
	}

	function updateCustomComboBoxs(select) {
		var boxID = select.attr('title');
		if (!$(boxID).length) return;
		$(boxID + ' li').remove();
		replace = "";
		var title = select.find('option:first').text();
		select.find('option').each(function(index){
			var thisval = $(this).attr('value');
			if(thisval == 0 && index == 0){
				replace += '<li><a href="#" rel="0"></a></li>';
			}else{
				replace += '<li><a href="#" rel="' + thisval + '">' + $(this).text() + '</a></li>';
			}
			if ($(this).attr('selected'))
				title = $(this).text();
		});
		$(boxID + ' ul').append(replace);
		disabled = select.attr('disabled');
		$(boxID+' a span').css('width', $(boxID+' a').outerWidth() - 24);
		$(boxID+' a span').text(title);
		if (disabled) {
			$(boxID).attr('disabled', 'disabled');
		}
		else {
			$(boxID).removeAttr('disabled');
		}
		enableDisableComboBox(boxID);
		updateCustomComboBoxSizes(boxID);
	}

	function updateCustomComboBoxSizes(boxID) {
		$(boxID+' li:last-child').addClass('last');
		var optionsWidth = 0; //$(boxID).find('ul').innerWidth();
		$(boxID).find('ul li a').css('width', 'auto');
		$(boxID).find('ul li a').css('overflow', 'visible');
		$(boxID).find('ul li a').css('float', 'left');
		$(boxID).find('ul li a').each(function(){
			$(this).hide();
			if ($(this).outerWidth() > optionsWidth) {
				optionsWidth = $(this).outerWidth();
			}
			$(this).show();
		});
		var boxWidth = $(boxID).find('a:first').outerWidth();
		if  ($(boxID).find('li').length > 5) {
			optionsWidth += 16;
		}
		if (boxWidth-2 > optionsWidth) {
			optionsWidth = boxWidth-2;
		}
		//$(boxID).find('ul').width(optionsWidth);
		$(boxID).find('ul li a').css('overflow', 'hidden');
		$(boxID).find('ul li a').css('float', 'none');
		$(boxID).find('ul a').css('width', optionsWidth - 4);
		$(boxID).find('ul').css('margin-left', 0);
	}

	function enableDisableComboBox(boxID) {
		$(boxID+' > a').unbind('click');
		if ($(boxID).attr('disabled')) {
			$(boxID+' > a').animate({opacity: '0.7'}, 0);
			$(boxID+' > a').click(function(){return false;});
			$(boxID+' > a').css('cursor', 'default');
			$(boxID+' > a > span').css('cursor', 'default');
		} else {
			$(boxID+' > a').animate({opacity: '1'}, 0);
			$(boxID+' > a').click(comboBoxClick);
			$(boxID+' > a').css('cursor', '');
			$(boxID+' > a > span').css('cursor', 'pointer');
		}
		$(boxID+' > ul > li > a').click(function(){
			var thisRel = $(this).attr('rel');
			var thisText = $(this).text();
			comboBoxItemClick(this,thisRel,thisText);
			return false;
		});
	};

	function comboBoxClick() {
		updateCustomComboBoxSizes('#'+$(this).parent().attr('id'));
		isVisible = !$(this).parent().find('ul').is(':visible');
		temp = this;
		$('.combobox > ul').hide();
		if (isVisible) {
			$(this).parent().find('ul').show();
			updateCustomComboBoxSizes('#'+$(this).parent().attr('id'));
		}
		if ($.customComboBox.needsIframe) {
			if(!isVisible) {
				$('#comboboxhelper').hide();
			} else {
				$('#comboboxhelper').show();
				$('#comboboxhelper').css('left', $(this).parent().find('ul').offset().left);
				$('#comboboxhelper').css('top', $(this).parent().find('ul').offset().top);
				$('#comboboxhelper').css('width', $(this).parent().find('ul').width());
				$('#comboboxhelper').css('height', $(this).parent().find('ul').outerHeight());
				$('#comboboxhelper').css('border', '5px solid red');
			}
		}
		return false;
	}

	function comboBoxItemClick(lnk,val,txt) {
		var boxID = $(lnk).parent().parent().parent().attr('id');
		var oldVal = $.customComboBox.xSelect[boxID].val();
		if(val==$("#"+boxID+">ul>li>a").get(0).rel){
			txt = $("select.replace-select[title='#"+boxID+"']").find('option:first').text();
		}
		$.customComboBox.xSelect[boxID].val(val);
		if (val != oldVal) {
			$.customComboBox.xSelect[boxID].change();
		}
		$(lnk).parent().parent().parent().find('a span').text(txt);
		$(document).click();
		return false;
	}

	$(document).click(function(e) {
		$('.combobox ul').hide();
		if ($.customComboBox.needsIframe) {
			$('#comboboxhelper').hide();
		}
	});

})(jQuery);


$(document).ready(function() {
	$('select.replace-select').customComboBox();
});
