var hovering = false;
var initial_timeout_int = 3000;
//var touch_timeout_int = 4000;
//var ss_timeout_int = 5000;
var animating = false;
var timer_int;
var banner_lockout = true;


//timer_int = setInterval('SlideShow.nextImg()', ss_timeout_int)


SlideInterval = {
	ss_timeout_int : 3000,
	int : 'h_timer',
	countdown : false,
	init_slide_int : 'init_slide_timer',
	
	start: function(){
		
		
		
		if(!SlideInterval.countdown){
			SlideInterval.end()
			SlideInterval.countdown = true
			//console.log('startssasdf countdown')
			window.clearInterval(SlideInterval.int);
			SlideInterval.int = setInterval('SlideShow.nextImg()', SlideInterval.ss_timeout_int)
		}
	},
	
	end : function(){
		SlideInterval.countdown = false
		//console.log('clearasdf countdown')
		window.clearInterval(SlideInterval.int);
		//SlideShow.playPause("pause")
		
	},
	
	showDesc : function(){
		SlideInterval.endIntialSlide()
		SlideInterval.end()
		slideDetails(0)	
		banner_lockout = true
		SlideInterval.startIntialSlide()
		//setTimeout('initialSlide()', initial_timeout_int);
		
	},
	startIntialSlide : function(){
		SlideInterval.endIntialSlide()
		SlideInterval.init_slide_int = setTimeout('initialSlide()', initial_timeout_int);
		
	},
	
	endIntialSlide : function(){
		//alert('kill int')
		window.clearInterval(SlideInterval.init_slide_int);
	}
	
	
	
}





function slideDetails(pos){
	animating = true;
	// this is to solve an issue on touch devices where the .stop was affecting the slideshow itself and stopping the animation all together.
	//console.log(banner_lockout + "if false, will run")
	
	if(!banner_lockout){
	
		if(is_touch_device()){
			$("#details").animate({  bottom: pos }, 320, function(){
				animating = false;										 												 
			})
		} else {
			$("#details").stop().animate({  bottom: pos }, 320, function(){
				animating = false;
			})
					
		}
		
		// upon close, set the timer for slideshow
		if(pos != 0){
			SlideInterval.start()
		} else {
			SlideInterval.end()
		} 
	
	}
	
	
}

function initialSlide (){
	banner_lockout = false;
	if (! hovering) {
		slideDetails(-75)
	}
}



function is_touch_device() {  
  try {  
    document.createEvent("TouchEvent");  
    	return true;  
  	} catch (e) {  
  		return false; 
	}  
}




