(function($) {

	$.fn.SelectImg = function(options) {		
		var opts = $.extend({}, $.fn.SelectImg.defaults, options);
		//window.console.log('SelectImg: Init...')
		
		//if($.browser.msie && $.browser.version < 7) { }
		//else { }
		$select = $(this);
		$.fn.SelectImg.ThisObj = $.fn.SelectImg;
		return this.each(function() {
			
			var Img = new Image();
			var Indice = $($select.selector).index($select);
			$select = $($select.selector);
			var ID = $select.attr('id');
			var IdImg = 'img-jq-selectimg-'+ID;
			
			if(opts.CreateContainer) {
				$('<div class="jq-selectimg-container" id="jq-selectimg-container-'+ID+'"></div>').insertAfter($select);
				$('#jq-selectimg-container-'+ID).html('<img src="" style="display:none" id="'+IdImg+'">');
			}
			else {
				('<img src="" style="display:none" id="'+IdImg+'">').insertAfter($select);
			}
			
			$(Img).load(function() {
				
				$select.hide();
				
				$img = $('#'+IdImg);
				
				DivID = "opt-jq-selectimg-"+$select.attr('id');
				$('<div id="'+DivID+'" style="display:none"></div>').insertBefore($img);
				$div = $('#'+DivID);
				
				Links = '';
				$($select.selector+' option').each(function() {
					OptIndex = $($select.selector+' option').index($(this));
					if(OptIndex == 0 && opts.PopulateAuto) {
						$('<span id="jq-selectimg-selected-'+ID+'">&nbsp;<span class="jq-selectimg-opcao">'+$(this).html()+'</span></span>').insertBefore($img);
					}
					Links += '<a href="#" id="link-jq-selectimg-'+OptIndex+'" class="jq-selectimg-links" value="'+$(this).attr('value')+'">'+$(this).html()+'</a>'
				});
				
				$div.html(Links);
				
				
				$img.bind('click',function() {
					if($div.is(':visible')) {
						$div.fadeOut(opts.speed);
					}
					else {
						$div.fadeIn(opts.speed);
					}
				});
				
				$img.attr({src: opts.imagem,style:'cursor: pointer','class':'jq-selectimg-imagem'});
				
				$('.jq-selectimg-links').click(function() {
					
					$('#jq-selectimg-selected-'+ID).remove();
					value = $(this).attr('value');
					$select.val(value);
					$('<span id="jq-selectimg-selected-'+ID+'">&nbsp;<span class="jq-selectimg-opcao">'+$(this).html()+'</span></span>').insertBefore($img);
					$div.fadeOut(opts.speed);
					
					if(opts.Redirect) {
						if($(this).attr('value') != '')
							window.location = $(this).attr('value');					
					}
					return false;
				});
				
			}).attr({src: opts.imagem,id:IdImg,style:'cursor: pointer','class':'jq-selectimg-imagem'});
			
			
			
		});
	};	
	
	$.fn.SelectImg.CriaDiv = function(IdImg) {
		
	}
	$.fn.SelectImg.defaults = {
		speed: "fast",
		PopulateAuto: true,
		CreateContainer: true,
		Redirect: false
	};

})(jQuery);