/*
	LightBox usando resize.php
*/
$.fn.lbox = function(opcoes){

		var settings = {
			'autoSize':true,
			'newWidth':'w=700',
			'newHeight':'h=350',
			'width':null,
			'height':null,
			'target':null,
			'imagens':null
		}
	  	settings = $.extend(settings,opcoes);
		var imagens = new Array();

		function clickImg(){
			var widthImg = new RegExp('^w=');
			var heightImg = new RegExp('^h=');
			var separado = this.src.split('&');
			for(j in separado)
			{
				if (widthImg.test(separado[j]))
					settings.width = separado[j];
				else if(heightImg.test(separado[j]))
					settings.height = separado[j];
			}
			if (settings.autoSize)
			{
				settings.newWidth = 'w='+$(settings.target).attr('width');
				settings.newHeight= 'h='+$(settings.target).attr('height');
			}
			var novo_caminho = this.src.replace(settings.width,settings.newWidth);
			novo_caminho = novo_caminho.replace(settings.height,settings.newHeight);
			trocaImagem(novo_caminho,this);
		}

		function trocaImagem(caminho,old_img){
			$(old_img).attr({'src':$(settings.target).attr('src'),'title':$(settings.target).attr('title')});
			$('body').css('cursor','wait');
			$(settings.target).attr('src',caminho).css('cursor','wait').load(loadImg);
		}

		function loadImg(){
			$('body').css('cursor','default');
			$(this).css('cursor','default');
			var local = new RegExp('.(localhost|192.)');

			$(this).hide().fadeIn(400,function(){
				var src = this.src.split('=')[1].split('&')[0]
				var testar = this.src.split('?')[0];
				if (local.test(testar))
					src = 'http://localhost/immob/'+src;
				else
					src = 'http://immob.com.br/'+src;
				$(this).parent().attr('href',src);
			});
		}

		return this.each(function(){
			imagens.push(this);
			$(this).click(clickImg);
		});
}
