/**
 * Script for Owner Stories / Camry Effect Section
 *
 * @author Michael Boucher
 */

(function() {
	
	document.observe( "dom:loaded" , function() {
		
		// SOCIAL MEDIA ICON HANDLERS
		$("storiesTwitter").observe("click" , function( evt ) {
			
			window.open("http://twitter.com/home?status="+escape("Be part of the Camry Effect. http://toyota.com/camryeffect #CamryEffect"));
			
		});
		
		$("storiesFacebook").observe("click" , function( evt ) {
			
			window.open("https://www.facebook.com/toyota");
			
		});
		
		
		
		
		
		
		
		
		var storiesData = 	$A([	{ id: 1
								, authorName: "Thelma R."
								, authorVehicle: "2006 Camry"
								, authorGender: 'female'
								, story: "I love this car, it handles great. I have 126 thousand miles on it and plan to drive it another 100k."
								, rating: 4
								, collage: "cameffect_collage_01.jpg"
								, disclaimer: '<a href="javascript:;" onclick="TMSSite.disclaimerHotlink(\'/disclaimers/jse_10_veh_life.html\');">[2]</a>'
								, photo: '/img/vehicles/2012/camry/landing/cameffect_story_photo01.jpg'}
								
							,	{ id: 2
								, authorName: "Dave B."
								, authorVehicle: "2007 Camry"
								, authorGender: 'male'
								, story: "This July we logged a 3,190 mile road trip and only used 86 gallons of gas."
								, rating: 5
								, collage: "cameffect_collage_02.jpg"
								, disclaimer: ''
								, photo: '/img/vehicles/2012/camry/landing/cameffect_story_photo02.jpg'}
								
							,	{ id: 3
								, authorName: "Kory K."
								, authorVehicle: "2007 Camry"
								, authorGender: 'male'
								, story: "I drove my Camry to church on my wedding day."
								, rating: 3
								, collage: "cameffect_collage_03.jpg"
								, disclaimer: ''
								, photo: '/img/vehicles/2012/camry/landing/cameffect_story_photo03.jpg'}
								
							,	{ id: 4
								, authorName: "Polly J."
								, authorVehicle: "2002 Camry"
								, authorGender: 'female'
								, story: "I've had a couple of other cars, but have COME BACK to my first love, Camry all the way!"
								, rating: 4
								, collage: "cameffect_collage_04.jpg"
								, disclaimer: ''
								, photo: '/img/vehicles/2012/camry/landing/cameffect_story_photo04.jpg'}
						
		
		]); // END STORY DATA
		
		var swapTimer;
		var swapInterval	= 4;
		
		var currentStory	= 1;
		var numStories		= storiesData.length;
		
		var storySection	= $$("#ownerStories")[0];
		var authorAvatar	= storySection.down(".authorAvatar");
		var authorName		= storySection.down(".authorName");
		var authorVehicle	= storySection.down(".authorVehicle");
		var storyText		= storySection.down(".storyText");
		var storyCollage	= storySection.down(".storyContent");
		var storyPhoto		= document.getElementById('storyPhoto');
		
		var authorRating	= storySection.down(".storyRating");
		var stars			= authorRating.childElements();
		
		var pageNav			= storySection.down(".storyTextWindow ul").childElements();
		pageNav.each( function( dot ) {
			
			dot.observe('click' , function() {
				var id = this.id.split("-")[1] * 1;
				
				// Reset Timer so story doesn't switch immediately after user click
				swapTimer.stop();
				setSwapTimer();
				switchStory(id);
			});
			
		});
		
		switchStory( 1 );
		setSwapTimer();
		
		function switchStory( id ) {
			
			var newStory = storiesData.detect( function( n ) {
				return n.id == id;
			} );
			
			currentStory = id;
			
			// Page Dot
			setPageNav(currentStory);
			
			// AVATAR
			authorAvatar.removeClassName("male");
			authorAvatar.removeClassName("female");
			authorAvatar.addClassName( newStory.authorGender );
			
			// AuthorName / Model / Story Text
			authorName.innerHTML	= newStory.authorName.toUpperCase();
			authorVehicle.innerHTML = newStory.authorVehicle.toUpperCase(); 
			storyText.innerHTML		= "&quot;"+newStory.story+"&quot; " + newStory.disclaimer;
			storyPhoto.innerHTML	= "<img src='" + newStory.photo + "'>";
			
			// Rating
			setRating(newStory.rating);
			
			
		}
		
		
		function setRating( rating ) {
			resetRating();
			
			for( var i = 0; i < rating; i++ )
			{
				stars[i].addClassName("fullStar");
			}
			
		}
		
		function resetRating() {
			
			stars.each( function( star ) {
				star.removeClassName("fullStar");
				star.removeClassName("halfStar");
			});
		}
		
		
		function setPageNav( page ) {
			resetPageNav();
			pageNav[page-1].addClassName("pageActive");
		}
		
		
		function resetPageNav() {
			pageNav.each( function( dot ) {
				dot.removeClassName("pageActive");
			});
		}
		
		
		function setSwapTimer() {
			swapTimer = new PeriodicalExecuter( function( pe ) {
				
				currentStory = ( currentStory == numStories ) ? 1 : ++currentStory;
				
				switchStory( currentStory );
				
			} , swapInterval );
		}
		
	});
	
	
})();