$(document).ready(function(){
						   
						   



var  wrapper = $("#home_banner_wrap");
	var offset = wrapper.offset();
	var x1 = 0;
	var x2 = 780;
	var y1 = 245;
	var y2 = 325;
						   
	
	
	
	// define coordinates
	
	
	 
						   
						   
	$('#message').click(function(e){
		e.preventDefault();
		$('#home_banner_wrap').fadeOut('fast', function(){
			$('#home_banner').html('<div id="ss_container">\
											<div id="ss_loading"> </div>\
											<div id="ss_nav">\
												<div id="details">\
													<a class="prev" href="#"></a>\
													<div id="desc"></div>\
													<a class="next" href="#"></a>\
													</div></div>\
											<div id="ss_1"></div>\
											<div id="ss_2"></div>\
										</div>');
				// start the slideshow
				SlideShow.init(slides, 'home');		
							
				// description slide functions	
				
				// initial show of info
				// timeout and then hide
				
				setTimeout('initialSlide()', initial_timeout_int);
				
				
				
				// track mouse
				$(document).mousemove(function(e){
					//console.log(parseInt(e.pageX - offset.left) +', '+ parseInt(e.pageY - offset.top));
					var m_x = parseInt(e.pageX - offset.left);
					var m_y = parseInt(e.pageY - offset.top);
					  
					  
					  if(m_x > x1 && m_x < x2 && m_y > y1 && m_y < y2){
						 
						 
						 //console.log('spot')
							  if(! hovering){
								slideDetails(0)
								hovering = true;
							  }
							  
							} else {
								
								if(hovering){
									slideDetails(-75)
									hovering = false;
								}
					  }
					  
				   }); 
	
	
	
// if iphone, ipad, touch device

	if(is_touch_device()){
		
		// see if you are shaking the device
		if (typeof window.DeviceMotionEvent != 'undefined') {
    		// Shake sensitivity (a lower number is more)
			var sensitivity = 12;
	
			// Position variables
			var tx1 = 0, ty1 = 0, tz1 = 0, tx2 = 0, ty2 = 0, tz2 = 0;
	
			// Listen to motion events and update the position
			window.addEventListener('devicemotion', function (e) {
				tx1 = e.accelerationIncludingGravity.x;
				ty1 = e.accelerationIncludingGravity.y;
				tz1 = e.accelerationIncludingGravity.z;
		}, false);

		// Periodically check the position and fire
   		 // if the change is greater than the sensitivity
    	setInterval(function () {
        	var change = Math.abs(tx1-tx2+ty1-ty2+tz1-tz2);
			if (change > sensitivity) {
            	//alert('Shake!');
				if (! animating){
					slideDetails(0)	
					hovering = false;
					//setTimeout('initialSlide()', initial_timeout_int);
					SlideInterval.startIntialSlide()
				}
			
        	}

			// Update new position
			tx2 = tx1;
			ty2 = ty1;
			tz2 = tz1;
		}, 150);
	} // end shake easter egg
 

		
		
		$('#ss_container').click(function(e){
			SlideInterval.end()
			e.preventDefault()
			slideDetails(0)	
			//alert('click')
			hovering = false;
			//setTimeout('initialSlide()', initial_timeout_int);
			SlideInterval.startIntialSlide()
		})
		
	}




	
	
	
				
				//$("#details_hov").mouseenter(function(){
					//hovering = true;
					//slideDetails(0)
			//	})
					
				//$("#details_hov").mouseleave(function(){
					//hovering = false;
					//slideDetails(-75)
				//})
				
				/*$("#ss_container").mouseenter(function(){
					$("#details").stop().animate({ 
					bottom: 0
					}, 320)
				})
					
				$("#ss_container").mouseleave(function(){
					$("#details").stop().animate({ 
					bottom: -80
				}, 320)
			})		*/
		})
	});
	
	NewsTicker.init(slides)
	
});



NewsTicker = {
		fade_speed : 400, // speed to crossfade images
		display_delay : 3800, // display duration
		content_area_id : 'news_item', 
		newsObj : '',
		amount : '',
		current_item : 0,
		timer_int : '',
		
		
	getNews : function(){
		
		var caption_string = NewsTicker.newsObj[NewsTicker.current_item].caption;
		//$('body').prepend('<div id="ticker_hold"></div>')
		$("#ticker_hold").html(NewsTicker.newsObj[NewsTicker.current_item].caption)
		var client_name = $("#ticker_hold p:eq(0)").html();
		var headline = $("#ticker_hold a").html();
		var link_url = $("#ticker_hold a").attr('href')
				
		var news_string = '<a href="' + link_url + '">' + client_name + ' : ' + headline + '&nbsp;<span class="more">MORE&nbsp;></span></a>'
		return(news_string)
	},
	
	init : function(news) {
		NewsTicker.newsObj = news; 
		NewsTicker.amount = NewsTicker.newsObj.length;
		//NewsTicker.fade_speed = news_fade; 
		//NewsTicker.display_delay = news_delay;
		NewsTicker.getNews();
		//for (i in $(caption_string)){
			//console.log("i= " + i + " " + $(caption_string)[i] )
		//}
		$('#' + NewsTicker.content_area_id).html(NewsTicker.getNews());
		//$('#' + NewsTicker.content_area_id + ' a').append(' <span class="more">MORE ></span>');
		NewsTicker.setTimer();
	},
		
		changeNews : function() {
			
		NewsTicker.current_item ++ ; // advance
		if(NewsTicker.current_item  == NewsTicker.amount){
			NewsTicker.current_item = 0;
		}
	
		$('#' + NewsTicker.content_area_id).fadeOut(NewsTicker.fade_speed , function(){
			//var caption_string = NewsTicker.newsObj[NewsTicker.current_item].caption;
			$('#' + NewsTicker.content_area_id).html(NewsTicker.getNews());
				//$('#' + NewsTicker.content_area_id + ' a').append(' <span class="more">MORE ></span>');
				$('#' + NewsTicker.content_area_id).fadeIn(NewsTicker.fade_speed , function(){
					NewsTicker.setTimer();							 
					})
															  
			});
	},
	
	setTimer : function() {
		NewsTicker.timer_int = setTimeout('NewsTicker.changeNews()', NewsTicker.display_delay)
		
	}
		
}
