$.fn.lightbox = function(options,configuracoes){

	var defaults = {
            'top':'0px',
            'left':'0px',
            'background':'#000',
            'borderColor':'#ccc',
            'position':'absolute',
            'display':'block',
            'width':$(document).width(),
            'height':$(document).height(),
            'z-index':'5',
            'opacity':0
	};

	var config = {
		trigger:null
	};
        
        var base_url = 'http://www.immob.com.br/';

	var opts = $.extend(defaults, options);
	var cfg = $.extend(config,configuracoes);
	var imgs = new Array();
	var titles = new Array();
	var atual = 0;
	function checaConfig() {
		if (cfg.trigger!=null) {
                    $(cfg.trigger).each(function(){
                        imgs.push(this.src);
                        titles.push(this.title);
                    });
		}
	}
	function fechaDiv(e) {
            if (e.keyCode!=null)
                if (e.keyCode==27)
                    var fecha = true;
                else
                    var fecha = false;
            if (fecha==undefined|| fecha || $.browser.msie) {
                $('#div_internalight').fadeOut(200);
                $('#bglight').slideUp(700,function(){
                        $(this).remove();
                        $('#div_internalight').remove();
                });
            }
	}
	function filterResults(n_win, n_docel, n_body) {
		var n_result = n_win ? n_win : 0;
		if (n_docel && (!n_result || (n_result > n_docel)))
			n_result = n_docel;
		return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
	}
	function getscrollTop(){
		return filterResults(
			window.pageYOffset ? window.pageYOffset : 0,
			document.documentElement ? document.documentElement.scrollTop : 0,
			document.body ? document.body.scrollTop : 0
		);
	}
	function clientHeight() {
		return filterResults (
			window.innerHeight ? window.innerHeight : 0,
			document.documentElement ? document.documentElement.clientHeight : 0,
			document.body ? document.body.clientHeight : 0
		);
	}
	function clientWidth() {
	return filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
		);
	}

	function carregaLoading() {
		var loading = document.createElement('img');
		$(loading).attr({'src':base_url+'images/js/ajax-loader.gif','id':'load'}).css({'position':'absolute','top':'45%','left':'45%','z-index':'999','padding':'10px','opacity':0.85});
		$('#div_internalight').children().filter('span').remove();
		var title_foto = document.createElement('span');
		$(title_foto).html((atual+1)+"/"+(imgs.length-1));
		$('#div_internalight').append(loading);
	}

	function openLightBox(){
		var caminho = this.src;
		atual = $(this).attr('data');
		if (atual==undefined)
			atual=0;
		var div_externa = document.createElement('div');
		$(div_externa).attr('id','bglight');
		$('body').prepend(div_externa);
		$(div_externa).click(fechaDiv);
		$(window).keyup(fechaDiv);
		$(div_externa).css(opts).fadeTo(800,0.8,function(){
			var topo = (getscrollTop() + 50)+'px';
			var direita = (clientHeight() * 0.25)+'px';
			var div = document.createElement('div');
			var css_interno ={'height':'450px','width':'400px','background':'#fff','position':'absolute','left':direita,'top':topo,'z-index':999,'border':'4px solid #000','padding':'3px'}
			$(div).attr('id','div_internalight');
			$(div).css(css_interno);
			$('#bglight').after(div);
			if (clientWidth()>1024)
				var src_tmp = caminho.replace('w=440','w=720');
			else
				var src_tmp = caminho.replace('w=440','w=640');
			carregaLoading();
			var img = document.createElement('img');
			$(img).attr({'src':src_tmp}).load(function() {
				var titulo = this.title;
				$(div).css('left',((clientWidth()/2) - (this.width/2)));
				$('#div_internalight img#load').remove();
				var imagem = this;
				if (!$.browser.msie)
					var topo_img = titulo!=''?'-5%':'1%';
				else
					var topo_img = titulo!=''?'-3%':'1%';
				$(imagem).css({'margin':topo_img+' 0 0 5px','overflow':'hidden','position':'relative','border':'1px solid #444'});
				var largura = (this.width+10)+'px';
				var altura = (this.height+72)+'px';
				$('#div_internalight').animate({'width':largura,'height':altura},500,function(){
                                    var p = document.createElement('p');
                                    var fechar = document.createElement('img');

                                    var prox = document.createElement('img');
                                    var ant = document.createElement('img');
                                    
                                    var img_prox = base_url+'images/js/proximo.jpg';
                                    var img_ant = base_url+'images/js/anterior.jpg';
                                    var img_fechar = base_url+'images/js/fechar.jpg';
                                    $(prox).attr({'src':img_prox,'id':'proxImg'}).click(proxFoto).css({'float':'right'});
                                    $(ant).attr({'src':img_ant,'id':'antImg'}).click(antFoto).css({'float':'left'});
                                    var topo_link = (titulo=='')?'0%':'-6.5%';
                                    $(fechar).css({'float':'right','margin':'3px','z-index':'200'}).attr({'id':'fechaLightbox','src':img_fechar}).click(fechaDiv);
                                    $(p).html(titulo).css({'width':'88%','opacity':0.8,'font-size':'2.5em','margin-left':'44px'});

                                    if (document.getElementById('fechaLightbox')==null) {
                                        $(imagem).hide().fadeIn();
                                        if (titulo=='') {
                                            $(this).append(fechar).append(imagem).append(ant).append(prox);
                                        }
                                        else
                                            $(this).append(fechar).append(imagem).append(ant).append(prox).append(p);
                                    }
				});
			});
		});
	}
	function trocaFoto(indice){
		var img = $('#div_internalight img')[1];
		var caminho = imgs[indice];
		if (clientWidth()>1024)
			var tmp = caminho.replace('w=120','w=720');
		else
			var tmp = caminho.replace('w=120','w=640');
		tmp = tmp.replace('&h=90','');
		carregaLoading();
		img.title = titles[indice];
		img.src = tmp;
	}
        
        function hasNext() {
            return atual==imgs.length-1?true:false;
        }
        
        function hasPrev() {
            return atual==0?true:false;
        }
        
	function proxFoto() {
            if (atual!=imgs.length) {
                atual++;
                trocaFoto(atual);
            }
            else
                alert("Você está na última foto.\n Só é possível retornar no momento.");
	}
	function antFoto() {
            if (atual!=0) {
                atual--;
                trocaFoto(atual);
            }
            else
                alert("Você está na primeira foto.\n Só é possível avançar no momento.");
	}
	function chanceCss() {
		$(this).mousemove(toolTip);
		var div = document.createElement('div');
		$(div).html('Clique para ampliar').css({'background':'#555','color':'#fff','position':'absolute','font-size':'1.4em','padding':'2px 8px','display':'none','opacity':0.8}).attr('id','tooltip');
		$('body').append(div);
		$('#tooltip').fadeIn(100);
		$(this).css('cursor','pointer');
	}

	function removeCss() {
		$(this).unbind('onmousemove');
		$('#tooltip').fadeOut(100,function(){
			$(this).remove();
		});
	}

	function toolTip(e) {
            $('#tooltip').css({'top':(e.pageY - 15)+'px','left':(e.pageX + 15)+'px'});
	}

	return this.each(function(){
		checaConfig();
		atual = parseInt($(this).attr('data'));
		$(this).click(openLightBox);
		$(this).hover(chanceCss,removeCss);
	});
};