var currSlide = 1;
var slideTotal = 1;
var slideInterval = null;
var isHTML5Video = true;
var isVideoPlaying = false;
	
function setUpCaseStudies() {
	//set up lightbox settings & activate
	$('.lightbox a').lightBox({
		keyToClose: 'x',
		imageLoading: '_media/lightbox/ajax-loader.gif',
		imageBtnClose: '_media/lightbox/btn_close.gif',
		imageBtnPrev: '_media/lightbox/lightbox-btn-prev.gif',
		imageBtnNext: '_media/lightbox/lightbox-btn-next.gif',
		imageBlank: '_media/lightbox/lightbox-blank.gif'
	});
			
}

/* SLIDE SHOW*/
function showSlideShow() {
	$('#slideshow ul').fadeIn(500);
	$("#media").animate({height: $('#slideshow ul').height()+'px'}, 500, 'easeOutCubic');
}

function changeSlide(type, index) {
	if(type == "fade") {
	    var active = $('#slideshow ul li.active');
	    if (active.length == 0 ) active = $('#slideshow ul li:last');
		var currIndicator = $('#slideshow_controls span.selected');
	    if (currIndicator.length == 0 ) currIndicator = $('#slideshow_controls span:last');
		if (typeof index == "undefined") {
			var next =  active.next().length ? active.next(): $('#slideshow ul li:first');
			var nextIndicator = currIndicator.next().length ? currIndicator.next(): $('#slideshow_controls span:first');
		} else {
			var next =  $('#slideshow ul li:nth-child('+index+')');
			var nextIndicator = $('#slideshow_controls span:nth-child('+(parseInt(index)+1)+')');
		}
	
		active.addClass('last-active');

	    next.css({opacity: 0.0})
	        .addClass('active')
	        .animate({opacity: 1.0}, 1000, function() {
	            active.removeClass('active last-active');
	        });
	
		nextIndicator.addClass("selected");
		currIndicator.removeClass("selected");

	} else {
		if(currSlide < slideTotal) {
			currSlide++;
			$("#slideshow ul").animate({marginLeft: '-=640px'}, 750, 'easeOutCubic');
		} else {
			currSlide = 1;
			$("#slideshow ul").animate({marginLeft: '0px'}, 750, 'easeOutCubic');
		}
	}
}

function pauseSlideShow() {
	$("#btn_play_pause_slideshow").text("Play");
	clearInterval(slideInterval);
}

function playSlideShow() {
	//hide videos
	if(!isVideoPlaying) {
		//hide videos
		hideVideos();
		
		$("#btn_play_pause_slideshow").text("Pause");

		$("#media").animate({height: $('#slideshow ul').height()+'px'}, 500, 'easeOutCubic', function() {
			if(!isVideoPlaying) {
				$('#slideshow ul').fadeIn(500);
				slideInterval = setInterval("changeSlide('fade')", 3000);
			}
		});
	}
}

function startSlideShow() {
	clearInterval(slideInterval);
	//$("#slideshow ul").css({marginLeft: '0px'});
	if(!isVideoPlaying) {
		playSlideShow();
	}
}

function setupSlideShow() {
	
	$("#slideshow ul").addClass("fade_show");
	$('#slideshow ul li:first').addClass("active");
	
	$("#btn_play_pause_slideshow").click(function(event) {
		event.preventDefault();
		if($(this).text() == "Play") {
			//hide all visible videos
			hideVideos();
			isVideoPlaying = false;
			playSlideShow()
		} else {
			pauseSlideShow();
		}

	});	
	
	$("#slideshow_controls span").click(function(event){
		event.preventDefault();
		//hide any videos
		hideVideos();
		
		isVideoPlaying = false;
		showSlideShow();
		
		if(!$(this).hasClass("selected")) {
			pauseSlideShow();
			changeSlide("fade",$(this).text());
		}
	})
	
	
	startSlideShow();
}

/* VIDEO */
function setUpVideo() {
	var video = detectVideoSupport();
	//check for ogg support but NOT mp4
    if(!video.mp4){
		//alert("i am NOT HTML5 and i can't play mp4")
		isHTML5Video = false;
		//load qtObject javascript
		$.getScript('_js/qtobject.js', function() {
			
		});
    }

	//video links
	$("#video_links a").click(function(event) {
		event.preventDefault();
		
		//pause slide show;
		if(slideTotal>1) {
			pauseSlideShow();
		}
		//hide all visible videos
		hideVideos();
		
		//set isVideoPlaying back to true
		isVideoPlaying = true;

		var videoFile = event.target.href;
		//fade out slideshow, after complete, resize viewport (if ncessessary) and show selected video
		$('#slideshow ul').fadeOut(500, function() {
			var id = $(event.target).attr("id").replace("video_link","");

			var video = $("#video"+id);
			// alert(video.attr("id"))
			var videoHeight = parseInt(video.attr("height"));
			var videoWidth = parseInt(video.attr("width"));
			
			//if not HTML5 native video, replace with video tag with quicktime objects
			if(!isHTML5Video) {
				if(video.is("video")) {
					var myQTObject = new QTObject(videoFile, "qt_"+video.attr("id"), videoWidth, videoHeight+15);
					myQTObject.addParam("autostart", "true");
					var newDiv = $('<div id="'+video.attr("id")+'" class="video"></div>').insertBefore(video);
					video.remove();
					newDiv.html(myQTObject.getHTML());
					//http://www.apple.com/quicktime/download/
				} else {
					var videoObj = $("#"+video.attr("id")+" embed");
					videoHeight = parseInt(videoObj.attr("height")) - 15;
					videoWidth = parseInt(videoObj.attr("width"));
					//alert($("#"+video.attr("id")+" embed"))
				}
			}
			if(isHTML5Video) {
				$("#media").animate({height: videoHeight+'px'}, 500, 'easeOutCubic', function() {
					$("#video"+id).fadeIn(500, function() {
						//auto play the native video element
						video.get(0).play();
					});
				}) 
			} else {
				$("#media").animate({height: (videoHeight+15)+'px'}, 500, 'easeOutCubic', function() {
					$("#video"+id).show();
				});
			}
		})			
	});

}

/* http://pipwerks.com/2010/03/19/html5-video-minus-ogg/ */
function detectVideoSupport() {
    var detect = document.createElement('video') || false;
    this.html5 = detect && typeof detect.canPlayType !== "undefined";
    this.mp4 = this.html5 && (detect.canPlayType("video/mp4") === "maybe" || detect.canPlayType("video/mp4") === "probably");
    this.ogg = this.html5 && (detect.canPlayType("video/ogg") === "maybe" || detect.canPlayType("video/ogg") === "probably");
    return this;
};

function hideVideos() {
	isVideoPlaying = false;
	
	//hide all visible videos
	$("#media .video:visible").each(function(idx, item){
		if(isHTML5Video) {
			$(item).get(0).pause();
			$(item).get(0).currentTime = 0;
			$(item).fadeOut(500)
		} else {
			//$(item).get(0).Rewind();
			$(item).hide();
		}
	})
}


$(document).ready(function(){
	//alert("DOM ready");
	slideTotal = $("#slideshow ul li").size();
	setUpVideo();
	setUpCaseStudies();
});

$(window).load(function(){
	// alert("images ready!");
	if(slideTotal>1) {
		setupSlideShow();
	}
})

