/* Common functions */

function add_load_event(f) {
	var old = window.onload;
	if (typeof old != 'function') {
		window.onload = f;
	} else {
		window.onload = function() { if (old) old(); f(); }
	}
}

function add_resize_event(f) {
	var old = window.onresize;
	if (typeof old != 'function') {
		window.onresize = f;
	} else {
		window.onresize = function() { if (old) old(); f(); }
	}
}

function get_text(o) {
	return o.innerText?o.innerText:o.textContent;
}

function submit_form(f,b)
{
	var i=document.createElement('input');
	i.type='hidden'; i.name=b;
	f.appendChild(i); f.submit();
	return false;
}

function submit_photo(b)
{
	if (b=='s')
	{
		if (document&&document.getElementById) {
			var f=document.getElementById('f');
			var h=document.getElementById('h');
			if (f&&h&&!f.value&&!h.checked)
			{
				alert('Select picture file or check "Delete photo" to delete current photo.');
				return false;
			}
		}
	}
	var i=document.createElement('input');
	i.type='hidden'; i.name=b;
	document.photo_form.appendChild(i);
	document.photo_form.submit();
	return true;
}

function login_submit()
{
	if (document&&document.getElementById) {
		var p1=document.getElementById('login_username');
		var p2=document.getElementById('login_password');
		if (p1&&p2&&(!p1.value||!p2.value)){return;}
	}
	document.login_form.submit();
}

function login_keypress(e)
{
	e=e||window.event;
	if (e.keyCode!=13){return true;}
	login_submit();
	return false;
}

function window_height() {
	var h=0;
	if (typeof(window.innerHeight)=='number'){h=window.innerHeight;}
	else{if(document.documentElement&&document.documentElement.clientHeight){
	h=document.documentElement.clientHeight;}
	else{if(document.body&&document.body.clientHeight){h=document.body.clientHeight;}}}
	return h;
}

function move_footer(f)
{
	if (document&&document.getElementById) {
		var h=window_height();
		var i=document.getElementById('wrap_inner');
		var o=document.getElementById('wrap_outer');
		if (i&&i.offsetHeight+121<h){
			if (o){o.style.minHeight='';o.style.height=(h-121)+'px';}
		} else {
			if (f&&o){o.style.minHeight='';o.style.height='';}
		}
		document.cookie='fx='+(h-121)+'; path=/';
	}
}

