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

setMaincover = function(){	
	g_nSlidingWidth = 468;
	g_nTotalcovers = $('#coverholder .cover').length;

	if($('#cover_0 > a > img').width() > 468) {
		$('#main_cover_reel').css({'width' :'1428px'});
		$('#main_cover_upcoming').css({'width' : '960px'});	
		doublewide = true;
	} else {
		doublewide = false;
	}	

	if( $('div#cover_0').length ){
		if(doublewide) {
			$('#main_cover_reel').css({'width' :'1920px'});
			$('#main_cover_current').css({'width' : '960px'}).html($('#cover_0').html());
			$('#main_cover_holder').css({'width' : '960px'});
			$('#coverholder_container').animate({'width' : '960px'}, 400);
			$('.main_cover').animate({'width' : '960px'}, 400, function() {				
				setTimeout(function(){		
					currRel = $('#cover_0').attr('rel');
					scrollToLead(currRel);			
					if(! $('.covercount_1').length ){
						setAutoRotate();
					}
					$('#covernav_0').addClass('current');
				},100);
			});
		} else {
			setTimeout(function(){
				$('#main_cover_current').html( $('#cover_0').html());			
				currRel = $('#cover_0').attr('rel');
				scrollToLead(currRel);			
				if(! $('.covercount_1').length ){
					setAutoRotate();
				}
				$('#covernav_0').addClass('current');
				setTimeout(function(){
				},500);
			},100);
		}
		
		
	} else {
		$('#cover_0 .covertext_0').load(function(){
			setTimeout(function(){
				$('#main_cover_current').html( $('#cover_0').html());
				currRel = $('#cover_0').attr('rel');
				if(! $('.covercount_1').length ){
					setAutoRotate();
					scrollToLead(currRel);
				}
				$('#covernav_0').addClass('current');
				setTimeout(function(){
				},500);
			},100);
		});
	}
	var pagination_padding = Math.ceil(($('#main_cover').width() - $('.maincover_pagination').width()) /2 +10);
	$('.maincover_pagination').css({'padding-left' : pagination_padding + 'px'});
	$(' .covernav').hover(function(){
		$(this).addClass('hover');
	},function(){
		$(this).removeClass('hover');
	});
}

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

rotateRight = function(nextLead){
	clearTimeout(timeoutObject);	
	currRel = $('#cover_'+nextLead).attr('rel');
	scrollToLead(currRel);		
	$('#main_cover_reel').animate({
	  left: (0 - g_nSlidingWidth )
	}, 800,   function(){
		if(doublewide) {
			$('#main_cover_reel').css({'width' :'1920px'});
			$('#coverholder_container').animate({'width' : '960px'}, 400);
			$('.main_cover').animate({'width' : '960px'}, 400, function() {
				setTimeout(function(){
					$('#main_cover_current').css({'width' : '960px'}).html( $('#main_cover_upcoming').html());
					$('#main_cover_holder').css({'width' : '960px'});
					$('#main_cover_reel').css('left','0px');
					g_bCurrentlyRotating = false;
					setAutoRotate();
				},400);
			});
		} else {
			$('#main_cover_reel').css({'width' :'936px'});
			$('#main_cover_current').html( $('#main_cover_upcoming').html());
			setTimeout(function(){
				$('#main_cover_reel').css('left','0px');
				g_bCurrentlyRotating = false;
				setAutoRotate();
			},400);
		}
	});
}
rotateLeft = function(nextLead){
	clearTimeout(timeoutObject);
	currRel = $('#cover_'+nextLead).attr('rel');
	scrollToLead(currRel);	
	$('#main_cover_reel').animate({
	  left: 0
	}, 800,   function(){
		if(doublewide) {
			$('#main_cover_reel').css({'width' :'1920px'});
			$('#main_cover_current').css({'width' : '960px'}).html( $('#main_cover_current').html());
			$('#coverholder_container').animate({'width' : '960px'}, 400);
			$('.main_cover').animate({'width' : '960px'}, 400, function() {
				setTimeout(function(){					
					$('#main_cover_holder').css({'width' : '960px'});
					$('#main_cover_reel').css('left','0px');
					g_bCurrentlyRotating = false;
					setAutoRotate();
				},400);
			});
		} else {
			$('#main_cover_reel').css({'width' :'936px'});
			$('#main_cover_upcoming').html( $('#main_cover_current').html());
			setTimeout(function(){
				$('#main_cover_reel').css('left','0px');
				g_bCurrentlyRotating = false;
				setAutoRotate();
			},400);
		}
	});
}

pausecover = function(){
	g_bAutoRorate = false;
	clearTimeout(timeoutObject);
	$('#main_cover').removeClass('show_pause');
	$('#main_cover').addClass('show_play');
}

playcover = function(){
	g_bAutoRorate = true;
	autoRotate();
	$('#main_cover').removeClass('show_play');
	$('#main_cover').addClass('show_pause');
}

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

