// (c) 1999-2008 Bright Interactive Limited. All rights reserved.
// http://www.bright-interactive.com | info@bright-interactive.com
// Tel: 0870 240 6520

// home.js - scripts for the homepage

// slideshow settings
slide_duration_secs = 4.7;      // how often should we change the image
fade_duration_secs  = 0.8;      // how long the fade up lasts
switch_delay_secs   = 0.2;      // how long the fade down lasts
services            = null;     // ref to list items
slideshow_int       = null;     // ref to interval 
index               = -1;       // current slideshow image index
previousIndex       = 0;        // previous slideshow image index
indexSet            = false;    // has the index been set manually?
manualSwitch        = false;
tab                 = null;
returnBack          = null;

function getNextIndex()
// return the index of the next slideshow image
{
    return index;
}
    
function getPreviousIndex()
// return the index of the previous slideshow image
{
    var p = (previousIndex > -1) ? previousIndex : null ;
    return p;
}
    
function setIndex(i)
// sets the next index
{
    
    indexSet = true;
    
    previousIndex = index;
   
    if (i >= 0) 
    {
        index = i;
    }
    else
    {
        index = (index == (services.length - 1)) ? 0 : index + 1;
    }
    
    return index;

}


function showNext(quick)
// fade out the previous image and then show the next one
{


    if (!indexSet) {setIndex();}
    
    var next = getNextIndex();
    
    var previous = getPreviousIndex();
    
    if (next == previous) {indexSet = false; return false;}
    
    var nextElm = $(document.getElementById("c"+(next+1)+"_top"));

    nextElm.fadeTo(0,0);
    
    $(services[next]).addClass("show");
    
    $(services[next]).addClass("selected");
    
    var fade = (quick === true) ? 0 : fade_duration_secs;
    
    var delay = (quick === true) ? 0 : switch_delay_secs;

    nextElm.fadeTo(fade*1000,1);
    
    if (previous !== null)
    {
    
        var prevElm = $(document.getElementById("c"+(previous+1)+"_top"));
        
        prevElm.fadeTo(100,0);
        
        $(services[previous]).removeClass("selected");
        $(services[previous]).removeClass("show");

    }
    
    indexSet = false;
    
}

function getTabUrl(tab) {
    
    return document.getElementById('c'+tab+"_link").href;
    
}

function initManualNavigation() 
// set up onclick for each service
{
    tabs = $('.service-tab');
    max = tabs.length;
    for ( i = 0; i < max; i++ ) {
    	tabs[i].index = i;
    	tabs[i].onmouseover = function (){
    	        manualSwitch = true;
    	        clearInterval(slideshow_int);
    	        setIndex(this.index);
    	        showNext(true);
    	}    	
    	tabs[i].onclick = function (){
            window.location = this.parentNode.href;
    	}    	
    	
    }
}


function init()
// kick off the slideshow and set the showNext func to run every x seconds
{
	
	if (!document.getElementById("service_nav")) {return false;}
	
	$("#service_nav_inner").removeClass('js-hide');
	
	$("#loader").hide();
	
	services = document.getElementById("service_nav").getElementsByTagName("li");
	
	initManualNavigation(services);
    
    var rand = Math.floor(Math.random() * 6);

    setIndex(rand);
    
    showNext();
    
    slideshow_int = setInterval(showNext,(slide_duration_secs * 1000));
    
    $(document.getElementById("c1")).removeClass('default');
	
}


$(document).ready(function()
{
    init(); // start the slideshow
   //setTimeout(init, 1000);

});
