jQuery.changeViewer = function(parameter) {
	var flag = new Array();
	var arr = new Array();
	
	var def = {
		css: 2,
		element: 'body',
		zIndex: 0,
		botao: true
	};
	
	var settings = jQuery.extend(def,parameter);
	
	var fnMouseover = function(objPrev,obj,index) {
		if(!flag[index]) {
			flag[index] = 1;
			if(obj.height() == obj.prev().height()) {
				obj.css({
					height: obj.height()+objPrev.height()+'px',
					top: '-'+objPrev.height()+'px'
				});
			}
			objPrev.animate(
				{top: '-'+objPrev.height()+'px'}, 100, 'swing',
				function() {
					flag[index] = 0;
					jQuery(this).css({zIndex: 500+settings.zIndex});
					fnVerificar(index);
				}
			);
		}
	};
	
	var fnMouseout = function(objPrev,obj,index) {
		if(!flag[index]) {
			flag[index] = 1;
			if(obj.height() > obj.prev().height()) {
				objPrev.css({zIndex: 1+settings.zIndex});
				obj.css({
					height: obj.height()-objPrev.height()+'px',
					top: '0px'
				});
			}
			objPrev.animate(
				{top: '0px'}, 100, 'swing',
				function() {
					flag[index] = 0;
					fnVerificar(index,flag[index]);
				}
			);
		}
	};
	
	var fnVerificar = function(index,flagIndex) {
		jQuery(settings.selector).each(function(i) {
			if(i==index) {
				if(isNaN(flagIndex)) {
					if((arr[i][0] == 0)&&(arr[i][1] == 0))
						fnMouseout(jQuery(this).prev(),jQuery(this).next(),i);
					else fnMouseover(jQuery(this).prev(),jQuery(this).next(),i);			
				} else {
					if((arr[i][0] == 0)&&(arr[i][1] == 0)) {
						if(flagIndex)
							fnMouseout(jQuery(this).prev(),jQuery(this).next(),i);
					} else if(!flagIndex) fnMouseover(jQuery(this).prev(),jQuery(this).next(),i);
				}
			}
		});
	};
	//-------------------------------------------------------------------------------------------------
	
	// verificar se o CSS existe e carregá-lo
	jQuery.ajax({
		url: settings.file,
		success: function() {
			jQuery('head').append('<link rel="stylesheet" href="'+settings.file+'" type="text/css" media="all" />');
		}
	});

	var zIndexNumber = 500+settings.zIndex; 
	var count = jQuery(settings.selector).length;
	jQuery(settings.selector).each(function(i) {
		var $this = jQuery(this); // container-cont
		var $next = $this.next(); // conteúdo
		var $prev = $this.prev(); // menu
		
		flag[i] = 0;
		arr[i] = new Array(0,0);
		
		if(navigator.appVersion.search("MSIE") != -1) { // IE
			$this.parent().css({zIndex: zIndexNumber});
			zIndexNumber -= 10;
		}
		
		objCSS = {};
		/*if(settings.css == 1) {
			objCSS = {
				left: '-6px',
				top: '0px',
				width: $this.width()+'px'
			};
		}*/
		objCSS = jQuery.extend(objCSS,{
			display: 'block',
			position: 'absolute',
			zIndex: 1+settings.zIndex
		});
		$prev.css(objCSS).parent().css({position: 'relative'});
		
		objCSS = (settings.css == 1) ? {
			left: $next.position().left,
			top: $next.position().top
		} : {
			left: '-6px',
			top: '-6px'
		};
		objCSS = jQuery.extend(objCSS,{
			backgroundColor: '#ffffff',
			border: '1px solid #666',
			
			height: ($next.height()+10)+'px',
			position: 'absolute',
			width: ($next.width()+10)+'px',
			zIndex: 3+settings.zIndex
		});
		//$this.css(objCSS).after('<input type="button" class="botao" />');
		if(settings.botao) {
			$this.css(objCSS).after('<input type="button" class="botao" />');
		} else {
			$this.css(objCSS);
		}
		
		$next.css({
			opacity: ($this.attr('despublicado')=="V") ? 0.5 : 1,
			position: 'relative',
			zIndex: 5+settings.zIndex
		}).find('img,iframe').load(function() {
			var higher = (jQuery(this).height() > $next.height()) ? jQuery(this).height() : $next.height();
			objCSS = {
				height: (higher+10)+'px',
				width: ($next.width()+8)+'px'
			};
			$this.css(objCSS).next().css(objCSS);
		});
		
		objCSS = (settings.css == 1) ? {
			left: $this.position().left,
			top: $this.position().top
		} : {
			left: '-6px',
			top: '0px'
		};
		objCSS = jQuery.extend(objCSS,{
			border: 'none',
		
			height: ($this.height())+'px',
			opacity: 0.1, // importante para o IE
			position: 'absolute',
			width: ($this.width())+'px',
			zIndex: 15+settings.zIndex
		});
		
		if(settings.botao) {
			$this.next().css(objCSS); // input[type=button]
		}

		$prev.css({
			left: $this.position().left,
			top: $this.position().top,
			width: ($this.width()-4)+'px'
		});
		
		if(count > 1) {
			$prev
				.bind('mouseover',function() { arr[i][1] = 1; })
				.bind('mouseout',function() { 	arr[i][1] = 0; })
				.bind('mouseover mouseout',function() { fnVerificar(i); });
			
			if(settings.botao) {
				$this.next()
					.bind('mouseover',function() { arr[i][0] = 1; })
					.bind('mouseout',function() { arr[i][0] = 0; })
					.bind('mouseover mouseout',function() { fnVerificar(i); });
			}
		} else {
			$prev.css({
				top: '-'+$prev.height()+'px',
				zIndex: 500+settings.zIndex
			});
		}
	});
	
	if(count >= 1) {
		jQuery('body').append('<div id="overlay"></div>');
		jQuery('#overlay').css({
			backgroundColor: '#000000',
			opacity: '0.6',
			zIndex: settings.zIndex,
			height: '100%',
			position: 'absolute',
			top: jQuery(settings.element).offset().top,
			width: jQuery(settings.element).width()
		});
	}
};
