// Constants  ==================================================================
//==============================================================================

//hard path to the php that returns folder contents
var pathToGetImages = 'http://loveoflindsay.com/wp-content/themes/LoveOfLindsay/po_getImages.php';

// Crazyness class definition ==================================================
//==============================================================================
Abstract.XMLHttpCatch = Class.create();  
Abstract.XMLHttpCatch.prototype = {  
	initialize: function(opts){},  

	receive: function(response, scope) {	
		scope.getImagesSuccess(response);  		
	}  
};  // end craziness

// Fader class definition ======================================================
//==============================================================================
Abstract.Fader = Class.create();
Abstract.Fader.prototype = {
	rotationPeriod: 10, // you could add this as an input

// Constructor -----------------------------------------------------------------
	initialize: function(e) {
		for (var prop in e) {
			this[prop] = e[prop];
		}
		this.XMLHttpCatch = new Abstract.XMLHttpCatch();
		
		//continer
		this.container = e;
		
		//get path
		var input = e.getElementsBySelector('input');
		input = input[0];
		this.path = input.value;		
		
		//get images
		this.getImages();
		
	}, // end constructor
  
// Shuffle ---------------------------------------------------------------------
	shuffle : function(a) {
		var i = a.length;
		if ( i == 0 ) return false;
		while ( --i ) {
			var j = Math.floor( Math.random() * ( i + 1 ) );
			var tempi = a[i];
			var tempj = a[j];
			a[i] = tempj;
			a[j] = tempi;
	}
	
	return a;  
	}, // end shuffle

// fadeImages -------------------------------------------------------------------
	fadeImages : function() {

		var width = this.container.getStyle('width');
		var height = this.container.getStyle('height');
		
		//find an appropriate background
		var sibs = this.container.getElementsBySelector('div');
		var takenBackground;
		//if its the first one skip the default background
		if(sibs.length == 0) {
			takenBackground = this.container.getStyle('backgroundImage');
		} else {
			takenBackground = sibs[sibs.length - 1].getStyle('backgroundImage');
		}

		//get a background image that isn't the same as the current one
		var backgroundImage;
		

		this.images = this.shuffle(this.images);
		if(takenBackground.endsWith(this.images[0] + ')')) {
			background = this.images[1];
		} else {
			background = this.images[0];
		}

				
		// create new div, set its properties and fade it in
		var div = Element.extend(document.createElement('div'));		
		this.container.appendChild(div);
		
		div.setStyle({
						width: width,
						height: height,
						backgroundImage: 'url(' + this.path + background + ')',
						position: 'absolute',
						zIndex: 1000,
						display: 'none'
					});
		
		Effect.Appear(div, { duration: 2.0, queue: 'end'});
		
		//alert(width + '::' + height + '::' + this.path + background);
		
		//delete stacked up divs
		if(sibs.length > 10) {
			sibs[0].remove();
		}
	}, // end fadeImages

// getImages -------------------------------------------------------------------
	getImages : function(path) {
		XMLHttpCatch = this.XMLHttpCatch;  
		var scope = this; 
		var safeDir = encodeURIComponent(this.path);		
		
		new Ajax.Request(pathToGetImages, {
			method: 'post',
			postBody: 'location=' + safeDir,
			onSuccess: function(transport) { XMLHttpCatch.receive(transport.responseText, scope); }
		});
	}, // end 

	getImagesSuccess : function(response) {
		this.images = response.split(',');	
	} // end getImages
}; // end Fader


//Genearl functions ============================================================
//==============================================================================
// setUpFaders -----------------------------------------------------------------
	function setUpFaders() {
		var containers = $$('.po_fader');
	
		var allFaders = new Array();
		containers.each(function(e) {
			var fader = new Abstract.Fader(e);
			allFaders.push(fader);
		});	

		allFaders.each(function(fader) {
			new PeriodicalExecuter(function(){fader.fadeImages()}, fader.rotationPeriod);		
			//new PeriodicalExecuter(function(){console.log('dude')}, 4);
		});
		

	} // setUpFaders

//Behaviours ===================================================================
//==============================================================================
Event.addBehavior({
  /*'#start:click' : function(e) {
	pe = new PeriodicalExecuter(fadeImages, rotationPeriod);
  },
  '#stop:click' : function(e) {
	pe.stop();
  } */ 
});

//OnReady ======================================================================
//==============================================================================
Event.onReady(function() {
	setUpFaders();
});