function SlideShow() {
    this.container = null;
    this.controls = null;
    this.thumbs_container = null;
    this.thumbs = [];
    this.slides = null;
    this.current_slide = null;
    this.cycle = false;
}

    SlideShow.prototype.init = function(container, controls) {
        this.container = jQuery(container);
        this.controls = jQuery(controls);
        this.slides = jQuery('#'+this.container.attr('id')+' .slide');
        this.current_slide = 0;
        this.total_slides = this.slides.length;
        
    	var GS = this;
    	jQuery('#'+this.container.attr('id')+' .control-next, #'+this.controls.attr('id')+' .control-next').click(function() {
    	    GS.eventScrollNext();
    	});
    	jQuery('#'+this.container.attr('id')+' .control-prev, #'+this.controls.attr('id')+' .control-prev').click(function() {
    	    GS.eventScrollPrev();
    	});        
    	
        this.thumbs_container = (typeof arguments[2] == 'undefined') ? null : jQuery(arguments[2]);
        if (this.thumbs_container) {
        	this.thumbs = jQuery('#' + this.thumbs_container.attr('id')+' .slide-preview');
        	this.thumbs.css({cursor: "pointer"});
        	this.thumbs.click(function(e) {
        		GS.eventScrollThis(e);
        	});
        }
        
        
        this.updateControls();
    }
    
    SlideShow.prototype.hide = function(object) {
    	jQuery(object).fadeOut('fast', function() {
	   		jQuery(object).removeClass('current');
    		jQuery(object).addClass('hidden');
    		jQuery(object).css('display', '');
    	});
    }
    
    SlideShow.prototype.show = function(object) {
   		jQuery(object).css('display', 'none');
   		jQuery(object).removeClass('hidden');
    	jQuery(object).fadeIn('fast', function() {
	   		jQuery(object).css('display', '');
    		jQuery(object).addClass('current');
    	});
    }
    
    SlideShow.prototype.disableControl = function(object) {
    	jQuery(object).addClass('inactive');
    }
    
    SlideShow.prototype.enableControl = function(object) {
    	jQuery(object).removeClass('inactive');
    }
        
    SlideShow.prototype.updateControls = function() {
        var n = this.current_slide+1;
        var total = this.total_slides;
        
        jQuery('#'+this.controls.attr('id')+' .slide-number').each(function(el) {
            el.update(n + '/' + total);
        });
        
        if (!this.cycle) {
	        var next_buttons = jQuery('#'+this.controls.attr('id')+' .control-next');
	        var prev_buttons = jQuery('#'+this.controls.attr('id')+' .control-prev');
	        var restart_buttons = jQuery('#'+this.controls.attr('id')+' .control-restart');
        
	        var GS = this;
	        if (this.isLastSlide()) {
	            next_buttons.each(function() {GS.disableControl(this)});
	            restart_buttons.each(function() {GS.enableControl(this)});
	        }
	        else {
	            next_buttons.each(function() {GS.enableControl(this)});
	            restart_buttons.each(function() {GS.disableControl(this)});
	        }
        
	        if (this.isFirstSlide()) {
	            prev_buttons.each(function() {GS.disableControl(this)});
	        }
	        else {
	            prev_buttons.each(function() {GS.enableControl(this)});
	        }
        }
        
        jQuery('.slide-preview', this.thumbs_container).removeClass("current");
        
        jQuery('.slide-preview:nth-child(' + n + ')', this.thumbs_container).addClass("current");
        
        return true;
    }
    
    SlideShow.prototype.eventScrollNext = function(e) {
    	this.scrollNext();
    }
    
    SlideShow.prototype.eventScrollPrev = function(e) {
    	this.scrollPrev();
    }
    
    SlideShow.prototype.eventScrollThis = function(e) {
    	this.scrollTo( jQuery(e.target).attr("forId"), jQuery(e.target).attr("id") );
    }
    
    SlideShow.prototype.scrollNext = function() {
        if (!this.isLastSlide() || this.cycle) {
        	var GS = this;
	    	jQuery(GS.slides[GS.current_slide]).fadeOut('fast', function() {
		   		jQuery(GS.slides[GS.current_slide]).removeClass('current');
	    		jQuery(GS.slides[GS.current_slide]).addClass('hidden');
	    		jQuery(GS.slides[GS.current_slide]).css('display', '');
	    		
	    		if (GS.isLastSlide() && GS.cycle) {
	    			GS.current_slide = 0;
	    		}
	    		else {
		            GS.current_slide++;
	    		}
	    		GS.updateControls();
	    		
		   		jQuery(GS.slides[GS.current_slide]).css('display', 'none');
		   		jQuery(GS.slides[GS.current_slide]).removeClass('hidden');
		    	jQuery(GS.slides[GS.current_slide]).fadeIn('fast', function() {
			   		jQuery(GS.slides[GS.current_slide]).css('display', '');
		    		jQuery(GS.slides[GS.current_slide]).addClass('current');
		    	});
	    	});
            
        }
    }
    
    SlideShow.prototype.scrollPrev = function() {
        if (!this.isFirstSlide() || this.cycle) {
        	var GS = this;
	    	jQuery(GS.slides[GS.current_slide]).fadeOut('fast', function() {
		   		jQuery(GS.slides[GS.current_slide]).removeClass('current');
	    		jQuery(GS.slides[GS.current_slide]).addClass('hidden');
	    		jQuery(GS.slides[GS.current_slide]).css('display', '');
	    		
	    		if (GS.isFirstSlide() && GS.cycle) {
	    			GS.current_slide = GS.total_slides-1;
	    		}
	    		else {
		            GS.current_slide--
	    		}
	            GS.updateControls();
	    		
		   		jQuery(GS.slides[GS.current_slide]).css('display', 'none');
		   		jQuery(GS.slides[GS.current_slide]).removeClass('hidden');
		    	jQuery(GS.slides[GS.current_slide]).fadeIn('fast', function() {
			   		jQuery(GS.slides[GS.current_slide]).css('display', '');
		    		jQuery(GS.slides[GS.current_slide]).addClass('current');
		    	});
		    	
	    	});
        }
    }
    
	SlideShow.prototype.scrollTo = function(img_id, thumb_id) {
		var GS = this;
		if (jQuery(GS.slides[GS.current_slide]).attr("id") != img_id) {
	    	jQuery(GS.slides[GS.current_slide]).fadeOut('fast', function() {
	    		for (var i=0; i<GS.slides.length; i++) {
	    			if (jQuery(GS.slides[i]).attr("id") == img_id) {
				   		jQuery(GS.slides[GS.current_slide]).removeClass('current');
			    		jQuery(GS.slides[GS.current_slide]).addClass('hidden');
			    		jQuery(GS.slides[GS.current_slide]).css('display', '');
			    		
				   		jQuery(GS.slides[i]).css('display', 'none');
				   		jQuery(GS.slides[i]).removeClass('hidden');
				    	jQuery(GS.slides[i]).fadeIn('fast', function() {
					   		jQuery(GS.slides[i]).css('display', '');
				    		jQuery(GS.slides[i]).addClass('current');
				    	});
				    	
			    		GS.current_slide = i;
			    		GS.updateControls();
	    			}
	    		}
	    	});
		}
    }

    
    SlideShow.prototype.firstSlide = function() {
        this.hide(this.slides[this.current_slide]);
        this.current_slide=0;
        this.show(this.slides[this.current_slide]);
            
        this.updateControls();
    }
    
    SlideShow.prototype.isLastSlide = function() {
        return (this.slides.length==(this.current_slide+1))?true:false;
    }
    
    SlideShow.prototype.isFirstSlide = function() {
        return (this.current_slide==0)?true:false;
    }




