//vars	
var bodyWidth;
var slideCount = -1;
var slidesLength = 4;
var interval;
var introArray;
var aSlide;
var pSlide;
var ulArray;
var mobile = false;
var pLi;
var startTouchX;
var endTouchX;
var touchLength;
var swipeDistance;
var touch;
var footerY;



$(document).ready(function(){	

	//Plays Intro
	intro();
	
	mobile = detectMobile();

	//Sets Up Body Resizing Listener
	document.body.onresize = resizeWindow;
	
	//Builds Navigation
	buildNav();
	
	//Sets Up timer
	buildIntro();
	
	

});

function detectMobile(){
	var m;
	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))  || (navigator.userAgent.match(/iPad/i))) {
		m = true;
	}else{
		m = false;
	}
	return m;
}



function intro(){
	var div = document.createElement('div');
	
	//Initial CSS Properties
	var contentHeight = $('#content').height();
	$('#content_shell').css("height", contentHeight + 80);
	
	//Kills visiblity of select DOM elements
	$('#content').hide();
	$('nav').hide();
	$('header > img').hide();
	$('#base').hide();
	$('content_shell').hide();
	$('footer').hide();
	$('#dotNav').hide();
	
	//Transition
	$('#content').delay(1500).fadeIn(2000)
	$('header > img').delay(1500).fadeIn(2000);
	$('nav').delay(1500).fadeIn(2000);
	$('#base').delay(1500).fadeIn(2000);
	$('footer').delay(1500).fadeIn(2000);
	$('header > div').delay(1500).fadeOut(2000);
	$('#dotNav').delay(5000).fadeIn(2000);
	$('#intro').delay(5000).fadeOut(2000, function(){
		advance();
		})
	
	ulArray = $('#dotNav').children();
	
}



function buildNav(){
	aSlide = $('#intro');
	ulArray = $('#dotNav').children();
	for(var x=0; x<ulArray.length; x++){
	
		$(ulArray[x]).click(function(event) {
			 var currIndex = $(this).index();
		  	 setSlide(currIndex);
		});
		
		$(ulArray[x]).hover(function(event){
			if(slideCount!=$(this).index()){
				$(this).css('cursor', 'pointer');
			}
		});
		
		$(ulArray[x]).mouseover(function(event){
			if(slideCount!=$(this).index() && detectMobile()==false){
				$(this).toggleClass('dotHover');
			}
		});
		
		$(ulArray[x]).mouseout(function(event){
			if($(this).hasClass('dotHover') && detectMobile()==false){
				$(this).toggleClass('dotHover');
			};
		});
	}
}


//Handles Window Resizing Event
function resizeWindow(){
		bodyWidth = $('body').width();
		
		//bodyWidth = 100;
		
		$('#container > article').css("width", bodyWidth);
		$('#featured').css("width", bodyWidth);
		$('header > div').css("width", bodyWidth);
		
		//Loops through all slides and resizes each div according to the <body> width;
		for(var x=0; x<introArray.length; x++){		
			var currDiv = introArray[x];
			if(x>0){
		}else{
			(currDiv).css("width", bodyWidth);
		}
	}
}


function buildIntro(){
	introArray = new Array($('#promo1'), $('#promo2'), $('#promo3') );
	
	//Resizes Window
	resizeWindow();
	
	//Sets initial PrevSlide
	pSlide = $('#intro');
	
	$('#intro').css("height", 329);
	
		$('#featured').css("position", "absolute");
		$('#featured').css("left", 0);
		$('#featured').css("top", 77);
		$('#featured').css("height", 329);
		
		$('#container').css("position", "absolute");
		$('#container').css("left", 0);
		$('#container').css("top", 0);
		
		$('#base').css("position", "absolute");
		$('#base').css("top", 405);
		$('#base').css("left", 0);
		
		$('#content').css("position", "absolute");
		$('#content').css("top", "447px");
		$('#content').css("left", 0);
		
		footerY = 530 + $('#content').height();

		$('footer').css("position", "absolute");
		$('footer').css("left", 0);
		$('footer').css("top", footerY);
		
	
	//Builds Out Touch Screen Version (Mobile)
	if(detectMobile()){
		
		
		//Initial CSS Properties
	
		$('#content_shell').css("height", footerY);

		
		//$(currSlide).css("width", bodyWidth);
			
			$('#intro').css("z-index", "10");
			$('#intro').css("position", "absolute");
			$('#intro').css("left", 0);
			$('#intro').css("float", "left");
			$('#intro').css("height", "329px");
			$('#intro').css("top", "0");
		
		for(var x=0; x<introArray.length; x++){
			var currSlide = introArray[x];
			var currX = bodyWidth * x;
			$(currSlide).css("width", bodyWidth);
			$(currSlide).css("z-index", "0");
			$(currSlide).css("position", "absolute");
			$(currSlide).css("left", currX);
			$(currSlide).css("float", "left");
			$(currSlide).css("height", "329px");
			$(currSlide).css("top", "0");
			//Previous Slide Transition Out
			
		}
		
		$(pSlide).delay(5000).fadeOut(1000, function(){
			stopInterval();
			slideCount+=1;
			updateNav(slideCount);
			startInterval();
			
			//Sets Appropriate Touch Screen Listeners
			setTouchListeners();
			setOrientationListeners();
	
		});
	
	
	//Builds Out PC Version	
	}else{
		
		$(currSlide).css("width", bodyWidth);
			
			$('#intro').css("z-index", "10");
			$('#intro').css("position", "absolute");
			$('#intro').css("left", 0);
			$('#intro').css("float", "left");
			$('#intro').css("height", "329px");
			$('#intro').css("top", "0");
		
		for(var x=0; x<introArray.length; x++){
			var currSlide = introArray[x];
			var currX = 0;
			$(currSlide).css("width", bodyWidth);
			$(currSlide).css("z-index", "0");
			$(currSlide).css("position", "absolute");
			$(currSlide).css("left", currX);
			$(currSlide).css("float", "left");
			$(currSlide).css("height", "329px");
			$(currSlide).css("top", "0");
		}
	
		
		$('#promo3').hide();
		$('#promo2').hide();
		$('#promo1').hide();
		pSlide = $('#intro');
		
		//Initial CSS Properties
		var contentHeight = $('#content').height();
		$('#content_shell').css("height", contentHeight + 500);
		
		console.log(contentHeight+' content height');
		//startInterval();
	}
}



