/* Mini Slider */
/* Made by Ross Davidson - http://www.daft-thoughts.com */
/* Please leave these comments in if you reuse my code, even if its not that great... - thanks. */

function intval (mixed_var, base)
{	     
	var type = typeof( mixed_var );
     
	if (type === 'boolean') {        return (mixed_var) ? 1 : 0;
	} else if (type === 'string') {
	    tmp = parseInt(mixed_var, base || 10);
	    return (isNaN(tmp) || !isFinite(tmp)) ? 0 : tmp;
	} else if (type === 'number' && isFinite(mixed_var) ) {        
		return Math.floor(mixed_var);
	} else {
	    return 0;
	}
}

var setupSlider = function(id){               
    this.increment = $(id).width();
    this.direction = 1;
    this.intId = 0;
    this.maxSize = this.increment*($(id+' > ul > li').size()) - this.increment;
    this.maxNum = $(id+' > ul > li').size();
    this.through = true;
    this.switch_interval_val = 8000;
    this.fade_speed = 400;          
    this.id = id;
    this.autoSwitch = false;
    this.current_slide = 1;
    
    this.autoAnimateSlider = function(){
        if(this.maxNum>1)
        {
            var to = 0;
            
            if(this.direction == 1)
            {            
                to = this.current_slide+1;
            }
            else
            {
                to = this.current_slide-1;
            }
            
            if(to>0 && to<=this.maxNum)
            {
                
                this.animateSlider(to);
            }
            else
            {
                if(!this.through && this.direction == 1)
                {
                    this.direction = 0;
                    this.autoAnimateSlider();
                }
                else if(!this.through && this.direction == 0)
                {
                    this.direction = 1;
                    this.autoAnimateSlider();
                }
                else if(this.direction == 0)
                {                
                    this.animateSlider(this.maxNum);
                }
                else if(this.direction == 1)
                {                
                    this.animateSlider(1);
                }                               
            }             
        }
        return true;
    };
    
    this.updateNumberButs = function()
    {        
        $($(this.id+' > .but-cont').find('.position')).removeClass('current');
        $($(this.id+' > .but-cont').find('.position').get(this.current_slide-1)).addClass('current');
    };
    
    this.animateSlider = function(new_pos){
        new_pos-=1;
        
        var e = this;
        
        function test()
        {
            e.autoAnimateSlider();
        }
        
        if(this.autoSwitch)
        {
            clearInterval(this.intId);
            this.intId = setInterval(test, this.switch_interval_val);
        }
        
        var new_pos_x = -1*(new_pos*this.increment);
        
        if(new_pos_x>=(0-this.maxSize) && new_pos_x<=0)
        {
            var old_pos_x = -1*(this.current_slide*this.increment);
            
            $($(this.id+' > ul').find('li').get(this.current_slide-1)).find('.caption').hide();
            $(this.id+' .minnimize').hide();
            
            function fadeInCap()
            {                
                $(e.id+' .minnimize').fadeIn(e.fade_speed);                
            }
            
            var index = this.new_pos_x*-1/this.increment;
            e = this;
            $(this.id+' > ul').stop().animate({marginLeft: new_pos_x+'px'}, e.fade_speed, null, function(){                 
                $($(this).find('li').get(new_pos)).find('.caption').fadeIn(e.fade_speed);
                
                fadeInCap();
                e.current_slide = new_pos+1;
                e.updateNumberButs();                
            });
        }     
        
        return true;
    };
    
    this.init = function(){
        if(this.maxNum>1)
        {                          
            var e = this;        
            
            $(e.id+' .prev').click(function(){
                
                var to = e.current_slide--;
                if(to>=0)
                {
                    e.animateSlider(to);
                }
                
                return false;
            });
            
            $(e.id+' .next').click(function(){
                var to = e.current_slide++;
                if(to<=e.maxNum)
                {
                    e.animateSlider(to);
                }
                
                return false;
            });
            
            $(e.id+' .minnimize').click(function(){
                $(this).fadeOut(200);
                $(e.id+' .caption').fadeOut(200);
                return false;
            });
            
            $(e.id+' .position').click(function(){           
                /*Gets number in html of link and goes to that tab */
				/*var to = intval($(this).html());            
                
                if(to>=0 && to<=e.maxNum)
                {                    
                    e.animateSlider(to);               
                }*/
				
				/* Gets href link for tab position */
				var to = $(this).attr('href');
				if(to.substr(0, 5) == "#tab-")
				{
					to = to.substr(5,1);				
					e.animateSlider(to);
				}				
                
                return false;
            });
            
            function test()
            {
                e.autoAnimateSlider();
            }
            if(this.autoSwitch)
            {
                clearInterval(this.intId);
                this.intId = setInterval(test, this.switch_interval_val);
            }
        }
        return true;
    }
}
