
(function(jQuery){
	
	jQuery.fn.banner = function(options) {

		/* Defaults */
		
		var defaults = {
			basePath: '/banner/',
			stylesheet: 'style.css', 
			config: 'config.js'
		};
				
		/* Globals */
		
		var options = jQuery.extend(defaults, options);
		var config = null;
		
		/*
		* Main Routine
		*/
		if (this.length > 1){
			this.each(function() { jQuery(this).banner(options) });
			return this;
		}
		
		/* Initialize */
		
		this.initialize = function() {
			obj = jQuery(this);
			loadStyleSheet();
			loadConfig();
			main(obj);
			return this;
		};
		
		/* Public Methods */
		
		this.changeAssignedEntityType = function(assignedEntityType,role_id) {
			if(options.assignedEntityType != assignedEntityType){
				startLoading();
				jQuery.ajax({
					'type' : 'get',
					'url' : '/assignment/admin/change-assigned-entity-type/old_assigned_entity_type/' + options.assignedEntityType + '/assigned_entity_id/' + options.assignedEntityId + '/new_assigned_entity_type/' +assignedEntityType + '/role_id/' + role_id,
					'data' : null,
					'success' : function(response){
						options.assignedEntityType = assignedEntityType;
						stopLoading();
					}
				});
			}
		}
		
		return this.initialize();	
		
		/* Private Methods */	
		
		function main(obj){
			obj.css('width',config.width+'px');
			obj.css('height',config.height+'px');
			obj.css('position','relative');
			obj.css('top','0px');
			obj.css('left','0px');
			obj.css('overflow','hidden');
			
			var z = config.slides.length;
			
			jQuery.each(config.slides,(function(i,slide){
				z--;
				var slideXhtml = jQuery('<div class="slide"></div>');
				slideXhtml.css('background-image','url('+options.basePath+'slides/'+slide.src+')');
				slideXhtml.css('background-repeat','no-repeat');
				slideXhtml.css('background-position','0px 0px');
				slideXhtml.css('background-color','transparent');								
				slideXhtml.css('width',config.width+'px');
				slideXhtml.css('height',config.height+'px');
				slideXhtml.css('position','absolute');
				slideXhtml.css('top','0px');
				slideXhtml.css('left','0px');
				jQuery.data(slideXhtml[0],'id',i);				
				obj.prepend(slideXhtml);
			}));
			
			var slideTimer = self.setInterval(function(){
				
				jQuerycurrent = obj.children('div.slide').eq(obj.children('div.slide').length-1);
				jQuerynext = obj.children('div.slide').eq(obj.children('div.slide').length-2);
				
				var id = jQuery.data(jQuerycurrent[0],'id');
				var easing = config.slides[id].easing;
				var speed = config.slides[id].speed;
								
				switch(config.slides[id].transition){
					case 'fade':
						fade(jQuerycurrent,jQuerynext,speed,jQuerynext,easing);
						break;
					case 'slideDown':
						slideVertical(jQuerycurrent,jQuerynext,speed,easing,1);
						break;
					case 'slideUp':
						slideVertical(jQuerycurrent,jQuerynext,speed,easing,-1);					
						break;
					case 'slideLeft':
						slideHorizontal(jQuerycurrent,jQuerynext,speed,easing,-1);					
						break;
					case 'slideRight':
						slideHorizontal(jQuerycurrent,jQuerynext,speed,easing,1);					
						break;
				}	
				
				
				}, (config.duration * 1000));
			
		}
		
		function fade(jQuerycurrent,jQuerynext,speed,easing){			
			jQuerycurrent.animate({opacity:'hide'},speed,easing,function(){
					jQuery(this).prependTo(obj);
					jQuery(this).show();
				}
			);	
		}
		
		function slideVertical(jQuerycurrent,jQuerynext,speed,easing,direction){
			var top = (parseInt(direction) * parseInt(config.height));
						
			jQuerycurrent.
					animate({'top':top+'px'},'slow',easing,function(){
					jQuery(this).css('top','0px').prependTo(obj);
			});
		}

		function slideHorizontal(current,next,speed,easing,direction){
			var left = (parseInt(direction) * parseInt(config.width));
						
			jQuerycurrent.
					animate({'left':left+'px'},'slow',easing,function(){
					jQuery(this).css('left','0px').prependTo(obj);
			});
		
		}		
		
		function loadStyleSheet(){
			var headID = document.getElementsByTagName("head")[0];         
			var cssNode = document.createElement('link');
			cssNode.type = 'text/css';
			cssNode.rel = 'stylesheet';
			cssNode.href = options.basePath + options.stylesheet;
			cssNode.media = 'screen';
			headID.appendChild(cssNode);
		}
		
		function loadConfig(){
			jQuery.ajax({
				async: false,
				dataType: "json",
				url: options.basePath + options.config,
				success: function(data){
					config = data;
				}
			});	
		}
					
	}
	
})(jQuery);