var g_bAutoRorate = true;
var timeoutObject;
var g_nPositionValue = 0;
var g_nTotaleditorials = 0;
var g_bCurrentlyRotating = false;
var g_nSlidingWidth = 0;
var currRel = 0;

setMaineditorial = function(){
	
	g_nSlidingWidth = 960;
	g_nTotaleditorials = $('#editorialholder .editorial').length;	
	if( $('div#editorial_0').length ){
		setTimeout(function(){
			$('#main_editorial_current').html( $('#editorial_0').html());
			
			currRel = $('#editorial_0').attr('rel');
			
			if(! $('.editorialcount_1').length ){
				setAutoRotate();
			}
			$('#editorialnav_0').addClass('current');
			setTimeout(function(){
			},500);
		},100);
	} else {
		$('#editorial_0 .editorialtext_0').load(function(){
			setTimeout(function(){
				$('#main_editorial_current').html( $('#editorial_0').html());
				currRel = $('#editorial_0').attr('rel');
				if(! $('.editorialcount_1').length ){
					setAutoRotate();
				}
				$('#editorialnav_0').addClass('current');
				setTimeout(function(){
				},500);
			},100);
		});
	}

	var pagination_padding = Math.ceil(($('#main_editorial').width() - $('.maineditorial_pagination').width()) /2 +10);
	$('.maineditorial_pagination').css({'padding-left' : pagination_padding + 'px'});
	
	
	$(' .editorialnav').hover(function(){
		$(this).addClass('hover');
	},function(){
		$(this).removeClass('hover');
	});
}

autoRotate = function(){
	if(! g_bCurrentlyRotating ){
		g_bAutoRorate = true;
		clearTimeout(timeoutObject);
		getNexteditorial();
	}
}

rotateRight = function(nextLead){
	clearTimeout(timeoutObject);
	
	//Add style to lead
	currRel = $('#editorial_'+nextLead).attr('rel');
	
	$('#main_editorial_reel').animate({
	  left: (0 - g_nSlidingWidth )
	}, 800,   function(){
		$('#main_editorial_current').html( $('#main_editorial_upcoming').html());
		setTimeout(function(){
			$('#main_editorial_reel').css('left','0px');
			g_bCurrentlyRotating = false;
			setAutoRotate();
		},400);
	});
}
rotateLeft = function(nextLead){
	clearTimeout(timeoutObject);

	//Add style to lead
	currRel = $('#editorial_'+nextLead).attr('rel');
	
	$('#main_editorial_reel').animate({
	  left: 0
	}, 800,   function(){
		$('#main_editorial_upcoming').html( $('#main_editorial_current').html());
		setTimeout(function(){
			g_bCurrentlyRotating = false;
			setAutoRotate();
		},200);
	});
}

pauseeditorial = function(){
	g_bAutoRorate = false;
	clearTimeout(timeoutObject);
	$('#play_pause .show_play_new').removeClass('hide');
	$('#play_pause .show_pause_new').addClass('hide');
}

playeditorial = function(){
	g_bAutoRorate = true;
	autoRotate();
	$('#play_pause .show_pause_new').removeClass('hide');
	$('#play_pause .show_play_new').addClass('hide');
}

setAutoRotate = function(){
	clearTimeout(timeoutObject);
	if(	g_bAutoRorate ){
		timeoutObject = setTimeout(function(){
			autoRotate();
		}, 9000);
	}
}

jumpToeditorial = function(p_nPos){
	if(! g_bCurrentlyRotating ){
		if(p_nPos != g_nPositionValue){
			g_bCurrentlyRotating = true;
			$('#editorialnav_'+g_nPositionValue).removeClass('current');

			//Remove current class for leads
			currRel = $('#editorial_'+g_nPositionValue).attr('rel');
			

			if(p_nPos < g_nPositionValue){
				g_nPositionValue = p_nPos;
				$('#editorialnav_'+g_nPositionValue).addClass('current');
				$('#main_editorial_upcoming').html( $('#main_editorial_current').html());
				setTimeout(function(){
					$('#main_editorial_reel').css('left','-'+g_nSlidingWidth+'px');
					$('#main_editorial_current').html( $('#editorial_'+g_nPositionValue).html());
					setTimeout(function(){
						$('.recent[rel*='+ currRel +']').css({'background': 'none'});
						rotateLeft(g_nPositionValue);
					},200);
				},200);
			} else {	
				g_nPositionValue = p_nPos;
				$('#editorialnav_'+g_nPositionValue).addClass('current');
				$('#main_editorial_upcoming').html( $('#editorial_'+g_nPositionValue).html());
				setTimeout(function(){
					$('.recent[rel*='+ currRel +']').css({'background': 'none'});
					rotateRight(g_nPositionValue);
				},100);
			}
		}
	}
}

getNexteditorial = function(){
	if(! g_bCurrentlyRotating ){
		g_bCurrentlyRotating = true;
		$('#editorialnav_'+g_nPositionValue).removeClass('current')

		//Remove current class for leads
		currRel = $('#editorial_'+g_nPositionValue).attr('rel');
		
		
				
		g_nPositionValue++;
		if(g_nPositionValue > g_nTotaleditorials-1) g_nPositionValue = 0;
		$('#editorialnav_'+g_nPositionValue).addClass('current')
		clearTimeout(timeoutObject);
		$('#main_editorial_upcoming').html( $('#editorial_'+g_nPositionValue).html());
		setTimeout(function(){
			$('.recent[rel*='+ currRel +']').css({'background': 'none'});
			rotateRight(g_nPositionValue);
		},100);
	}
}

getPreveditorial = function(){
	if(! g_bCurrentlyRotating ){
		g_bCurrentlyRotating = true;
		$('#editorialnav_'+g_nPositionValue).removeClass('current');
		
		currRel = $('#editorial_'+g_nPositionValue).attr('rel');
		
		
		g_nPositionValue--;
		if(g_nPositionValue < 0) g_nPositionValue = g_nTotaleditorials-1;
		$('#editorialnav_'+g_nPositionValue).addClass('current')
		
		$('#main_editorial_upcoming').html( $('#main_editorial_current').html());
		setTimeout(function(){
			$('#main_editorial_reel').css('left','-'+g_nSlidingWidth+'px');
			clearTimeout(timeoutObject);
			$('#main_editorial_current').html( $('#editorial_'+g_nPositionValue).html());
			setTimeout(function(){
				$('.recent[rel*='+ currRel +']').css({'background': 'none'});
				rotateLeft(g_nPositionValue);
			},200);
		},200);
	}
}

