var highlightWidget = {

	selection: null,
	selectionText: '',
	selectionButton: null,
	newRange: null,
	activeArea: null,
	apiKey: '',
	
	apply: function() {
		var activeArea;
		if (highlightWidget.activeArea != null && document.getElementById(highlightWidget.activeArea))
			activeArea = document.getElementById(highlightWidget.activeArea);
		else
			activeArea = document.body;
		highlightWidget.addEvent(activeArea, 'mouseup', highlightWidget.handleClick);
	},
	
	addEvent: function(obj, evType, fn) { 
		if(obj!=null){
			if (obj.addEventListener) {
				obj.addEventListener(evType, fn, false); 
				return true;
			} else if (obj.attachEvent) {
				var r = obj.attachEvent("on"+evType, fn);
				return r;
			} else {
				return false;
			}
		}
	},

	handleClick: function(e) {
		if (highlightWidget.selectionButton && highlightWidget.selectionButton != null) {
			highlightWidget.cleanUp();
		}
		highlightWidget.selection = highlightWidget.getUserSelection();
	    highlightWidget.selectionText = highlightWidget.selection && highlightWidget.selection.toString();
		if (highlightWidget.selectionText) {
			window.setTimeout(highlightWidget.insertButton, 10);
		}
	},

	getUserSelection: function() {
		if (window.getSelection) {
			return window.getSelection();
		} else if (document.selection) {
			var newSelection = document.selection && document.selection.createRange();
			newSelection.toString = function() { return this.text };
			return newSelection;
		} else {
			return false;
		}
	},
	
	insertButton: function() {
		highlightWidget.selectionButton = document.createElement('span');
		highlightWidget.selectionButton.setAttribute('id', 'highlightWidgetButton');
		highlightWidget.selectionButton.setAttribute('class', 'highlightWidgetButton');
		highlightWidget.selectionButton.setAttribute('title', 'Search this on DeepDyve');
		//highlightWidget.selectionButton.setAttribute('style', 'margin:-20px 0 0 -20px; position:absolute; z-index: 9999; background: transparent url(http://www.deepdyve.com/assets/images/search_bubble.png) no-repeat 0 0; width: 25px; height: 30px; cursor: pointer;');
		highlightWidget.selectionButton.style.margin = '-20px 0 0 -20px';
		highlightWidget.selectionButton.style.position = 'absolute';
		highlightWidget.selectionButton.style.zIndex = 9999;
		highlightWidget.selectionButton.style.background = 'transparent url(http://www.deepdyve.com/assets/images/search_bubble.png) no-repeat 0 0';
		highlightWidget.selectionButton.style.width = '25px';
		highlightWidget.selectionButton.style.height = '30px';
		highlightWidget.selectionButton.style.cursor = 'pointer';
		
		
		var browser=navigator.appName;
	//	var b_version=navigator.appVersion;
	//	var version=parseFloat(b_version);
		
		if (browser == "Microsoft Internet Explorer") {
		
			var tmp = document.createElement('<div>');
			tmp.appendChild(highlightWidget.selectionButton);
			highlightWidget.newRange = highlightWidget.selection.duplicate();
			highlightWidget.newRange.setEndPoint('StartToEnd', highlightWidget.selection);
			highlightWidget.newRange.pasteHTML(tmp.innerHTML);
			
			highlightWidget.selectionButton = document.getElementById('highlightWidgetButton');
		
		} else {
				if (highlightWidget.selection.getRangeAt && (browser == "Safari")) {
					var range = highlightWidget.selection.getRangeAt(0);
				} else { //Safari!
					var range = document.createRange();
					range.setStart(highlightWidget.selection.anchorNode,highlightWidget.selection.anchorOffset);
					range.setEnd(highlightWidget.selection.focusNode,highlightWidget.selection.focusOffset);
				}
				highlightWidget.newRange = document.createRange();
				highlightWidget.newRange.setStart(highlightWidget.selection.focusNode, range.endOffset);
				highlightWidget.newRange.insertNode(highlightWidget.selectionButton);
		}
		
	
		highlightWidget.addEvent(highlightWidget.selectionButton, 'mouseup', function() {
			highlightWidget.selectionButton.setAttribute('class', 'highlightWidgetButton active');
			location.href = 'http://www.deepdyve.com/search?key='+highlightWidget.apiKey+'&query=' + encodeURIComponent(highlightWidget.selectionText).substring(0,1200);
			return false;
		});
	},

	cleanUp: function() {
		highlightWidget.selection = null;
		highlightWidget.newRange && highlightWidget.newRange.pasteHTML && highlightWidget.newRange.pasteHTML('');
		highlightWidget.newRange = null;
		highlightWidget.selectionButton.onmouseup = null;
		highlightWidget.selectionButton.parentNode.removeChild(highlightWidget.selectionButton);
		highlightWidget.selectionButton = null;
		highlightWidget.selectionText = '';
	}
}


highlightWidget.addEvent(window, 'load', highlightWidget.apply);
