// Featured box JavaScript
// Original coding by Chris Eggison (www.ssukstudios.co.uk)
// Additional code by brainerror.net and somesite.com


/* -----------------------------------------------------------
	Usage:
	
	autoChange() 				- Used at load of a page
		|- autoChangeCode() 	- Used at load with autoChange()
	shiftOpacity(<DIV ID>, <MILISECONDS (INTEGER)>, <DIRECTION ("next"/"prev")>) - Used to rotate featured box
		|- changeImg(<DIV ID>)	- Used with shiftOpacity() to rotate background image for the image box
		|- changeText()			- Used with shiftOpacity() to rotate text from the feat_text array
	opacity(<DIV ID>, <START OPACITY (INTEGER)>, <END OPACITY (INTEGER)>, <MILISECONDS (INTGER)>) - Black magic
		|- changeOpac(<OPACITY (INTEGER), <DIV ID>) - Cross browser opacity style changing code
	addLoadEvent(<CODE>)		- Automatically initiates code on page load

	----------------------------------------------------------
	Disclaimer:
	
	This JavaScript code has been created and tested to work with Firefox 3.0, 3.1 Beta 2 and 3.1 Beta 2, Internet Explorer 6, 7 and 8 Beta 2.
	
	---------------------------------------------------------- */

/* DECLARES */
// Do not edit these variables unless you're editing the basic functionality of this JavaScript
var currentImg = 1; // Sets the first array access point
var autochange; // Interval variable


/* CONFIGURATION */

var feat_text = new Array();
feat_text[0]=""; // To help the end user, the array starts at [1]
//feat_text[1]="<span>Mick Swan said...</span><br />&acute;We believe that Alison helped EHG prepare itself successfully and deliver the best possible outcome for the organisation throughout the inspection process.&acute;<br /><a href='testimonials.html'>Read More</a>";
feat_text[1]="<!--<span>Mick Swan said...</span>-->&acute;We believe that Alison helped EHG prepare itself successfully and deliver the best possible outcome for the organisation throughout the inspection process.&acute;<br /><br /><a href='testimonials.html'>Read More</a>";
feat_text[2]="<!--<span>Steve Smith said...</span><br /><br />-->&acute;Quite simply, she delivers exactly what's needed every time.&acute;<br /><br /><br /><br /><br /><a href='testimonials.html'>Read More</a>";
feat_text[3]="<!--<span>David Jelley said...</span><br />-->&acute;Alison delivered the goods and always in an engaging enthusiastic manner which secured the willing contributions and co-operation of staff, customers and external agencies.&acute;<br /><br /><a href='testimonials.html'>Read More</a>";

var imagefolder = "img";
var feat_img = new Array();
feat_img[0] = "";
feat_img[1] = "feat_1.jpg";
feat_img[2] = "feat_2.jpg";
feat_img[3] = "feat_3.jpg";

var maxitems = 3; // Number of items in the array above.

var secondInterval = 7; // Number of seconds desired before items auto change

var milisec = secondInterval * 1000;

/* FUNCTIONS */

function autoChange() {
	autochange = setInterval(autoChangeCode, milisec);
}

function autoChangeCode() {
	shiftOpacity('featured_image','70','next');
}

function changeImg(id) {
	if (currentImg > maxitems) {
		currentImg = 1;
	}
	document.getElementById(id).style.background = "url(./" + imagefolder + "/" + feat_img[currentImg] + ")";
}

function changeText() {
	document.getElementById('featured_text').innerHTML = feat_text[currentImg];
}

function shiftOpacity(id, millisec, direc) {
	if (direc == "next") {
		currentImg++;
		if (currentImg > maxitems) {
			currentImg = 1;
		}
	} else {
		currentImg--;
		if (currentImg < 1) {
			currentImg = maxitems;
		}
	}
	opacity(id, 100, 0, millisec);
	changeImg(id);
	opacity(id, 0, 100, millisec);
	changeText();
	clearInterval(autochange); //An attempt to reset the interval used when the featured box changes.
	autoChange();
}

// Code edited from http://brainerror.net/scripts/javascript/blendtrans/ Thanks, guys!
function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
} 

function changeOpac(opacity, id) {
	// Cross Browser fade
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}

var sPath = window.location.pathname;
var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
// Only run this code if the page is "index.html"
// Saves CPU usage on the client-side
if (sPage = "index.html") {
	// On page load, run code to swap images
	function addLoadEvent(func) {
	  var oldonload = window.onload;
	  if (typeof window.onload != 'function') {
		window.onload = func;
	  } else {
		window.onload = function() {
		  if (oldonload) {
			oldonload();
		  }
		  func();
		}
	  }
	}
	
	addLoadEvent(autoChange());
}
