// Easing Equations (c) 2003 Robert Penner, all rights reserved.
// This work is subject to the terms in http://www.robertpenner.com/easing_terms_of_use.html.
////quadratic easing: ease (current time, beginning value, change in value, duration)
function ease (t, b, c, d) {
	if((t/=d/2) < 1) return c/2*t*t + b;
	return -c/2 * ((--t)*(t-2) - 1) + b;
}


firsttime = new Array (); //the first time easedwidth has to be initialized;
locked_entry = ""; //remembers which entry is currently fixed (disables mouseout action)
status_flags = new Array (); //remebers if the entry_id or the tbb_button is "open" or "closed"
status_flags_old = new Array ();
anirun = new Array ();	//flag if an animation is currently running for anirun[entry_id]s
rollover_int = new Array (); //container for all running intervals rollover_int[entry_id]
anicycles = new Array (); //initialization for the anicycles[entry_id]-counter; max=aniduration
easedwidth = new Array (); //container for all easedwidth[entry_id]
easedwidth_old = new Array ();
table_startwidth = 220;
aniduration = 15;
table_maxwidth = 380;
entry_startheight = 120;

int_links_count = new Array (); //baseref for internal link collection int_links[entry_id][level][numberOfLinksInThatLevel]

pos_x = 0; //hoz and vert (relative) position of the div"content"
pos_x_max = -1000; //max-value for the position to change (relative movement!)
pos_y = 0;
pos_y_max = -2000;
scroll_step = 2; //px per scroll-cycle
scroll_amplify = 1; //amplifier start_value

//sets up the inline_links within the entries, could be done in php mostly
function build_inline_link (entry_id, level, link_target, link_text, rss_text) {
	if (!int_links_count[entry_id]) int_links_count[entry_id] = new Array ();
	if (!int_links_count[entry_id][level]) {
		int_links_count[entry_id][level] = new Array ();
		int_links_count[entry_id][level] = 0;
	}
	int_links_count[entry_id][level]++;

	link_id = entry_id +"_"+ level + "_link_" + int_links_count[entry_id][level];
	var htmlsource = "";
	  
	//build the html-source
	htmlsource += "<span class='" + level + "_link' name='" + link_id + "'>";
	htmlsource += "<a href='" + link_target + "' target='_blank'>" + link_text + "</a>";
   
	//no rss means that it's a standard outgoing link, not a blogged one
	if (rss_text) {
   	htmlsource += "<a href='#" + "' onClick=\"show_inline_link('" + link_id + "')\">&Xi;</a>";
		htmlsource += "<span id='" + link_id + "'>" + rss_text + "</span>";
   }
	htmlsource += "</span>";
	document.write (htmlsource);
	//hide the rss-part
	rss_text?document.getElementById(link_id).style.display = "none":"";
}


//builds at startup a div class=[level]_link_bar for every inline_link in the entry (should be done in PHP)
function build_internal_link_bars (entry_id) {
	if (int_links_count[entry_id]) {
   	var m = 0;
   	var n = 0;
   	while (!document.getElementById(entry_id).childNodes[m].getAttribute || document.getElementById(entry_id).childNodes[m].getAttribute("class") != "entry") {m++;}
   	var entry_temp = document.getElementById(entry_id).childNodes[m];
   	
      while (!entry_temp.parentNode.childNodes[n].getAttribute || entry_temp.parentNode.childNodes[n].getAttribute("class") != "int_links") {n++;}
   	int_links_node = entry_temp.parentNode.childNodes[n];
   	
   	if (int_links_count[entry_id]["pol"] > 0) build_internal_link_bars_helper (entry_id, "pol", "political");
   	if (int_links_count[entry_id]["soc"] > 0) build_internal_link_bars_helper (entry_id, "soc", "social");
   	if (int_links_count[entry_id]["cre"] > 0) build_internal_link_bars_helper (entry_id, "cre", "creative");
	}
}
	
function build_internal_link_bars_helper (entry_id, level, level_long) {
	//create divs and headings that represent the internal_links -- out of nothing!
	link_level = document.createElement ("h3");
	link_level_txt = document.createTextNode(level_long + " sources");
	link_level.appendChild (link_level_txt);
	int_links_node.appendChild (link_level);
	
	for (var o = 0; o<int_links_count[entry_id][level]; o++) {
   	link_bar_attr = document.createAttribute ("class");
   	link_bar_attr.nodeValue = level + "_link_bar";					
   	link_bar = document.createElement("div");
   	link_bar.setAttributeNode (link_bar_attr);
   	int_links_node.appendChild (link_bar);
   }
}



