/* 
 * this file should contain javascript functions required by the index
 * page.
 */

// this counter will be used to keep up to identify
// when a timer is out of date, that is, another
// counter is now rotating the images, clicking on
// one control to jump to a landing image should
// cancel out the timer that is waiting in the
// interim
var rotationCounter = 0;

naturallyRotateLandingImage(rotationCounter);

// these varriables I will used to keep track of
// which imaegs and text we are up to
var landingImageIndex = 1;
var landingImagePrevIndex = 0;

rotateBottomImage();

//these varriables I will used to keep track of
//which imaegs and text we are up to
var bottomImageIndex = 1;
var bottomImagePrevIndex = 0;

/**
 * Switches the landing image to the one that 
 * 
 */
function jumpToLandingImage(i) {
	
    // set the previous index as the current index
    landingImagePrevIndex = landingImageIndex;
	landingImageIndex = i;

	// now rotate the images
	rotateLandingImage();
	
}

/**
 * Rotates the landing image on the index page to the
 * next image sequentially
 * 
 */
function naturallyRotateLandingImage(passedRotationCounterValue) {
	
	// only continue if the rotationCounter value passed matches
	// that of the class
	if(passedRotationCounterValue == rotationCounter) {
	
	    // set the previous index as the current index
	    landingImagePrevIndex = landingImageIndex;
	    
		landingImageIndex = landingImagePrevIndex+1;
		if(landingImageIndex > 3) {
			landingImageIndex = 1;
		}
		
		// now rotate the images
		rotateLandingImage();

	}
		
}

/**
 * Rotates the landing image on the index page to the
 * next image sequentially
 * 
 */
function rotateLandingImage() {
	
	// increment the rotation counter
	rotationCounter++;
	
	// define the new and the old text panels
	var newText = "landing-text"+landingImageIndex;
	var oldText = "landing-text"+landingImagePrevIndex;
	
	// define the new and old image
	var newImage = "/images/home/wf-lead"+landingImageIndex+".jpg";
    var oldImage = "/images/home/wf-lead"+landingImagePrevIndex+".jpg";
    
    var newControlImage = "/images/home/landing-control-"+landingImageIndex+"-blue.png";
    var oldControlImage = "/images/home/landing-control-"+landingImagePrevIndex+"-green.png";
    
    // set the background image
    $("#main-landing-panel-image").css({backgroundImage: "url('" + oldImage + "')"});

    // hide the image object, change it's source and fade in
    $("#landing-img").hide(0).attr('src', newImage).fadeIn(1300);
    
    // change the control images
    $("#landing-controls"+landingImageIndex).attr('src', newControlImage);
    $("#landing-controls"+landingImagePrevIndex).attr('src', oldControlImage);

    // remove the old text and show the new
    $("#"+oldText).hide();
    $("#"+newText).fadeIn(400);
    
    setTimeout("naturallyRotateLandingImage("+rotationCounter+")", 12000);
    
}

/**
 * Rotates the bottom image on the index page.
 * 
 * @param i the current image index
 */
function rotateBottomImage() {
	
	// set the previous index as the current index
	bottomImagePrevIndex = bottomImageIndex;
	
	bottomImageIndex = bottomImagePrevIndex+1;
	if(bottomImageIndex > 3) {
		bottomImageIndex = 1;
	}
	
	// define the new and old image
	var newImage = "/images/home/wf-quote"+bottomImageIndex+".jpg";
    var oldImage = "/images/home/wf-quote"+bottomImagePrevIndex+".jpg";
    
    // set the background image
    $("#bottom-panel-image").css({backgroundImage: "url('" + oldImage + "')"});

    // hide the image object, change it's source and fade in
    $("#bottom-img").hide(0).attr('src', newImage).fadeIn(1300);
    
    setTimeout("rotateBottomImage()", 10000);
    
}
