var stageCounter = 0;
var stageTimer;
var totalSlides = 4;
var nextStage = stageCounter + 1;
var stageDelay = 3000;
var stopStage = false;
var endPlay = false;

// *****************************************************************************

function stageShow2(index) {
	window.clearTimeout(stageTimer);
	nextStage = index;
	rotateStage2(true);
	stageCounter = nextStage;
}

// *****************************************************************************

function startSlide2() {
	
	stageCounter = nextStage;
	nextStage++;
	
	if ( nextStage > totalSlides ) { nextStage = 1;  stopStage = true; }
	
	stageTimer = window.setTimeout('rotateStage2(stopStage)', stageDelay);
}

// *****************************************************************************

function rotateStage2(stop) {
	nextEvent = 'rotateStory2(nextStage);';
	if (!stop) {nextEvent = nextEvent + ' startSlide2();'} else {nextEvent = nextEvent + ' baseLi();' }
	stageTimer = window.setTimeout(nextEvent,300);
	stageCounter = nextStage;
	
}

// *****************************************************************************

function rotateStory2(num) {	
	if (!endPlay){
		var $i = num;
  		var $elemId = 's'+$i;
  	
  		$('ul#stagelist li.x div').hide();
  		//$('ul#stagelist li#'+$elemId+' h1').unbind('click');
		var $parentLi = $('ul#stagelist li#'+$elemId+' h1').parent('li');
		
		$parentLi.toggleClass('x');
		$parentLi.siblings('li.x').toggleClass('x');	
		$('ul#stagelist li.x div').fadeIn('slow');
		
		var $imgId = 'i'+$parentLi.attr('id'); 
		var $imgLi = $('ul#imagelist li#'+$imgId);
		var $imgPrevId = $imgLi.siblings('li.x').attr('id');		
		$('ul#imagelist li#'+$imgPrevId+' img').fadeOut();
		$('ul#imagelist li#'+$imgId+' img').fadeIn();
		
		$('ul#imagelist li#'+$imgPrevId).toggleClass('x');		
		$('ul#imagelist li#'+$imgId).toggleClass('x');		
		
		//return false;
	} else {
		baseLi();		
	}
}

function baseLi() {	
	if (endPlay != true){
		$('ul#stagelist li').addClass('base');	
  		$('ul#stagelist li.x div').hide();  	
	}
}
function whichBrs() {
	var agt=navigator.userAgent.toLowerCase();
	if (agt.indexOf("safari") != -1) {
		$('div#menu').css({ fontSize:"12px"});
	}
}
function check_rating() {	
	//alert('test');
	var $rating = $('input#edit-vote_value').val();
	if ($rating < 1){
		$('div.errormsg').html('You must click on a star to rate this 1-5 before we can post your review.');
		return false;
	}else {
		$('div.errormsg').html('');
		return true;
	}
}
$(document).ready(function() { 
	
	whichBrs(); //if safari fix navbar
	
	$('ul#stagelist li div').hide();
	$('ul#stagelist li.x div').show();
	$('ul#imagelist li.x img').show();
	
	$('ul#stagelist li h1').click(function() {
		window.clearTimeout();
		endPlay = true;
		stopStage = true;
		
		var $parentLi = $(this).parent('li');
		
		if ($parentLi.attr('class')=='x base'){
			jQuery('ul#stagelist li').removeClass('base');
			$('ul#stagelist li.x div').fadeIn('slow');
			
			return false; 
		} else {
			
		jQuery('ul#stagelist li').removeClass('base');		
		
		if ($parentLi.attr('class')=='x'){
			$(this).unbind('click');
		} else {
		
		$('ul#stagelist li.x div').hide();
		$parentLi.toggleClass('x');
		$parentLi.siblings('li.x').toggleClass('x');	
		$('ul#stagelist li.x div').fadeIn('slow');
		
		var $imgId = 'i'+$parentLi.attr('id'); 
		var $imgLi = $('ul#imagelist li#'+$imgId);
		var $imgPrevId = $imgLi.siblings('li.x').attr('id');		
		$('ul#imagelist li#'+$imgPrevId+' img').fadeOut();
		$('ul#imagelist li#'+$imgId+' img').fadeIn();
		
		$('ul#imagelist li#'+$imgPrevId).toggleClass('x');		
		$('ul#imagelist li#'+$imgId).toggleClass('x');	
		
		return false;}
		}


  });  
  
  startSlide2();
  
  //hide "more information about formatting" link
  $('ul.tips').next('a').text('');  
  
  $('a#proximamente').mouseover(function() {
		$('div#tooltip').show();	
		onMOverSubNav(11);
		return false;
  });	
  $('a#proximamente').mouseout(function() {
		$('div#tooltip').hide();	
		return false;
  });
  
  var h, w, aspect;
    $("img.mical").each(function() {
    	if (($(this).width() > 135) || ($(this).height() > 98)){
    		aspect = $(this).height() / $(this).width();
    		if (aspect < 98 / 135) {
    			w = Math.min(135, $(this).width());
    			h = Math.round(135 * aspect);
  			} else {
    			h = Math.min(98, $(this).height());
    			w = Math.round(98 / aspect);
  			}	
  			$(this).attr({height : h });
            $(this).attr({width : w});  	
    	}    	
   });
 	
});

