// Simple JavaScript Rotating Banner Using jQuery,  from www.mclelun.com
// modified by Conor 19aug10
// 20 aug 10	add dots for manual control
// 21 aug 10 	centre dots horiz and question vertically
// 13 sep 10	add livitrans() for emails

var jqb_vCurrent = 0;
var jqb_vTotal = 0;
var jqb_vDuration = 5000;
var jqb_intInterval = 0;
var bPaused = false;
// data now set up on Wordpress home page
// setInterval now called from Home page
// note that dot width is 12px + 6px margin either side, so any 12 below is probably for that


// initialise dot controller functions
function initDotControls () {
    // centre dotdiv horizontally, depending on number of dots, and the question vertically 
	// and recalc centres if browser resized
	window.onresize = resizeHandler;  // and set up the event handler for resize event
	doCentering();
	
	$('#dotdiv .dot').click(function() {
				//which dot is this ?
				for (i = 0; i <= jqb_vTotal; i++) {
					if ( $(this).get(0) == $('.dot:eq('+ i + ')' ).get(0) ){ // note use of get() to get to html element
					  break;
					}
				}
				// alert ("Dot " + i);
				// if timer running, stop it and show selected question (& bkgd and link)
				if (!bPaused) {
					clearInterval(jqb_intInterval);
					bPaused = true;
					jqb_vCurrent = i;  
					show_Question();
				} else if (i == jqb_vCurrent) {
					//otherwise if pressing white dot again, restart timer
					next_Question();
					jqb_intInterval = setInterval(next_Question, jqb_vDuration);
					bPaused = false;
					return;
				} else {
					// if this dot not highlighted, then move to it (don't start timer)
					bPaused = true;
					jqb_vCurrent = i;  
					show_Question();	
				}
				
	});
}



// show next question, this also used as timer function, every 5 secs ?
function next_Question(){
	jqb_vCurrent == jqb_vTotal ? jqb_vCurrent = 0 : jqb_vCurrent++;
	show_Question();
}

function show_Question() {
	// hide the current question, change the question text and link, fade new qn in
	$("#homeContent h1 a").
			hide().text(jqb_text[jqb_vCurrent]).attr('href',jqb_link[jqb_vCurrent]).animate({ opacity: 'show' }, 1000);
	// and change the background image
	$("body").css('background-image', 'url('+jqb_image[jqb_vCurrent]+')');
	// centre the question
	vertCentreQn(); 
	// and make appropriate button white
	highlightDot(jqb_vCurrent);
}

function highlightDot(dotnum) {
	// first unhighlight all dots (cos previous is not necessarily the prev one)
	$('.dot').css('background-position', '0 0');
	// and highlight this one, by sliding bkgd image up
	$('.dot:eq(' + dotnum + ')').css('background-position', '0 -12px');
}

// Position dots and question first time, and if user resizes browser window 
// do dots as a percent, so they move with browser if resizing, but then recalc at end for accuracy
// nb dots 12px wide + 6px margin either side
function doCentering () { 
	var onepercent = $(window).width() / 100.0;
	var myPercent = Math.round(50.0 - ((jqb_vTotal+1) * 12  / onepercent));
	$('#dotdiv').css('left', myPercent + '%');
	
	vertCentreQn();
}

// to vertically centre the question text
function vertCentreQn() {
	var bh = $(window).height();
	var qh = $('#homeContent h1').height();
	var myPad = (bh - 180 - qh) / 2;
	myPad = myPad - 10;  // it's 10px too low, even when everything set to 0, so adjust
	if (myPad < 0) {
		myPad = 0;
	}
	$('#homeContent').css('padding-top',myPad+'px');	
}

//(but wait till resize has 'stopped' (pause for 1/10th sec ?) otherwise it does it too much ?)
var timeOut = null;

function resizeHandler() {
	doCentering(); // actually it seems to cope OK, in Firefox anyway. Test in others
	// if we need to only execute code when resize has finished, here is how :
    //if (timeOut != null) clearTimeout(timeOut);
    //timeOut = setTimeout(doCentering, 100);
}

// function for email addresses - see documentation
function livitrans(s) {
	news = "";
	if (s.length > 1) {
		for (i=s.length; i > 0; i--) {
			news += s.charAt(i);
		}
		news += "@livity.co.uk";
		window.location = "mailto:" + news;
	}
}
