var g_bAutoRorate = true;
var timeoutObject;
var g_nPositionValue = 0;
var g_nTotalFeatures = 0;
var g_bCurrentlyRotating = false;
var p1, p2, p3, p4, p5, p6,p2_out,p3_out,p4_out,p5_out,p1_out,p6_out;

if(document.images)
	{
		//CreateImages();
		//alert("Initializing document Images");
		p1 =  new Image();
		p1.src = "/images/modules/NewsPlayer/h_pager_lft_arrow.gif";
		
		p2 =  new Image();
		p2.src = "/images/modules/NewsPlayer/h_img_1.gif";
		
		p3 =  new Image();
		p3.src = "/images/modules/NewsPlayer/h_img_2.gif";
		
		p4 =  new Image();
		p4.src = "/images/modules/NewsPlayer/h_img_3.gif";
				
		p5 =  new Image();
		p5.src = "/images/modules/NewsPlayer/h_img_4.gif";
		
		p6 =  new Image();
		p6.src = "/images/modules/NewsPlayer/h_pager_rht_arrow.gif";
        
        p2_out =  new Image();
		p2_out.src = "/images/modules/NewsPlayer/img_1.gif";
		
		p3_out =  new Image();
		p3_out.src = "/images/modules/NewsPlayer/img_2.gif";
		
		p4_out =  new Image();
		p4_out.src = "/images/modules/NewsPlayer/img_3.gif";
		
		p5_out =  new Image();
		p5_out.src = "/images/modules/NewsPlayer/img_4.gif";
		
		p1_out =  new Image();
		p1_out.src = "/images/modules/NewsPlayer/pager_lft_arrow.gif";
		
		p6_out =  new Image();
		p6_out.src = "/images/modules/NewsPlayer/pager_rht_arrow.gif";
	}

	SetInitialImages = function() {
	    //alert('SetInitialImages');
	    if (document.getElementById('a2') != null) {
	        document.getElementById('a2').src = p2.src;
	    }
	    if (document.getElementById('a3') != null) {
	        document.getElementById('a3').src = p3_out.src;
	    }
	    if (document.getElementById('a4') != null) {
	        document.getElementById('a4').src = p4_out.src;
	    }
	    if (document.getElementById('a5') != null) {
	        document.getElementById('a5').src = p5_out.src;
	    }
	}

	SetImageOfCurrentPage = function(prev, next) {
	    prev = prev + 1;
	    next = next + 1;
	    //alert("SetImageOfCurrentPage");

	    if (next == 1) {
	        a_nxt = document.getElementById('a2');
	        if (a_nxt != null) {
	            a_nxt.src = p2.src;
	        }
	        if (document.getElementById('a5') != null) {
	            a_pre = document.getElementById('a5');
	            a_pre.src = p5_out.src;
	        }
	    }
	    else if (next == 2) {
	        a_nxt = document.getElementById('a3');
	        if (a_nxt != null) {
	            a_nxt.src = p3.src;
	        }

	        if (document.getElementById('a2') != null) {
	            a_pre = document.getElementById('a2');
	            a_pre.src = p2_out.src;
	        }
	    }
	    else if (next == 3) {
	        a_nxt = document.getElementById('a4');
	        if (a_nxt != null) {
	            a_nxt.src = p4.src;
	        }
	        if (document.getElementById('a3') != null) {
	            a_pre = document.getElementById('a3');
	            a_pre.src = p3_out.src;
	        }
	    }
	    else if (next == 4) {
	        a_nxt = document.getElementById('a5');
	        if (a_nxt != null) {
	            a_nxt.src = p5.src;
	        }
	        if (document.getElementById('a4') != null) {
	            a_pre = document.getElementById('a4');
	            a_pre.src = p4_out.src;
	        }
	    }
	}
	
setMainFeature = function(){
	 
	g_nTotalFeatures = $('#featureholder .feature').length;
	
	$('#main_feature_current').html( $('#feature_0').html()  );
	setAutoRotate();
	$('#featurenav_0').addClass('current');
		 
	$('#featureholder .featuretext').each(function(){ $(this).attr('src',  $(this).attr('rev'))});
		
	
	$(' .featurenav').hover(function(){
		$(this).addClass('hover');
	},function(){
		$(this).removeClass('hover');
	});
	fixCredits('main_feature_current');
	
}

