// ==UserScript==
// @name          Yad Vashem search result highlighting
// @namespace     http://www.shoahconnect.org/
// @description	  Highlights Pages of Testimony associated with user through ShoahConnect in Yad Vashem search results
// @include       http://*.yadvashem.*
// @include				http://www.shoahconnect.org/
// ==/UserScript==
// Author: logan (logan@shoahconnect.org)


var page;
var urls = new Array()
var tempcheckarray = new Array()

if(unsafeWindow.location.toString().match('yv=')) {
      document.getElementById('connect').addEventListener('submit',addlink,false)
 }
 
if (GM_getValue('orange')!=0) {GM_setValue('orange',1)} 
 
function getHTML(url,pname,ppass) {
  GM_xmlhttpRequest({
    method:"POST",
    url: url,
		headers: {'Content-type': 'application/x-www-form-urlencoded'},
		data: 'name='+pname+'&pass='+ppass,
    onload:function(details) {
      page = details.responseText;
      processHTML();
    }
  });
}

function processHTML() {
  urls = page.split(' ')
	if (urls.length<2) {alert('Wrong username or password.')}
  for(j=0; j<urls.length; j++) {
   GM_setValue(urls[j], 1)
   if(!GM_getValue('allURLs', '\n').match(urls[j])) GM_setValue('allURLs', GM_getValue('allURLs', '\n') + urls[j] + '\n')
  }
  highlight()
}

function addlink() {
GM_setValue(document.getElementById('id').value, 1)
   if(!GM_getValue('allURLs', '\n').match(document.getElementById('id').value)) GM_setValue('allURLs', GM_getValue('allURLs', '\n') + document.getElementById('id').value + '\n')
}

function addvisited() {
var vemp=new Array()
vemp=this.href.split("\'")
if (vemp[1]>100) {
GM_setValue('v'+vemp[1],1)
if(!GM_getValue('allvURLs', '\n').match(vemp[1])) GM_setValue('allvURLs', GM_getValue('allvURLs', '\n') + vemp[1] + '\n')
}
}


function highlight() {
  allLinks = document.getElementsByTagName('a')
	if (GM_getValue('orange')==1) {
  for(i=0; i<allLinks.length; i++) {
  var temp=new Array()
	temp=allLinks[i].href.split("\'")
	if (temp.length>1) {
		allLinks[i].addEventListener('click',addvisited,false)
    if(GM_getValue(temp[1]) == 1) {
     allLinks[i].style.backgroundColor = 'blue'
		 allLinks[i].style.color='white'
    }
		else if(GM_getValue('v'+temp[1]) == 1) {
     allLinks[i].style.backgroundColor = 'orange'
		 allLinks[i].style.color='white'
    }
		}
  }
	}
	else {
	for(i=0; i<allLinks.length; i++) {
  var temp=new Array()
	temp=allLinks[i].href.split("\'")
	if (temp.length>1) {
		allLinks[i].addEventListener('click',addvisited,false)
    if(GM_getValue(temp[1]) == 1) {
     allLinks[i].style.backgroundColor = 'blue'
		 allLinks[i].style.color='white'
    }
		}
  }
	}
}

function update() {
  var name = prompt('Email address for your ShoahConnect account?', '')
  var pass = prompt('Password for your ShoahConnect account?', '')
  getHTML('http://www.shoahconnect.org/highlight.php',name,pass)
}

function removeall() {
  linksarray = GM_getValue('allURLs').split('\n')
  for(i=0; i<linksarray.length; i++) {
    GM_setValue(linksarray[i], 0)
  }
  GM_setValue('allURLs', "")
	vlinksarray = GM_getValue('allvURLs').split('\n')
  for(i=0; i<vlinksarray.length; i++) {
    GM_setValue('v'+vlinksarray[i], 0)
  }
  GM_setValue('allvURLs', "")
	document.location.reload()
}

function toggle() {
GM_setValue('orange',1-GM_getValue('orange'))
document.location.reload()
}


GM_registerMenuCommand("Get list of PoTs from ShoahConnect for Yad Vashem highlighting", update)
GM_registerMenuCommand("Clear all Yad Vashem highlighting", removeall)
GM_registerMenuCommand("Toggle orange Yad Vashem highlighting", toggle)


highlight()