/**
 * @author Andrew
 */
if(typeof Effect == 'undefined')
  throw("lightwindow.js requires including script.aculo.us' effects.js library!");

var Slideshow = Class.create();
Slideshow.prototype = {
	
	Version: '1.0.0',
	CurrentSlide: 0,
	slideShowContainer: null,
	slideshowContent: '',
	
	slideshowContainer_Start : 	'<div id=\'divSlideShowContainer\'>' +
											'<div id=\'divSlideCollection\' >',
											
	slideshowContainer_End : 	'</div>' +
								 		'</div>',
										
	slideshowSlide_Start: 		'<div id=\'divSlide\' style=\'display: none; position: relative; \'>',
	
	slideshowSlide_End: 		'</div>',
	
	initialize : function(slideShowContainer) {

		this.slideShowContainer = $(slideShowContainer.id);
		this.slideShowContainer.setAttribute('style', 'float: left; position: relative; height: ' + slideShowContainer.height + '; width: ' + slideShowContainer.width);
		this.slideShowContainer.innerHTML = this.slideshowContainer_Start + this.slideshowSlide_Start + this.slideshowSlide_End +  this.slideshowContainer_End;

		// Implement if a slide will not be placed in the box at random by the server-side script
		this.loadSlides();
	},

	loadSlides : function(){

		var url = 'slideshow.xml';

		// Load the XML data into the slideshow processor...
		new Ajax.Request (url, {
			method: 'get',
			contentType: 'application/json',
			onSuccess: function(transport) {

				var response = transport.responseText;

				// Convert the XML data into JSON format
				xotree = new XML.ObjTree()
				var jsonSlideshow = xotree.parseXML(transport.responseText);
				
				// Check we are using the correct version data for the slideshow control
				if (convertVersionString(jsonSlideshow.slideshow.version) < convertVersionString(Slideshow.prototype.Version))
					throw('You are using the incorrect version of the Slideshow Control');

				var previousSlideItem = jsonSlideshow.slideshow.slides.slide[Slideshow.prototype.CurrentSlide - 1];
				
				if (previousSlideItem == null) {
					previousSlideItem = jsonSlideshow.slideshow.slides.slide[Slideshow.prototype.CurrentSlide];
				}

				// Perform the finishing effect on the previous slide...
				switch (previousSlideItem.effect.finish.type){
					case 'Effect.Appear':

						new Effect.Appear('divSlide', { // the id of the <DIV> containing the photos 
						      duration: .2, 
						      fps: 100, 
						      afterFinish: function() { 
		
								slideprocess(jsonSlideshow);
								
							}
						});
						break;
						
					case 'Effect.Fade':
						new Effect.Fade('divSlide', { // the id of the <DIV> containing the photos 
						      duration: .2, 
						      fps: 100, 
						      afterFinish: function() { 
		
								slideprocess(jsonSlideshow);
								
							}
						});
						break;
				}
			}
			
		});
		
		slideprocess = function(jsonSlideshow) {
			
			// ... process the new slide
			var objSlideItem = $('divSlide');
			var slideshowContent = '';

			var slideItem = jsonSlideshow.slideshow.slides.slide[Slideshow.prototype.CurrentSlide];

			if (slideItem != null) {
			
				for (var iElementLoop = 0; iElementLoop < slideItem.elements.element.length; iElementLoop++) {
					
					var slideElement = slideItem.elements.element[iElementLoop];

					switch (slideElement.type)
					{
						case '1':			// IMAGE ELEMENT
							slideshowContent += '<img ';
							slideshowContent += ' src="' + slideElement.content + '"';
							slideshowContent += ' alt="' + slideElement.name + '"';
							slideshowContent += ' title="' + slideElement.name + '"';
							slideshowContent += ' style="';
								slideshowContent += ' position: absolute;';
								slideshowContent += ' top: ' + slideElement.positioning.top + ' ;';
								slideshowContent += ' left: ' + slideElement.positioning.left + ' ;';
								slideshowContent += ' height: ' + slideElement.positioning.height + ' ;';
								slideshowContent += ' width: ' + slideElement.positioning.width + ' ;';
							slideshowContent += ' "';
							slideshowContent += ' />';
							break;
						
						case '2':			// TEXT AREA ELEMENT
							slideshowContent += '<div ';
							slideshowContent += ' style="';
								slideshowContent += ' position: absolute;';
								slideshowContent += ' top: ' + slideElement.positioning.top + ' ;';
								slideshowContent += ' bottom: ' + slideElement.positioning.bottom + ' ;';
								slideshowContent += ' left: ' + slideElement.positioning.left + ' ;';
								slideshowContent += ' height: ' + slideElement.positioning.height + ' ;';
								slideshowContent += ' width: ' + slideElement.positioning.width + ' ;';
								slideshowContent += ' padding: ' + slideElement.format.padding + ' ;';
								slideshowContent += ' margin: ' + slideElement.format.margin + ' ;';
								slideshowContent += ' font-family: ' + slideElement.format.foreground.font.family + ' ;';
								slideshowContent += ' font-size: ' + slideElement.format.foreground.font.size + ' !important;';
								slideshowContent += ' color: ' + slideElement.format.foreground.font.color + ' ;';
								slideshowContent += ' font-style: ' + slideElement.format.foreground.font.style + ' ;';
								slideshowContent += ' text-align: ' + slideElement.format.foreground.font.alignment + ' ;';
								
								if (slideElement.format.foreground.font.style != null) {
									if (slideElement.format.foreground.font.style.bold == 'true') 
										slideshowContent += ' font-weight: bold;';
								}
									
								slideshowContent += ' background-color: ' + slideElement.format.background.color + ' ;';

								if (slideElement.format.background.transparent != null && slideElement.format.background.transparent != 'false')
									slideshowContent += ' filter:alpha(opacity=80); -moz-opacity: 0.8; opacity: 0.8; ';
									
							slideshowContent += ' "';
							slideshowContent += ' >';
								slideshowContent += slideElement.content;								
							slideshowContent += '</div>';
							break;
					}
					
				}
			}
			
			objSlideItem.innerHTML = slideshowContent;

			// Perform the starting effect on the new slide
/*			new eval(slideItem.effect.start.type)('divSlide', {
				duration: 2,
				fps: 50
			});*/

			switch (slideItem.effect.start.type){
				case 'Effect.Appear':

					new Effect.Appear('divSlide', { // the id of the <DIV> containing the photos 
					      duration: .2, 
					      fps: 100
					});
					break;
					
				case 'Effect.Fade':
					new Effect.Fade('divSlide', { // the id of the <DIV> containing the photos 
					      duration: .2, 
					      fps: 100
					});
					break;
			}

			// Set the current slide variable
			if (Slideshow.prototype.CurrentSlide < jsonSlideshow.slideshow.slides.slide.length - 1)
				Slideshow.prototype.CurrentSlide++;
			else
				Slideshow.prototype.CurrentSlide = 0;
						
		}

		function convertVersionString(versionString){
	      var r = versionString.split('.');
	      return parseInt(r[0])*100000 + parseInt(r[1])*1000 + parseInt(r[2]);
	    }

	}
}
