/* 
	ImageRotator
	@author: Eric Lindstrom @ Web Cube CMS (webcubecms.com)
	
	Methods:
	==========================================
	- stackImages()
	- stackTransition()
	- userControl()
	- getSlide(_number_)
	--	This is meant to pull multiple images and transition accordingly.
	- adjust()
	
	
	# ADD transSpeed and transDelay
*/

ImageRotator = function(feed, selector, args) {
	this.divID = selector;
        this.control_selector = args.control_selector ? args.control_selector : this.divID;
	this.feed = feed;
	// ARGS
	this.divHeight = args.height; 
	this.divMinWidth = (args.min_width) ? args.min_width : '100%'; 
	this.tranSpeed = args.speed;
	this.controls = args.controls;
	this.background_color = (args.background_color) ? args.background_color : '#FFFFFF';
	this.FULL_SCREEN = (args.fullscreen) ? args.fullscreen : 'true';  // MAKE BOOLEAN
        this.update_callback = (args.update_callback) ? args.update_callback : function(i) {}
	
	this.controlsID = (args.controls_id) ? args.controls_id : 'wc_user_controls';
	this.controlsPosition = (args.controlPosition) ? args.controlsPosition : 'bottomRight';
	this.controls_left_offset = (args.controls_left_offset) ? args.controls_left_offset : 0;
	// this.IMG_WIDTH = (args.img_width) ? args.img_width : 1500;

	this.currentImage = 0;
	var self = this;

	this.init = function() {
		$(this.divID).css({'background': this.background_color, 'height':this.divHeight, 'overflow':'hidden' });

		if (this.FULL_SCREEN == 'false') $(this.divID).css({'width':this.divMinWidth});

		this.stackImages();
        }

	this.stackImages = function() {
		zInd = this.feed.length;
		html = '<ul id="wc_ir_list" style="margin:0">';
		$(this.feed).each(function(k,v) {
			var window_width = (self.FULL_SCREEN == 'true') ? $(window).width() : self.divMinWidth;

			html += '<li id="wc_img'+ k +'" class="ir_img" order-num="'+ k +'" style="margin:0; text-align:center; position:absolute; width:'+ self.divMinWidth +'; height:'+ self.divHeight +'px; display:none; z-index:'+ zInd +'; background:url(/media/homeimages/'+ v.path +') no-repeat top center;">';

			if (v.link != '') html += '<a class="banner_click_area" style="float:left; position:absolute; width:'+ $(window).width() + 'px; height:'+ self.divHeight +'px; margin:0; display:block;" href="'+ v.link +'" ></a>';
			html += '</li>';
			zInd--;
		});
		html += '</ul>';
		$(this.divID).empty().append(html);

		//INIT IMAGE ROTATOR
		$('#wc_img0').addClass('wc_active_hp').css({'display':'block'});
		$(this.divID + ' ul', this.divID + ' li').css({'list-style-type':'none', 'list-style': 'none'});


		$(window).resize(function() { self.adjust(); });

		this.stackTransition();
		if (this.feed.length > 1 && this.controls != false) {
                    this.userControl();
                }

	}
    
	this.stackTransition = function() {
		loopImages = window.setInterval(function() {
			if (self.feed.length > 1) {
				(self.currentImage < self.feed.length-1) ? self.currentImage += 1 : self.currentImage = 0;
				self.getSlide(self.currentImage);
			}
		}, this.tranSpeed*1000);
        }

	this.userControl = function() {
		control_list = '<ul id="'+ this.controlsID +'">';
                var last = false;
		for(i=0; i<this.feed.length; i++) {
                        if (i == this.feed.length - 1) { 
			    control_list += '<li class="last" id="wc_c'+ i +'" alt="'+ i +'" ><a href="#">'+ (i+1) +'</a></li>';
                        } else { 
			    control_list += '<li id="wc_c'+ i +'" alt="'+ i +'" ><a href="#">'+ (i+1) +'</a></li>';
                        }
		}
		control_list += '</ul>';
		$(this.control_selector).append(control_list);
		
		// CLICK
		$('#'+ this.controlsID +' li a').click(function() {
			self.getSlide($(this).parent().attr('alt'));
			clearInterval(loopImages);
			return false;
		});

		// CSS
		/*$('#'+ this.controlsID).css({
			'z-index': (this.feed.length + 1), 
			'margin':(self.divHeight - 30) + 'px 0 0 0'
		});
                */

		$('#wc_c0').addClass('selected').append('<div class="arrow"></div>');
		this.adjust();
	}

        this.nextSlide = function() { 
            var current_slide = parseInt($('#'+this.controlsID+' li.selected').attr('alt'));
            var total_slides = this.feed.length -1;
            var next_slide = current_slide + 1;
	    clearInterval(loopImages);
            if (next_slide > total_slides) { 
                next_slide = 0;
            }
            this.getSlide(next_slide);
        }

        this.prevSlide = function() { 
            var current_slide = parseInt($('#'+this.controlsID+' li.selected').attr('alt'));
            var total_slides = this.feed.length;
            var prev_slide = current_slide - 1;
	    clearInterval(loopImages);
            if (prev_slide < 0) { 
                prev_slide = total_slides-1;
            }
            this.getSlide(prev_slide);
        }


	this.getSlide = function(newImage) {
		if (this.prevImage != newImage) {
			$('.wc_active_hp').animate({'opacity':0}, 800, function() {
				$(this).removeClass('wc_active_hp').css({'display':'none', 'z-index':self.feed.length-1});
			});
			$('#wc_img'+newImage).css({'display':'block', 'opacity':0}).animate({'opacity':1}, 801, function() {
				$(this).addClass('wc_active_hp').css({'z-index':self.feed.length});
			});
			
			$('#' + self.controlsID + ' li').removeClass('selected');
			$('.arrow').remove();
			$('#wc_c'+newImage).addClass('selected').append('<div class="arrow"></div>');
		} 
		this.prevImage = newImage;
                this.update_callback(newImage);
	}

	this.adjust = function() {

		var window_width = (this.FULL_SCREEN == 'true') ? $(window).width() : self.divMinWidth;

		$('.ir_img').css({'width':window_width});
		$('.banner_click_area').css({'width':window_width, marginLeft:0 })
		
		// TODO: Determine Top and Left position, not just Left
		switch(this.controlsPosition) {
			case 'topLeft':
				break;
			case 'topRight':
				break;
			case 'bottomLeft':
				break;
			default: // bottomRight
				controls_offset = (window_width/2) + ((self.divMinWidth/2) - $('#'+this.controlsID).width());
		}

/*
		$("#" + this.controlsID).css({marginLeft:controls_offset + self.controls_left_offset});
*/
	}
	// INITIALIZE
	this.init();
}

