//@cc_on

/* *** Simple browser detection *** */

DOM = (document.getElementById) ? 1 : 0;
NS4 = (document.layers) ? 1 : 0;
IE4 = (document.all) ? 1 : 0;
IE5 = IE4 && navigator.appVersion.indexOf('MSIE 5.0')!=-1;

/* *** Debug functions *** */

_do_clear=true;
function show(o,clear) {
	w=getId('watch');
	if(w.currentStyle && w.currentStyle.visibility=='hidden') return;
	s=w.innerHTML;
	if(_do_clear || clear) {
		s=''; _do_clear=false;
	}
	if(s) s+='<br>';
	s+=o;
	w.innerHTML=s;
}

function elements(obj) {
    var s = new String();
    for (i in obj) {
//@		try{
            s += i+" = "+eval("obj."+i)+"\n";
//@		}catch(o) { }
    }
	return s;
}

function element_list(obj) {
    var s = new String();
    for (i in obj) {
//@		try{
            s += i/*+" = "+eval("obj."+i)*/+"\n";
//@		}catch(o) { }
    }
	return s;
}



function getId(name) {
	//return document.getElementById(name);
	if(document.getElementById) {
		return document.getElementById(name);
	} else if(document.all) {
		return document.all[name];
	} else if(document.layers) {
		return document.layers[name];
	}
}

function childOf(child,parent) {
	if(!child || !parent) return false;
	if(child==parent) return true;
	if(!child.parentNode || child.parentNode==child) return false;
	return childOf(child.parentNode,parent);
}

function _goto(url) {
	document.location.href=url;
	return 1;
}

function launch(url) {
	window.open(url);
}

function popupImg(url) {
	window.open(url);
}

/* *** Pretty date formatting functions *** */

var _month=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var _days=new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');

function takeYear(theDate) {
	x = theDate.getYear();
	var y = x % 100;
	y += (y < 38) ? 2000 : 1900;
	return y;
}

function leadingZero(nr) {
	if(nr<10) nr="0"+nr;
	return nr;
}

function prettyDate(date) {
	if(!date) date=new Date();
	var dayName=_days[date.getDay()];
	var monthName=_month[date.getMonth()];
	var year=takeYear(date);
	var time=''+leadingZero(date.getHours())+'.'+leadingZero(date.getMinutes());
	
	
	var dayNum=date.getDate();
	var suff='th';
	if(dayNum>20 || dayNum<10) {
		switch(dayNum%10) {
		case 1: suff='st'; break;
		case 2: suff='nd'; break;
		case 3: suff='rd'; break;
		}
	}
	
  	return ''+dayName+' '+dayNum+suff+' '+monthName+' '+year+' '+time;
}


function shortDate(date) {
	if(!date) date=new Date();
	var year=takeYear(date);
	var day=date.getDate();
	var month=date.getMonth();
  	return ''+leadingZero(date.getDate())+'/'+leadingZero(date.getMonth())+'/'+year;
}


/* *** Cookies operations *** */

