// ==UserScript==
// @name          Finncon 2008 ohjelma
// @description   Valitse ohjelmia Finnconin ohjelmakartasta ja tulosta valitut ohjelmat
// @include       http://2008.animecon.fi/ohjelmakartta/
// ==/UserScript==

var selections=GM_getValue("selection","").split("|");

function get_id(table_cell) {
	return table_cell.getElementsByTagName('div')[0].id;
}

function remove_item(array,item) {
	var index=array.indexOf(item);
	if (index != -1) array.splice(index,1);
}

function click_cell(table_cell) {
	var id=get_id(table_cell);
	if (id =="")
		return;
	if (table_cell.className == "ohjelma_anime") {
		table_cell.className="anime_hl";
		selections.push(id);
		return;
	} 
	if (table_cell.className == "") {
		table_cell.className="hl";
		selections.push(id);
		return;		
	}
	if (table_cell.className == "ohjelma_yhteinen") {
		table_cell.className="yhteinen_hl";
		selections.push(id);
		return;		
	}
	
	if (table_cell.className == "anime_hl") {
		table_cell.className="ohjelma_anime";
		remove_item(selections,id);
		return;
	}
	if (table_cell.className == "hl") {
		table_cell.className = "";
		remove_item(selections,id);
		return;
	}

	if (table_cell.className == "yhteinen_hl") {
		table_cell.className="ohjelma_yhteinen";
		remove_item(selections,id);
		return;
	}
}

function highlight_parent(infodiv,highlight) {
	var table_cell=infodiv.parentNode;
	if (highlight) {
		if (table_cell.className == "ohjelma_anime") {
			table_cell.className="anime_hl";
			return;
		} 
		if (table_cell.className == "") {
			table_cell.className="hl";
			return;		
		}

		if (table_cell.className == "ohjelma_yhteinen") {
			table_cell.className="yhteinen_hl";
			return;
		} 
	} else {
		if (table_cell.className == "anime_hl") {
			table_cell.className="ohjelma_anime";
			return;
		}
		if (table_cell.className == "hl") {
			table_cell.className="";
			return;
		}
		if (table_cell.className == "yhteinen_hl") {
			table_cell.className="ohjelma_yhteinen";
			return;
		} 
	}
}

var oldtime=0;

function print_id(doc,id) {
	if (id=="")
		return;
	var infodiv=document.getElementById(id);
	var eventinfo=id.replace(/ohjelma_info_([^_]+)_(.*)_([0-9]*)$/,"$1,$2,$3").split(",");
	var evday,evloc,evtime,evdate;
	evday=eventinfo[0];
	evtime=parseInt(eventinfo[2])+9;
	
	if (oldtime==0) {
		doc.write("<div class=\"hourdiv\">"); // First hour
	} else if (oldtime<evtime) {
		doc.write("</div>\n<div class=\"hourdiv\">"); // Switch houe		
	} else if (oldtime>evtime) {
		doc.write("</div></div>\n\n<div class=\"daydiv\"><div class=\"hourdiv\">");
	}
	oldtime=evtime;
 	evtime=evtime+":00";
	evplace=eventinfo[1].replace(/_/g," ");
	if (evday=="lauantai")
		evdate="26.7.2008";
	else
		evdate="27.7.2008";
	doc.write("<div class=\"infodiv\" style=\"visibility: visible; position: static;\">");
	doc.write("<h1>"+evday+" "+evdate+" "+evtime+" "+evplace+"</h1>");
	doc.write(infodiv.innerHTML);
	doc.write("</div>");
}

function id_time_sort(a,b) {
	if (a==b) return 0;
	if (a=="") return -1;
	if (b=="") return 1;
	var a_array=a.replace(/ohjelma_info_([^_]+)_.*_([0-9]*)$/,"$1,$2").split(",");
	var b_array=b.replace(/ohjelma_info_([^_]+)_.*_([0-9]*)$/,"$1,$2").split(",");
	if (a_array[0]!=b_array[0]) {
		if (a_array[0]=="lauantai") 
			return -1; 
		else
			return 1;
	}
	return parseInt(a_array[1])-parseInt(b_array[1]);
}
function print_selections() {
	var newwin=window.open("about:blank");
	var head = document.getElementsByTagName('head')[0];
  newwin.document.write("<html><head>"+head.innerHTML+"</head><body>");
	newwin.document.write("<div class=\"daydiv\">")
	selections.sort(id_time_sort);
	
	for (var i=0; i < selections.length; i++) {
		print_id(newwin.document,selections[i]);
	};
	newwin.document.write("</tr></table></body></html>");
	newwin.document.close();
	newwin.print();
}

function clear_selections() {
	for (var i=0; i < selections.length; i++) {
		var infodiv=document.getElementById(selections[i]);
		if (infodiv != null) highlight_parent(infodiv,false);
	};
	selections=[];
}

function addGlobalStyle(css) {
    var head, style;
    head = document.getElementsByTagName('head')[0];
    if (!head) { return; }
    style = document.createElement('style');
    style.type = 'text/css';
    style.innerHTML = css;
    head.appendChild(style);
}

addGlobalStyle('td.hl {	background: #FEE700;}');
addGlobalStyle('td.anime_hl { background: #F9A51A;}');
addGlobalStyle('td.yhteinen_hl { background: #fbc60d;}');
var button=document.createElement("button");
button.id="save_selections";
button.innerHTML="Muista valinnat";
var main_div=document.getElementById("main");
var br_elem=main_div.getElementsByTagName("BR")[0];
main_div.insertBefore(button,br_elem);

button=document.createElement("button");
button.id="print_selections";
button.innerHTML="Tulosta valinnat";
main_div.insertBefore(button,br_elem);

button=document.createElement("button");
button.id="clear_selections";
button.innerHTML="Poista valinnat";
main_div.insertBefore(button,br_elem);

for (var i=0; i < selections.length; i++) {
	var infodiv=document.getElementById(selections[i]);
	if (infodiv != null) highlight_parent(infodiv,true);
};

document.addEventListener('click', function(event) {
	if (event.target.nodeName == "TD")
		click_cell(event.target);
	if (event.target.id == "save_selections") {
		GM_setValue("selection",selections.join("|"));
	}
	if (event.target.id == "print_selections") {
		print_selections();
	}
	if (event.target.id == "clear_selections") {
		clear_selections();
	}
}, true);