
// this is used for toggling a group of plugin outputs in a table
// this script is used in plugin-block.html
// key = (library):plugin:DOMelementID
function hideTable(key) 
{
    var tr, td, tbody;

    tbody = key + ":tbody";
    tbody = document.getElementById(tbody);

	img = key + ":img";
	img = document.getElementById(img)

    // tr = tbody.insertRow(tbody.rows.length);
    // td = tr.insertCell(tr.cells.length);
    // td.setAttribute("align", "center");
    // td.innerHTML = "test";

	if (tbody.style.display == 'none') 
	{ 
		tbody.style.display = '';
		img.src="img/collapse.png";
		 
	}
	else
	{ 
		tbody.style.display = 'none';
		img.src="img/expand.png";
	 }

	// tbody.deleteRow(1)
	// img.src="/img/expand.png";
}

//same as above, but do it in batch
function toggle_plugin_outputs()
{
	var pbodies=document.getElementsByName("plugin_output_table");
	var triangles=document.getElementsByName("plugin_output_triangle");
 	for (var i = 0; i < pbodies.length; i++) {
		tbody = pbodies[i]
		img = triangles[i]

		if (tbody.style.display == 'none') 
		{ 
			tbody.style.display = ''; 
			img.src="img/collapse.png";
		}
		else
		{ 
			tbody.style.display = 'none'; 
			img.src="img/expand.png";
		}
    }

}

//
// Show/Hide an element, e.g. a div block
//

function toggle_element_by_id(element_id) {
	element = document.getElementById(element_id);
	if (element.style.display == 'none') 
	{ 
		element.style.display = '';
	}
	else
	{ 
		element.style.display = 'none';
	 }
	return true;
}


//
// This is a workarount for the function: getEmementsByName, 
// so that it works in IE also.
//
// This is used by 'toggle outputs' in the plugin-bock template.
// 

if(typeof(window.external) != 'undefined'){
//yes, this is evil browser sniffing, but only IE has this bug
document.getElementsByName = function(name, tag){
    if(!tag){
        tag = '*';
    }
    var elems = document.getElementsByTagName(tag);
    var res = []
    for(var i=0;i<elems.length;i++){
        att = elems[i].getAttribute('name');
        if(att == name) {
            res.push(elems[i]);
        }
    }
    return res;
}

}

//
//  This script draws a table and opens an iFrame where a config page can be pulled
//

var _startTable, _endTable, _configHead, _inFrame;

_startTable = '<table id = "content" cellpadding = "5"><tbody name="config_table">'
_configHead = '<tr><td bgcolor="wheat"> <a title="Close" OnClick = "delete_config_table(_TBODY_ID_)"> <img src="/img/collapse.png" alt="Close" id="key" /> </a> _HEAD_TEXT_ </td></tr>'        
_inFrame = '<tr><td bgcolor="wheat"><iframe name="ConfigFrames" id="_OUTPUT_KEY_" frameborder="1" width="100%"></iframe> </td></tr>';
_endTable = '</tbody></table>'

// Draw table which contains an iFrame for plugin config HTML form
function drawIFrame(tbody_id,output_key,row) {
    var tr, td, subject, tbody, iframe; 

    tbody = tbody_id + ":tbody";
    tbody = document.getElementById(tbody);

	// tr = tbody.insertRow(tbody.rows.length);
	tr = tbody.insertRow(0);
	td = tr.insertCell(tr.cells.length);
	td.setAttribute("align", "left");
	// td.setAttribute("bgcolor", "lightblue");
	_configHead = _configHead.replace('_TBODY_ID_',"'"+tbody_id+"'");
	_configHead = _configHead.replace('_HEAD_TEXT_',"Configure: "+output_key+" tbody_id = "+tbody_id);

	inFrame = _inFrame.replace('_OUTPUT_KEY_',output_key+":config");
	td.innerHTML = _startTable+_configHead+inFrame+_endTable;

	// load data into the frame
    iframe = output_key + ":config";
    iframe = document.getElementById(iframe);
	// issue a http GET with the output key parameter
    iframe.src = 'configure_transform?outputkey='+output_key;

}