function createCookie(name,value,days) {
	if(days) {
		var date=new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function checkItLocal(string) {
	placeOfDetect = detect.indexOf(string) + 1;
	thestring = string;
	return placeOfDetect;
}


/* *** *** */


function setPageTitle() {
	if(parent && parent.document)
		parent.document.title=document.title;
}


/* *** dd-menu workaround *** */
function switchMenu(pageid) {
	var c=getId('current');
	if(c) c.id='p'+c.smf_pageId;
	
	var o=getId('p'+pageid);
	if(!o) return;
	o.id="current";
	o.smf_pageId=pageid;
	createCookie(document.smf_PageName,pageid);
	createCookie('smf_eraseCookie',document.smf_PageName);
	
	
	//*** the following for a 'linear' sub-menu
	o=getId('sitem');
	if(o) o.id=o.smf_prevId;
	
	o=getId('submenu_item'+pageid);
	if(o) {
		o.smf_prevId=o.id;
		o.id='sitem';
	}
	
}

function ddItemClick(menu,o) {
	if(!o || o.id=="ddsitem") return;
	var c=getId('ddcitem');	if(c) c.id='';
	
	ddHide(menu); //hiding the menu
	var s=getId('ddsitem');
	if(s) s.id='';
	o.id="ddsitem";
	
	switchMenu(o.smf_menuNumber);
	return false;
}


function ddShow(menu) {
	if(!menu || menu.smf_opened) return;
	menu.smf_opened=true;
	//var c=getId('ddcitem'); if(c) c.id='';
	
	if(menu.smf_popup) {
		menu.smf_popup.className='popupopen';
		//menu.smf_popup.style.display='block';
		//menu.smf_popup.style.visiblitity='visible';
		return;
	}
	for(var i=0;i<menu.childNodes.length;i++) {
		if(menu.childNodes[i].className=='popup') {
			menu.smf_popup=menu.childNodes[i];
			menu.smf_popup.className='popupopen';
			//menu.smf_popup.style.display='block';
			//menu.smf_popup.style.visiblitity='visible';
			return;
		}
	}
}

function ddHide(menu) {
	if(!menu || !menu.smf_popup) return;
	var c=getId('ddcitem');	if(c && c!=this) c.id='';
	menu.smf_opened=false;
	menu.smf_popup.className='popup';
	//menu.smf_popup.style.display='none';
	//menu.smf_popup.style.visiblitity='hidden';
}

function getRelatedTarget(e) {
	var relTarg;
	if (!e) var e = window.event;
	if(!e) return null;
	if (e.relatedTarget) relTarg = e.relatedTarget;
	else if (e.toElement) relTarg = e.toElement;
	return relTarg;
}

function buildMenu() {
	var o=getId("smenu");
	if(!o) return;
	
	o.onmouseover = function() {
		ddShow(this);
	}
	o.onmouseout = function() {
		ddHide(this);
	}
	
	var lnks = document.getElementsByTagName('li');
	for(var i=0;i<lnks.length;i++) {
		if(lnks[i].parentNode.className != 'popup') continue;
		lnks[i].smf_menuNumber=i;
		lnks[i].smf_menu=o; // link to menu
		
		lnks[i].onclick = function () {
			return ddItemClick(this.smf_menu, this);
		}
		lnks[i].onkeypress = lnks[i].onclick;
		lnks[i].onmouseover = function (e) {
			var c=getId('ddcitem');	if(c && c!=this) c.id='';
			if(this.id!='ddsitem')
				this.id='ddcitem';
		}
		lnks[i].onmouseout = function (e) {
			var relTarg;
			if (!e) var e = window.event;
			if (e.relatedTarget) relTarg = e.relatedTarget;
			else if (e.toElement) relTarg = e.toElement;
			
			if(relTarg==this) return;
			if(!childOf(relTarg,this.smf_menu)) {
				ddHide(this.smf_menu);
				return;
			}
			
			if(this.id!='ddsitem')
				this.id='';
		}
	}
	
	var cp=readCookie("smf_AboutPage");
	if(cp!=null)
		ddItemClick(o, lnks[cp]);
	else
		ddItemClick(o, lnks[0]);
	
	lnks = document.getElementsByTagName('ul');
	for(i=0;i<lnks.length;i++) {
		if(lnks[i].className != 'popup') continue;
		lnks[i].smf_menu=o;
		lnks[i].onmouseout = function (e) {
			var relTarg=getRelatedTarget(e);
			if(!childOf(relTarg,this.smf_menu)) {
				ddHide(this.smf_menu);
				return;
			}
		}
	}
	
}

function buildMenu2() {
	var lnks = document.getElementsByTagName('a');
	for(var i=0;i<lnks.length;i++) {
		var pc=lnks[i].parentNode ? lnks[i].parentNode.className : '';
		if(pc != 'popup' && pc != 'line') continue;
		lnks[i].href = 'javascript:switchMenu('+i+');';
		lnks[i].id='submenu_item'+i;
	}
	
	var gt=readCookie("smf_GotoPage");
	eraseCookie("smf_GotoPage");
	if(gt != null) {
		switchMenu(gt);
	} else {
		var cp=readCookie(document.smf_PageName);
		if(cp!=null) switchMenu(cp);
		else switchMenu(0);
	}
}


function syncMenu(name) {
	if(!top || !top.menu || !top.menu.selectMenu) {
		createCookie("smf_CurrentMenuId",name);
		setTimeout('top.document.location.href="index.html"',100);
		return;
	}
	top.menu.selectMenu(name);
}

/* initialization */

function userInit() {}

function buildDummyLink() {
    // this is needed to catch the focus!!!
	
    if(!top || !top.content) return;
    var a=top.content.document.createElement('a');
    a.href='javascript:void(0);';
    a.innerHTML='Dummy Link';
    a.style.position='absolute';
    a.style.top='0px';
    a.style.left='0px';
    a.style.overflow='hidden';
    a.style.width="0px";
    a.style.height="0px";
    
    
    var c=top.content.document.getElementsByTagName('div');
    var i=0;
    while(i<c.length) {
        if(c[i].className=='content') break;
        i++;
    }
    if(i>=c.length) return;
    top.content.document.body.insertBefore(a,c[i]);
    
    a.focus();
    // if the following is uncommented, it crashes Opera 7.6
    //a.id='dummylink';
    //a.style.display='none';
    
    a.style.visiblity='hidden';
}



function init(page_name) {
	document.smf_PageName='smf_'+page_name+'Page';
	
	createCookie("smf_contentPage",document.location.href);
	setPageTitle();

	var ec=readCookie('smf_eraseCookie');
	if(ec!=document.smf_PageName) {
		eraseCookie(ec);
		eraseCookie('smf_eraseCookie');
	}
	
	//buildMenu();
	
	userInit();
	
	buildDummyLink();
	
}


//window.onload=init;
