/*
 * (c) 2009 itn-systems.com All rights Reserved.
 * 
 */


function nextImage(o) {
    var o = o.firstChild;
    var imagesource = [];
    var i = 0;
    var randomnumber = 0;
    
    while(o != null ) {
     
        if (o.tagName == 'A')
        {
            imagesource[i] = o.href;                        
            i++;    
        }
        o = o.nextSibling;
        
    }
    randomnumber=Math.floor(Math.random()*(imagesource.length+1));    
    document.getElementById('blendme').innerHTML='<a href="?type=gallery&page=20"><img style="width:300px; height:150px;" id="spic" src="'+imagesource[randomnumber]+'"></a>';
    o = document.getElementById('spic');    
    return o;
}

function getImage(imgArray) {    
    var randomnumber = 0;
    
    //    <img id="Status" width="16" height="16" src= ... >
    //document.getElementById("Status").src = "loading-indicatorXX.gif";
    //'+imagesource[randomnumber]+'

    randomnumber=Math.floor(Math.random()*(imgArray.length+1));    
    document.getElementById('blendme').innerHTML='<a href="?type=gallery&page=20"><img style="width:300px; height:150px;" id="spic" src=""></a>';    
    document.getElementById('spic').src = imgArray[randomnumber];
    o = document.getElementById('spic');    
    return o;
}

function preloadImages(o) {
     var i = 0;
     var imgArray = [];
     var o = o.firstChild;
     
     while(o != null ) {
     
        if (o.tagName == 'A')
        {
            imgArray[i] = (new Image()).src = o.href;            
            i++;    
        }
        o = o.nextSibling;        
    }
    
    return imgArray;
    
}

function setOpacity(obj, o) {

    obj.style.opacity = (o / 100);
    obj.style.MozOpacity = (o / 100);
    obj.style.KhtmlOpacity = (o / 100);
    obj.style.filter = 'alpha(opacity=' + o + ')';
}


function getNextImage(imgArray) {
	
    var oldimage = document.getElementById('spic').src;
    var blend = document.getElementById(id);
    var image = getImage(imgArray);
    while(oldimage == image.src)
    {
        image = getImage(imgArray);
    }
    next = image;
    return next;
}

function blendImages(id, speed, pause, caption) {
    
    var imgArray = [];

    if(speed == null) {
        speed = 30;
    }
    
    if(pause == null) {
        pause = 1500;
    }

    var blend = document.getElementById(id);    
    imgArray = preloadImages(blend);
    
    var image = getImage(imgArray);
    startBlending(image, speed, pause, caption,imgArray);
}

function startBlending(image, speed, pause, caption,imgArray) {

    image.style.display = 'block';

    if(caption != null) {
	document.getElementById(caption).innerHTML = image.alt;
    }

    setTimeout(
	   function() {continueFadeImage(image, 0, speed, pause, caption,imgArray)}, 3000
    );
}

function continueFadeImage(image, opacity, speed, pause, caption,imgArray) {

    opacity = opacity + 3;

    if (opacity < 103) 
    {

	   setTimeout(
	       function() {fadeImage(image, opacity, speed, pause, caption, imgArray)}, speed
	   );

    } 
    else 
    {
    	
    	var paws=pause-1000;
    	if (paws < 0 ) {
    		paws = 0;
    	}
    	
        setTimeout(function() {continueNegFadeImage(image, 100, speed, pause, caption, imgArray)}, speed+3000);
        		
    }
}

function continueNegFadeImage(image, opacity, speed, pause, caption, imgArray) {

    opacity = opacity - 3;

    if (opacity > -3) {

	   setTimeout(function() {fadeNImage(image, opacity, speed, pause, caption, imgArray)}, speed);

    } 
    else 
    {
        setOpacity(image,0);    
    	var paws=pause-1000;
    	if (paws < 0 ) {
    		paws = 0;
    	}
    	
    	
    	image = getImage(imgArray);
    	setTimeout(function() {startBlending(image, speed, pause, caption, imgArray)}, paws);
    	
    }
}

function fadeImage(image, opacity, speed, pause, caption, imgArray) {
    setOpacity(image,opacity);
    continueFadeImage(image, opacity, speed, pause, caption, imgArray);
}

function fadeNImage(image, opacity, speed, pause, caption, imgArray) {
    setOpacity(image,opacity);
    continueNegFadeImage(image, opacity, speed, pause, caption, imgArray);
}

function requestOnlineState() 
{        
        xajax_onlineprogress();
        setTimeout(
    	   function() {requestOnlineState()}, 15000
        );
}