function opacity(id, opacStart, opacEnd, millisec) {
	var speed = Math.round(millisec / 100);
	var timer = 0;
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("change_opac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++) {
			setTimeout("change_opac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

function change_opac(v, id) {
	var o=typeof(id)=='string'?document.getElementById(id).style:id.style;
	o.opacity=v/100;
	o.MozOpacity=v/100;
	o.KhtmlOpacity=v/100;
	o.filter="alpha(opacity="+v+")";
	if(v==0){o.display='none';}
}

/* Search autocomplete */

var _at=null,_ad=null,_aq=null,_ax='',_ac=null;

function ac_plan_req()
{
	if (!_at){_at = window.setTimeout('ac_request()',5);}
}

function ac_move_sel(dir,fl) {
	if (!_ad){return;}
	var ul = document.getElementById('search_list_worker');
	if (!ul){return;}
	var ll = null;
	var hl = null;
	var sn = 0;
	var i = 0;
	while (i<ul.childNodes.length) {
		var li = ul.childNodes[i];
		if (li.nodeName=='LI') {
			if (sn==1) {
				li.className='hot';
				hl.className='';
				return;
			}
			if (li.className=='hot'){
				switch (dir)
				{
				case 0:
					hl=li;sn=1;
					break;
				case 1:
					if (ll&&li!=ll) {
						ll.className='hot';
						li.className='';
					}
					return;
				case 2:
					if (fl&&li!=fl) {
						fl.className='hot';
						li.className='';
					}
					return;
				case 3:
					hl=li;sn=2;
					break;
				}
			}
			ll=li;
			if (!fl){fl=li;}
		}
		i++;
	}
	if (sn==2&&ll&&ll!=hl)
	{
		ll.className='hot';
		hl.className='';
	}
}

function ac_commit() {
	var p = document.getElementById('search_text');
	if (!p||!p.value) return false;
	if (_ac) {
		for (i=0;i<_ac.length;i+=2) {
			if (_ac[i+1]==p.value) {
				var u = document.getElementById('search_user');
				if (u){document.location='/'+encodeURI(u.value)+'/artists/'+_ac[i]+'/';}
				return true;
			}
		}
	}
	if (_ad) {
		var ul = document.getElementById('search_list_worker');
		if (ul) {
			var i = 0;
			while (i<ul.childNodes.length) {
				var li = ul.childNodes[i];
				if (li.nodeName=='LI'&&li.className=='hot') {
					for (i=0;i<li.childNodes.length;i++) {
						var a = li.childNodes[i];
						if (a.nodeName=='A'){
							p.value=get_text(a);
							document.location=a.href;
							return true;
						}
					}
					break;
				}
				i++;
			}
		}
	}
	return false;
}

function ac_request(id) {
	var p = document.getElementById('search_text');
	var u = document.getElementById('search_user');
	if (p&&u&&_ax!=p.value&&document.body) {
		_ax = p.value;
		if (_aq) document.body.removeChild(_aq);
		if (_ax) {
			_aq=document.createElement('script');
			_aq.type='text/javascript';
			_aq.src='/alist?u='+encodeURI(u.value)+'&a='+encodeURI(_ax);
			document.body.appendChild(_aq);
		} else {
			_aq = null;
			_ac = null;
			ac_update();
		}
	}
	_at = null;
}

function ac_update() {
	if (_aq) {
		document.body.removeChild(_aq);
		_aq = null;
	}
	var p = document.getElementById('search_text');
	var u = document.getElementById('search_user');
	var pp = document.getElementById('search_main');
	if (p&&u&&pp) {
		if (_ad) pp.removeChild(_ad);
		if (_ac && _ac.length>0) {
			_ad = document.createElement('div');
			_ad.id = 'search_list';
			var e = document.createElement('div');
			e.id = 'search_list_top';
			_ad.appendChild(e);
			e = document.createElement('div');
			e.id = 'search_list_main';
			_ad.appendChild(e);
			var ul = document.createElement('ul');
			ul.id = 'search_list_worker';
			for (i=0;i<_ac.length;i+=2)
			{
				var url = '/'+encodeURI(u.value)+'/artists/'+_ac[i]+'/';
				var li = document.createElement('li');
				li.onmouseover=function(){ac_move_sel(2,this);return true};
				var a = document.createElement('a');
				if (i==0) li.className = 'hot';
				a.href=url;
				a.appendChild(document.createTextNode(_ac[i+1]));
				li.appendChild(a);
				ul.appendChild(li);
			}
			e.appendChild(ul);
			e = document.createElement('div');
			e.id = 'search_list_bottom';
			_ad.appendChild(e);
			pp.appendChild(_ad);
		} else {
			_ad = null;
		}
	}
}

function ac_hide() {
	_aq = null;
	_ac = null;
	ac_update();
}

function ac_keydown(e) {
	e=e||window.event;
	var p=e.target||e.srcElement;
	switch(e.keyCode) {
		case 8: ac_plan_req(); break;
		case 27: ac_hide(); return false;
		case 46: ac_plan_req(); break;
		case 33: ac_move_sel(2,null); return false;
		case 34: ac_move_sel(3,null); return false;
		//case 35: if (_ad&&!e.shiftKey) { ac_move_sel(3); return false; } break;
		//case 36: if (_ad&&!e.shiftKey) { ac_move_sel(2); return false; } break;
		case 38: ac_move_sel(1,null); return false;
		case 40: ac_move_sel(0,null); return false;
	}
	return true;
}

function ac_keypress(e) {
	e = e||window.event;
	var p=e.target||e.srcElement;
	switch(e.keyCode) {
		case 13: return !ac_commit();
		case 8: break;
		default: ac_plan_req(); break;
	}
	return true;
}

/* Social album info */

var _sq=null,_sc=0;

function sa_update(e,i,t)
{
	e=e||window.event;
	e.returnValue=false;
	if(t<0){t=_st[i]}
	if (document&&document.getElementById&&(_sm!=i||t!=_st[_sm])) {
		if(_sm!=i){
			var p=document.getElementById(_si[_sm]);
			if (p){p.className=''}
			_sm=i; p=document.getElementById(_si[_sm]);
			if (p){p.className='hot'}
			document.cookie='MC='+_sm+'; expires='+_sx+'; path=/';
		}else{
			document.cookie='M'+_sm+'='+t+'; expires='+_sx+'; path=/';
		}
		_st[_sm]=t;
		p=document.getElementById(_sz);
		if (p){p.style.height=p.offsetHeight+'px';
			p.innerHTML='<p id="social_progress"><img src="/images/spin.gif" /> Loading users...</p>'}
		_sc++;
		if (_sq){document.body.removeChild(_sq)}
		_sq=document.createElement('script');
		_sq.type='text/javascript';
		var url = '/abbey?u='+encodeURI(_su)+'&a='+_sa+'&s='+_sb+'&m='+_sm+'&v='+_st[_sm]+'&x='+_sc;
		_sq.src = url;
		document.body.appendChild(_sq);
	}
}

/* User activity */

var _uq=null,_uc=0;

function ua_update(e,i)
{
	e=e||window.event;
	e.returnValue=false;
	if (document&&document.getElementById&&_um!=i) {
		var p=document.getElementById(_ui[_um]);
		if (p){p.className=''}
		_um=i; p=document.getElementById(_ui[_um]);
		if (p){p.className='hot'}
		document.cookie='UC='+_um+'; expires='+_ux+'; path=/';
		p=document.getElementById(_uz);
		if (p){p.style.height=p.offsetHeight+'px';
			p.innerHTML='<p id="recent_progress"><img src="/images/spin2.gif" /> Loading updates...</p>'}
		_uc++;
		if (_uq){document.body.removeChild(_uq)}
		_uq=document.createElement('script');
		_uq.type='text/javascript';
		var url = '/quarry?u='+encodeURI(_uw)+'&m='+_um+'&x='+_uc;
		_uq.src = url;
		document.body.appendChild(_uq);
	}
}

/* Friend management */

var _fq=null,_fs=null,_fa=null;

function fr_add(u) {
	if (!_fq&&document&&document.getElementById) {
		_fq=document.createElement('script');
		_fq.type='text/javascript';
		_fq.src='/fmanage?s=add&u='+encodeURI(u);
		document.body.appendChild(_fq);
		var e=document.getElementById('profile_friend');
		if(e){e.blur();}
		e=document.getElementById('profile_stat');
		if(e&&!_fs){
			_fs=document.createElement('img');
			_fs.id='profile_spin';
			_fs.src='/images/spin.gif';
			e.appendChild(_fs);
		}
	}
}

function fr_end_add() {
	if (document&&document.getElementById) {
		if (_fs){_fs.style.display='none';}
		opacity('profile_friend', 100, 0, 500);
	}
	_fq=null;
}

function hide_banner(x,u) {
	opacity('banner', 100, 0, 300);
	document.cookie='hb=1; expires='+x+'; path=/'+encodeURI(u);
}
