var mainRotatorInterval;

function switchStory(newStory) {
	
	if ((newStory == 0) || (newStory == undefined)) {
		$('#main_rotator #story'+$mr_currentStory).css('display','none');
		$('#main_rotator #rotator_buttons #btn'+$mr_currentStory).attr("class","off");
		
		if ($mr_currentStory == $mr_storyCount) {
			$mr_currentStory = 1;
		} else {
			$mr_currentStory++;
		}
		
		$('#main_rotator #rotator_buttons a#btn'+$mr_currentStory).attr("class","on");
		
		$mr_currentStoryTitle = $('#main_rotator #story'+$mr_currentStory+' a').html();
		$mr_currentStoryLink = $('#main_rotator #story'+$mr_currentStory+' a').attr("href");
		
		$('#main_rotator #story'+$mr_currentStory).css('display','block');
		$('#main_rotator a#rotatorLink').attr("href",$mr_currentStoryLink);
		$('#main_rotator #rotator_titlebar').html($mr_currentStoryTitle);
		$('#main_rotator #story'+$mr_currentStory).css('display','block');
	} else {
		if ((newStory > 0) && (newStory <= $mr_storyCount)) {
			$('#main_rotator #story'+$mr_currentStory).css('display','none');
			$('#main_rotator #rotator_buttons #btn'+$mr_currentStory).attr("class","off");
			
			$mr_currentStory = newStory;
			
			$('#main_rotator #rotator_buttons #btn'+$mr_currentStory).attr("class","on");
			
			$mr_currentStoryTitle = $('#main_rotator #story'+$mr_currentStory+' a').html();
			$mr_currentStoryLink = $('#main_rotator #story'+$mr_currentStory+' a').attr("href");
			$mr_currentStoryTarget = $('#main_rotator #story'+$mr_currentStory+' a').attr("target"); // ep
			
			
			$('#main_rotator #story'+$mr_currentStory).css('display','block');
			$('#main_rotator a#rotatorLink').attr("href",$mr_currentStoryLink);
			$('#main_rotator a#rotatorLink').attr("target",$mr_currentStoryTarget); // ep
			$('#main_rotator #rotator_titlebar').html($mr_currentStoryTitle);
			$('#main_rotator #story'+$mr_currentStory).css('display','block');
			
			clearInterval(mainRotatorInterval);
			mainRotatorInterval = window.setInterval(switchStory,5000);	
		}
	}
}

$(document).ready(function(){
	$mr_storyCount = $('#main_rotator > .story').length;
	$mr_currentStory = 1;	
	
	$('#main_rotator #rotator_buttons #btn'+$mr_currentStory).attr("class","on");
	$mr_currentStoryTitle = $('#main_rotator #story'+$mr_currentStory+' a').html();
	$mr_currentStoryLink = $('#main_rotator #story'+$mr_currentStory+' a').attr("href");
	$mr_currentStoryTarget = $('#main_rotator #story'+$mr_currentStory+' a').attr("target"); // ep
	$('#main_rotator #story'+$mr_currentStory).css('display','block');
	$('#main_rotator a#rotatorLink').attr("href",$mr_currentStoryLink);
	$('#main_rotator a#rotatorLink').attr("target",$mr_currentStoryTarget); // ep
	$('#main_rotator #rotator_titlebar').html($mr_currentStoryTitle);
		
	if ($mr_storyCount <= 1) {
		$('#rotator_buttons').css('display','none'); //only 1 story, hide the nav buttons
	} else {
		$('#rotator_buttons').css('display','block');
		
		i = 1;
		while (i <= $mr_storyCount) {
			$('#main_rotator #rotator_buttons #btn'+i).css("display","block");			
			i++;
		}
		
		$('#main_rotator #rotator_buttons a#btn1').click( function() { switchStory(1); } );
		$('#main_rotator #rotator_buttons a#btn2').click( function() { switchStory(2); } );
		$('#main_rotator #rotator_buttons a#btn3').click( function() { switchStory(3); } );
		$('#main_rotator #rotator_buttons a#btn4').click( function() { switchStory(4); } );
		$('#main_rotator #rotator_buttons a#btn5').click( function() { switchStory(5); } );
		
		$('#main_rotator #rotator_buttons a#btn1').hover(function(){
			$("#main_rotator img#thmb1").animate({
				left: '8px'	
			}, "normal")
		},function(){
			$("#main_rotator img#thmb1").animate({
				left: '-144px'	
			}, "normal")
		});
		
		$('#main_rotator #rotator_buttons a#btn2').hover(function(){
			$("#main_rotator img#thmb2").animate({
				left: '8px'	
			}, "normal")
		},function(){
			$("#main_rotator img#thmb2").animate({
				left: '-144px'	
			}, "normal")
		});
		
		$('#main_rotator #rotator_buttons a#btn3').hover(function(){
			$("#main_rotator img#thmb3").animate({
				left: '8px'	
			}, "normal")
		},function(){
			$("#main_rotator img#thmb3").animate({
				left: '-144px'	
			}, "normal")
		});
		
		$('#main_rotator #rotator_buttons a#btn4').hover(function(){
			$("#main_rotator img#thmb4").animate({
				left: '8px'	
			}, "normal")
		},function(){
			$("#main_rotator img#thmb4").animate({
				left: '-144px'	
			}, "normal")
		});
		
		$('#main_rotator #rotator_buttons a#btn5').hover(function(){
			$("#main_rotator img#thmb5").animate({
				left: '8px'	
			}, "normal")
		},function(){
			$("#main_rotator img#thmb5").animate({
				left: '-144px'	
			}, "normal")
		});
		
		mainRotatorInterval = window.setInterval(switchStory,5000);
	}
	
	$(window).scroll(function () {
		positionWalkout();
	});
	
	$(window).resize(function () {
		positionWalkout();
	});
	
	window.onload = startWalkout();
	/*window.onload = insertadCurtain();*/
	
	
	
	
});