/**
 * @author Thomas RAMBAUD
 * @copyright 2010
 */
var AparteJs = function(){
    this._current = null;
    this._teaserWidth = null;
    this._teaserEq = 0;
    this._navSpeed = 1150;
    this._navAnimate = true;
    this._navMoving = false;
    this._notaBeneSpeed = 1000;
    this._notaBeneEq = 0;
};

AparteJs.prototype = {
    bootstrap: function(){
        this.navInit();
        this.computerInit();
        this.versoInit();
        this.notabeneInit();
        this.fixPng();
    },
    
    notabeneInit: function(){
        var container = $('#nota-bene .right .items-container');
        var items = container.children('.item');
        var itemwidth = Aparte.withoutPx(items.eq(0).css('width'));
        var arrows = $('#nota-bene .right .arrow');
        var links = $('#nota-bene .left .nav li a');
        var move = function(eq){
            container.animate({left:-(eq *itemwidth)}, Aparte._notaBeneSpeed);
            if(eq + 1 <= items.length - 1) arrows.eq(1).show();
            else arrows.eq(1).hide();
            if(eq - 1 >= 0) arrows.eq(0).show();
            else arrows.eq(0).hide();  
            links.removeClass('on');
            links.eq(eq).addClass('on');
            var right_container = $('#nota-bene .right .right-container');
            var undisplayed = items.eq(eq).find('.undisplayed');
            var siteurlhref = undisplayed.children('.siteurl').attr('href');            
            var website =  right_container.children('.website');            
            if(siteurlhref != '') {website.attr('href', siteurlhref);website.show();}
            else{website.hide();}
            $('.postit.actions').hide();
            var actions_com = $('.postit.actions:eq('+eq+')');
            if(actions_com.find('a').length > 0)
            actions_com.show();        
            return false;
        };
        var af = function(){
            Aparte._notaBeneEq = $(this).hasClass('arrow-right') ? Aparte._notaBeneEq + 1 : Aparte._notaBeneEq - 1;
            move(Aparte._notaBeneEq);
            return false;
        };        
        arrows.eq(0).hide();
        arrows.click(af);
        links.each(function(eq){$(this).click(function(){Aparte._notaBeneEq = eq; return move(eq);});});
        $('.actions a').click(function(){
            var links = $(this).attr('rel').split(',');
            var inline_html = '';

            for(var i = 0; i < links.length; i++){
                var link = links[i];
                if(link.length > 0)
                    inline_html += '<a href="telecharger/?f='+link+'" title="Télécharger"><span>'+link.substring(12, link.length)+'</span></a>';
            }                                          
            if($('#overlay').length == 0){
                var overlay = $('<div id="overlay"><div style="position:relative;"><a href="javascript:;" class="close-overlay" onclick="$(\'#overlay\').remove();"><span>Fermer</span></a></div></div>').css({
                    position: 'absolute',                    
                    opacity : 0.9,
                    width : '100%',
                    top : $('html, body').scrollTop(),
                    height: $(window).height(),
                    backgroundColor : '#000',
                    zIndex: 9999
                });
                $('<div class="overlay-content"><h2 class="overlay-title">Téléchargements :</h2>'+inline_html+'</div>').css({left: ($(document).width() - 400) / 2}).appendTo(overlay);
                overlay.appendTo('body');
            }            
            return false;
        });
        $(document).scroll(function(){
            $('#overlay').remove();
        });
    },    

    navInit: function(){        
        $(window).scroll(function(){
            var offsettop = parseInt($(this).scrollTop() + 140) + 'px';
            if(this._navAnimate === false) 
                $('#nav').css('top', offsettop);
            else 
                $('#nav').animate(
                    {top: offsettop}, 
                    {queue:false, duration: 'fast'}
                );
        });
        var links = $('#nav li a ').click(function(){
            if(!Aparte._navMoving){
                Aparte._navMoving = true;
                links.removeClass('on');
                $(this).addClass('on');
                Aparte.moveToAnchor($(this).attr('href'));                
            }
            return false;
        });        
    },
    
    computerInit: function(){
        var computer = $('#computer');
        var arrows = computer.find('.arrow');
        var items = computer.find('.item');
        this._teaserWidth = Aparte.withoutPx($('.teaser:eq(0)').css('width'));
        arrows.eq(0).hide();
        arrows.click(function(){
            var goleft = $(this).hasClass('arrow-left');
            var operator = goleft ? '+=' : '-=';
            Aparte._teaserEq = goleft ? Aparte._teaserEq - 1 : Aparte._teaserEq + 1; 
            $('.teaser-body-layout').animate({left: operator + Aparte._teaserWidth});            
            items.filter(':visible').fadeOut('slow');
            items.eq(Aparte._teaserEq).fadeIn('slow');                          
            if(Aparte._teaserEq + 1 <= items.length - 1) arrows.eq(1).show();
            else arrows.eq(1).hide();
            if(Aparte._teaserEq - 1 >= 0) arrows.eq(0).show();
            else arrows.eq(0).hide();  
            return false;
        });
    },
    
    versoInit: function(){
        var fakeLink = $('#verso').find('.fake-link');
        fakeLink.click(function(){
            var recto = fakeLink.parent('div').children('.recto');
            var verso = fakeLink.parent('div').children('.verso');
            var versohtml = verso.html();
            var rectohtml = recto.html();
            recto.html(versohtml);
            verso.html(rectohtml);
            return false;
        });
    },
    
    fixPng: function(){
        if(document.all){
            $('img[src$=.png]').each(function(){
                $(this).css('filter', "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale src='"+ this.src +"')");                
            });       
        }
    },
    
    isInt: function(value){
        return !isNaN(value) && parseInt(value) == value;
    },
    
    isUndefined: function(value){
        return typeof(value) == 'undefined' || value == null;
    },
    
    withoutPx: function(val){
        if(val.indexOf('px') == -1) return val;
        return val.substring(0, val.indexOf('px'));
    },
    
    moveToAnchor: function(dieseid){
        $('html, body').animate({scrollTop: $(dieseid).offset().top-30}, Aparte._navSpeed, null, function(){
            Aparte._navMoving = false;
        });
    }
};


var Aparte = null;
$(document).ready(function(){
    Aparte = new AparteJs();
    Aparte.bootstrap();    
});