/*
 * 
 * jQuery EZJax v. 1.0.1
 * http://www.thecreativeoutfit.com
 * 
 * 
 */

jQuery.fn.ezjax = function(o) {
  
  obj = jQuery(this).attr('class'); 
  
  // Defaults
  var o = jQuery.extend( {
    container: '#ezjax_content',
	initial: null, //The initial page which is loaded
	effect: 'fade',//pode ser fade, slide ou NULL
	speed: 150,
	easing: null,
	bind: '.'+obj
  },o);
  
  // Load initial
  
	if(o.initial!=null){
		jQuery(o.container).load(o.initial,function(){
		bind();
	});
	}
  
  // Re-bind for any links internal to the content
  
	function bind(){
		jQuery(o.container+' '+o.bind).ezjax({
			container: o.container,
			initial: null,
			effect: o.effect,
			speed: o.speed,
			easing: o.easing
		});
	}
  
  // Main functionality
  
  return this.each(function() {

  	jQuery(this).change(function(){
		var url = jQuery(this).attr('value');
		
		// Check for transition effect
		
		if (o.effect != null) {
			
			// Run effect
			switch(o.effect){
				
				// Slide
				case 'slide':
				jQuery(o.container).animate({height:"0px"}, o.speed, function(){
					jQuery(o.container).css('overflow','hidden'); // Fix glitchies
					jQuery(o.container).load(url, function(){
						jQuery(o.container).animate({
							height: jQuery(o.container).children().height() + 20
						},o.speed,o.easing,function(){
							jQuery(o.container).css('overflow','visible'); // Undo glitchy fix
						});
						bind();
					});
				});
				break;
				
				// Fade
				case 'fade':
				jQuery(o.container).animate({ opacity: 0.0 }, o.speed, function(){
					jQuery(o.container).load(url, function(){
						jQuery(o.container).animate({ opacity: 1.0 }, o.speed);
						bind();
					});
				});
				break;
			}
		
		}
		else {
			// Standard load (no effect)
			jQuery(o.container).load(url,function(){
				bind();
			});
		}
		a = url.split('&');
		a = a[1].substr(2,a[1].length-2);
		document.location.hash = '#anc';
		// Keeps the href from firing
		return false;
		
	});
  
  	jQuery(this).click(function(){
		var url = jQuery(this).attr('href');

		if (url != null)	{
		// Check for transition effect
		
		if (o.effect != null) {
			
			// Run effect
			switch(o.effect){
				
				// Slide
				case 'slide':
				jQuery(o.container).animate({height:"0px"}, o.speed, function(){
					jQuery(o.container).css('overflow','hidden'); // Fix glitchies
					jQuery(o.container).load(url, function(){
						jQuery(o.container).animate({
							height: jQuery(o.container).children().height() + 20
						},o.speed,o.easing,function(){
							jQuery(o.container).css('overflow','visible'); // Undo glitchy fix
						});
						bind();
					});
				});
				break;
				
				// Fade
				case 'fade':
				jQuery(o.container).animate({ opacity: 0.0 }, o.speed, function(){
					jQuery(o.container).load(url, function(){
						jQuery(o.container).animate({ opacity: 1.0 }, o.speed);
						bind();
					});
				});
				break;
			}
		
		}
		else {
			// Standard load (no effect)
			jQuery(o.container).load(url,function(){
				bind();
			});
		}
//		ant = ant;
//		if (ant!='') { alert('#l'+ant);}
//		$('#l'+ant).removeClass("destacado");
//		$(this).addClass("destacado");
		a = url.split('&');
		$('#selectId')[0].selectedIndex = parseInt(a[1].substr(2,a[1].length-2)) + 1;
//		ant = parseInt(a[1].substr(2,a[1].length-2)) + 1;
		document.location.hash = '#anc';
		// Keeps the href from firing
		return false;
		}
		
	});
  
  });
  
};
