thispg = '';

function InitContext(){ 

	var _divContext = $dd('divContext'); 
	_divContext.onmouseover = function() { _mouseOverContext = true; };
	_divContext.onmouseout = function() { _mouseOverContext = false; };

	try{
	a = document.getElementsByTagName('table');
	$dd('aDisable').onclick = DisableContext;
	//$dd('aEnable').onclick = EnableContext;

	for(x=0;x<a.length;x++){
		a[x].onmousedown = ContextMouseDown;
		a[x].oncontextmenu = ContextShow;
		if(a[x].className=="sorttable"){
			z = a[x].rows[0].cells.length
			zz = a[x].rows[0].cells[z-1].innerHTML
			//a[x].rows[0].className = 'contxt';
			//a[x].rows[0].cells[z-1].innerHTML = "<div class='context' title = 'Right click within the table to view context menu'></div>" + zz
		}
	}

//	$dd('tab1').onmousedown = ContextMouseDown;
//	$dd('tab1').oncontextmenu = ContextShow;

	} catch(e){}

	document.body.onmousedown = ContextMouseDown;
	//document.body.oncontextmenu = ContextShow;
}

// call from the onMouseDown event, passing the event if standards compliant
function ContextMouseDown(event)
{
	var _divContext = $dd('divContext'); 
    if (_noContext || _mouseOverContext)
        return;

    // IE is evil and doesn't pass the event object
    if (event == null)
        event = window.event;

    // we assume we have a standards compliant browser, but check if we have IE
    var target = event.target != null ? event.target : event.srcElement;



    // only show the context menu if the right mouse button is pressed
    //   and a hyperlink has been clicked (the code can be made more selective)
//    if (event.button == 2 && target.tagName.toLowerCase() == 'a')
	if (event.button == 2){
		a = changelinks(target)
        	if(a!="<ul></ul>")_replaceContext = true;
	}
    else if (!_mouseOverContext)
        _divContext.style.display = 'none';
}

function CloseContext()
{
	var _divContext = $dd('divContext'); 
    _mouseOverContext = false;
    _divContext.style.display = 'none';
}

// call from the onContextMenu event, passing the event
// if this function returns false, the browser's context menu will not show up

function ContextShow(event)
{

	var _divContext = $dd('divContext'); 
    if (_noContext || _mouseOverContext)
        return;

    // IE is evil and doesn't pass the event object
    if (event == null)
        event = window.event;

    // we assume we have a standards compliant browser, but check if we have IE
    var target = event.target != null ? event.target : event.srcElement;

    if (_replaceContext)
    {
        // document.body.scrollTop does not work in IE
        var scrollTop = document.body.scrollTop ? document.body.scrollTop :
            document.documentElement.scrollTop;
        var scrollLeft = document.body.scrollLeft ? document.body.scrollLeft :
            document.documentElement.scrollLeft;

        // hide the menu first to avoid an "up-then-over" visual effect
        _divContext.style.display = 'none';
        _divContext.style.left = event.clientX + scrollLeft + 'px';
        _divContext.style.top = event.clientY + scrollTop + 'px';
        _divContext.style.display = 'block';

        _replaceContext = false;

        return false;
    }
}

function DisableContext()
{
    _noContext = true;
    CloseContext();
    //$('aEnable').style.display = '';

    return false;
}

function EnableContext()
{
    _noContext = false;
    _mouseOverContext = false; // this gets left enabled when "disable menus" is chosen
    $dd('aEnable').style.display = 'none';

    return false;
}

// comes from prototype.js; this is simply easier on the eyes and fingers
function $dd(id)
{
    return document.getElementById(id);
}

var _replaceContext = false;        // replace the system context menu?
var _mouseOverContext = false;      // is the mouse over the context menu?
var _noContext = false;             // disable the context menu?
var _divContext = $dd('divContext');  // makes my life easier
var lnks = "";


