var numSlides = 5;

var prevNone = false;
$(document).ready(function() {		
	//Execute the slideShow, set 4 seconds for each images
	slideShow(6000);
	activateIndicatorClickability();
	loadContent('LandingPage/pages/main.html',0);
});

function activateIndicatorClickability() {
	for ( var i = 1; i <= numSlides; i++ ) {
		setIndicatorClick(i);
	}
}

function setIndicatorClick(i) {
	$('#image'+i).click(function () {
		while ( counter != i ) {
			gallery();
		}
	});
}

function slideShow(speed) {
	//append a LI item to the ol list for displaying caption
	//$('ol.slideshow').append('<li id="slideshow-caption" class="caption"><div class="slideshow-caption-container"><h3></h3><p></p></div></li>');

	//Set the opacity of all images to 0
	$('ol.slideshow li').children('.content').css({opacity: 0.0});
	
	//Get the first image and display it (set it to full opacity)
	$('ol.slideshow li:first').children('.content').css({opacity: 1.0});
	
	//Get the caption of the first image from REL attribute and display it
	//$('#slideshow-caption h3').html($('ol.slideshow li.show').attr('title'));
	//$('#slideshow-caption p').html($('ol.slideshow li.show').attr('caption'));
		
	//Display the caption
	$('.caption').css({opacity: 0.7});
	
	//Call the gallery function to run the slideshow	
	var timer = setInterval('gallery()',speed);
	
	//pause the slideshow on mouse over
	$('ol.slideshow').hover(
		function () {
			clearInterval(timer);	
		}, 	
		function () {
			timer = setInterval('gallery()',speed);			
		}
	);
	$('#indicators').hover(
		function () {
			clearInterval(timer);	
		}, 	
		function () {
			timer = setInterval('gallery()',speed);			
		}
	);
	
}

var counter = 1;
function gallery() {
	document.getElementById('image' + counter).src = 'LandingPage/images/next_image_bubble.png';
	var tempAlt = document.getElementById('image' + counter).alt;
	tempAlt = tempAlt.split('-');
	document.getElementById('image' + counter).alt = 'disabled-' + tempAlt[tempAlt.length-1];
	
	counter = counter+1;
	if(counter > numSlides) {
		counter = 1;
	}
	document.getElementById('image' + counter).src = 'LandingPage/images/next_image_bubble_full.png';
	var tempAlt = document.getElementById('image' + counter).alt;
	tempAlt = tempAlt.split('-');
	document.getElementById('image' + counter).alt = 'active-' + tempAlt[tempAlt.length-1];
	
	//if no IMGs have the show class, grab the first image
	var current = ($('ol.slideshow li.show')?  $('ol.slideshow li.show') : $('#ol.slideshow li:first'));

	//Get next image, if it reached the end of the slideshow, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().attr('id') == 'slideshow-caption')? $('ol.slideshow li:first') :current.next()) : $('ol.slideshow li:first'));
	
	//Get next image caption
	//var title = next.attr('title');	
	//var desc = next.attr('caption');	

	//Set the fade in effect for the next image, show class has higher z-index
	//next.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 1000);
	next.children('.content').css({opacity: 0.0});
	next.addClass('show');
	next.children('.content').animate({opacity: 1.0}, 1000);
	
	/*
	if((title == '') && (desc == '') && prevNone == false) {
		prevNone = true;
		$('#slideshow-caption').slideToggle(300);
		//alert('remove');
	} else if((title != '') && (desc != '') && (prevNone == true)) {
		//alert('remove adding');
			prevNone = false;
		$('#slideshow-caption h3').html(title); 
			$('#slideshow-caption p').html(desc); 
			$('#slideshow-caption').slideToggle(500); 
	} else if((title == '') && (desc == '') && prevNone == true) { } else {
			prevNone = false;
		//Hide the caption first, and then set and display the caption
		
		//alert('toggle');
		//$('#slideshow-caption').slideToggle(300, function () { 
			$('#slideshow-caption h3').html(title); 
			$('#slideshow-caption p').html(desc); 
			$('#slideshow-caption').slideToggle(500); 
		//});		
	 }
	 */
	//Hide the current image
	//current.animate({opacity: 0.0}, 1000).removeClass('show');
	current.children('.content').animate({opacity: 0.0}, 1000);
	current.removeClass('show');
}

startList = function() {
	if (document.all&&document.getElementById) {
		navRoot = document.getElementById("menu");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
				}
			}
		}
 	}
}
window.onload=startList;

function launchWindow2(src) {
	launchPopup('800','560', src);
}
function launchPopup(w,h,src) {
	var width = w;
	var height = h;
	var left = (screen.availWidth-width-10)/2;
	var top = (screen.availHeight-height-36)/2;
	var strOpenOptions = 'scrollbars=yes,resizable=yes,width=' + width + ',height=' + height + ',left=' + left + ',top=' + top
	newWindow = window.open(src, 'newWindow', strOpenOptions);
	window.opener = window.top;
}

var lastPage = 0;
function loadContent(desiredPage, pageNum){
	if ( pageNum != 0 )
		$('#menu'+pageNum).addClass("current");
	if ( lastPage != 0 )
		$('#menu'+lastPage).removeClass("current");
	lastPage = pageNum;
	
    $.ajax({
        type: "GET",
		url: desiredPage,
        dataType: "html",
        success: function(data){
            $('#right').html(data);
		},
        error: function(){
            alert('The content you requested could not be found.');
        }
    });
}
