/** JavaScript Document: Nutisal Slider	
	author: neil.malgaonkar@gmail.com  **/
/* js template reference from http://starter.pixelgraphics.us/ (04 April , 2011 )*/	

(function($){
    $.slide = function(el, options){
        // To avoid scope issues, use 'base' instead of 'this'
        // to reference this class from internal events and functions.
        var base = this;
        
        // Access to jQuery and DOM versions of element
        base.$el = $(el);
        base.el = el;
        
        // Add a reverse reference to the DOM object
        base.$el.data("slide", base);
        base.options = $.extend({},$.slide.defaultOptions, options);
		var container = base.options.container , loader_img = base.options.loader_img , loader = base.options.loader , start = base.options.start ,  prev , totalLength , playInterval , pauseTimeout;
		
        base.init = function(){            			
			$(container , base.$el).css({
				'overflow' : 'hidden',
				'position' : 'relative'
			}); // apply css property to container
			
			$(container , base.$el).children().css({
					'display' : 'none',
					'position' : 'relative',
					'zIndex' : 0,
					'top' : 0
			});
			
			if(loader && $(container).find('img').length)
			{				
				$(container , base.$el).css({
					background : 'url('+loader_img+') 50% 50% no-repeat'
				});
				var img_obj = $(container , base.$el).find('img:eq('+start+')');
				
				var first_img = img_obj.attr('src');
				var imgParent = '';
				
				if(img_obj.parent().attr('class') != container)
				{					
					imageParent = $(container , base.$el).children(':eq('+start+')')[0].tagName.toLowerCase();
				}
				else
				{
					imageParent = $(container , base.$el).find(':eq('+start+')');
				}
				
				totalLength = $(container , base.$el).children().length;
				if(base.options.pagination)
				{
					base.pagination();
				}
				base.$el.find('img:eq('+start+')').attr('src' , first_img).load(function(){
					$(container , base.$el).css({
						'background' : 'none'
					});					
					base.log("Loading Image ");
					$(imageParent+':eq('+start+')' ,container).fadeIn(base.options.fadeSpeed , function(){
						$(container , base.$el).children().removeClass('active-slide');
						$(this).addClass('active-slide');					
						base.log("Initiating Timer ");
						base.timer();
					});
					
						
				});
				
			}
			$("."+base.options.paginationClass+" a").live('click' , function(){
				base.pause();
				//position = $(this).attr('href').match('[^#/]+$');
				position = $(this).attr('id').substring(14);
				start = parseInt(position[0] , 10);
				base.animate('paginate');
			});
			
			
			
			// Put your initialization code here
        };
		
		base.pagination = function(){
			base.$el.append('<ul class="'+base.options.paginationClass+'"></ul>');
			var count =0;
			
			$(container , base.$el).children().each(function(){
				cls = (count == start) ? 'current' : '';
				//$("."+base.options.paginationClass , base.$el).append("<li><a href='#"+count+"' class='"+cls+"'></a></li>");
				$("."+base.options.paginationClass , base.$el).append("<li><a href='javascript:void(0)' id='paginate-link-"+count+"' class='"+cls+"'></a></li>");
				count++;
			})
			
		}
		
		base.log = function(message){
			if(!$.browser.msie)
			{
				//console.log(message);
			}
		}
		
		base.animate = function(type){
			
			switch(type){
				case 'next':					
					next = start + 1;					
					prev = start;
					
					if(next > (totalLength -1))
					{
						next = 0;				
						start = 0;
					}
					else
					{
						start = next;
					}
					
					$("."+base.options.paginationClass).children(':eq('+prev+')').children('a').removeClass('current');
					$("."+base.options.paginationClass).children(':eq('+next+')').children('a').addClass('current');
					
					$(container , base.$el).children(':eq('+prev+')').fadeOut(base.options.fadeSpeed , function(){
						$(container , base.$el).children(':eq('+next+')').fadeIn(base.options.fadeSpeed , function(){
							$(container , base.$el).children().removeClass('active-slide');
							$(this).addClass('active-slide');
						})
					});
					//base.pause();
				break;
				case 'paginate':
					next = start;
					//var position = $("."+base.options.paginationClass+" li a.current").attr('href').match('[^#/]+$');
					var position = $("."+base.options.paginationClass+" li a.current").attr('id').substring(14);
					prev = parseInt(position[0] , 10);
					$("."+base.options.paginationClass).children(':eq('+prev+')').children('a').removeClass('current');
					
					$("."+base.options.paginationClass).children(':eq('+next+')').children('a').addClass('current');
					$(container , base.$el).children(':eq('+prev+')').fadeOut(base.options.fadeSpeed , function(){
						$(container , base.$el).children(':eq('+next+')').fadeIn(base.options.fadeSpeed , function(){
							$(container , base.$el).children().removeClass('active-slide');
							$(this).addClass('active-slide');
						});
					});
				break;					
			}
		}
		
		base.timer = function(){
			
			if(base.options.autoPlay)
			{
				playInterval = setInterval(function(){
					base.log("In log "); 
					base.animate('next');
				} , parseInt(base.options.play));
				base.$el.data('interval' , playInterval)
			}
		}
		
		base.pause = function(){
			// first clear interval
			
			clearInterval(base.$el.data('interval')); 				
			
			clearTimeout(base.$el.data('pause'));
			
			
			pauseTimeout = setTimeout(function(){
				clearTimeout(base.$el.data('pause'));
				playInterval = setInterval(function(){
					base.animate('next')
				} , base.options.play);
				base.$el.data('interval' , playInterval);
				
			} , base.options.play);
			base.$el.data('pause' , pauseTimeout);
		}
        
        // Sample Function, Uncomment to use
        // base.functionName = function(paramaters){
        // 
        // };
        
        // Run initializer
        base.init();
    };
    
    $.slide.defaultOptions = {
		container: '.slide-container',
		loader: true,
		loader_img: 'images/system/ajax-loader.gif',
		start : 0,
		fadeSpeed : 1000,
		autoPlay : true,
		pagination: true,
		paginationClass : 'pagination',
		play : 7000
    };
    
    $.fn.slide = function(options){
        return this.each(function(){				
            (new $.slide(this, options));			
        });
    };
    
    // This function breaks the chain, but returns
    // the slide if it has been attached to the object.
    $.fn.getslide = function(){
        this.data("slide");
    };
    
})(jQuery);