fixCredits = function(p_sStr){
	var l_xPos = $('#'+p_sStr+' .content').position();
	var l_nHeight = $('#'+p_sStr+' .mainfeature-image').height() + 11;
	if(l_xPos!=null &&  l_xPos['top'] < l_nHeight)
	 	$('#'+p_sStr+' .photo-credit').css('top',l_xPos['top']);
	else
		$('#'+p_sStr+' .photo-credit').css('top',l_nHeight);
}
autoRotate = function(){
	if(! g_bCurrentlyRotating ){
		g_bAutoRorate = true;
		clearTimeout(timeoutObject);
		getNextFeature();
	}
}

rotateRight = function(){
	 
	clearTimeout(timeoutObject);
	fixCredits('main_feature_upcoming');
	
	$('#main_feature_reel').animate({
	  left: -422,
	  easing: 'easeOutCubic'
	}, 800,   function(){
		$('#main_feature_current').html( $('#main_feature_upcoming').html() );
		$('#main_feature_reel').css('left','0px');
		
 
		g_bCurrentlyRotating = false;
		setAutoRotate();
	});
	
}
rotateLeft = function(){
	 
	clearTimeout(timeoutObject);
	fixCredits('main_feature_current');
	$('#main_feature_reel').animate({
	  left: 0,
	  easing: 'easeOutCubic'
	}, 800,   function(){
		$('#main_feature_upcoming').html( $('#main_feature_current').html() );
		
		g_bCurrentlyRotating = false;
		setAutoRotate();
	});
	
}


pauseFeature = function(){
	g_bAutoRorate = false;
	clearTimeout(timeoutObject);
	$('#main_feature').removeClass('show_pause');
	$('#main_feature').addClass('show_play');
}


playFeature = function(){
	g_bAutoRorate = true;
	autoRotate();
	$('#main_feature').removeClass('show_play');
	$('#main_feature').addClass('show_pause');
	
}


setAutoRotate = function(){
	clearTimeout(timeoutObject);
	
	if(	g_bAutoRorate ){
		timeoutObject = setTimeout(function(){
			autoRotate();
		}, 9000);
	}
	
}

jumpToFeature = function(p_nPos){
	
	if(! g_bCurrentlyRotating ){
		
		if(p_nPos != g_nPositionValue){
			
			g_bCurrentlyRotating = true;
			$('#featurenav_'+g_nPositionValue).removeClass('current');
			
			if(p_nPos < g_nPositionValue){
				g_nPositionValue = p_nPos;
				$('#featurenav_'+g_nPositionValue).addClass('current');
				
				
				$('#main_feature_upcoming').html( $('#main_feature_current').html() );
				$('#main_feature_reel').css('left','-422px');
			
				$('#main_feature_current').html( $('#feature_'+g_nPositionValue).html()  );
				rotateLeft();
			}
			else{	
				g_nPositionValue = p_nPos;
				$('#featurenav_'+g_nPositionValue).addClass('current');
				
				$('#main_feature_upcoming').html( $('#feature_'+g_nPositionValue).html()  );
				rotateRight();
			}
		}
	}
}

getNextFeature = function(){
	if(! g_bCurrentlyRotating ){
		g_bCurrentlyRotating = true;
		
		$('#featurenav_'+g_nPositionValue).removeClass('current')
		var pre = g_nPositionValue;
		g_nPositionValue++;
		
		if(g_nPositionValue > g_nTotalFeatures-1) g_nPositionValue = 0;
		
		$('#featurenav_'+g_nPositionValue).addClass('current')
		SetImageOfCurrentPage(pre,g_nPositionValue);
		clearTimeout(timeoutObject);
		
		$('#main_feature_upcoming').html( $('#feature_'+g_nPositionValue).html()  );
		rotateRight();
	}
}

getPrevFeature = function(){
	
	if(! g_bCurrentlyRotating ){
		g_bCurrentlyRotating = true;
		
		$('#featurenav_'+g_nPositionValue).removeClass('current')
		g_nPositionValue--;
		if(g_nPositionValue < 0) g_nPositionValue = g_nTotalFeatures-1;
		$('#featurenav_'+g_nPositionValue).addClass('current')
		
		$('#main_feature_upcoming').html( $('#main_feature_current').html() );
		$('#main_feature_reel').css('left','-422px');
		
		clearTimeout(timeoutObject);
		
		$('#main_feature_current').html( $('#feature_'+g_nPositionValue).html()  );
		
		rotateLeft();
		
	}
}