(function(jQuery) {

    jQuery.fn.jSquareSlide = function(settings) {

        var config = {
            speed: 500,
            timeout: 5000,
            easing: 'linear',
            captions: undefined,
            autoslide: true,
            squares: true
        };
        
        if (settings) jQuery.extend(config, settings);

        this.each(function() {

            var that = jQuery(this);
            var imgs = jQuery('img:not(.control)',that).remove();
            var l = imgs.length;
            
           	          
            
            var cur = 0;
            var interval = false;
            var isSliding = false;
            var base = '/';

            var prev = jQuery('<img>');
            var next = jQuery('<img>');
            
            prev.attr('src',base+'images/icons/arrow-left.jpg').addClass('control');
            next.attr('src',base+'images/icons/arrow-right.jpg').addClass('control');
            
            prev.css({
                visibility:'visible',
                display:'block',
                position:'absolute',
                top:'50%',
                marginTop:'-22px',
                left:'0',
                cursor:'pointer',
                opacity:'0.5',
                zIndex:'3000'
            }).hover(function(){
                jQuery(this).stop(false,true).animate({
                    opacity:'1'
                },200,'linear');
            },function(){
                jQuery(this).stop(false,true).animate({
                    opacity:'0.5'
                },200,'linear');
            }).click(function(){
                if(!isSliding){
                    that.goTo((cur+l-1)%l);
                }
            });
            next.css({
                visibility:'visible',
                display:'block',
                position:'absolute',
                top:'50%',
                marginTop:'-22px',
                left:'536px',
                cursor:'pointer',
                opacity:'0.5',
                zIndex:'3000'
            }).hover(function(){
                jQuery(this).stop(false,true).animate({
                    opacity:'1'
                },200,'linear');
            },function(){
                jQuery(this).stop(false,true).animate({
                    opacity:'0.5'
                },200,'linear');
            }).click(function(){
                if(!isSliding){
                    that.goTo((cur+1)%l);
                }
            });
            
            if(l>0){
            	that.append(prev).append(next);
            }
            
            that.append('<span class="squares"></span>');
            
            for(var j=0;j<l;j++){
                jQuery('<a>').attr('href','javascript:;').addClass('square').appendTo(jQuery('.squares',that));
            }
            
            jQuery('a:first',that).addClass('active');
            if(config.squares){
                jQuery('<div>').addClass('tl').hide().appendTo(that);
                jQuery('<div>').addClass('tr').hide().appendTo(that);
                jQuery('<div>').addClass('bl').hide().appendTo(that);
                jQuery('<div>').addClass('br').hide().appendTo(that);
            }
            else{
                jQuery('<div>').addClass('mc').hide().appendTo(that);
            }
            
            jQuery('<img>').css({
                zIndex:'500',
                display: 'block'
            }).attr('src',jQuery(imgs[0]).attr('src')).appendTo(that);

            if(config.captions!=undefined){
                jQuery(config.captions[0]).show();
            }

            that.changeImg = function(i,src){
                if(i<jQuery('div',that).length){
                    jQuery('div:eq('+i+')',that).css({
                        backgroundImage: 'url('+src+')'
                    }).slideDown(config.speed,config.easing,function(){
                        that.changeImg(i+1,src);
                    });
                }
                else{
                    jQuery('img:not(.control)',that).attr('src',src);
                    jQuery('div',that).hide();
                    isSliding = false;
                    return;
                }
            }

            that.go = function(){
                if(config.autoslide===true){
                    interval = window.setInterval(function(){
                        jQuery('a:eq('+cur+')',that).removeClass('active');
                        jQuery(config.captions[cur]).hide(config.speed);
                        cur = (cur+1)%l;
                        jQuery('a:eq('+cur+')',that).addClass('active');
                        jQuery(config.captions[cur]).show(config.speed);
                        isSliding = true;
                        that.changeImg(0,jQuery(imgs[cur]).attr('src'));
                    },config.timeout);
                }
            }

            that.stop = function(){
                if(interval !== false){
                    window.clearInterval(interval);
                }
            }

            that.goTo = function(to){
                cur = to;
                var oldindex = jQuery('a.active',that).index();
                jQuery('a.active',that).removeClass('active');
                jQuery(config.captions[oldindex]).hide(config.speed);
                jQuery('a:eq('+cur+')',that).addClass('active');
                jQuery(config.captions[cur]).show(config.speed);
                that.stop();
                isSliding = true;
                that.changeImg(0,jQuery(imgs[cur]).attr('src'));
                that.go();
            }

            jQuery('.square',that).click(function(){
                if(!isSliding){
                    that.stop();
                    that.goTo(jQuery(this).index());
                }
            });

            that.go();

        });

        return this;

    };

})(jQuery);