function setup () {
	//set the standard-width of the wrapping tables. could be done in css as well, but should correspond with the entry-widths
	for (var o = 0; o<document.getElementById("content").childNodes.length; o++) {;
   	if (document.getElementById("content").childNodes[o].getAttribute && document.getElementById("content").childNodes[o].getAttribute("class") == "outerwrapper") {
			document.getElementById("content").childNodes[o].style.width = table_startwidth +"px";
		}
	}
   
   //set the standard-height for the closed "entry"-divs
   alltds = document.getElementsByTagName("TD");

	for (var n = 0; n<alltds.length; n++) {
      for (var m = 0; m<alltds[n].childNodes.length; m++) {
      	if (alltds[n].childNodes[m].getAttribute && alltds[n].childNodes[m].getAttribute("class") == "entry") {
				alltds[n].childNodes[m].style.height = entry_startheight+"px";
			}
		}     
   }
	
	//get the referrer and store it in the corresponding entry as "pol" 
	//(pol is for testing reasons only until the allmighty DB takes its paralyzing takeoff) well, looks like i'm in need for php ( :
	if (document.referrer) {
   	referrerwindow = open(document.referrer, "referrerwindow", "scrollbars=no, location=no, menubar=mo, statusbar=no, toolbar=no");
   	self.focus();
   	window.referrerwindow.resizeTo(200,200);
   	var refer_content = "boah ";
   	for (var p = 0; p<window.referrerwindow.document.getElementsByTagName("meta").length; p++) {
   		if (window.referrerwindow.document.getElementsByTagName("meta")[p].getAttribute && window.referrerwindow.document.getElementsByTagName("meta")[p].getAttribute("name") == "description") {
     			//document.getElementById("testfeld").firstChild.nodeValue = 
   			refer_content += window.referrerwindow.document.getElementsByTagName("meta")[p].content;
   		}
   	}
   	//get the properties of the incoming link
   	var refer_uri = document.referrer;
   	if (window.referrerwindow.document.title) {
   		var refer_title = window.referrerwindow.document.title;
      	var refer_time = new Date ();
      	
      	//parse "get-variables"
      	var index_equal = window.location.search.indexOf("=");
   
      	var incomming_id = window.location.search.slice(index_equal+1);
      	//find the corresponding "pol_layer"
      	var q = 0;
      	while (!document.getElementById(incomming_id+"_pol_layer").childNodes[q].getAttribute || document.getElementById(incomming_id+"_pol_layer").childNodes[q].getAttribute("class") != "comment") {q++;}
      	var firstdiv = document.getElementById(incomming_id+"_pol_layer").childNodes[q];
      	//clone first "comment" and fill in the new values
      	var clone_comment = firstdiv.cloneNode(true);
      	clone_comment.getElementsByTagName("A")[0].setAttribute ("id", "incomming_referrer");
      	clone_comment.getElementsByTagName("A")[0].setAttribute ("href", refer_uri);
      	clone_comment.getElementsByTagName("A")[0].setAttribute ("title", refer_uri);
      	clone_comment.getElementsByTagName("A")[0].firstChild.nodeValue = refer_title;
      	clone_comment.getElementsByTagName("A")[0].nextSibling.nodeValue = " ";
      	clone_comment.getElementsByTagName("A")[1].setAttribute ("href", "#");
      	clone_comment.getElementsByTagName("A")[1].firstChild.nodeValue = refer_time.getFullYear()+"-"+refer_time.getMonth()+1+"-"+refer_time.getDate()+" "+refer_time.getHours()+":"+refer_time.getMinutes();
      	clone_comment.getElementsByTagName("DIV")[0].childNodes[1].firstChild.nodeValue = refer_content;
      	firstdiv.parentNode.insertBefore(clone_comment, firstdiv);
      	document.getElementById("testfeld").firstChild.nodeValue = window.referrerwindow.document.title?window.referrerwindow.document.title:"bäh";//visualcheck
      	setTimeout("window.referrerwindow.close()", 5000);
   	}
	}
}


   	

