// featureBlocks.js
// Used to control feature block content

// ******** VARIABLES ********

// HOME IMAGE FEATURE VARIABLES
var totalHomeImageFeatures;                       // total number of features
var currentHomeImageFeature;                      // number of the current feature being displayed

// HOME TEXT FEATURE VARIABLES
var totalHomeTextFeatures;                       // total number of features
var currentHomeTextFeature;                      // number of the current feature being displayed


// ******** FUNCTIONS ********

// GENERAL SCROLLING FUNCTIONS
function initHomeBlock() {

	// initialize the home text and image feature
	initHomeImageFeature();
	
	// initialize the home text feature
	initHomeTextFeature();
	
	// check if we need a scroll bar on this page
	checkScrollFeature();
}

		
function getElWidth( el ) {
	//alert( 'el.offsetWidth:' + el.offsetWidth + ', el.clientWidth:' + el.clientWidth + ', el.style.Width:' + el.style.Width );
	return parseInt(el.offsetWidth);
}

function getPageLeft(el){
	var x = 0;
	while(el.offsetParent!=null){
		//alert( 'getPageLeft-->el.offsetLeft:' + el.offsetLeft ); 
		x+=el.offsetLeft;
		el=el.offsetParent;
	}
	x+=el.offsetLeft;
	//alert( 'getPageLeft-->final_X:' + x );
	return x;
}

function getPageTop(el){
	var y=0;
	while(el.offsetParent!=null){
		//alert( 'getPageTop-->el.offsetTop:' + el.offsetTop );
		y+=el.offsetTop;
		el=el.offsetParent;
	}
	y+=el.offsetTop;
	//alert( 'getPageTop-->final_Y:' + y );
	return y;
}

function renameObjects( messageName, maxMessages ) {
	// All message objects start off with the same name, so we go through and rename them incrementally, starting at 0
	var currentMessageObj;             // the current message object to be renamed
	var totalMessages = 0;             // a rolling number of messages found
	var foundAllMessages = false;      // determine when all the messages have been renamed
	var safeGaurd = 0;                 // just a safe gaurd to make sure we don't get stuck in an endless loop

	while( !foundAllMessages ) {   // loop until we've found all the messages
		//alert('Looking for ' + messageName + totalMessages );
		if( currentMessageObj = document.getElementById( messageName ) ) {
			currentMessageObj.id = messageName + totalMessages;
			//alert('Renamed: ' + currentMessageObj.id );
			totalMessages++;
		} else {
			foundAllMessages = true;
			//alert('All \'' + messageName + '\' messages are found');
		}
		safeGaurd++;
		// don't want to get stuck in an endless loop, so if we're still finding stuff when we hit the max, somethings gone wrong-o. Bail
		if( safeGaurd > maxMessages ) foundAllMessages = true;
	}
	return totalMessages;  // return the total number of messages found
}


// HOME IMAGE FEATURE FUNCTIONS
function initHomeImageFeature() {
	
	// initialize the home text and image feature area with the top feature (should be the only feature)
	totalHomeImageFeatures = homeImageFeatureObject.homeImageFeatureItems.length;
	totalHomeImageFeatures--;  // remove the trailing default item.
	//currentHomeImageFeature = Math.floor( Math.random() * totalHomeImageFeatures );
	currentHomeImageFeature = totalHomeImageFeatures - 1;
	//alert( "HomeImage: currentFeature / totalFeatures: " + currentHomeImageFeature + "/" + totalHomeImageFeatures );
	
	if( showHomeImageFeatureCheck() ) {
		showHomeImageFeature();
	}
	
}

function showHomeImageFeatureCheck() {
	
	//check that we have at least one feature
	if( totalHomeImageFeatures == 0 ) {
		return false
	}
	return true;
}


