// JavaScript Document
function startSlideShow() {
	
	if(counter == 0) {counter = 1;}
	
	switchBlurb(counter);			
	switchSlide(counter);
	switchActive(counter);
	counter++;	
	if(counter > 4) {counter = 1;}
	
	
}
		
		

function switchSlide(id) {
	
	//var $active = document.getElementById('v'+id);
	var $active = $('#slideshow IMG.active');	
	
	var $current = $('#slideshow IMG#v'+id);	
	
	
	$active.addClass('last-active');	
	$current.css({opacity: 0.0})
	.addClass('active')
	.animate({opacity: 1.0}, 1000, function() {
	$active.removeClass('active last-active');						
			
	});
}

function switchActive(id) {	
	var maxNum = 4; 		
	var activeNum = document.getElementById('a'+id);			
	/*Find out id of previously selected number*/	
	
	for(x=1;x < 5;x++) {
		var previousNum = document.getElementById('a'+x);
		previousNum.className = '';	
	}
	
	
	activeNum.className='active';	
	switchBlurb(id-1);
	
}



function switchBlurb(id) {
$('#slide_blurb').html(blurb[id]);
}

/*Loads the page specified*/
function loadPage(page) {
 window.location = page;
}
/*Pauses the animation on mouseover*/
function clearSlideInterval() {
	
	counter = parseInt(id);
	clearInterval(mainInterval);
}
/*Starts the animation on mouseout*/
function startSlideInterval() {	
	mainInterval = setInterval( "startSlideShow()", 4000 );
}	


function pauseSlideInterval() {
	clearInterval(mainInterval);
}


function unpauseSlideInterval(id) {
	//setTimeout("switchSelected(id)",4000);
	if(parseInt(id) == 0) {counter = 1;}
	if(parseInt(id) >= 1) {counter = 2;}
	if(parseInt(id) >= 2) {counter = 3;}
	if(parseInt(id) >= 3) {counter = 4;}
	mainInterval = setInterval( "startSlideShow()", 4000 );


}

function switchSelected(id) {
	
	/*set counter as id. if counter */
	counter = parseInt(id);


    clearSlideInterval();	
	/*pause animation*/
	switchActive(id);
	//switchBlurb(id);
	switchSlide(id);

	if(parseInt(id) == 0) {counter = 1;}
	if(parseInt(id) >= 1) {counter = 2;}
	if(parseInt(id) >= 2) {counter = 3;}
	if(parseInt(id) >= 3) {counter = 4;}
	
	
	
	mainInterval = setInterval( "startSlideShow()", 4000 );
}


/*Global variables*/
var counter = 2;
var mainInterval = setInterval( "startSlideShow()", 8000 );		
var blurb = [];		

// Updates Tabs
$(document).ready(function(){

    $("#tabs").tabs();
	//{ fx: { opacity: 'toggle' } }
	
	var randomnumber = Math.floor(Math.random()*11);
	$.get("/includes/hp_latest.php",{type: "mr", rnd: randomnumber},function(data){
		$("#media_releases_content").replaceWith(data);
	});
	
	// Investor Resources
	$.get("/includes/hp_latest.php",{type: "ir", rnd: randomnumber},function(data){
		$("#investor_resources_content").replaceWith(data);
	});
	
	//legal_framework
	$.get("/includes/hp_latest.php",{type: "lf", rnd: randomnumber},function(data){
		$("#legal_framework_content").replaceWith(data);
	});

	//publications
	$.get("/includes/hp_latest.php",{type: "pub", rnd: randomnumber},function(data){
		$("#publications_content").replaceWith(data);
	});

	// Open the students.xml file
	$.get("/xml/slideshow.xml",{},function(xml){
	
	// Build an HTML string
	HTMLOutput = '';	 
	
	// Run the function for each student tag in the XML file
	$('slide',xml).each(function(i) {
		source = $(this).find("source").text();
		blurbContent = $(this).find("blurb").text();
		id = $(this).find("id").text();
		classname = $(this).find("class").text();
		url = $(this).find("url").text();
		
		blurb.push(blurbContent);
		
		// Build row HTML data and store in string
		data = '<img src="/media/'+source+'" id="v'+id+'" onmouseover="pauseSlideInterval();" onmouseout="unpauseSlideInterval('+id+');" onclick=loadPage("'+url+'");   class="'+classname+'"/>';
		
		HTMLOutput = HTMLOutput + data;
		
	});		
	// Update the DIV called Content Area with the HTML string		
	$("#slideshow").append(HTMLOutput);
});



});
