/*
 * ajxSlider v0.1
 * http://www.swegrafix.net/
 *
 * Author: Kimmy Andersson ( kimmy-a [at] hotmail [dot] com )
 * 
 * This work is protected under a Attribution-Noncommercial-No Derivative license
 * under creative commons.
 * http://creativecommons.org/licenses/by-nc-nd/3.0/
 *
 * Date: 2009-10-12 11:03 +0200 (Monday, 12 Oct 2009)
 * Revision: 2
 * 
 * Dependencies:
 * jQuery
 * 
 */


//ajxSlider Configuration
var ajxDir = 'ajxslider/'; //Directory to ajxSlider folder
var ajxImageDir = 'files/gallery/ajx_previews/'; //Directory to the image directory of ajax.
var ajxNewTab = false; //Should the links open in a new window/tab?

//ajxSlider Javascript
var currentImage;
var doOnce = 0;

//XML Parse Variables.
var image_file = [];
var image_link = [];

function changeImage(image)
{
	
	resetButtons();
	
	document.getElementById("ajxB"+image).src = ajxDir+'images/ajx_oud.png';
	
	$(document.getElementById("ajxMainImage")).fadeOut("slow", function()
	{
		
		var iSwitch = imageSwitch(image);
		currentImage = image;
		
	});
	
	$(document.getElementById("ajxMainImage")).fadeIn("slow");
	
	if( !iSwitch )
	{
		
		return false;
		
	}
	else
	{
		
		return true;
		
	}
	
}

function imageSwitch(id)
{
	
	var idVar = (id - 1);
	
	document.getElementById("ajxMainImage").src = image_file[idVar];
	document.getElementById("ajxMainLink").href = image_link[idVar];
	
	if(ajxNewTab)
	{
		
		document.getElementById("ajxMainLink").target = "_blank";
		
	}
	
	return true;
	
}

function resetButtons()
{
	
	document.getElementById("ajxB1").src = ajxDir+'images/ajx_ind.png';
	document.getElementById("ajxB2").src = ajxDir+'images/ajx_ind.png';
	
	return true;
	
}

function imageRotate()
{
	
	
	if(currentImage == 1)
	{
		
		changeImage(2);
		
	}
	else
	{
		
		changeImage(1);
		
	}
	
}

/* XML Related Functions */

function xmlLoad()
{
	
	var xmlFile = ajxDir+'xml/index.php';
	
	if(window.XMLHttpRequest)
	{
		
		xhttp = new window.XMLHttpRequest();
		
	}
	else
	{
		
		xhttp = new ActiveXObject("Microsoft.XMLHTTP");
		
	}
	
	xhttp.open("GET", xmlFile, false);
	xhttp.send("");
	
	xmlDoc = xhttp.responseXML;
	
	xmlParser(xmlDoc);
	
	return true;
	
}

function xmlParser(data)
{
	
	var i;
	
	for (i = 0; i < 2; i++)
	{
		
		image_file[i] = ajxImageDir+data.getElementsByTagName('file')[i].childNodes[0].nodeValue;
		image_link[i] = data.getElementsByTagName('link')[i].childNodes[0].nodeValue;
		
	}
	
	return true;
	
}

function doXML()
{
	
	xmlLoad();
	return true;
	
}

/* jQuery Functions */

$(document).ready(function()
{
	
	if(doOnce == 0)
	{
		
		doXML();
		
		$.cacheImage(image_file[0], {
			
			load : function(e)
			{
				
				$.cacheImage(image_file[1], {
					
					load : function()
					{
						
						changeImage(1);
						
					}
					
				});
				
				doOnce = 1;
				
			}
			
		})
		
	}
	
	var rotateInterval = self.setInterval('imageRotate()', 5000);
	
})