function build_rollover (entry_id) {
	
	if (firsttime[entry_id] !=0) {
   	easedwidth[entry_id] = table_startwidth;//initialize the easedwidth_value
 		anicycles[entry_id] = 0; //initalizes the array for numbers
		firsttime[entry_id] = 0;
	}
	// check if item is locked
	if ((!locked_entry || locked_entry != entry_id) && anirun[entry_id] != 1) {
		
		//get the div "entry"
		var n = 0;
		while (!document.getElementById(entry_id).childNodes[n].getAttribute || document.getElementById(entry_id).childNodes[n].getAttribute("class") != "entry") {n++;}
		entry = document.getElementById(entry_id).childNodes[n];
   	
   	anirun[entry_id] = 1;
   	//alert(rollover_int[entry_id]);  ##################### how can i make him use the same ID (rollover_int[entry_id]) everytime he starts the interval for the "entry"???####################
		if (status_flags[entry_id] != "open") {
			rollover_int[entry_id] = setInterval ("rollover_open('"+entry_id+"', entry)", 20);//animate width
		} else {
         rollover_int[entry_id] = setInterval ("rollover_close('"+entry_id+"', entry)", 20); //animate width

   		//change the visibility of "int_link_bars"
   		var p = 0;
   		while (!entry.parentNode.childNodes[p].getAttribute || entry.parentNode.childNodes[p].getAttribute("class") != "int_links") {p++;}
   		entry.parentNode.childNodes[p].style.display = "block";
 
   		//change the visibility of all sub_nodes of "entry"
         for (var m=0; m<entry.childNodes.length; m++) {
   			if (entry.childNodes[m].style && entry.childNodes[m].nodeName != "H2" && entry.childNodes[m].nodeName != "H3") {
   				entry.childNodes[m].style.display = "none";
   			}
   		}
     		//close all trackback layers
   		if (status_flags[entry_id+"_cre_layer"] == "open") build_trackback_button (entry_id+"_cre_layer");
   		if (status_flags[entry_id+"_soc_layer"] == "open") build_trackback_button (entry_id+"_soc_layer");
   		if (status_flags[entry_id+"_pol_layer"] == "open") build_trackback_button (entry_id+"_pol_layer");
   		entry.style.height = entry_startheight+"px";
			
   		//jump to top of entry
			pos_y = 10;
			document.getElementById("content").style.top = pos_y +"px";
		} 
	}
}

function rollover_open (entry_id, entry) {
	easedwidth_old[entry_id] = easedwidth[entry_id];
	easedwidth[entry_id] = ease (anicycles[entry_id], easedwidth_old[entry_id], (table_maxwidth-easedwidth_old[entry_id]), aniduration); // current time, beginning value, change in value, duration
	entry.parentNode.parentNode.parentNode.parentNode.style.width = easedwidth[entry_id]+"px";
   anicycles[entry_id] += 1;
	//document.getElementById("testfeld").firstChild.nodeValue = status_flags["a003388"]+ "\n"+ anicycles["a003388"];//###############
	if (anicycles[entry_id] >= aniduration) { //max ani_cycles = ease (_, _, _, d): duration?????
		window.clearInterval(rollover_int[entry_id]);
		anicycles[entry_id] = 0;
				
		status_flags[entry_id] = "open";
		anirun[entry_id] = 0;
      entry.style.height = ""; //delete fixed height
				
		//change the visibility of "int_links"
		var p = 0;
		while (!entry.parentNode.childNodes[p].getAttribute || entry.parentNode.childNodes[p].getAttribute("class") != "int_links") {p++;}
		entry.parentNode.childNodes[p].style.display = "none";
		
		//change the visibility of all sub_nodes of "entry"
		for (var n=0; n<entry.childNodes.length; n++) {
			if (entry.childNodes[n].style) entry.childNodes[n].style.display = "block";
		}
	}
}

function rollover_close (entry_id, entry) {
	easedwidth_old[entry_id] = easedwidth[entry_id];
	easedwidth[entry_id] = ease (anicycles[entry_id], easedwidth_old[entry_id], (table_startwidth-easedwidth_old[entry_id]), aniduration); // current time, beginning value, change in value, duration
	entry.parentNode.parentNode.parentNode.parentNode.style.width = easedwidth[entry_id]+"px";
   anicycles[entry_id] += 1;
	//document.getElementById("testfeld").firstChild.nodeValue = status_flags["a003388"]+ "\n"+ anicycles["a003388"];//###############
	if (anicycles[entry_id] >= aniduration) { //max ani_cycles = ease (_, _, _, d): duration?????
		window.clearInterval(rollover_int[entry_id]);
		anicycles[entry_id] = 0;
		
		status_flags[entry_id] = "closed";		
		anirun[entry_id] = 0;
	}
}   		
			



