function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function focus_fld(obj, type) {
	var object = (obj || document);
	var in_type = (type || 'text');
	var tag = 'input';
	if ((in_type == 'textarea') || (in_type == 'select') || (in_type == 'button')) tag = in_type;
	
	var inputs = object.getElementsByTagName(tag);
	//for(var elem in inputs) { //safari was not happy with this
	for(var elem=0;elem<inputs.length;elem++) { 
	
		if (inputs[elem].tagName && ((tag != 'input') || (!inputs[elem].getAttribute('type')) || (inputs[elem].getAttribute('type').toLowerCase() == 'text'))) {
			if (!inputs[elem].getAttribute('skip')) {
				if (inputs[elem].focus) inputs[elem].focus();
				if (inputs[elem].select) inputs[elem].select();
				break;
			}
		}
	}

}

function page_bookmark() {
	var url = window.top.location.href;
	var title = document.getElementsByTagName('title');
	title = (title && title[0]) ? title[0].innerHTML : "Logogriph Ltd";

	if (window.sidebar) {
		window.sidebar.addPanel(title, url,"");

	} else if( window.external ) {
		window.external.AddFavorite( url, title);
		
	} else {
		alert('Sorry this function only works in MS Internet Explorer and Firefox.');
	}
	return false;

}

function page_print() { //or <a href="javascript:window.print();" >Print Page</a>
	window.print();
}

function msg_window(anchor, name, width, height, params) {
	if (typeof params != 'string') params = 'status=no,scrollbars=yes,resizable=yes';
	if (typeof width  == 'number') params += (params?', ':'')+'width='+width;
	if (typeof height  == 'number') params += (params?', ':'')+'height='+height;

	var url = (anchor.href ? anchor.href : '');

	var new_win = window.open(anchor.href, name, params);

	if (new_win && new_win.focus) new_win.focus();
	
	return new_win;
}


function cookie_dough(doc, name, value, expires, path, domain, secure) {
	/*	cookie manager: pass doc+name to read, pass value =null to delete
		doc: pass any 'false' value for the current document
		name: the string naming the cookie, if this is the last param a read is done
		value: the value of the coockie which will be escaped, pass null to delete
		expires: if omitted or false, this is a session cookie
		path: omitted or false: path is same dir as original doc, but '/' gives whole domain
		dmain: omiited or default same as original doc, if set allows subdomains
		secure: only send on secure connection
	*/

	var c_doc = (doc || document);
	
	if (arguments.length == 2) {	//read
		var picker = new RegExp(';[\s\n\r]*'+name+'=([^;]*);','gi');
		var matches = picker.exec(';'+c_doc.cookie+';');

		if (matches && matches[1]) return unescape(matches[1]);
		return '';	
		
	} else {	//setting or (setting to null -> delete)
		var mycookie = name+'=';
		var now = new Date();
	 	if (value === null) {
	 		now.setTime(0);	//a long time ago
	 		mycookie += '; expires='+now.toUTCString();
	 	} else {
	 		mycookie += escape(value);
	 		if ((typeof expires == 'object') && expires.toUTCString) { //use a date object directly
				mycookie += '; expires='+expires.toUTCString();
			} else if (typeof expires == 'number') {	//number of seconds, so add it to now
				now.setTime(now.getTime()+expires);
				mycookie += '; expires='+now.toUTCString();
			}
	 	}
	 	if (path) mycookie += '; path='+path;
	 	if (domain) mycookie += '; domain='+domain;
	 	if (path) mycookie += '; secure';

	 	c_doc.cookie = mycookie;
	}
}

Effect.Colourise = Class.create();
Object.extend(Object.extend(Effect.Colourise.prototype, Effect.Base.prototype), {
  initialize: function(element) {
    this.element = $(element);
    var options = Object.extend({ startcolor: '#ffff99' }, arguments[1] || {});
    this.start(options);
  },
  setup: function() {
    // Prevent executing on elements not in the layout flow
    if(Element.getStyle(this.element, 'display')=='none') { this.cancel(); return; }
    if(!this.options.endcolor)
      this.options.endcolor = Element.getStyle(this.element, 'color').parseColor('#ffffff');
    if(!this.options.restorecolor)
      this.options.restorecolor = Element.getStyle(this.element, 'color');
    // init color calculations
    this._base  = $R(0,2).map(function(i){ return parseInt(this.options.startcolor.slice(i*2+1,i*2+3),16) }.bind(this));
    this._delta = $R(0,2).map(function(i){ return parseInt(this.options.endcolor.slice(i*2+1,i*2+3),16)-this._base[i] }.bind(this));
  },
  update: function(position) {
    Element.setStyle(this.element,{color: $R(0,2).inject('#',function(m,v,i){
      return m+(Math.round(this._base[i]+(this._delta[i]*position)).toColorPart()); }.bind(this)) });
  }
});

var anim_ref = 0;
function problem() {
	alert('here');
}

function anim_start(event, toggle) {
	var stopped = cookie_dough(false, 'animate');
	if ((typeof toggle != 'undefined') && toggle) {	//(typeof event == 'undefined') {
		stopped = (stopped ? '' : 'stopped');
		cookie_dough(false, 'animate', stopped);
	}
	if (!stopped != !anim_ref) return;	//should not have a reference when stopped and vice versa
	if (anim_ref) {
		clearInterval(anim_ref);
		anim_ref = 0;
	} else {
		anim_ref = setInterval(anim, 1000);
	}
}

function anim() {
	var elem = document.getElementById('mainmenu');
	var kids = elem.getElementsByTagName('a');
	var linkNo = 'link'+Math.round(1+(Math.random()*(kids.length-1)));
	elem = document.getElementById(linkNo);
	if (!elem) {
		alert(linkNo);
		return;
	}
	
	var endc = 150+Math.round(Math.random()*95);
	endc = 'rgb('+endc+','+endc+','+endc+')';
	endc = endc.parseColor();
	new Effect.Colourise(elem, {startcolor:'#ffff55', endcolor: endc})
}

//addLoadEvent(function () {setInterval(anim, 1000);});
addLoadEvent(anim_start);

