/******************************************************************************
 * Slideshow functionality
 *****************************************************************************/
var $Slideshow       = null;    // Element holding slideshow images
var $SlideshowPaging = null;    // Page control of slideshow
var TIME_PER_SLIDE   = 7000;    // Time (in ms) to display an image
var SLIDE_COUNT      = 4;       // Total number of slides
var SLIDE_WIDTH      = 880;     // Width (in px) of a slide
var TRANSITION_TIME  = 1000;    // Time (in ms) to spend transitioning between slides
var SlideshowTimer   = null;    // Timer for managing slideshow
var SlideNumber      = 0;       // Number of currently displayed slide
var ProjectDetails   = [];      // Array of project detail data
var ImageCaches      = [];      // Array of cached images

// Images to preload
var Preloader = 
    ["BEV", 
     "Concrete5", 
     "ECE", 
     "Embedded", 
     "FashionShow",
     "Honorsys", 
     "Koofers", 
     "Qualcomm", 
     "Syncrod", 
     "VirginiaTech"];

// Handles slideshow funcationality
var Slideshow = function()
{
    // Constructor begins the slideshow
    $Slideshow = $("div#Slideshow");
    $SlideshowPaging = $Slideshow.parent().prev().find("div.Center:first").find("div.PageNumbers:first");
	SlideshowTimer = setInterval("Slideshow.advance();", TIME_PER_SLIDE);

	// Enable page number clicking to traverse
	$SlideshowPaging.find("div.Page").click(function()
	{
		// Highlight only the clicked indicator
		clearInterval(SlideshowTimer);
		$SlideshowPaging.children().removeClass("Current");
		$(this).addClass("Current");
		
		// Scroll the background
		SlideNumber = Slideshow.findPageNumber($(this));
		var BackgroundPosition = "-" + (SLIDE_WIDTH * SlideNumber);
		$Slideshow.animate({backgroundPosition: BackgroundPosition}, TRANSITION_TIME);
	});
}

// Find the page number of a given indicator
Slideshow.findPageNumber = function($pElement)
{
	var PageNumber = 0;
	var $CurrentElement = $pElement;
	while ($CurrentElement.prev().length)
	{
		$CurrentElement = $CurrentElement.prev();
		PageNumber++;
	}
	return PageNumber;
};

// Plays slideshow forever
Slideshow.advance = function()
{    
    // Update page number
    SlideNumber = (SlideNumber + 1) % SLIDE_COUNT;
    $SlideshowPaging.find("div.Page").each(function(i)
    {
        $(this).removeClass("Current");
        if (i == SlideNumber)
        {
            $(this).addClass("Current");
        }
    });
    
    // Advance to next slide, with wraparound
	if (SlideNumber)
	{
		$Slideshow.animate({backgroundPosition: "-=" + String(SLIDE_WIDTH)}, TRANSITION_TIME);
	}
	else
	{
	    clearInterval(SlideshowTimer);
        SlideshowTimer = setInterval("Slideshow.advance();", TIME_PER_SLIDE);
		$Slideshow.animate({backgroundPosition: "+=" + String(SLIDE_WIDTH * (SLIDE_COUNT - 1))}, TRANSITION_TIME);
	}
};

// Project navigator
var Navigator = function()
{
	// Attach event handlers
	$("div#Experience div.Body ul li").click(function()
	{
		// Update highlighted entry
		$("div#Experience div.Body ul li").removeClass("Current");
		$(this).addClass("Current");
		var IconClass = $(this).find("p.Icon").attr("class");
		
		// Update main attraction
		var $MainTitle = $("div#MainAttraction div.Title:first div.Center:first");
		var $MainBody  = $("div#MainAttraction div.Body:first");
		$MainTitle.find("p.Icon").attr("class", "").addClass(IconClass + " Left");
		$MainTitle.find("p.Text").html($(this).find("div span.Project").html());
		
		var Image = '<img class="BodyIcon" alt="" src="Images/' + IconClass.replace("Icon ", "") + '.png" />';
		
		$MainBody.html(Image + ProjectDetails[IconClass.replace("Icon", "").replace(" ", "")]);
		parseLinks();
	});
};

/******************************************************************************
 * Quote functionality
 *****************************************************************************/
var Quotes = function()
{
	setInterval("Quotes.rotate();", 2 * TIME_PER_SLIDE);
};

var QuoteIndex = 0;
Quotes.rotate = function()
{
	QuoteIndex = (QuoteIndex + 1) % QuoteLines.length;
	$("p#Quote").html(QuoteLines[QuoteIndex]);
	$("p#Caption").html('- ' + Captions[QuoteIndex]);
	parseLinks();
};

var QuoteLines = 
	["Zach is one of the most talented and intelligent colleagues I have ever met.",
	 "Not only is Zach an engineer of the highest caliber, he is also a damn good writer.",
	 "Some people like to ask the question, &quot;If you were stuck on a desert island and you could bring one thing, what would it be?&quot; Only a fool would answer something other than &quot;Zach Rattner&quot;.",
	 "You sir, are a steely-eyed missile man. You rock.",
	 "Zach is awesome at seeing new age design and development and delivering it with the utmost passion." ,
	 "Zach is extremely responsive and provides a high quality product. His attention to detail and willingness to work towards a common goal will help him become even more successful in the future."];

var Captions = 
	['Matthew Hughes, Associate Chief Justice at the <a href="http://www.honorsystem.vt.edu">Virginia Tech Undergraduate Honor System</a>',
	 'Lindsay Felter, girlfriend/liberal arts major',
	 'Conley Owens, Software Engineer at <a href="http://www.google.com">Google</a>', 
	 'Neal Kegley, Operations Manager at the <a href="http://www.honorsystem.vt.edu">Virginia Tech Undergraduate Honor System</a>',
	 'Wade Hammess, Marketing/Business Development Intern at <a href="http://www.koofers.com">Koofers.com</a>',
	 'Matt Franz, Freelance client at <a href="http://www.butlertech.org">Butler Tech</a>'];
 
 
$(document).ready(function()
{
    // Initialize slideshow
    var Rotator = new Slideshow();
	
	// Initialize project navigation
	var Nav = new Navigator();
	
	// Initialize quotes
	var Quote = new Quotes();
	
	// Show first page of projects
	$("ul.ExperiencePage:first").show();
	
	// Load project details
	$.ajax(
	{
	    url: "AJAX/PreloadProjects.php",
	    dataType: "json",
	    success: function(ResponseData)
	    {
	        if (typeof(ResponseData) != "undefined")
	        {
                ProjectDetails = ResponseData;
                $("div#Experience div.Body:first ul:first li:first").click();
	        }
	    }
	});
	
	// Allow switching through experience pages
	$("div#Experience div.Center div.PageNumbers div.Page").click(function()
	{
	    var PageNumber = $(this).index();
	    
	    var $Page;
	    $("div#Experience div.Body ul.ExperiencePage").each(function(i)
	    {
	        $Page = $("div#Experience div.Center div.PageNumbers div.Page:nth-child(" + String(i + 1) + ")");
	        
	        if (i == PageNumber)
	        {
	            $Page.addClass("Current");
	            $(this).show();
	        }
	        else
	        {
	            $Page.removeClass("Current");
	            $(this).hide();
	        }
	    });
	});
	
	// Preload images
	var ImageCache;
	for (var i in Preloader)
	{
        ImageCache = new Image(100, 100);
        ImageCache.src = "Images/" + Preloader[i] + ".png";
        ImageCaches.push(ImageCache);
	}
});