jumpTocover = function(p_nPos){
	if(! g_bCurrentlyRotating ){
		if(p_nPos != g_nPositionValue){
			g_bCurrentlyRotating = true;			
			$('#covernav_'+g_nPositionValue).removeClass('current');
			currRel = $('#cover_'+g_nPositionValue).attr('rel');
			if(p_nPos < g_nPositionValue){
				g_nPositionValue = p_nPos;
				if(doublewide) {
					$('#coverholder_container').animate({'width' : '468px'}, 200);
					$('.main_cover').animate({'width' : '468px'}, 200, function() {
						$('#main_cover_current').css({'width' : '468px'});
						$('#main_cover_holder').css({'width' : '468px'});
						$('#main_cover_reel').css({'width' :'936px'});
						$('#main_cover_upcoming').css({'width' : '468px'});	
					});
				}				
				$('#covernav_'+g_nPositionValue).addClass('current');
				if($('#cover_'+ g_nPositionValue + ' > a > img').width() > 468) {
					$('#main_cover_reel').css({'width' :'1428px'});
					$('#main_cover_upcoming').css({'width' : '960px'});	
					doublewide = true;
				} else {
					doublewide = false;
				}				
				$('#main_cover_upcoming').html( $('#main_cover_current').html());
				setTimeout(function(){
					$('#main_cover_reel').css('left','-'+g_nSlidingWidth+'px');
					$('#main_cover_current').html( $('#cover_'+g_nPositionValue).html());
					setTimeout(function(){
						$('.recent[rel*='+ currRel +']').css({'background': 'none'});
						rotateLeft(g_nPositionValue);
					},300);
				},300);
			} else {	
				g_nPositionValue = p_nPos;
				if(doublewide) {
					$('#coverholder_container').animate({'width' : '468px'}, 200);
					$('.main_cover').animate({'width' : '468px'}, 200, function() {
						$('#main_cover_current').css({'width' : '468px'});
						$('#main_cover_holder').css({'width' : '468px'});
						$('#main_cover_reel').css({'width' :'936px'});
						$('#main_cover_upcoming').css({'width' : '468px'});	
					});
				}				
				$('#covernav_'+g_nPositionValue).addClass('current');				
				if($('#cover_'+ g_nPositionValue + ' > a > img').width() > 468) {
					$('#main_cover_reel').css({'width' :'1428px'});
					$('#main_cover_upcoming').css({'width' : '960px'});	
					doublewide = true;
				} else {
					doublewide = false;
				}
				$('#main_cover_upcoming').html( $('#cover_'+g_nPositionValue).html());
				setTimeout(function(){
					$('.recent[rel*='+ currRel +']').css({'background': 'none'});
					rotateRight(g_nPositionValue);
				},300);
			}
		}
	}
}

getNextcover = function(){
	if(! g_bCurrentlyRotating ){
		g_bCurrentlyRotating = true;		
		if(doublewide) {
			$('#coverholder_container').animate({'width' : '468px'}, 200);
			$('.main_cover').animate({'width' : '468px'}, 200, function() {
				$('#main_cover_current').css({'width' : '468px'});
				$('#main_cover_holder').css({'width' : '468px'});
				$('#main_cover_reel').css({'width' :'936px'});
				$('#main_cover_upcoming').css({'width' : '468px'});	
			});
		}
		$('#covernav_'+g_nPositionValue).removeClass('current')
		currRel = $('#cover_'+g_nPositionValue).attr('rel');				
		g_nPositionValue++;
		if(g_nPositionValue > g_nTotalcovers-1) g_nPositionValue = 0;
		$('#covernav_'+g_nPositionValue).addClass('current')
		clearTimeout(timeoutObject);		
		if($('#cover_'+ g_nPositionValue + ' > a > img').width() > 468) {
			$('#main_cover_reel').css({'width' :'1428px'});
			$('#main_cover_upcoming').css({'width' : '960px'});	
			doublewide = true;
		} else {
			doublewide = false;
		}		
		$('#main_cover_upcoming').html( $('#cover_'+g_nPositionValue).html());
		setTimeout(function(){
			$('.recent[rel*='+ currRel +']').css({'background': 'none'});
			rotateRight(g_nPositionValue);
		},300);
	}
}

getPrevcover = function(){
	if(! g_bCurrentlyRotating ){
		g_bCurrentlyRotating = true;		
		if(doublewide) {
			$('#coverholder_container').animate({'width' : '468px'}, 200);
			$('.main_cover').animate({'width' : '468px'}, 200, function() {
				$('#main_cover_current').css({'width' : '468px'});
				$('#main_cover_holder').css({'width' : '468px'});
				$('#main_cover_reel').css({'width' :'936px'});
				$('#main_cover_upcoming').css({'width' : '468px'});	
			});
		}		
		$('#covernav_'+g_nPositionValue).removeClass('current');		
		currRel = $('#cover_'+g_nPositionValue).attr('rel');		
		g_nPositionValue--;
		if(g_nPositionValue < 0) g_nPositionValue = g_nTotalcovers-1;
		$('#covernav_'+g_nPositionValue).addClass('current');
		clearTimeout(timeoutObject);		
		if($('#cover_'+ g_nPositionValue + ' > a > img').width() > 468) {
			$('#main_cover_reel').css({'width' :'1428px'});
			$('#main_cover_upcoming').css({'width' : '960px'});	
			doublewide = true;
		} else {
			doublewide = false;
		}		
		$('#main_cover_upcoming').html( $('#main_cover_current').html());
		setTimeout(function(){
			$('#main_cover_reel').css('left','-'+g_nSlidingWidth+'px');			
			$('#main_cover_current').html( $('#cover_'+g_nPositionValue).html());
			setTimeout(function(){
				$('.recent[rel*='+ currRel +']').css({'background': 'none'});
				rotateLeft(g_nPositionValue);
			},300);
		},300);
	}
}

function scrollToLead(currRel) {
	if(currRel == "" || currRel == null || currRel == false || currRel == undefined) {
		if(currRel == "0") {
			$('.recent[rel*='+currRel+']').css({'background' : '#FFFFFF url(/images/misc/leads_background.gif) no-repeat top right'});
		} else {
			g_nTotalrecent = $('.cover').length;
			for(i=0;i<=g_nTotalrecent;i++) {
				$('.recent[rel*='+ i +']').css({'background': 'none'});
			}
		}
	} else {
		$('.recent[rel*='+currRel+']').css({'background' : '#FFFFFF url(/images/misc/leads_background.gif) no-repeat top right'});
	}
}
