/* Initial function for IE */
function Reflect_showAddSynonym() {
	if (Reflect_tool.isPageReflected()) {
		if (!Reflect_tool.containsMoreThanAGivenNumberOfWords(Reflect_tool.getSelectedText(),5))
		{//show popup
			Reflect_invokeAddSynonymWindow_IE();
		}
		else
		{
			alert ('Too many words selected. Please select not more than 5 words.');
		}
	}
	else {
		alert("You can't add the selection to Reflect, because the page is not Reflected yet.\nPlease Reflect it first.");
	}
}

/* Initial function for all other browsers */
function Reflect_invokeAddSynonymWindow()
{
  var selectedText = Reflect_popup_trim(window.getSelection().toString());
  overlib(Reflect_popup_add(selectedText),STICKY,CAPTION,selectedText,DRAGCAP,CLOSECLICK,HAUTO,OFFSETY,0);
}

/* Show the popup for IE */
function Reflect_invokeAddSynonymWindow_IE() {
	var selectedText = Reflect_tool.getSelectedText();
	if(Reflect_preference.debuggingIsEnabled())
		Reflect_tool.log('invokeAddSynonymWindow: ' + selectedText);
    //var addURL = Reflect_preference.getReflectServerURL() + Reflect_preference.getAddSynonymServiceURL() + '?' + selectedText;
    //var addURL = "http://reflect.cbs.dtu.dk/cgi-bin/addNewSynonym.cgi?" + selectedText;
    var addURL = "http://reflect.ws/popup//cgi-bin/createAddName.cgi?" + selectedText;
	if(Reflect_tool.getDocument().getElementById('reflect_div_doi') != null)
		addURL += '.DOI:'+Reflect_tool.getDocument().getElementById('reflect_div_doi').innerHTML;
	Reflect_tool.getWindow().open(addURL,'Add_Synonym_to_Reflect','width=380,height=250,resizable=yes,status=no,menubar=no,toolbar=no,location=no,directories=no,status=no'); //external.menuArguments.window.open
}

/* Show the popup for all other browsers */
function Reflect_popup_add(selectedText) {
  var width = "401px";
  var height = "243px";
  var returnString = "";
  var addURL = "http://reflect.ws/popup//cgi-bin/createAddName.cgi?" + selectedText;

  if(document.getElementById('reflect_div_doi') != null)
    addURL += '.DOI:'+document.getElementById('reflect_div_doi').innerHTML;

  returnString  = '<iframe id="item_info_frame" style="width: ' + width + ';height: ' + height + ';overflow: hidden; z-index: 80;" frameborder="0"\' '
  returnString  += 'src=\'' + addURL + '\'></iframe>';
  returnString += '<div id="overLayDiv" style="width: ' + width + ';height: ' + height + '; position: absolute; z-index: 100; top: 20px; left: 2px; background-color: #EEEEEE; display: none;">';
  returnString += '<br><br>&nbsp;&nbsp;&nbsp;&nbsp;Moving the PopUp...<br>&nbsp;&nbsp;&nbsp;&nbsp;Information will reappear if you release the mouse button.';
  returnString += '</div>';
  
  return returnString;
}

function Reflect_popup_trim( str ) {
  return str.replace( /^\s*/, '' ).replace( /\s*$/, '' );
}

/* Check User Agent (Browser) */
function Reflect_checkBrowserName(name) {
  var agent = navigator.userAgent.toLowerCase();
  if (agent.indexOf(name.toLowerCase())>-1)
  {
    return true;
  }
  return false;
}

/* Add custom Event (not for IE) */
if (!Reflect_checkBrowserName('MSIE'))
{
  document.addEventListener("addSynomymRequested", function (e) { Reflect_invokeAddSynonymWindow(); }, true);
}


/* OLD CODE */
/*function invokeAddSynonymWindow() {
  var selectedText = popup_trim(window.getSelection().toString());
  overlib(popup_add(selectedText),STICKY,CAPTION,selectedText,DRAGCAP,CLOSECLICK,HAUTO,OFFSETY,0);
}


function popup_add(selectedText) {
  
  var width = "401px";
  var height = "243px";
  var returnString = "";
  var base_url = "http://reflect.cbs.dtu.dk/cgi-bin/addNewSynonym.cgi?";
  var parameters = selectedText + '.DOI:' + document.getElementById('reflect_div_doi').innerHTML;

  returnString  = '<iframe id="item_info_frame" style="width: ' + width + ';height: ' + height + ';overflow: hidden; z-index: 80;" frameborder="0"\' '
  returnString  += 'src=\'' + base_url + parameters + '\'></iframe>';
  returnString += '<div id="overLayDiv" style="width: ' + width + ';height: ' + height + '; position: absolute; z-index: 100; top: 20px; left: 2px; background-color: #EEEEEE; display: none;">';
  returnString += '<br><br>&nbsp;&nbsp;&nbsp;&nbsp;Moving the PopUp...<br>&nbsp;&nbsp;&nbsp;&nbsp;Information will reappear if you release the mouse button.';
  returnString += '</div>';
  
  return returnString;
}
*/
//function popup_trim( str ) {
//  return str.replace( /^\s*/, '' ).replace( /\s*$/, '' );
//}

//document.addEventListener("addSynomymRequested", function (e) { invokeAddSynonymWindow(); }, true);

