/*
Slideshow Script based on work by Geoff Pack, February 2008
*/

function slideshow(name, nickname, autoPlay) {
	this.name = name;
	this.nickname = nickname;
	this.autoPlay = autoPlay;
	var slideTimer;
	var slideCount;
	var timer;
	var currentSlide=0;

	// Work out the slide count and delay values
	var slideTimerID = '' + nickname + 'SlideTimer';
	var slideCountID = '' + nickname + 'SlideCount';
	var slideTimerElement = document.getElementById(slideTimerID);
	var slideCountElement = document.getElementById(slideCountID);
	if (slideTimerElement) {
		slideTimer = slideTimerElement.value;
	} else {
		slideTimer = 5000;
	}
	if (slideCountElement) {
		slideCount = slideCountElement.value;
	} else {
		slideCount = 2;
	}
	this.delay = slideTimer;
	var N = slideCount;

	// slide controls
	this.show = function(n) {
		if (n!=currentSlide) {
			// hide current slide
			var currentSlideID = '' + nickname + currentSlide;
			animateOpacity(currentSlideID, 100, 0, 800);
			setTimeout("hide('"+currentSlideID+"')",800);

			// show new slide
			var newSlideID = '' + nickname + n;
			animateOpacity(newSlideID, 0, 100, 800);
			setTimeout("show('"+newSlideID+"')",100); // delay to reduce flicker
			currentSlide = n;
		}
		return false;
	}
	this.show1 = function(n) {
		this.show(n);
		this.pause();
		return false;
	}
	this.back = function(pause) {
		x = currentSlide -1;
		if (x < 0) x = N-1;
		this.show(x);
		if (pause) {
			this.pause();
		}
		return false;
	}
	this.next = function(pause) {
		x = currentSlide + 1;
		if (x >= N) x = 0;
		this.show(x);
		if (pause) {
			this.pause();
		}
		return false;
	}
	this.play = function() {
		str = name + '.next(false)';
		timer = setInterval(str,this.delay);
		return false;
	}
	this.pause = function() {
		clearInterval(timer);
		return false;
	}

	if (autoPlay) {
		this.play();
	}
}

/* ------------------------------------ */
// Opacity functions
// (source: http://www.brainerror.net/scripts_js_blendtrans.php)

// Fade from one opacity setting to another
function animateOpacity(id, opacStart, opacEnd, millisec) {
	var skip = 5;
    var speed = Math.round((millisec / 100) * skip);
    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-=skip) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for (i = opacStart; i <= opacEnd; i+=skip) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = (opacity < 100 ? "alpha(opacity=" + opacity + ")" : "none");
}

// Show/Hide functions clean up later...
function show(id) {
	document.getElementById(id).style.display = 'block';
}
function hide(id) {
	document.getElementById(id).style.display = 'none';
}

// Add new string functions
String.prototype.trim = function() {return this.replace(/^\s+|\s+$/, '');};

function classAdd(element, theclass) {
	// Add a CSS class to the specified element (will not add the class if it already exists)
	if (!element) return;
	if (!element.className) element.className = '';
	var reg = new RegExp('(^| )'+theclass+'( |$)', 'g');
	if (element.className.search(reg) == -1) element.className = (element.className + ' ' + theclass).trim();
}

function classRemove(element, theclass) {
	// Remove a CSS class from the specified element
	if (!element) return;
	if (!element.className) return;
	var reg = new RegExp('(^| )'+theclass+'( |$)', 'g');
	element.className = element.className.replace(reg, ' ').trim();
}

function esqMouseOverCF()
{
	jQuery("#esq-contact-form").slideToggle(2000,function() {});
}

