(function(jQuery) {
    jQuery.fn.govrotator = function(o) {
        return this.each(function() {
            new $vb(this, o);
        });
    };

    // Default configuration properties.
    var defaults = {
        autorotate: false, 
		showarrows: true,
		timeoffset: 5000, 
        hideimg: 0, 
		hidetext: 0,
		buttonNextHTML: '<div class="gov_rotator-next"></div>',
        buttonPrevHTML: '<div class="gov_rotator-prev"></div>',
		width:300, 
		height:200,
		arrowsPositionTop: 0.5,
		randomPlay: false
    };

	jQuery.govrotator = function(e, o) {
		this.options    = jQuery.extend({}, defaults, o || {});
		this.items	 		= null;
		this.itemsNum	 	= null;
		this.selectedItem	= null;
        this.nextItem  		= null;
        this.prevItem  		= null;
		this.timer 			= null;
		this.buttonNext		= null;
        this.buttonPrev		= null;
		this.container 		= jQuery(e);
		this.init();
		
    };

    var $vb = jQuery.govrotator;
    $vb.fn = $vb.prototype = {
        govrotator: '1.0.0'
    };

    $vb.fn.extend = $vb.extend = jQuery.extend;

    $vb.fn.extend({
	    init: function() {
			
			if(this.options.randomPlay){jQuery(this.container).reorder();}
			jQuery(this.container).css('display','block');
			this.items = jQuery(this.container).children('li');
			this.itemsNum = this.items.length;
			
			this.containerWidth = this.options.width;
			this.containerHeight = this.options.height;
			
			jQuery(this.container).width(this.containerWidth).height(this.containerHeight);
			
			this.selectedItem = 0;
			this.nextItem = 1;
			this.prevItem = this.itemsNum-1;
			this.zindex = 0;

			if(this.itemsNum>1){
				var self = this;
				for (var i = 0; i < this.itemsNum; i++) {
					jQuery(this.items[i]).css('z-index', (this.itemsNum-i)).css('position', 'absolute').css('overflow', 'hidden').width(this.containerWidth).height(this.containerHeight);
					jQuery('.rot_cont',jQuery(this.items[i])).height(this.containerHeight);
					if(i>0){jQuery(this.items[i]).children().hide()}
					else{this.zindex=jQuery(this.items[i]).css('z-index')};
					if(this.options.hideimg)(jQuery('.rot_img',jQuery(this.items[i])).remove());
					if(this.options.hidetext)(jQuery('.rot_cont',jQuery(this.items[i])).remove());
				};
				
				this.buttonNext	= jQuery(this.options.buttonNextHTML).insertBefore(this.container);
				this.buttonPrev	= jQuery(this.options.buttonPrevHTML).insertBefore(this.container);
	
				buttonNextleftOffset = this.containerWidth-this.buttonNext.innerWidth();
				//Chrome safari fix
				if(buttonNextleftOffset>0){
					this.buttonPrev.css('z-index', 1000).css('left', 0).css('top', this.containerHeight*this.options.arrowsPositionTop-this.buttonNext.innerHeight()*this.options.arrowsPositionTop);
					this.buttonNext.css('z-index', 1000).css('left', buttonNextleftOffset).css('top', this.containerHeight*this.options.arrowsPositionTop-this.buttonNext.innerHeight()*this.options.arrowsPositionTop);
				}else{
					this.buttonPrev.css('z-index', 1000).css('position', 'absolute').css('left', 0).css('top', this.containerHeight*this.options.arrowsPositionTop-this.buttonNext.innerHeight()*this.options.arrowsPositionTop);
					this.buttonNext.css('z-index', 1000).css('right', 0).css('top', this.containerHeight*this.options.arrowsPositionTop-this.buttonNext.innerHeight()*this.options.arrowsPositionTop);
				}
				
				this.buttonNext.click(function() {self.nextClick(self);});
				this.buttonPrev.click(function() {self.prevClick(self);});
				
				if(!this.options.showarrows){	
					this.buttonNext.hide();
					this.buttonPrev.hide();
				}
				
				jQuery(this.container).parent().mouseover(function(){
					if(self.options.autorotate){
						self.stopautorotate();
					}
					if(!self.options.showarrows){	
						self.buttonNext.show();
						self.buttonPrev.show();
					}
					//self.zindex = jQuery(self.items[self.selectedItem]).css('z-index');
					jQuery(self.items[self.selectedItem]).css('z-index', 999);
				});
				jQuery(this.container).parent().mouseout(function(){
					if(self.options.autorotate){
						self.startautorotate();
					}
					if(!self.options.showarrows){	
						self.buttonNext.hide();
						self.buttonPrev.hide();
					}
					jQuery(self.items[self.selectedItem]).css('z-index', self.zindex);
				});
							
				this.setup();
			}
        },
		
		setup: function() {
			var self = this;
			if(this.options.autorotate && this.options.timeoffset>0){
				this.timer = setTimeout(function() { self.next(); }, this.options.timeoffset);
			}
        },
		
		next: function() {
//alert(jQuery(this.items[this.prevItem]).css('z-index')+','+jQuery(this.items[this.selectedItem]).css('z-index')+','+jQuery(this.items[this.nextItem]).css('z-index'));
			var self = this;
			jQuery('.rot_img',jQuery(this.items[this.selectedItem])).fadeOut(1000);
			jQuery('.rot_cont',jQuery(this.items[this.selectedItem])).hide();
			jQuery(this.items[this.selectedItem]).css('z-index', this.zindex);
			
			this.prevItem = this.selectedItem;
			jQuery('.rot_img',jQuery(this.items[this.nextItem])).fadeIn(1500);
			jQuery('.rot_cont',jQuery(this.items[this.nextItem])).show();
			this.zindex = jQuery(this.items[this.nextItem]).css('z-index');
			jQuery(this.items[this.nextItem]).css('z-index', 999);
			this.selectedItem = this.nextItem++;
			
			if(this.nextItem==this.itemsNum){
				this.nextItem = 0;
			}
			
			if (this.timer != null){
				this.timer = setTimeout(function() { self.next(); }, this.options.timeoffset);
			}
        },
		
		prev: function() {
			var self = this;
			jQuery('.rot_img',jQuery(this.items[this.selectedItem])).fadeOut(1000);
			jQuery('.rot_cont',jQuery(this.items[this.selectedItem])).hide();
			jQuery(this.items[this.selectedItem]).css('z-index', this.zindex);
			
			this.nextItem = this.selectedItem;
			jQuery('.rot_img',jQuery(this.items[this.prevItem])).fadeIn(1500);
			jQuery('.rot_cont',jQuery(this.items[this.prevItem])).show();
			this.zindex = jQuery(this.items[this.prevItem]).css('z-index');
			jQuery(this.items[this.prevItem]).css('z-index', 999);
			this.selectedItem = this.prevItem--;
			
			if(this.prevItem<0){
				this.prevItem = this.itemsNum-1;
			}
        },
		nextClick: function() {
			this.options.autorotate=false;
			this.stopautorotate();
			this.next();
        },
		prevClick: function() {
			this.options.autorotate=false;
			this.stopautorotate();
			this.prev();
        },
		startautorotate: function(s) {
            if (this.options.autorotate && this.timer != null)
                return;
				
            var self = this;
            this.timer = setTimeout(function() { self.next(); }, this.options.timeoffset);
        },
		
		stopautorotate: function() {
			if (this.timer == null)
                return;

            clearTimeout(this.timer);
            this.timer = null;
        }
	});
	
})(jQuery);

(function(jQuery){
	jQuery.fn.reorder = function() {
	  function randOrd() { return(Math.round(Math.random())-0.5); }
	  return(jQuery(this).each(function() {
	    var $this = jQuery(this);
	    var $children = $this.children();
	    var childCount = $children.length;
	    if (childCount > 1) {
	      $children.remove();
	      var indices = new Array();
	      for (i=0;i<childCount;i++) { indices[indices.length] = i; }
	      indices = indices.sort(randOrd);
	      jQuery.each(indices,function(j,k) { $this.append($children.eq(k)); });
	    }
	  }));
	}
})(jQuery);
