﻿/**
 * Glincer 0.1 - Toolbox effects.
 *
 * Copyright (c) 2008  Michaël Ortali (http://silent-strength.com).
 * 
 **/
function Glincer() {
    // Do not edit this part. 
    var elmi = 1; 
    var self = this;
    
    // You can edit this part. 
    this.gname = 'glincer'; // Name of the division. 
    var effectDuration = 500;
    
    // Add the division inside <body>
    //$('body').html($('body').html()+'<div id="'+this.gname+'"></div>');

    /**
     * Add links to Glincer. 
     * @param selector The selector (see JQuery for informations).
     * @param condition {link : [external|internal|both]} 
     * @param classCSS name of a personalized style. 
     */
    this.addToGlincer = function(selector, condition, cssClassName)
    {
        $(selector).each(function(i) {
            // Prise en charge du titre.
            // Vérification de l'existence du titre. 
            var title = $(this).attr('title');
            var url = $(this).attr('href');
            
            // Checking if the cssClassName is defined. 
            if(cssClassName == null)
            {
                cssClassName = 'glincer';
            }
            
            /**
                Checking all conditions. 
            **/
            if(
                (condition.link == 'external' && !url.match(/^http:\/\//, url)) ||
                (condition.link == 'internal' && url.match(/^http:\/\//, url)) ||
                (condition.title == "needed" && title == null)
            )
            {
                return 0;
            }

            /** 
                For each link in the selection add an id, if an id
                is already here, the application use it. 
            **/
            var id = null; 
            if($(this).attr('id') == null)
            {
                elmi++;
                id = 'glincer'+elmi;
                $(this).attr('id',id);
            }
            else 
            {
                id = $(this).attr('id');
            }
            
            /**
                Truncate the link if the size is over 30
                characters. 
            **/
            if(url != null && url.length > 30)
            {
                var url2 = '';
                for(d=0; d < 30; d++)
                {
                    url2 += url.charAt(d);
                }
                url = url2+"...";
            }

            if(title == null)
            {
                //title = "<span>"+url+"</span>";
				title = " ";
            }
            else 
            {
                $(this).removeAttr('title');
                //title +="<span>"+url+"</span>"
				title += " ";
            }

            // Création du html correspondant pour
            // chaque infobulle. 
            $("div#"+self.gname).html(
                $("div#"+self.gname).html()+
                '<div id="d-'+id+'" style="display:none;" class="'+cssClassName+'">'+
                title
                +'</div>'
            );
        });

        $(selector).mouseover(function() 
        {
            self.effectMouseOver($(this).attr('id'));
        });

        $(selector).mouseout(function() 
        {
           self.effectMouseOut($(this).attr('id'));
        });
    }
    
    /**
     * Here is the code of the mouse over effect. 
     * You can configure the duration of the effect, or
     * the movement.  
     */
    this.effectMouseOver = function(id)
    {
        // Get the screen position of the link. 
        var offset =  $("a#"+id).offset();
        
        // CSS informations. 
        $("div#d-"+id).css('top', offset.top + 100);
        $("div#d-"+id).css('left', offset.left);
        $("div#d-"+id).css('opacity',0);
        $("div#d-"+id).css('display','block');
        
        // Animation.
        $("div#d-"+id).animate({
            opacity : 0.98,
            top:offset.top+30
        }, self.effectDuration);
    }
    
    /**
     * Here is the code of the mouse out effect. 
     * You can configure the duration as well. By default 
     * it's 400 ms, you can make it longer. 
     */
    /*this.effectMouseOut = function(id) 
    {
        var offset =  $("a#"+id).offset();
        $("div#d-"+id).animate({
            opacity : 0,
            top:offset.top+100
            }, self.effectDuration, 
            function() {$("div#d-"+id).css("display","none");
        });
    }*/
	this.effectMouseOut = function(id) 
    {
		$("div#d-"+id).css("display","none");
    }
};