function startInterval(){
	interval = setInterval('advance()', 10000);
}

function stopInterval(){
	clearInterval(interval);
}




function setTouchListeners(){
	
		resizeWindow();
		
		var newX;
		var featured = document.getElementById('featured');
		$('#container').css('position', 'absolute');
		$('#container').css('left', 0);

		//==== Touch Start
		(featured).addEventListener('touchstart', function(event) {
			event.preventDefault();
			if(event.touches.length==1){
					
				distance=0;
				endTouchX=0;
				startTouchX=0;
				
				touch = event.touches[0];
				startTouchX = touch.pageX;
				stopInterval();
			
			}
		}, false);
		
		
		//===== Touch Move
		(featured).addEventListener('touchmove', function(event) {
			event.preventDefault();
			//Var Set
			endTouchX = event.touches[0].pageX;
			distance = endTouchX - startTouchX;
			//Slides Container
			var tempX = distance +( bodyWidth*slideCount*-1)
			$('#container').css('left', tempX);
		}, false);
		
		
		//===== Touch End
		(featured).addEventListener('touchend', function(event) {
			//var nextSlide;
			var killSlide;
			
			// From Left to Right (Backwards) ---------> 			
			if(distance>0){		
				slideCount-=1;
				if(slideCount<0){slideCount=0};

			// <--------- From Right to Left (Forward)
			}else if(distance<0){
				slideCount+=1;
				if(slideCount==introArray.length){slideCount=introArray.length-1};	
			}
			
			//Actual Animation	
			newX = bodyWidth*slideCount*-1;
			$('#container').animate({left:newX}, 500, function(){});
			
			updateNav(slideCount);
			

		}, false);	
}


function setOrientationListeners(){
	//window.addEventListener('devicemotion', function(event) {
		//alert(event.accelerationIncludingGravity.x +' X '+event.accelerationIncludingGravity.y+' Y '+event.accelerationIncludingGravity.z+' Z');
	//}, false);	
}//



//Advances Slide Show
function advance(){
	stopInterval();
	//Slide Counter
	slideCount+=1;
	if(slideCount==introArray.length){slideCount=0;};
	setSlide(slideCount);
}




function setSlide(n){
	
	stopInterval();

	slideCount=n;
	
	updateNav(slideCount);
	
	//Sets Active Slide
	aSlide = introArray[slideCount];

	if(detectMobile()){

		//Actual Animation	
		var newX = bodyWidth*slideCount*-1;
		$('#container').css('position', 'absolute');
		//$('#container').css('left', 0);
		$('#container').animate({left:newX}, 500, startInterval);
		
	}else{
		
		$(aSlide).css("width", bodyWidth);
		$(aSlide).css("left", 0);
		$(aSlide).css("position", "absolute");
		
		//Active Slide Transition In
		$(aSlide).fadeIn(1000);
		
		//Previous Slide Transition Out
		$(pSlide).fadeOut(1000, startInterval);
	}

	//Pigg-back Previous Slide for next rotation
	pSlide = aSlide;
}




function updateNav(n){
	for(var x=0; x<introArray.length; x++){
		var currLi = ulArray[x];
		
		if($(currLi).index() == n){
			$(currLi).addClass('dotActive');
		}else{
			if($(currLi).hasClass('dotActive')){
				$(currLi).removeClass('dotActive');
				$(currLi).addClass('dotInactive');
			}
		}
	}
}


function killSlideShow(){
	clearInterval(interval);
}


function startSlideShow(){
	//Sets Interval
	interval = setInterval('advance()', 8000);
	
}

/*------------------------
* Updates Layout
*-------------------------
*/

function updateLayout() {
  if (window.innerWidth != currentWidth) {
    currentWidth = window.innerWidth;
    var orient = (currentWidth == 320) ? "profile" : "landscape";
    document.body.setAttribute("orient", orient);
    window.scrollTo(0, 1);
  }
};


