// script written by Radek HULAN and Aleto
// http://hulan.info/ and http://aleto.ch/

// This work is licensed under the Creative Commons Attribution License. To  view a
// copy of this license, visit http://creativecommons.org/licenses/by/1.0/  or send
// a letter to Creative Commons, 559 Nathan Abbott Way, Stanford,  California 94305,
// USA.

// use like onclick="return openwin(this.href)"
function openwin(url) {
  if( window.open(url,'','width=800,height=600,left=0,top=0,scrollbars=yes,location=yes,resizable=yes') ) return false; else return true;
}
 
// hideH2
function hideH2(itemtext,totalitems){
  for (var i = 1; i<=Number(totalitems); i++) if (p=document.getElementById(itemtext+i+String("h2"))) p.style.display="none";
}

// switchCells
function switchCells(currentitem,itemtext,totalitems){
  var p;
  for (var i = 1; i<=Number(totalitems); i++)
    if ( i != Number(currentitem) ){
      if (p=document.getElementById(itemtext+i)) p.style.display='none';
      if (p=document.getElementById(itemtext+i+'a')) p.className='';
    } else {
      if (p=document.getElementById(itemtext+i)) p.style.display='block';
      if (p=document.getElementById(itemtext+i+'a')) p.className='active';
    }
}

// helper functions
var isXHTML;
function createNewEle(ele){
	if(isXHTML) { return document.createElementNS('http://www.w3.org/1999/xhtml', ele); }
	else { return document.createElement(ele); };
};	

function statMenu(menuStats,itemtext,desc,tid) {
	isXHTML=/html\:/.test(document.getElementsByTagName('body')[0].nodeName);
	var ele=document.getElementById(itemtext+'1');
	if (!ele) return;
	if (desc) ele.parentNode.insertBefore(createNewEle('h2'),ele).appendChild(document.createTextNode(desc));
	var base=ele.parentNode.insertBefore(createNewEle('div'),ele);
	base.className='nav';
	if (tid) base.id=tid;
	for(var i=0; i<menuStats.length; i++) {
		var aEle;
		(aEle=base.appendChild(createNewEle('div')).appendChild(createNewEle('a'))).appendChild(document.createTextNode(menuStats[i]));
		aEle.id=itemtext+(i+1)+'a';
		aEle.href='javascript:switchCells('+(i+1)+',\''+itemtext+'\','+menuStats.length+')';
	};
	hideH2(itemtext,menuStats.length);
	switchCells(1,itemtext,menuStats.length);
}


// article statistics
function initStatMenu() {
  var menuStatsItems=['Hot topics','Latest comments','Latest articles','Most karma','Most viewed'];
  statMenu(menuStatsItems,'articlestats','Statistics',false);
}

// search menu
function initSearchMenu() {
  var menuSearchItems=['Exports','Local search','Google search'];
  statMenu(menuSearchItems,'searchstats',false,'search');
}
