var xPosition;
var yPosition;
var $dialog;

function startApp()
{	   
	var windowSelection={};
	windowSelection.capture=function()
	{
		if(typeof window.getSelection!="undefined")
		{
			this.selection=window.getSelection();
		}
		else
		{
			this.selection=document.selection;
		}
		return this;
};

//var callback = function(response, content) {
//    var adTitle1, adTitle2, adTitle3, adDescription1, adDescription2, adDescription3, adLink1, adLink2, adLink3;

//    //IE
//    if (response.xml != undefined) {
//        var responseXML = response.xml;
//        var splitXML = responseXML.split("<Listing");

//        var reg = new RegExp("rank=\"\\d\" title=\"([\\S\\s]+)\" description=\"([\\s\\S]+)\" .+<ClickUrl>(.+)</ClickUrl></Listing>(</ResultSet></Results></urlset>)?");

//        adTitle1 = splitXML[1].replace(reg, "$1");
//        adDescription1 = splitXML[1].replace(reg, "$2");
//        adLink1 = splitXML[1].replace(reg, "$3");

//        adTitle2 = splitXML[2].replace(reg, "$1");
//        adDescription2 = splitXML[2].replace(reg, "$2");
//        adLink2 = splitXML[2].replace(reg, "$3");

//        adTitle3 = splitXML[3].replace(reg, "$1");
//        adDescription3 = splitXML[3].replace(reg, "$2");
//        adLink3 = splitXML[3].replace(reg, "$3");
//    }
//    //Everything else
//    else {
//        adTitle1 = response.firstChild.firstChild.firstChild.childNodes.item(0).attributes.item(1).nodeValue;
//        adTitle2 = response.firstChild.firstChild.firstChild.childNodes.item(1).attributes.item(1).nodeValue;
//        adTitle3 = response.firstChild.firstChild.firstChild.childNodes.item(2).attributes.item(1).nodeValue;

//        adDescription1 = response.firstChild.firstChild.firstChild.childNodes.item(0).attributes.item(2).nodeValue;
//        adDescription2 = response.firstChild.firstChild.firstChild.childNodes.item(1).attributes.item(2).nodeValue;
//        adDescription3 = response.firstChild.firstChild.firstChild.childNodes.item(2).attributes.item(2).nodeValue;

//        adLink1 = response.firstChild.firstChild.firstChild.childNodes.item(0).firstChild.textContent;
//        adLink2 = response.firstChild.firstChild.firstChild.childNodes.item(1).firstChild.textContent;
//        adLink3 = response.firstChild.firstChild.firstChild.childNodes.item(2).firstChild.textContent;
//    }

//    if ($dialog) {
//        $dialog.dialog("destroy");
//    }
//    $dialog = $('<div class="Infix-window">Window</div>')
//		    .html('<div class="Infix-content" id="moxyInfix"><p><a href="' + adLink1 + '" target="_blank">' + adTitle1 + '</a><br />' + adDescription1 + '<br /><a href="' + adLink2 + '" target="_blank" >' + adTitle2 + '</a><br />' + adDescription2 + '<br /><a href="' + adLink3 + '" target="_blank" >' + adTitle3 + '</a><br />' + adDescription3 + '</p></div>')
//		    .dialog({
//		        autoOpen: false,
//		        title: '<div class="Infix-title"> Search Results for ' + content + '</div>',
//		        position: [xPosition, yPosition],
//		        dialogClass: 'Infix-content',
//				closeText: ''
//		    });

//		    var ScrollTop = document.body.scrollTop;

//		    if (ScrollTop == 0) {
//		        if (window.pageYOffset)
//		            ScrollTop = window.pageYOffset;
//		        else
//		            ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
//		    }
//		    $dialog.dialog('open');
//            window.scrollTo(xPosition - 200, ScrollTop);
//    };
//	
//	var trim=function(A) {
//		return A.replace(/^\s+|\s+$/g,"");
//    };
//		
//	function ajaxMethod(content)
//	{
//		var params = {keyword: content};

//		var callUrl = "/Quicksearch-results.xml?atext=" + trim(content) + "&serveurl=" + location.href;
//        
//		$.ajax({
//		    type: "POST",
//		    url: callUrl,
//		    data: params,
//		    success: function(response) {
//		        callback(response, content);
//		    }
//		});
//	};

	var clickHandler = function(A) {
	    xPosition = A.clientX;
	    yPosition = A.clientY;
	};
	
	//Important Function
	var startListeningForCopies = function() {
	    if (!window.navigator.userAgent.match("Firefox/2")) {
	        addEventHandler(document.body, "copy", copyHandler);
	    }
	};
	
	var addEventHandler=(function()	{
		if(window.addEventListener)
		{
			return function(Location,Name,Func)
			{
				Location.addEventListener(Name,Func,false);
            };
		}
		else
		{
			return function(Location,Name,Func)
			{
				Location.attachEvent("on"+Name,Func);
			};
		}
	})();

	var copyHandler = function(A) {
	    var C = windowSelection.capture();
	    var content = (typeof C.selection.toString != "undefined") ? C.selection.toString() : document.selection.createRange().text;
	    if (content.length < 150) {
	        beginInfixCall(content, xPosition, yPosition);
	    }
	};

	startListeningForCopies();
	addEventHandler(document, "mouseup", clickHandler);
} //what does this close??

$(document).ready(function() {
    startApp();
});