function changelinks(tcell){
 lnks = '<ul>';
 mid = 0;
 g = tcell.className.substr(0,1);
 if(g=='m'){
 	mid = tcell.className.substr(1,10);
 }


 if(tcell.tagName.toLowerCase() != 'td')tcell = tcell.parentNode
 if(tcell.tagName.toLowerCase() != 'td')tcell = tcell.parentNode
 if(tcell.tagName.toLowerCase() != 'td')tcell = tcell.parentNode
 srctab = tcell.parentNode.parentNode.parentNode;

 row = tcell.parentNode.rowIndex
 cell = tcell.cellIndex; 
 //alert('A' + srctab.rows[row].cells.length);
 rdz = -1; t2z = -1;  t1nz = ''; t2nz = ''; 
 yrz = (linkyr!=-1) ? linkyr : -1;
 t1z = -1;
 //alert('B' + srctab.rows.length);
 if(srctab.tagName.toLowerCase() != "table")return '';

 

 //alert(jqry)
  
 hdrrow = 0; 



 if(srctab.rows[0].className.substr(0,9)=='notsorted' || linkt1 != -1){
	if(srctab.rows[0].className.substr(0,9)=='notsorted')hdrrow=1; 
	t1pos = srctab.rows[0].cells[1].innerHTML;
	for(x=0;x<teams.length;x+=2){
		if(linkt1==teams[x]){
			t1nz = teams[x+1]; t1z=linkt1;
		}
	}

}

 intxt = srctab.rows[row].cells[cell].innerHTML; 
 hdr = srctab.rows[hdrrow].cells[cell].innerHTML
 if(hdr == "Year" || hdr== "First" || hdr== "Last" || hdr== "From" || hdr== "To")
	if(yrz==-1 && intxt!="")yrz = intxt
 
 for(y=0;y<srctab.rows[row].cells.length;y++){
	hdr = srctab.rows[hdrrow].cells[y].innerHTML
	intxt = srctab.rows[row].cells[y].innerHTML
	if(hdr == "Rd"){
		rdz = intxt;
	}
	else if(hdr == "Year" || hdr== "First" || hdr== "Last" || hdr== "From" || hdr== "To"){
		if(yrz==-1)yrz = intxt;
	}
	else {
		for(x=1;x<teams.length;x+=2){
			t1zp = intxt.split('/',2); 
			if(t1zp[0]==teams[x] || t1zp[1]=="<BR>"+teams[x]){
				if(t1z==-1){t1z = teams[x-1]; t1nz=teams[x]}
				else if(t2z==-1){t2z = teams[x-1]; t2nz=teams[x]}
			}
		}
	}
 }

 seasrv = "?option=com_content&view=article&id=139&catid=36&Itemid=56&qry=1";
 hd2hd = "?option=com_content&view=article&id=112&catid=34&Itemid=56&qry=1";
 tmseas = "?option=com_content&view=article&id=138&catid=36&Itemid=56&qry=1"
 vwrd = "?option=com_content&view=article&id=140&catid=34&Itemid=56&qry=1";
 vwmatch = "?option=com_content&view=article&id=160&catid=34&Itemid=56&qry=1";
 vwclub = "?option=com_content&view=article&id=187&catid=34&Itemid=56&qry=1"
 ml = $dd('mylinks')
 //lnks = '<li>Row ' + row + ' Cell ' + cell + '</li>'


 if(mid>0)lnks += "<li><a href='" + vwmatch + "&match=" + mid + "' title='View Match Details'>View Match Details</a></li> ";
 

 if(t1z>-1 && rdz!=-1 && yrz>-1){
	lnks += "<li><a href='" + vwmatch + "&inyr=" + yrz + "&inrd=" + rdz +"&t1=" + t1z + "' title='View Match Details'>View Match Details</a></li> ";
  }
 
 if(t1z>-1){
	lnks += "<li>"
	if(thispg != 'overall') lnks += "<a href='" + vwclub + "&t1=" + t1z +"' title='View Club Overall Summary'>" + t1nz + "</a> ";
	else lnks += t1nz + " ";
	if(yrz>-1)lnks += "<a href='" + tmseas + "&t1=" + t1z + "&inyr=" + yrz + "'  title='View Club Season Review'>in " + yrz + "</a></li>";
	else lnks += "</li>";
 }
 if(t2z>-1){
	lnks += "<li><a href='#' title='View Club Review'>" + t2nz + "</a> ";
	if(yrz>-1)lnks += "<a href='" + tmseas + "&t1=" + t2z + "&inyr=" + yrz + "'  title='View Club Season Review'>in " + yrz + "</a></li>";
	else lnks += "</li>";
 }

 if(t1z>-1 && t2z>-1 && yrz>-1)lnks += "<li><a href='"+hd2hd+"&t1="+t1z+"&t2="+t2z+"&yrto="+yrz+"'>Head to head upto " + yrz + "</a></li>";


 if(yrz>-1 && thispg != 'seasreview')lnks += "<li><a title='View Season Summary' href='" + seasrv + "&inyr=" + yrz + "'>" + yrz + " Season Review</a></li>";

 if(yrz>-1 && rdz!=-1)lnks += "<li><a href='"+vwrd+"&inyr="+yrz+"&inrd="+rdz+"'>Rd " + rdz + ", " + yrz + " Review</a></li>";

 lnks += '</ul>'; 
 ml.innerHTML = lnks
 return lnks;
}