function getMainPage(){
	var uriArray = location.pathname.split('/');
	if(uriArray[uriArray.length-1] == '' || uriArray[uriArray.length-1] == 'index.html'){
		if(uriArray[uriArray.length - 2] == '') return 'homepage';
		else return uriArray[uriArray.length - 2];
	} else {
		return null;
	}
}

function setupNavDropDown(){
	var currentDropDown = null;
	
	$$('div#nav a.link').addEvents({
		'mouseover': function(){
			if(currentDropDown) currentDropDown.setStyle('display', 'none');
			currentDropDown = $(this.get('data-subNav')+'-subNav').setStyle('display', 'block');
		},
		'focus': function(){
			this.fireEvent('mouseover');
		}
	});
	
	$('nav').addEvent('mouseleave', function(){
		if(currentDropDown) currentDropDown.setStyle('display', 'none');
	});
}

function setupIAmACalloutCarousel(iAmASlideSelect, iAmAPeople, iAmAContainer){
	var slideCount = iAmASlideSelect.length,
		currentSlide = 0;
		
	var timer = setInterval(function(){
		currentSlide++;
		if(currentSlide >= slideCount){
			clearInterval(timer);
			setSlideTo(0);
		} else {
			setSlideTo(currentSlide);
		}
	}, 3000);
	
	$$('span.slideSelect').addEvent('click', function(){
		clearInterval(timer);
		setSlideTo(this.get('data-slide'));
	});
	
	$$('div.iAmA-container a').addEvent('focus', function(){
		clearInterval(timer);
		setSlideTo(this.get('data-slide'));
	});
	
	function setSlideTo(number){
		iAmASlideSelect.removeClass('active');
		iAmASlideSelect[number].addClass('active');
		iAmAContainer.removeClass('highZIndex');
		iAmAContainer[number].addClass('highZIndex');
		if(Browser.ie6 || Browser.ie7 || Browser.ie8){
			iAmAPeople.setStyle('opacity', 0); 
			iAmAContainer.setStyle('opacity', 0);
			$$(iAmAPeople[number], iAmAContainer[number]).setStyle('opacity',1);
		} else {
			iAmAPeople.tween('opacity', 0);
			iAmAContainer.tween('opacity', 0);
			$$(iAmAPeople[number], iAmAContainer[number]).tween('opacity',1);
		}
	}
	
	setSlideTo(currentSlide);
}


window.addEvent('domready', function(){

	var iAmASlideSelect = $$('span.slideSelect'),
		iAmAPeople = $$('img.iAmA'),
		iAmAContainer = $$('div.iAmA-container');
	
	HeroSlider.loadManifest({
		onMouseEnter: function(){
			HeroSlider.fadeControls('in');
			HeroSlider.stopSlideShow();
		},
		onMouseLeave: function(){
			HeroSlider.fadeControls('out');
			HeroSlider.startSlideShow();
		},
		onNextSlide: function(){
			//console.log('next slide')
		},
		onPreviousSlide: function(){
			//console.log('previous slide')
		},
		onSlideTo: function(){
			//console.log('slide to')
		},
		onStartShow: function(){
			//console.log('start show')
		},
		onStopShow: function(){
			//console.log('stop show')
		}
	});
	
	setupNavDropDown();
	setupDownloadViewLinks();
	if(iAmASlideSelect.length) setupIAmACalloutCarousel(iAmASlideSelect, iAmAPeople, iAmAContainer);
	
	$$('div.downloadButton span.download').addEvent('mouseover', function(){
		this.getParent().getPrevious().fade('in')
	});
	$$('div.downloadButton').addEvent('mouseover', function(){
		this.getPrevious().fade('in')
	});
	$$('.download').addEvent('mouseleave', function(){
	var elem = this.getElement('.downloadLinks');
		if(elem) elem.fade('out');
	});
	
	/* /content/get-the-facts/state-laws.html */
	
	var scroller = new Fx.Scroll(window,{
		offset: {
			x: 0,
			y:-90
		}
	});
	var scroller2 = new Fx.Scroll(window,{
		offset: {
			x: 0,
			y:-120
		}
	});
	$$('a[href=#top]').addEvent('click', function(){
		scroller.toTop();
	});
	
	var stateLawList = $$('.state-laws-list-container');
	if(stateLawList){
	
		stateLawList.addEvent('click:relay(a)', function(e){
			var elemName = this.get('href').replace('#','');
			scroller.toElement(document.body.getElement('a[name='+elemName+']'));
		});
	}
	
	$$('.downloadButton2').addEvent('click', function(){
		//location.href = 'http://www.youtube.com/watch?v=' + this.get('data-video');
		_playVidja(this.get('data-video'))
	});
	if(window.swfobject){
		swfobject.embedSWF("http://www.youtube.com/e/vjmlv1rbGKE?enablejsapi=1&playerapiid=ytplayer",
	           "ytapiplayer", "775px", "485px", "8", null, null, params, atts);
	   	$('closeVideo').addEvent('click', function(e){
	   		e.stop();
	   		if(ytPlayer) ytPlayer.stopVideo();
	   		$('videoBox').removeClass('activate');
	   		mask.hide();
	   	});
   	}
   	
   	$$('.downloadLink').addEvent('click', function(){
   		var name = $('videoBoxInfo').getElement('h3').get('text');
   		name = name.split(',')[0].replace(' ','');
   		location.href = '/download/Faces_Videos/FacesOfDD_' + name + '_OC.mp4.zip';
   	});

});

function setupDownloadViewLinks(){
	var spin = new Spinner(null, {'hideOnClick':true,'id':'download-view'});
	$(spin).setStyles({'background-color': '#000','z-index':9006});
	var img = $(spin).getElement('.spinner-img')
	img.setStyles({
		'background': 'orange',
		'width':'600px',
		'height':'600px',
		'opacity': 1,
		'position': 'absolute'
	});
	$$('div.downloadButton span.view').addEvents({
		'click': function(){
			spin.show();
			img.position({position:'center'});
			img.setStyle('background-image', 'url(' + this.get('data-image') + ')');
		}
	});
}

function getLaws(){
	var stateLaws = $$('.state-law').map(function(elem){
		var state = {};
		state.laws = [];
			
		state.name = elem.getElement('h3').get('text');
		elem.getElements('li').each(function(li){
			state.laws.push(li.get('text'));
		});
		state.note = elem.getElement('p').get('text');
		return state;
	});
}


//test codeBase
var _videoID;
var videoInfoTemplate = '<h3><span class="highlightText">{firstName}</span> {lastName}{Age}</h3>'
						+'<p class="highlightText heavyFont">{Description}</p>';
var hashTemplate = '{Name} | Faces of Distracted Driving | Texting and Driving';						
function videoModal(data){
		var data = JSON.decode(data),
			name = data.Name.split(' ');
		data.firstName = name[0];
		data.lastName = name[1];
		data.Age = (data.Age != '') 
			? ', ' + data.Age : '';
		
	$('videoBoxInfo').set('html', videoInfoTemplate.substitute(data));
	document.title =  hashTemplate.substitute(data);
	_playVidja(data.Path);
}


var params = {allowScriptAccess: 'always', wmode: 'opaque', bgcolor:'#000'}
	, atts = {id: 'ytplayer'}
	, ytPlayer = null;

function onYouTubePlayerReady(id) {
	ytPlayer = document.getElementById(id);
	//ytPlayer.playVideo();
}
var mask = null;
window._playVidja = function(id){
	if(!mask) mask = new Mask('body');
	$('videoBox').addClass('activate');
	mask.show();
	if(ytPlayer) ytPlayer.loadVideoById(id);
};
