// JavaScript Document

/* Copyright (c) 2010 - cirrus b.v.
 *
 * Version: 1.0
 * 
 */

var xmlDOMTree = undefined;

var slideContent;
var loadedSlides = 0;

var imagePath;
var firstTime;
var imageCache;

var preLoadedContent;

function loadSlides(xmlFilePath, imgPath) {
		firstTime=true;
		imagePath = imgPath;
		$.ajax({
				type: "GET",
				url:  xmlFilePath,	
				dataType: "xml",
				success: function(xml) {
					xmlDOMTree = xml;
					slidesReadyToRead();
				}
			});
}

function readSlideShowFromXML() {
	slideContent = new Array();	
	$(xmlDOMTree).find('slide').each(function(){
		var themecolor = $(this).find('theme').text() ;
		slideContent.push({title: ($(this).find('title').text().replace("/", "<p>") + "</p>"),
					    slideImageURL:  imagePath + '/slides/' + $(this).find('imageURL').text(),
						cachedImage: null,
						themeHEAD: (imagePath + "/" +themecolor + "_theme/content_" + themecolor + ".png"),											 
			            themeBACK: (imagePath + "/" +themecolor + "_theme/content_" + themecolor + "_background.png")});		
	});	
	//console.log(slideContent);
}

function loadImages() {
	for (i = 0; i < slideContent.length; i++) {
		var image = new Image();
		//console.log(slideContent[i].slideImageURL);
		$(image).load(function() {
								  $('#slideshowImage').append(this);
								  startSlideShow();
								 })
				.attr('src', slideContent[i].slideImageURL)
				.attr('id', i);
		
		var cssBackgroundImage1 = $('<img />').attr('src', slideContent[i].themeHEAD);
		var cssBackgroundImage2 = $('<img />').attr('src', slideContent[i].themeBACK);

	}	
}

function startSlideShow() {
	loadedSlides++;
	if (loadedSlides == slideContent.length) {
		// start the show
		$("#slideshowImage").css('background-image','none');
		$("#slideshowText").show();
		$("#slideshowImage").cycle({
			random:1,
			speed: 2000,
			timeout: 5000,
			before: onBefore
    	});
	}
}

function slidesReadyToRead() {
	readSlideShowFromXML();
	loadImages();
}
	
function onBefore(currSlideElement, nextSlideElement, options, forwardFlag) {
	//console.log(this);
	$('#content').css("background-image","url("+ slideContent[this.id].themeBACK + ")");
	$('#contentheader').css("background-image","url("+ slideContent[this.id].themeHEAD + ")");
	$('#slideshowText').html(slideContent[this.id].title);
}

		