function ScrollShow() {
    this.container = null;
    this.slide_container = null;
    this.control_left = null;
    this.control_right = null;
    this.scroll_shift = 50;
    this.scroll_time = 10;
    this.scroll_offset = 0;
    this.maximum_scroll_offset = null;
    
    this.scrolling_flag = false;
}

    ScrollShow.prototype.init = function(container, slide_container) {
        this.container = jQuery('#'+container);
        this.slide_container = jQuery('#'+slide_container);
		this.createControls(this.container);
		
		this.scroll_offset = 0;
		this.maximum_scroll_offset = this.getMaximumScrollOffset();
    }

    ScrollShow.prototype.createControls = function(container) {
    	jQuery(container).append("<div id='brand-selector-control-left'></div><div id='brand-selector-control-right'></div>");
    	this.control_right = jQuery('#brand-selector-control-right')[0];
    	this.enableControl(this.control_right);
    	this.control_left = jQuery('#brand-selector-control-left')[0];
    	this.disableControl(this.control_left);
    	

    	var GS = this;
    	jQuery(this.control_right).mousedown(function() {
    		GS.scrolling_flag = true;
    	    GS.eventScrollRight();
    	}).mouseup(function() {
    		GS.scrolling_flag = false;
    	});
    	jQuery(this.control_left).mousedown(function() {
    		GS.scrolling_flag = true;
    	    GS.eventScrollLeft();
    	}).mouseup(function() {
    		GS.scrolling_flag = false;
    	});
    }

    ScrollShow.prototype.getMaximumScrollOffset = function() {
        var last_slide = jQuery("#"+jQuery(this.slide_container).attr("id")+" span.brand-slide:last");
        var pos = last_slide.position();
        var max_offset = pos.left +last_slide.outerWidth() - this.container.width();
        return max_offset;
    }
    
    ScrollShow.prototype.disableControl = function(object) {
    	jQuery(object).addClass('inactive');
    }
    
    ScrollShow.prototype.enableControl = function(object) {
    	jQuery(object).removeClass('inactive');
    }
        
    ScrollShow.prototype.updateControls = function() {
        var GS = this;
        if (this.scroll_offset==this.maximum_scroll_offset) {
            this.disableControl(this.control_right);
        }
        else {
            this.enableControl(this.control_right);
        }
        
        if (this.scroll_offset==0) {
            this.disableControl(this.control_left);
        }
        else {
            this.enableControl(this.control_left);
        }
    }
    
    ScrollShow.prototype.eventScrollRight = function(e) {
    	this.scrollRight();
    }
    
    ScrollShow.prototype.eventScrollLeft = function(e) {
    	this.scrollLeft();
    }
    
    ScrollShow.prototype.scrollRight = function() {
    	var GS = this;
        if (this.scroll_offset < this.maximum_scroll_offset) {
            var shift = Math.min(this.maximum_scroll_offset - this.scroll_offset, this.scroll_shift);
        	jQuery(this.slide_container).animate(
        		{marginLeft: '-='+shift}, 
        		this.scroll_time,
        		function() {
		        	GS.scroll_offset += shift;
		        	GS.updateControls();
        			if (GS.scrolling_flag) {
        				GS.scrollRight();
        			}
        		}
        	);
        }
        else {
        	this.scrolling_flag = false;
        	return false;
        }
    }
    
    ScrollShow.prototype.scrollLeft = function() {
    	var GS = this;
        if (this.scroll_offset > 0) {
            var shift = Math.min(this.scroll_offset, this.scroll_shift);
        	jQuery(this.slide_container).animate(
        		{marginLeft: '+='+shift}, 
        		this.scroll_time,
        		function() {
		        	GS.scroll_offset -= shift;
		        	GS.updateControls();
        			if (GS.scrolling_flag) {
        				GS.scrollLeft();
        			}
        		}
        	);
        }
        else {
        	this.scrolling_flag = false;
        	return false;
        }
    }

    




