/*
 *   _____                                 _             _                    
 *  / ____|                               | |           | |                   
 * | |      ___  _ __ ___  _ __   ___  ___| |_  __ _  __| | ___  _ __ ___ ___ 
 * | |     / _ \| '_ ` _ \| '_ \ / _ \/ __| __|/ _` |/ _` |/ _ \| '__/ _ | __|
 * | |____| (_) | | | | | | |_) | (_) \__ \ |_| (_| | (_| | (_) | | |  __|__ \
 *  \_____|\___/|_| |_| |_| .__/ \___/|___/\__|\__,_|\__,_|\___/|_|  \___|___/
 *                        | |                                                 
 *                        |_|
 *                        
 * [jquery.glosario.js] iquadrat 2010
 * 
 * 		[Glosario]
 * 		jQuery Glosario (Compostadores)
 * 		Developet by Javier Ferrón
 * 
 */

jQuery.fn.glosario = function(options) {
    var settings = jQuery.extend({
        id: 'glosario',
        top: 0,
        left: 15,
        ajax_url: '/glosario.php?op=view&word=',
        glosario_url: '/glosario/'
    }, options);

    var handle = null;
    
    function show(event) {
        if(!handle) {
            handle = $('<div id="'+settings.id+'"></div>').appendTo(document.body).hide();
        }
        if(event) {
        	var finalPosY = event.pageY - settings.top;
        	var finalPosX = event.pageX + settings.left;
        	if(($('#' + settings.id).width() + event.pageX + 20) > $(window).width()) {
        		finalPosX = event.pageX - (settings.left + 20) - $('#' + settings.id).width();
        	}
        	if(($('#' + settings.id).height() + event.pageY + 20) > $(window).height()) {
        		finalPosY = event.pageY - (settings.top + 20) - $('#' + settings.id).height();
        	}
            handle.css({
                top: finalPosY + "px",
                left: finalPosX + "px"
            });
        }
        return handle;
    }
    
    function wajax(o)
    {
    	var w = o.html();
    	var ret = '';
    	$.ajax({
    		async: false,
    		cache: false,
    		url: settings.ajax_url + urlencode(w),
    		success: function(data) {
    			var values = data.split("amF2aQ==");
    			var intro = values[0];
    			var content = values[1];
    			var link = values[2];
    			var target = values[3];
    			var moretext = values[4];
    			if(content != "" || link != "") {
    				// Activar el click
    				o.css("cursor", "pointer");
    				o.bind('click', function(){
    					if(content.length > 0) {
    						document.location = settings.glosario_url + urlFriend(w);
    					} else if(link.length > 0) {
    						if(target == "_self") {
    							document.location = link;
    						} else {
    							window.open(link, 'glosario-' + w, '');
    						}
    					} else {
    						// Desactivar el click
    	    				o.unbind('click');
    	    				o.css("cursor", "default");
    					}
    				});
    			} else {
    				// Desactivar el click
    				o.unbind('click');
    				o.css("cursor", "default");
    			}
    			ret = intro + moretext;
    			
    		}
    	});
    	return '<span style="font-size:12px; color:#ee6b00; font-weight:bold;">' + w + "</span><br/>" + ret;
    }
    
    function urlFriend(str)
    {
		var replace1 = new Array("'"," ","À","Á","Â","Ã","Ä","Å","à","á","â","ã","ä","å","Ò","Ó","Ô","Õ","Ö","Ø","ò","ó","ô","õ","ö","ø","È","É","Ê","Ë","è","é","ê","ë","Ç","ç","Ì","Í","Î","Ï","ì","í","î","ï","Ù","Ú","Û","Ü","ù","ú","û","ü","ÿ","Ñ","ñ","\\(","\\)","\\/","&",",","¿","\\?","%","\\.",":","!","¡");
		var replace2 = new Array("-","-","a","a","a","a","a","a","a","a","a","a","a","a","o","o","o","o","o","o","o","o","o","o","o","o","e","e","e","e","e","e","e","e","c","c","i","i","i","i","i","i","i","i","u","u","u","u","u","u","u","u","y","n","n","","","-","and","","","","","","","","");
		for(var i=0; i<replace1.length; i++) {
			var reg = new RegExp(replace1[i], "gi");
			str = str.replace(reg, replace2[i]); 
		} 
    	return str.toLowerCase();
    }

    this.each(function() {
        $(this).hover(
            function(e) {
                this.t = wajax($(this));
                this.title = '';
                this.alt = '';
                show(e).html(this.t).show();
            },
            function() {
                if (this.t) {
                    this.title = this.t;
                    show().hide();
                }
            }
        );
        $(this).mousemove(show);
    });
};
