/**
 * Librairie JQuery : Handblr.
 */

jQuery.fn.extend({
    handblr: {
        
        movelayer: {
            
            move: function(elemId,elemContainerId,type){
 
                var position = $('#'+elemId).position();
                switch(type){
                    case 'up':
                        if((position.top + $('#'+elemId).height()) > $('#'+elemContainerId).height()) {
                            position.top-=1;
                        }
                    break;
                    case 'down':
                        if(position.top < 0){
                            position.top+=1;
                        }
                    break;
                }

                $('#'+elemId).css('top',position.top+'px');
            },

            // pixels est facultatif et est un entier il vaut hauteur de déplacement+1.
            start: function(elemId,elemContainerId,type,pixels){

                if(!pixels || pixels <= 1){
                    pixels = false;
                }
                
                $.fn.handblr.movelayer.stop(elemId);
                switch(type){
                    case 'up':
                        $(document).everyTime(8,'handBlr_movelayer-'+elemId,function(){
                            if(pixels && pixels > 1){
                                pixels--;
                            }
                            
                            $.fn.handblr.movelayer.move(elemId,elemContainerId,'up');
                            
                            if(pixels && pixels == 1){
                                pixels = false;
                                $.fn.handblr.movelayer.stop(elemId);
                            }
                        });
                    break;
                    case 'down':
                        $(document).everyTime(8,'handBlr_movelayer-'+elemId,function(){
                            if(pixels && pixels > 1){
                                pixels--;
                            }
                            
                            $.fn.handblr.movelayer.move(elemId,elemContainerId,'down');
                            
                            if(pixels && pixels == 1){
                                pixels = false;
                                $.fn.handblr.movelayer.stop(elemId);
                            }
                        });
                    break;
                    default:
                    break;
                }
            },

            stop: function(elemId){
                $(document).stopTime('handBlr_movelayer-'+elemId);
            }
        },

        tooltip: {

            attribut: 'title',

            live: function(selector){

                // div pour le tooltip :
                $(selector).live('mouseover',function(e){

                    if($('.handblr-tooltip',$(this)).length <= 0){
                        $(this).append('<div class="handblr-tooltip"></div>');
                        
                         var params = $(this).attr($.fn.handblr.tooltip.attribut);
                             params = params.split(';');

                         var textTooltip = {};
                         for(var i in params){

                             params[i] = params[i].split('=');
                             if(params[i][0] == 'header'){
                                textTooltip.header = params[i][1];
                             }
                             else if(params[i][0] == 'body'){
                                 textTooltip.body = params[i][1];
                             }
                         }

                         var tooltip = $('.handblr-tooltip',$(this));
                             tooltip.css('position','absolute');
                             tooltip.css('top',parseInt(e.pageY-(tooltip.height()+10))+'px');
                             tooltip.css('left',parseInt(e.pageX + 15)+'px');
                             tooltip.append('<div class="handblr-tooltip-header">'+textTooltip.header+'</div>');
                             tooltip.append('<div class="handblr-tooltip-body">'+textTooltip.body+'</div>');
                             tooltip.show();
                             
                         delete tooltip;
                         delete params;
                         delete textTooltip;
                    }
                    else{
                        $('.handblr-tooltip',$(this)).show();
                    }
                });

                $(selector).live('mouseout',function(){
                    if($('.handblr-tooltip',$(this)).length > 0){
                        $('.handblr-tooltip',$(this)).hide();
                    }
                })
            }

        },
        
        scrolldiv: {
            
            // pixels n'est PAS facultatif!
            live: function(elementId,containerId,pixels){
                
                //Function called when a mousewheel event happen on the selected area (html here)
                $('#'+elementId).mousewheel(function(event, delta) {
                    
                    if (delta > 0) { //Scroll up
                        $.fn.handblr.movelayer.start(elementId,containerId,'down',pixels);
                        
                    } else if (delta < 0) { //Scroll Down
                        $.fn.handblr.movelayer.start(elementId,containerId,'up',pixels);
                        
                    }
                    return false;//cancel the default scroll action on the selected area (html here)
                });
            }
            
        },
        
        tabletool: {

            upLine: function(elem,inputText,value){
                if($(elem).parents('tr').prev().length > 0){
                    var trParent =  $(elem).parents('tr');
                    var trPrev = $(elem).parents('tr').prev();

                    var trHtml = trParent.html();
                    var trPrevHtml = trPrev.html();

                    trParent.html(trPrevHtml);
                    trPrev.html(trHtml);

                    var trSortArray = $(inputText).val().split(',');
                    for( var i in trSortArray){
                        if(trSortArray[i] == value){
                            trSortArray[i] = trSortArray[parseInt(i)-1];
                            trSortArray[parseInt(i)-1] = value;
                            break;
                        }
                    }

                    $(inputText).val(trSortArray.join(','));
                }
            },

            downLine: function(elem,inputText,value){
                if($(elem).parents('tr').next().length > 0){
                    var trParent =  $(elem).parents('tr');
                    var trNext = $(elem).parents('tr').next();

                    var trHtml = trParent.html();
                    var trNextHtml = trNext.html();

                    trParent.html(trNextHtml);
                    trNext.html(trHtml);

                    var trSortArray = $(inputText).val().split(',');
                    for( var i in trSortArray){
                        if(trSortArray[i] == value){
                            trSortArray[i] = trSortArray[parseInt(i)+1];
                            trSortArray[parseInt(i)+1] = value;
                            break;
                        }
                    }

                    $(inputText).val(trSortArray.join(','));
                }
            }
        },

        include: function(url,callbackSuccess){
            var script = document.createElement('script');
                script.type = 'text/javascript';
                script.src = url;
                script.jqIndex = $.fn.handblr.includecallback.length-1;
                $.fn.handblr.includecallback[script.jqIndex] = callbackSuccess;

                script.onload = function(){
                    if($.fn.handblr.includecallback[this.jqIndex]){
                        $.fn.handblr.includecallback[this.jqIndex].call(script);
                    }
                };

                script.onreadystatechange = function(){
                    if ( this.readyState != "complete" && this.readyState != "loaded" ){
                        return;
                    }

                    if($.fn.handblr.includecallback[this.jqIndex]){
                        $.fn.handblr.includecallback[this.jqIndex].call(script);
                    }
                };
            
            document.getElementsByTagName('head')[0].appendChild(script);
            
        },

        includecallback: new Array()

    }
});