// Delete the first row of the table:
// This is temporary as we assume only one opened config row
function delete_config_table(tbody_id)
{
	var tbody
	    tbody = tbody_id + ":tbody";
	    tbody = document.getElementById(tbody);
	tbody.deleteRow(0);
}

// Display a popup window
// Temporarily this is used instead the above
function popup(mylink, window_name)
{
	if (! window.focus) return true;

	var href;
	if (typeof(mylink) == 'string')
   		href=mylink;
	else
   		href=mylink.href;

    //Note: the session id is obtained from the cookie instead.
	//send the session ID to the server to check if expired
	//we take this from the main-content instead of the (probably) cached plugin-block html
	//var session_id = document.getElementById('session_id').innerHTML
	//href = href + session_id
	w = window.open(href, window_name, 'screenX=100, screenY=40, screenLeft=100, screenTop=40, width=900, height=300, scrollbars=yes, toolbar=no, location=no, directories=no, status=no, menubar=no, resizable=yes, copyhistory=no');
	return false;
}

//
//	Used for IE instead of the above
//

if(typeof(window.external) != 'undefined'){
function popup(url,window_name)
{
	newwindow=window.open(url,'name','height=300,width=900');
	if (window.focus) {newwindow.focus()}
	return false;	
}
}

function select_checkbox(output_key)
{
	var chkboxID = output_key + ":chkbox";
	var chkbox = document.getElementById(chkboxID);
	chkbox.checked = true;
	// chkbox.style.display = 'none'
	// alert(chkbox.name)
}

//
//	XMLHttpRequest stuff
//

function XMLHttpRequestHandler() {
	// do nothing yet
	return True
}

function check_session() {
	var client = new XMLHttpRequest();
	client.onreadystatechange = XMLHttpRequestHandler;
	client.open("GET", "check_session");
	client.send();
}

///
///
///			THIS IS FROM fileID-block 
///
///


// Draw table from 'trackdata' objects
function drawTable3(filehash,mode) {
    var tr, td, subject;
    subject = filehash;

	// this is the specific tbody element under the file name line
    tbody = filehash + ":tbody";
	tbody = document.getElementById(tbody);
	img = filehash + ":img";
	img = document.getElementById(img)

	// if already added just toggle
	if (tbody.rows.length > 1) {
		if (mode == 'expand') {
			tbody.style.display = '';
			img.src="img/collapse.png";
			return true;
		}
		if (mode == 'collapse'){
			tbody.style.display = 'none';
			img.src="img/expand.png";
			return true;
		}
		//otherwise: toggle the display
		if (tbody.style.display == 'none') 
		{ 
			tbody.style.display = '';
			img.src="img/collapse.png";

		}
		else
		{ 
			tbody.style.display = 'none';
			img.src="img/expand.png";
		 }
		return true;
	}

    // loop through data source and draw table rows
    var trackMetadata = trackdata[subject]
    for (var i = 0; i < trackMetadata.length; i++) {
	     
        tr = tbody.insertRow(tbody.rows.length);
		// if (trackMetadata[i].subject != subject) continue;

        td = tr.insertCell(tr.cells.length);
        td.setAttribute("align", "left");
        td.innerHTML = trackMetadata[i].predicate;

        td = tr.insertCell(tr.cells.length);
        td.setAttribute("align", "left");
        td.innerHTML = trackMetadata[i].object;
    }
	img.src="img/collapse.png";
}

// by coincidence, the string repr of a python list is also a JSON list
var metadata_state = 'expand';
function toggle_metadata_views(metadata_hash_list) {
		// toggle the individual descriptors
    	for (var i = 0; i < metadata_hash_list.length; i++) {
			drawTable3(metadata_hash_list[i],metadata_state);
		}
		metadata_state = (metadata_state=='expand') ? 'collapse':'expand';
		toggle_element_by_id('extra_text')
}




