/****
imageFlicker
by Matt Rose
support: MLWCreative.co.uk
****/
	
	var imageFlicker = {
		
		//#	Declare some global variables for this project to
		//#	save data too as a temporary store.
		s: [],
		d: [],
		c: 0,
		x: 1,
		t: null,
		
		//#	This is an empty event handle for onTick. This 
		//#	function will be triggered when the tick function
		//#	is fired. Call in the object as the first parameter
		//#	to get the information saved to the instance.
		onTick: function(o){},
		
		//#		- Function make
		//#	This function will prepare the object parsed to it
		//#	for insertion. Must bee called first!
		//#		i = Identity (Must start with # or .)
		//#		h = Height
		//#		l = Images Location
		//#		d = Delay
		//#		s = Speed of transaction.
		make: function(i,h,f,d,s){
			
			//#	Save the information.
			this.s['id']=i;
			this.s['height']=h;
			this.s['file']=((typeof f!='undefined')?(f):(''));
			this.s['delay']=d;
			this.s['speed']=s;
			
			//#	Prepare the container tag.
			$(i).css({'position':'relative','overflow':'hidden','height':h+'px'});
			
		},
		
		//#		- Function append
		//#	This function will add an image to the cycle. This
		//#	can be called at run time, but its safer to call
		//#	before the render function.
		//#		i = Image name and format.
		//#		h = HTTP link.
		append: function(i,h){
			this.c++;
			this.d[this.c]={ id:this.c, image:i, href:h, instance:null };
			
			//#	Prepare the image and insert.
			$(this.s['id']).append('<div id="image_'+ this.c +'"><a href="'+ h +'"><img src="'+ this.s['file']+i +'" /></a></div>');
			this.d[this.c].instance = $('#image_'+ this.c);
			this.d[this.c].instance.css({'position':'absolute','top':'0px','left':'0px'});
			
		},
		
		//#		- Function render
		//#	Initiates the timer and uses the information
		render: function(){
			for(c=1;c<=this.c;c++){ this.d[c].instance.css('z-index', ((this.c+1)-c)); }
			this.t = setTimeout(function(){ imageFlicker.tick(); }, this.s['delay']);
		},
		tick: function(){
			if(this.x<this.c){
				this.d[this.x].instance.fadeOut(this.s['speed']);
				this.x++;
			}else{
				this.d[1].instance.fadeIn(this.s['speed'], function(){ for(p=2;p<=imageFlicker.c;p++){ imageFlicker.d[p].instance.show(); } });
				this.x=1;
			} 
			this.onTick();
			this.t = setTimeout(function(){ imageFlicker.tick(); }, this.s['delay']);
		},
				
	};
	
