//------------------------------------------------------------------------------
(function(document, window, undefined){

	function kwm_img_rotator(){
		var self = this;
		self.images = [];
		self.timer = false;
		self.total_loaded = 0;
		self.status = '';

		//TO MAKE THE IMAGE ROTATOR ROTATE FASTER DESCREASE THE SPEED
		//TO MAKE THE IMAGE ROTATOR ROTATE SLOWER INCREASE THE SPEED
		self.speed = 20;

		//TO INCREASE/DESCREASE THE STEPS OF OPACITY EACH FRAME JUMPS WHEN
		//AMNIMATING, ADJUST STEP ACCORDINGLY
		self.step = 5;

		//TO INCREASE/DECREASE THE AMOUNT OF TIME BETWEEN IMAGES, ADJUST THE
		//TIME_BTWN_IMAGES ACCORDINGLY (TIME IN MS)
		self.time_btwn_images = 2000;

		self.add = function(){
			if($('kwm_img_script').parentNode){
				var p = $('kwm_img_script').parentNode;
				var f = p.firstChild;
			}
			else{
				var p = document.body;
				var f = p.firstChild;
			}

			var count = 0;

			while(f.nextSibling && f.nextSibling !== $('kwm_img_script')){
				count++;
				f = f.nextSibling;
			}

			p.removeChild($('kwm_img_script'));

			var elems = p.getElementsByTagName('*');
			var elem = null;
			for(i = 0; i <= count; i++){
				elem = elems[i];
			}

			//create the image rotator
			create_image_rotator(arguments);

			if(elem === undefined){
				p.appendChild(self.ir);
			}
			else{
				p.insertBefore(self.ir, elem);
			}

			//now start the checker for when all the images have loaded
			setTimeout(function(){kwm_image_rotator.check_images_progress();}, 1);
		}

		var create_image_rotator = function(images){

			//setup the step, time_btwn_frames and speed
			var settings = images[0];
			self.step = images[0].step ? images[0].step : 5;
			self.time_btwn_images = images[0].time_btwn_images ? images[0].time_btwn_images : 2000;
			self.speed = images[0].speed ? images[0].speed : 20;

			//create the element for our image_rotator
			self.ir = document.createElement('div');
			self.ir.id = 'kwm_image_rotator';

			//grab the loading bar
			self.loader = images[1];

			//grab the height and width
			var css = images[2];

			//setup the css for this image_rotator
			self.ir.style.cssText = css;
			self.ir.style.overflow = 'hidden';
			self.ir.style.position = 'relative';

			//create the image loading bar
			var l = document.createElement('img');
			l.style.cssText = 'position: absolute; width: 36px; height: 36px; top: 50%; left: 50%; margin-left: -18px; margin-top: -18px;';
			l.id = 'kwm_image_rotator_loading';
			l.src = self.loader;
			self.ir.appendChild(l);

			for(i = 0, len = images[3].length; i < len; i++){
				var img = document.createElement('img');
				img.onload = function(){
					kwm_image_rotator.total_loaded++;
				}
				img.src = images[3][i];
				img.style.cssText = 'position: absolute; top: 0; left: 0; cursor: default; z-index: 0;';
				img.style.height = parseInt(self.ir.style.height) + 'px';
				img.style.width = parseInt(self.ir.style.width) + 'px';
				img.showing = false;
				set_opacity(0, img);
				img.opacity = 0;

				/*if(images[4][i] !== undefined && images[4][i] !== ''){
					var url = images[4][i];
					img.url = url;
					add_event('click', img, function(){
						window.open(this.url);
					});
					delete url;
				}*/

				self.images.push(img);

				self.ir.appendChild(img);
				delete img;
			}

			self.current_image = 0;
			self.images[self.current_image].style.zIndex = 1;
		}

		var add_event = function(evt, obj, func){
			if(obj.addEventListener){
				obj.addEventListener(evt, func, false);
			}
			else if(obj.attachEvent){
				obj.attachEvent('on' + evt, func);
			}
		}

		self.check_images_progress = function(){
			if(self.total_loaded !== self.images.length){
				setTimeout(function(){kwm_image_rotator.check_images_progress();}, 1);
			}
			else{
				$('kwm_image_rotator_loading').style.display = 'none';

				//begin the animations
				self.animate();
			}
		}

		self.animate = function(){

			var i = self.images[self.current_image];

			if(!i.showing){

				//get the opacity of the current image
				var o = i.opacity;

				//check to see if the current image is all the way showing

				if(o < 100){
					//setup the status
					self.status = 'animating';

					//the image is less than 100% and is not showing, increment
					//the images opacity by the step
					set_opacity(o + self.step, i);

					//now increae the opacity on the img element
					i.opacity = o + self.step;

					//now fire the timer again
					clearInterval(self.timer);
					self.timer = setInterval(function(){kwm_image_rotator.animate();}, self.speed);
				}
				else if(o === 100){
					//setup the status
					self.status = 'waiting';

					i.style.zIndex = 1;
					i.showing = true;

					clearInterval(self.timer);
					self.timer = setInterval(function(){kwm_image_rotator.animate();}, self.time_btwn_images);
				}
			}
			else if(i.showing){

				//get the next image to show
				var n = get_next_image(self.current_image);
				//console.log(n);

				//get the opacity of the next image we are fading in
				var o = i.opacity;
				var no = n.opacity;

				//now check the opacity of the new image to see if it has faded
				//all the way in or not
				if(no < 100){
					//setup the status
					self.status = 'animating';

					//the new image is not showing, increment the images opacity
					//the step
					set_opacity(no + self.step, n);

					//decrease the current image by the step too
					set_opacity(o - self.step, i);

					//now change both images opacity property to reflect the new
					// values
					i.opacity = o - self.step;
					n.opacity = no + self.step;

					i.style.zIndex = 0;
					n.style.zIndex = 1;

					//now fire the timer again
					clearInterval(self.timer);
					self.timer = setInterval(function(){kwm_image_rotator.animate();}, self.speed);
				}
				else if(no === 100){
					//setup the status
					self.status = 'waiting';

					//the new image has completely faded in, change the values of
					//the showing variables for both elements
					i.showing = false;
					n.showing = true;

					//increment the current_image variable
					if(self.current_image === self.images.length - 1){
						self.current_image = 0;
					}
					else{
						self.current_image++;
					}

					//now continue the timer again
					clearInterval(self.timer);
					self.timer = setInterval(function(){kwm_image_rotator.animate();}, self.time_btwn_images);
				}
			}
			
		}

		var get_next_image = function(current){
			if(current === self.images.length - 1){
				return self.images[0];
			}
			else{
				return self.images[current + 1];
			} 
		}

		var set_opacity = function(opacity, obj){
			obj.style.opacity = opacity / 100;
			obj.style.filter = 'alpha(opacity=' + opacity + ')';
		}

		var $ = function(div){
			if(document.getElementById(div)){
				return document.getElementById(div);
			}
			else{
				return false;
			}
		}

		// UTILITY FUNCTIONS FOR PAUSE, PLAY, ETC... //

		self.pause = function(){
			clearInterval(self.timer);
			self.timer = false;
		}
		self.resume = function(){
			if(self.timer){
				return;
			}
			switch(self.status){
				case 'animating':
					self.timer = setInterval(function(){kwm_image_rotator.animate();}, self.speed);
					break;
				case 'waiting':
					self.timer = setInterval(function(){kwm_image_rotator.animate();}, self.time_btwn_images);
					break;
				default:
					return;
					break;
			}
		}
		self.previous = function(){

			var img = 0;

			if(self.current_image === 0){
				img = self.images.length - 1;
			}
			else{
				img = self.current_image - 1;
			}

			self.goto(img + 1);

		}
		self.forward = function(){

			var img = 0;

			if(self.current_image === self.images.length - 1){
				img = 0;
			}
			else{
				img = self.current_image + 1;
			}

			self.goto(img + 1);
		}
		self.goto = function(img){

			if(self.images[img - 1] === undefined){
				return;
			}

			self.pause();

			reset_images();

			self.current_image = img - 1;

			set_opacity(100, self.images[self.current_image]);
			self.images[self.current_image].showing = true;
			self.images[self.current_image].opacity = 100;

			self.status = 'waiting';
			self.resume();
		}

		var reset_images = function(){
			for(i = 0, len = self.images.length; i < len; i++){
				var img = self.images[i];
				img.opacity = 0;
				set_opacity(0, img);
				img.showing = false;
				img.style.zIndex = 0;
			}
		}

		var on_resize = function(height, width){
			//loop through all images and change the height and width to the new
			//height and width
			var img;

			for(i = 0, len = self.images.length; i < len; i++){
				img = self.images[i];
				img.style.height = height + 'px';
				img.style.width = width + 'px';
			}
		}

		self.resize = function(height, width){
			if(typeof height !== 'number' || typeof width !== 'number'){
				return false;
			}

			self.ir.style.height = height + 'px';
			self.ir.style.width = width + 'px';

			//fire our on resize event
			on_resize(height, width);
		}
	}

	//expose it to the page
	window.kwm_image_rotator = new kwm_img_rotator();
}
)(document, window);
//------------------------------------------------------------------------------
