// 
// version 0.3 BETA!
// 2009-10-14
// Copyright (c) 2009, Splitted-Desktop Systems
// Author Frederic Lepied <frederic.lepied@splitted-desktop.com>
// Released under the GPL license
// http://www.gnu.org/copyleft/gpl.html
//
// --------------------------------------------------------------------
//
// This is a Greasemonkey user script.  To install it, you need
// Greasemonkey 0.3 or later: http://greasemonkey.mozdev.org/
// Then restart Firefox and revisit this script.
// Under Tools, there will be a new menu item to "Install User Script".
// Accept the default configuration and install.
//
// To uninstall, go to Tools/Manage User Scripts,
// select "Hello World", and click Uninstall.
//
// --------------------------------------------------------------------
//
// ==UserScript==
// @name          Touchscreen
// @namespace     http://diveintomark.org/projects/greasemonkey/
// @description   always find a link to follow
// @include       *
// ==/UserScript==

//GM_log('Touchscreen gm script loaded');

//document.addEventListener('click', function(event) {
//	GM_log('Type name: '+event.target.tagName);
//	switch (event.target.tagName) {
//	case 'A':
//	case 'INPUT':
//	case 'TEXTAREA':
//	case 'SELECT':
//	case 'OPTION':
//	    break;
//	default:
//	    {
//	    var allAnchors, theURL, object, oRect, x, y, max, dist;

//	    max = 100000000;
//	    theURL = location.href;
	    
//	    allAnchors = document.getElementsByTagName('A');
	    //GM_log('number links '+allAnchors.length);
//	    for (var i = 0; i < allAnchors.length; i++) {
//		object = allAnchors[i];
//		oRect = object.getBoundingClientRect();

	//	x = (oRect.right + oRect.left) / 2;
//		y = (oRect.bottom + oRect.top) / 2;

//		dist = (event.clientX - x) * (event.clientX - x) + (event.clientY - y) * (event.clientY - y);

//		if (dist < max) {
//		    max = dist;
//		    theURL = object.href;
//		}
//	    }
	    //GM_log('end loop ' + theURL);
//	    window.location.href = theURL;
	//    return false;
//	    }
//	}
//    }, false);

// 20.7.10end of this version. *************************************

String.prototype.trim = function() {      
    return this.replace(/^\s+|\s+$/g, "");       
}  
	 //Step 1: Bind TextArea “Double Click” event to the required JavaScript code. 
function OpenLink(hyperLinkID) {      
    var txtHyperLink = document.getElementById(hyperLinkID); 

    //Step 2: Get Caret/Cursor position with in the text area      
    var CaretPosition = GetCaretPosition(txtHyperLink); 

    //Step 3: Extract current line text from text area.      
    var line = GetCurrentLine(txtHyperLink.value, CaretPosition); 

    //Step 4: Check for valid URL format and fix URL if necessary      
    if (line) {       
        line = line.trim();       
        if (line.length > 0) {       
            if (line.indexOf(‘://’) == -1) {       
                line = "http://" + line;       
            } 

            //Step 5: Open New window/Tab with selected URL.      
            OpenHyperLink(line);       
        }       
    }       
} 

function GetCaretPosition(control) {      
    var CaretPos = 0;       
    // IE Support       
    if (document.selection) {       
        control.focus();       
        var Sel = document.selection.createRange();       
        var Sel2 = Sel.duplicate();       
        Sel2.moveToElementText(control);       
        var CaretPos = 0;       
        var CharactersAdded = 1;       
        while (Sel2.inRange(Sel)) {       
            //old GetCaretPosition always counts 1 for linetermination       
            //fixed       
            if (Sel2.htmlText.substr(0, 2) == "\r\n") {       
                CaretPos += 2;       
                CharactersAdded = 2;       
            }       
            else {       
                CaretPos++;       
                CharactersAdded = 1;       
            }                    
            Sel2.moveStart(‘character’);       
        }       
        CaretPos -= CharactersAdded;       
    }       
    // Firefox support       
    else if (control.selectionStart || control.selectionStart == '0')       
        CaretPos = control.selectionStart;      
    return (CaretPos);  
}  

function GetCurrentLine(Text, Position) {      
    var lineTermination = "\n"; //Mozilla opera etc       
    if (document.all) { // IE       
        lineTermination = "\r\n";       
    }       
    var lineTerminationLength = lineTermination.length;       
    var startPosition = Position;       
    //always search one character position back to avoid wrong calculations       
    //when cursor position is at the end of the line       
    if (Position > 0)       
        Position–;
    var lineStart = Text.lastIndexOf(lineTermination, Position);      
    //if cursor at first line or  new line and cursor at the beginning       
    if (lineStart == -1 || (lineStart <= 0 && startPosition == 0))       
        lineStart = 0;       
    else       
        lineStart = lineStart + lineTerminationLength;  
    var lineEnd = Text.indexOf(lineTermination, lineStart);      
    if (lineEnd == -1)       
        lineEnd = Text.length;       
    return Text.substring(lineStart, lineEnd);       
} 

function OpenHyperLink(line) {      
    window.open(line);       
} 

// 20.7.10 end of this version. *************************************