var sexton = {
    
    init: function() {
        $('a').focus(function(){ this.blur(); });
        
        sexton.lightbox.init();
    },
    
    // fancybox plugin is required and should be included before this file
    lightbox: {
        
        init: function(){
            sexton.lightbox.vimeo.init();
        },
        
        vimeo: {
            init: function(){
                //alert('test');
                //$('body').append(sexton.lightbox.vimeo.buildEmbed(8921154,640,360));
                //$("a.vimeo").fancybox({ 'hideOnContentClick': true });
                
                anchors = $('.vimeo');
                $.each(anchors, function(i, a){
                    a = $(a);
                    re = new RegExp(".*vimeo.com\/([0-9]+).*");
                    id = re.exec(a.attr('href'))[1];
                    
                    if(id){
                        a.attr('id','inline');
                        divId = 'vimeo_'+id;
                        $('body').append($('<div />').attr('id',divId).css('display','none').append(sexton.lightbox.vimeo.buildEmbed(id)));
                        a.attr('href','#'+divId);
                        a.fancybox({
                            'frameWidth': 640,
                            'frameHeight': 360,
                            'hideOnContentClick': false,
                            'overlayShow': true,
                            'overlayColor': 'black',
                            'overlayOpacity': .8
                        });
                    }
                });
            },
            
            buildEmbed:function(id,width,height) {
                
                if(!width) width = 640;
                if(!height) height = 360;
                
                src = "http://vimeo.com/moogaloop.swf?clip_id="+id+"&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=ffffff&amp;fullscreen=1";
                embed = $('<embed />').attr('src',src).attr('type','application/x-shockwave-flash').attr('allowfullscreen','true').attr('allowscriptaccess','always').attr('width',width+'px').attr('height',height+'px');
                
                return embed;
            }
        }
    }
};


var flipper = function(el) {
    
    var itemWidth = 132;
    var shown = 6;
    
    var current = 0;
    var items = el.find('.item');
    var count = items.length;

    var track = el.find('.track');
    track.css('position','relative');
    track.css('width',(itemWidth*count)+'px');
    
    var rarrow = el.find('.rarrow');
    // bind rbutton
    rarrow.click(function(){
        goToNext();
        return false;
    });
    
    var larrow = el.find('.larrow');
    // bind lbutton
    larrow.click(function(){
        goToPrevious();
        return false;
    });
    
    var goToByNumber = function(i){
        if(i < 0) i = 0;
        if(i >= (count-shown)) i=count-shown;
        current = i;
        current == 0 ? larrow.css('display','none') : larrow.css('display','block');
        current == count-shown ? rarrow.css('display','none') : rarrow.css('display','block');
        
        //track.css('left',(itemWidth*i*-1)+'px');
        
        track.animate({
            left: (itemWidth*i*-1)+'px'
        }, 100);

        
    };
    
    var goToNext = function() {
        if(current == (count-shown)) return false;
        current = current+1;
        return goToByNumber(current);
    };
    
    var goToPrevious = function() {
        if(current == 0) return false;
        current = current-1;
        return goToByNumber(current);
    };
    
    // init
    goToByNumber(0);
    
    // vertically center
    /*
    var feedback = '';
    jQuery.each(items, function(i, item) {
        item = $(item);
        var height = item.find('a img').css('height');
        feedback = feedback + ':' + height;
    });
    alert(feedback);
    */
    
};


$(function(){
    sexton.init();
});


