﻿var imageTimeout = 3000;
var nextImage = 0;
var clearFunction;
var counter = 0 ; 
var clearclick ; 
var clearFade; 
var lastImage ;
var currentImageNo = 0 ; 
var arrImages;
  
function setOpacity(el, opacity) 
{
	opacity /= 100;
	el.style.opacity = opacity;
	el.style.MozOpacity = opacity;
	el.style.filter = "alpha(opacity=" + (opacity*100) + ")";
}

function fadeImage(el, currentOpacity)
{ 
    currentOpacity += 5;
    if(currentOpacity > 100)
    {
        var prevEl;
        setOpacity(el,100);
        
        el.style.zIndex = 1;
        counter = 0; 
        clearTimeout(clearclick);
        clearTimeout(clearFade);
        clearFunction = window.setTimeout(startFading,imageTimeout);
    }
    else 
    {
        counter++ ;
        setOpacity(el, currentOpacity);
        clearFade = window.setTimeout(function() { fadeImage(el, currentOpacity); },50);
    }
}

function startFading() 
{
	clearTimeout(clearclick);
 	var el = lastImage = document.getElementById('tdImageContainer').childNodes[nextImage];
 	
 	prevEl =  el.previousSibling ? el.previousSibling : el.parentNode.lastChild;
    prevEl.style.display = 'none';
    
  	el.style.display = 'block';
	el.style.zIndex = 2;
	setOpacity(el, 0);
	fadeImage(el, 0);
	
	currentImageNo = nextImage ; 
  	nextImage = (nextImage < arrImages.length-1) ? nextImage + 1 : 0;
}

function preLoadImages()
{
    arrImages = [
        ["images/customers/atcorp.jpg", "http://www.atcorpindia.in"],
        ["images/customers/aditi.jpg", "http://www.aditi.com"],
        ["images/customers/wirkle.jpg", "http://www.wirkle.com"],
        ["images/customers/one97.jpg", "http://www.one97world.com"]
    ];
    
    var arImageList = new Array();
    for (var x=0; x< arrImages.length; x++)
    {
        arImageList[x] = new Image();
        arImageList[x].src = arrImages[x][0];       
    }
     
    var el = document.getElementById('tdImageContainer');
    
    while (el.firstChild)
        el.removeChild(el.firstChild);
    
    for(var x = 0; x < arrImages.length; x++)
    {
        var a = document.createElement('a');
        a.setAttribute('href', arrImages[x][1]);
        a.setAttribute('target', '_blank');
        a.style.display = 'none';
        
        t = document.createElement('img');            
        t.setAttribute('src', arrImages[x][0]);
        t.setAttribute('alt', arrImages[x][1]);
        
        a.appendChild(t);
        el.appendChild(a);
    }

    window.setTimeout(startSlideShow, 5000)
}

function startSlideShow()
{
    //document.getElementById('loader').style.display = "none";
    document.getElementById('tdImageContainer').style.display = "";
    clearFunction = window.setTimeout(startFading,imageTimeout);
}

window.onload = preLoadImages;