function showHomeImageFeature() {
	
	// supply the pageNav elements with the correct HTML
	var homeImageFeatureContentArea = document.getElementById('homeImageFeatureContent');
	var homeImageFeatureImageArea = document.getElementById('homeImageFeatureImage');
	var homeImageFeatureLink = document.getElementById('homeImageFeatureLink');
   
   // content (with title)
   var contentHTML = '';
   if( homeImageFeatureObject.homeImageFeatureItems[currentHomeImageFeature].featureTitle ) {
   	contentHTML += '<h3>' + homeImageFeatureObject.homeImageFeatureItems[currentHomeImageFeature].featureTitle + '</h3>';
   }
   contentHTML += homeImageFeatureObject.homeImageFeatureItems[currentHomeImageFeature].featureContent;
   homeImageFeatureContentArea.innerHTML = contentHTML;
   
   // image
   homeImageFeatureImageArea.style.background = 'url("/img_lib/' + homeImageFeatureObject.homeImageFeatureItems[currentHomeImageFeature].featureImagePath + '") top left no-repeat #ffffff';
   //alert( 'url("/img_lib/' + homeImageFeatureObject.homeImageFeatureItems[currentHomeImageFeature].featureImagePath + '") top left no-repeat #ffffff' );
   
   // link
   var linkHTML = '<p>&bull; <a href="' + homeImageFeatureObject.homeImageFeatureItems[currentHomeImageFeature].featureLink + '"';
   if( homeImageFeatureObject.homeImageFeatureItems[currentHomeImageFeature].linkInNewWindow == "1" ) {
		linkHTML += ' target="_blank"';
	}
	linkHTML += '>Learn more</a></p>';
	//alert( 'linkHTML: ' + linkHTML );
	homeImageFeatureLink.innerHTML = linkHTML;
	
}

function changeHomeImageFeaturePrev() {
	if( currentHomeImageFeature <= 0 ) {
		currentHomeImageFeature = totalHomeImageFeatures;
	}
	currentHomeImageFeature--;
	showHomeImageFeature();
}

function changeHomeImageFeatureNext() {
	currentHomeImageFeature++;
	if( currentHomeImageFeature >= totalHomeImageFeatures ) {
		currentHomeImageFeature = 0;
	}
	showHomeImageFeature();
}


// HOME TEXT FEATURE FUNCTIONS
function initHomeTextFeature() {
	
	// initialize the home text feature area with the top feature (should be the only feature)
	totalHomeTextFeatures = homeTextFeatureObject.homeTextFeatureItems.length;
	totalHomeTextFeatures--;  // remove the trailing default item.
	//currentHomeTextFeature = Math.floor( Math.random() * totalHomeTextFeatures );
	currentHomeTextFeature = totalHomeTextFeatures - 1;
	//alert( "HomeText: currentFeature / totalFeatures: " + currentHomeTextFeature + "/" + totalHomeTextFeatures );
	
	if( showHomeTextFeatureCheck() ) {
		showHomeTextFeature();
	}
	
}

function showHomeTextFeatureCheck() {
	
	//check that we have at least one feature
	if( totalHomeTextFeatures == 0 ) {
		return false
	}
	return true;
}


function showHomeTextFeature() {
	
	// supply the pageNav elements with the correct HTML
	var homeTextFeatureContentArea = document.getElementById('homeTextFeatureContent');
	var homeTextFeatureLink = document.getElementById('homeTextFeatureLink');
   
   // content (with title)
   var contentHTML = '';
   if( homeTextFeatureObject.homeTextFeatureItems[currentHomeTextFeature].featureTitle ) {
   	contentHTML += '<h3>' + homeTextFeatureObject.homeTextFeatureItems[currentHomeTextFeature].featureTitle + '</h3>';
   }
   contentHTML += homeTextFeatureObject.homeTextFeatureItems[currentHomeTextFeature].featureContent;
   homeTextFeatureContentArea.innerHTML = contentHTML;
   
   // link
   var linkHTML = '<p>&bull; <a href="' + homeTextFeatureObject.homeTextFeatureItems[currentHomeTextFeature].featureLink + '"';
   if( homeTextFeatureObject.homeTextFeatureItems[currentHomeTextFeature].linkInNewWindow == "1" ) {
		linkHTML += ' target="_blank"';
	}
	linkHTML += '>Learn more</a></p>';
	//alert( 'linkHTML: ' + linkHTML );
	homeTextFeatureLink.innerHTML = linkHTML;
	
}

function changeHomeTextFeaturePrev() {
	if( currentHomeTextFeature <= 0 ) {
		currentHomeTextFeature = totalHomeTextFeatures;
	}
	currentHomeTextFeature--;
	showHomeTextFeature();
}

function changeHomeTextFeatureNext() {
	currentHomeTextFeature++;
	if( currentHomeTextFeature >= totalHomeTextFeatures ) {
		currentHomeTextFeature = 0;
	}
	showHomeTextFeature();
}
