(function($) {

	$.fn.SelectImg = function(options) {		
		var opts = $.extend({}, $.fn.SelectImg.defaults, options);
		//window.console.log('SelectImg: Init...')
		
		if($.browser.msie && $.browser.version < 7) { }
		else { }
		$this = $(this);
		$.fn.SelectImg.ThisObj = $.fn.SelectImg;
		return this.each(function() {
			
			var Img = new Image();
			var Indice = $($this.selector).index($this);
			$select = $($this.selector);
			var ID = $this.attr('id');
			var IdImg = 'img-jq-selectimg-'+ID;
			
			
			$(Img).load(function() {
				$('<div class="jq-selectimg-container" id="jq-selectimg-container-'+ID+'"></div>').insertAfter($this);
				if(opts.CreateContainer) {
					$('#jq-selectimg-container-'+ID).html($(this));
				}
				else {
					$(this).insertAfter($this);
				}
				$this.hide();
				$.fn.SelectImg.CriaDiv(IdImg);
				
				$img = $('#'+IdImg);
				
				
				DivID = "opt-jq-selectimg-"+$this.attr('id');
				$('<div id="'+DivID+'" style="display:none"></div>').insertBefore($('#'+IdImg));
				$div = $('#'+DivID);
				
				Links = '';
				$($this.selector+' option').each(function() {
					OptIndex = $($this.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);
					}
				});
				
				$('.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);