function build_trackback_button (layer) {

	if (document.getElementById(layer)) {
   	if (status_flags[layer] == "closed") {
			//increase all divs.comment and display their subnodes (=remove the "none")
   		status_flags[layer] = "open";
   		for (var n=0; n<document.getElementById(layer).childNodes.length; n++) {
   			if (document.getElementById(layer).childNodes[n].nodeName == "DIV") {
   				for (m=0; m<document.getElementById(layer).childNodes[n].childNodes.length; m++) {
						if (document.getElementById(layer).childNodes[n].childNodes[m].style) {
							document.getElementById(layer).childNodes[n].childNodes[m].style.display = "";
						}
					}
					document.getElementById(layer).childNodes[n].style.height = "";
					document.getElementById(layer).childNodes[n].style.marginBottom = "";
   			}
   		}
   	} else {
			//reduce all divs.comment to a bar of the specified height and hide their subnodes
   		status_flags[layer] = "closed";
    		for (var n=0; n<document.getElementById(layer).childNodes.length; n++) {
   			if (document.getElementById(layer).childNodes[n].nodeName == "DIV") {
   				for (var m=0; m<document.getElementById(layer).childNodes[n].childNodes.length; m++) {
						if (document.getElementById(layer).childNodes[n].childNodes[m].style) {
							document.getElementById(layer).childNodes[n].childNodes[m].style.display = "none";
						}
					}
					document.getElementById(layer).childNodes[n].style.height = "5px";
					document.getElementById(layer).childNodes[n].style.marginBottom = "2px";
   			}
   		}	
   	}
	}
}





function show_inline_link (link_id) {
	if (document.getElementById(link_id).style.display == "none") {
   	document.getElementById(link_id).style.display = "inline";
   } else {
		document.getElementById(link_id).style.display = "none";
   }
}


function set_locked_entry (entry_id) {
	if (locked_entry && locked_entry == entry_id) {
		locked_entry = "";
	} else {
		locked_entry = entry_id;
	}
}




//wander around on the screen...
/*function scroll_x (dir, flag) {
	if (flag == "go") {
		old_x = 0;
		onmouseover = function (e) {old_x = e.pageX;}
		scroll_x_int = setInterval ("scroll_x_helper()", 10);
	} else {
		clearInterval(scroll_x_int);
	}
}		
function scroll_x_helper (e) {
	if ((dir == 1 && pos_x < 20) || (dir == -1 && pos_x > pos_x_max)) {//emergency break
		pos_x += (old_x-window.event.pageX)/2;
		document.getElementById("content").style.left = pos_x + "px";
		document.getElementById("testfeld").firstChild.nodeValue = e.pageX;
	} else {
		clearInterval(scroll_x_int);
	}
}
*/
function scroll_x (dir, flag) {
	if (flag == "go") {
		old_x = 0;
		onmouseover = function (e) {old_x = e.pageX;}
		onmousemove = function scroll_x_helper (e) {
			if ((dir == 1 && pos_x < 20) || (dir == -1 && pos_x > pos_x_max)) {//emergency break
				pos_x += (old_x-e.pageX)/2;
				document.getElementById("content").style.left = pos_x + "px";
   			//document.getElementById("testfeld").firstChild.nodeValue = pos_x;
  			} else {
				delete onmousemove;
			}
		}
	} else {
		delete onmousemove;
	}
}	
	
function scroll_y (dir, flag) {
	if (flag == "go") {
		old_y = 0;
		onmouseover = function (e) {old_y = e.pageY;}
		onmousemove = function scroll_y_helper (e) {
			if ((dir == 1 && pos_y < 10) || (dir == -1 && pos_y > pos_y_max)) {//emergency break
				pos_y += (old_y-e.pageY)/2;
				document.getElementById("content").style.top = pos_y + "px";
   			//document.getElementById("testfeld").firstChild.nodeValue = pos_y;
  			} else {
				delete onmousemove;
			}
		}
	} else {
		delete onmousemove;
	}
}	

