/**
 * for bebe-bebe.com by laurent @ eosgarden.com, but WIP must be completly rewrited
 * Date: 2010-03-01
 */
(function($){
    $.fn.bslide = function(cfg){
        var cf = jQuery.extend({
            width: 150,
			height: 150,
			pos: 1,
			col: 1,
			effect: 'swing',
			fade: false,
            fadeCol : '#fff',
			horiz: false,
			autolink: true,
			duration: 1500,
            auto: 4000
        }, cfg);
            
        this.each(function(i){

            var uniqid = $(this).attr('id'),
            w,li,
            interval,
            fx = cf.effect,
            typex = cf.width,
            typey = cf.height,
            litypex,litypey,col,line,manip='';

            if(uniqid == undefined){
                uniqid = 'bslide'+i;
            }

            $(this).wrap('<div class="bslide-wrap" id="'+uniqid+'-wrap"></div>');
            w = $('#'+uniqid+'-wrap');

            li = w.find('ul li');
            if( li.length ) {

                if(jQuery.easing.easeInQuad == undefined && (fx!=='swing' || fx!=='normal')){
                    fx = 'swing';
                }
                
                function rslt(prop){
                    return Number(prop.split('px')[0]);
                }

                litypex = typex-(rslt(li.css('padding-left'))+rslt(li.css('padding-right'))),
                litypey = typey-(rslt(li.css('padding-top'))+rslt(li.css('padding-bottom'))),
                col = cf.col;
                
                if(cf.horiz){
                    col =  Number(li.length+1);
                }
                line = Math.ceil(Number(li.length)/col);

                w.css('overflow','hidden').css('position','relative').css('text-align','left').css('height',typey+'px').css('width',typex+'px').css('margin','0').css('padding','0');
                w.find('ul').css('position','absolute').css('margin','0').css('padding','0').css('width',Number((col+0)*typex)+'px').css('height',Number(line*typey)+'px');

                li.css('display','block').css('overflow','hidden').css('float','left').css('height',litypey+'px').css('width',litypex+'px');
                li.each(function (i) {
                    var offset = w.offset(),
                    thisoffset = $(this).offset();
                    $(this).attr('id',uniqid+'-'+Number(i+1)).attr('rel', Number(thisoffset.left-offset.left)+':'+Number(thisoffset.top-offset.top));
                    manip += '<a href="#'+uniqid+'-'+Number(i+1)+'">'+Number(i+1)+'</a>';
                });

                if(typeof cf.autolink == 'boolean'){
                    if(cf.autolink){
                        a.after('<div class="bslide-menu" id="'+uniqid+'-menu">'+manip+'</div>');
                    }
                } else if (typeof cf.autolink == 'string'){
                    if($('#' + cf.autolink).length){
                        $('#' + cf.autolink).html(manip);
                    }else{
                        w.after('<div id="#'+cf.autolink+'">'+manip+'</div>');
                    }
                }

                var start = '#'+uniqid+'-';
                var stoccurent = "";

                $('a[href*="'+start+'"]').click(anim);

                li.bind('mouseleave',function(){
                    if(typeof interval === 'object' && cf.auto ) {
                        interval = setInterval( next,cf.auto);
                    }
                });
                li.bind('mouseover',function(){
                    if(typeof interval === 'number' && cf.auto ) {
                        clearInterval(interval);
                        interval = null;
                    }
                });

                if(cf.pos <= 0){
                    cf.pos = 1;
                }
                $('a[href$="'+start+cf.pos+'"]').addClass("wactive");
                stoccurent = $('a[href*="'+start+'"]:eq('+Number(cf.pos-1)+')').attr('href').split('#')[1];

                var decal = $('#' + stoccurent).attr('rel').split(':');
                w.find('ul').css('top',(-decal[1])+'px').css('left',(-decal[0])+'px');
            }

            function anim( a ) {
                
                if( typeof $(a).attr('href') === 'undefined' ) {
                    a = this;
                    if(cf.auto) {
                        clearInterval(interval);
                        interval = null;
                    }
                }
                
                if( $(a).is('.wactive') ) {
                    return false;
                }

                $('a[href*="'+stoccurent+'"]').removeClass("wactive");
                $(a).addClass("wactive");

                stoccurent = $(a).attr('href').split('#')[1];
                var decal = $('#' + stoccurent).attr('rel').split(':');

                if(cf.fade) {
                        w.prepend('<div id="'+uniqid+'-mask" style="height:' + cf.height +'px;width:' + cf.width +'px;position:absolute;z-index:1000;background-color:' + cf.fadeCol +';"><!-- --></div>');
                        $('#'+uniqid+'-mask').css('opacity','0').animate({opacity:1},(cf.duration/2), fx, function(){
                            w.find('ul').css('top',(-decal[1])+'px').css('left',(-decal[0])+'px');
                            $(this).animate({opacity:0}, cf.duration, fx, function(){ $(this).remove();});
                        } );
                } else {
                    w.find('ul').animate({ top: (-decal[1])+'px',left: (-decal[0])+'px' }, cf.duration, fx );
                }
                return false;
            }

            function next() {

                var anchors = $('a[href*="#'+uniqid+'-"]'),
                a = $('a[href*="'+stoccurent+'"]');

                if( anchors[(a.index()+1)] !== undefined ) {
                   anim( $(anchors[(a.index()+1)]) );
                } else {
                    anim( $(anchors[0]) );
                } 
            }

            if(cf.auto){
                interval = setInterval( next,cf.auto);
            }
        }); // eo each

		return this;
    }
})(jQuery);

