var ts = false, sm = false;
function showat(obj,id,xextra,yextra)
{
	var xextra = (xextra == null) ? 0 : xextra;
	var yextra = (yextra == null) ? 0 : yextra;
	var coors = findPos(obj);
	var x = document.getElementById(id);
	x.style.top = coors[1] + yextra + 'px';
	x.style.left = coors[0] + xextra + 'px';
	$S(id).display = 'block';
}
function hideat(id) {
	if (ts == false) {
		$S(id).display = 'none';
	}
}
function rollover() {
	ts = true;
}
function rollout(id) {
	document.getElementById(id).select()
	document.getElementById(id).focus();
	ts = false;
}
function findPos(obj)
{
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}
function logout() {
	document.logoutForm.submit();
}
function login() {
	show('login_form');
	if (document.getElementById('username').value == "") {
		textbox = 'username';
	}
	else {
		textbox = 'password';
	}
	document.getElementById(textbox).select();
	document.getElementById(textbox).focus();
}
function show(id,time) {
	e=document.getElementById(id);
	if (e.style.left != "auto") {
		if (time != false) {
			if (time == null) {
				time = 250;
			}
			p=1000/20;
			t=0;
			s= 100/(time/p);
			o=0;
			changeOpac(o,id);
			e.style.left= "auto";
			while (o<=100) {
				setTimeout("changeOpac("+Math.round(o)+",'"+id+"')",t);
				o=o+s;
				t = t+p;
			}
		}
		else {
			e.style.display = "block";
		}
	}
}

function hide(id,time) {
	e=document.getElementById(id);
	if (e) {
		if (e.style.left != "-999em") {
			if (time != false) {
				if (time == null) {
					time = 250;
				}
				p=1000/20;
				t=0;
				s= 100/(time/p);
				o=0;
				o=100;
				changeOpac(o,id);
				while (o>=0) {
					setTimeout("changeOpac("+Math.round(o)+",'"+id+"')",t);
					o=o-s;
					t = t+p;
				}
				setTimeout('document.getElementById("'+id+'").style.left = "-999em";',t+p);
			}
			else {
				e.style.display = "none"
			}
		}
	}
}
function changeOpac(opacity, id) { 
    var object = document.getElementById(id).style; 
    object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")"; 
}
function showhide(boxid,time) {
	if (document.getElementById(boxid).style.display=='none' || document.getElementById(boxid).style.display=="") {
		show(boxid,time);
	}
	else {
		hide(boxid,time);
	}
}

function uncheck(formref,start,end) {
	for (var i=0;i<formref.elements.length;i++) {
		var e = formref.elements[i];
		if (!isNaN(e.name.substring(4,5)*1)) {
			if (!isNaN(e.name.substring(5,6)*1)) {
				if (!isNaN(e.name.substring(6,7)*1)) {
					ci = e.name.substring(4,7)*1;
				}
				else {
					ci = e.name.substring(4,6)*1;
				}
			} 
			else {
				ci = e.name.substring(4,5)*1;
			}
		}
		else {
			ci = -1;
		}
		
		if ((e.name.substring(0,4) == 'url|') && (ci>=start) && (ci<=end)) {
			e.checked = false;
		}
	}
}
function hoverhelp(e,helphtml) {
	sethtml("helpbox",helphtml);
	showatmouse(e,"helpbox");
}
function hidehelp() {
	if (sm = true) {
		sm = false;
		hide("helpbox",false);
	}
}
function sethtml(boxid,html) {
	document.getElementById(boxid).innerHTML = html;
}
function showatmouse(e,boxid) {
	var posx=0,posy=0;
	if(e==null) e=window.event;
	if(e.pageX || e.pageY){
	    posx=e.pageX; posy=e.pageY;
    }
	else if(e.clientX || e.clientY){
	    if(document.documentElement.scrollTop){
	        posx=e.clientX+document.documentElement.scrollLeft;
	        posy=e.clientY+document.documentElement.scrollTop;
        }
	    else{
	        posx=e.clientX+document.body.scrollLeft;
	        posy=e.clientY+document.body.scrollTop;
        }
    }
	document.getElementById(boxid).style.top=(posy+10)+"px";
	document.getElementById(boxid).style.left=(posx)+"px";
	sm = true;
	show(boxid,false);
}
function hoverlinkinfo(e,linkinfoid) {
    hoverhelp(e,document.getElementById(linkinfoid).innerHTML);
}

function new_link(set_id) {
	show('new_link');
	document.getElementById('new_link_set_id').value = set_id;
	document.getElementById('new_link_url').select();
	document.getElementById('new_link_url').focus();
}
var colourpickobj
function colourpickfor(obj,xextra,yextra) {
	colourpickobj = obj;
	showat(obj,'mini',xextra,yextra);
}
/* DHTML Color Sphere v1.0.1, Programming by Ulyses, ColorJack.com */
/* Updated August 24th, 2007 */

function $(v) { return(document.getElementById(v)); }
function $S(v) { return(document.getElementById(v).style); }
function absPos(e) { var r={x:e.offsetLeft,y:e.offsetTop}; if(e.offsetParent) { var v=absPos(e.offsetParent); r.x+=v.x; r.y+=v.y; } return(r); }
function agent(v) { return(Math.max(navigator.userAgent.toLowerCase().indexOf(v),0)); }
function isset(v) { return((typeof(v)=='undefined' || v.length==0)?false:true); }
function toggle(i,t,xy) { var v=$S(i); v.display=t?t:(v.display=='none'?'block':'none'); if(xy) { v.left=xy[0]; v.top=xy[1]; } }
function XY(e,v) { var z=agent('msie')?Array(event.clientX+document.body.scrollLeft,event.clientY+document.body.scrollTop):Array(e.pageX,e.pageY); return(v==3?z:z[zero(v)]); }
function XYwin(v) { var z=agent('msie')?[document.body.clientHeight,document.body.clientWidth]:[window.innerHeight,window.innerWidth]; return(!isNaN(v)?z[v]:z); }
function zero(v) { v=parseInt(v); return(!isNaN(v)?v:0); }
function zindex(d) { d.style.zIndex=zINDEX++; }	

/* PLUGIN */

var stop=1;

function cords(W) {

	var W2=W/2, rad=(hsv[0]/360)*(Math.PI*2), hyp=(hsv[1]+(100-hsv[2]))/100*(W2/2);

	$S('mCur').left=Math.round(Math.abs(Math.round(Math.sin(rad)*hyp)+W2+3))+'px';
	$S('mCur').top=Math.round(Math.abs(Math.round(Math.cos(rad)*hyp)-W2-21))+'px';

}

function coreXY(o,e,xy,z,fu) {

	function point(a,b,e) { eZ=XY(e,3); commit([eZ[0]+a,eZ[1]+b]); }
	function M(v,a,z) { return(Math.max(!isNaN(z)?z:0,!isNaN(a)?Math.min(a,v):v)); }

	function commit(v) { if(fu) fu(v);
	
		if(o=='mCur') { var W=parseInt($S('mSpec').width), W2=W/2, W3=W2/2; 

			var x=v[0]-W2-3, y=W-v[1]-W2+21, SV=Math.sqrt(Math.pow(x,2)+Math.pow(y,2)), hue=Math.atan2(x,y)/(Math.PI*2);

			hsv=[hue>0?(hue*360):((hue*360)+360), SV<W3?(SV/W3)*100:100, SV>=W3?Math.max(0,1-((SV-W3)/(W2-W3)))*100:100];

			$('mHEX').innerHTML=hsv2hex(hsv); colourpickobj.value="rgb("+hsv2rgb(hsv)+")"; cords(W);
			changebgc();
			colourpickobj.select(); colourpickobj.focus();
		}
		else if(o=='mSize') { var b=Math.max(Math.max(v[0],v[1])+oH,75); cords(b);

			$S('mini').height=(b+28)+'px'; $S('mini').width=(b+20)+'px';
			$S('mSpec').height=b+'px'; $S('mSpec').width=b+'px';

		}
		else {
		
			if(xy) v=[M(v[0],xy[0],xy[2]), M(v[1],xy[1],xy[3])]; // XY LIMIT

			if(!xy || xy[0]) d.left=v[0]+'px'; if(!xy || xy[1]) d.top=v[1]+'px';

		}
	}

	if(stop) { stop=''; var d=$S(o), eZ=XY(e,3); if(!z) zindex($(o));

		if(o=='mCur') { var ab=absPos($(o).parentNode); point(-(ab['x']-5),-(ab['y']-28),e); }
		
		if(o=='mSize') { var oH=parseInt($S('mSpec').height), oX=-XY(e), oY=-XY(e,1); } else { var oX=parseInt(d.left)-eZ[0], oY=parseInt(d.top)-eZ[1]; }

		document.onmousemove=function(e){ if(!stop) point(oX,oY,e); };
		document.onmouseup=function(){ stop=1; document.onmousemove=''; document.onmouseup=''; };

	}
}

/* CONVERSIONS */

function toHex(v) { v=Math.round(Math.min(Math.max(0,v),255)); return("0123456789ABCDEF".charAt((v-v%16)/16)+"0123456789ABCDEF".charAt(v%16)); }
function rgb2hex(r) { return(toHex(r[0])+toHex(r[1])+toHex(r[2])); }
function hsv2hex(h) { return(rgb2hex(hsv2rgb(h))); }

function hsv2rgb(r) { // easyrgb.com/math.php?MATH=M21#text21

    var R,B,G,S=r[1]/100,V=r[2]/100,H=r[0]/360;

    if(S>0) { if(H>=1) H=0;

        H=6*H; F=H-Math.floor(H);
        A=Math.round(255*V*(1.0-S));
        B=Math.round(255*V*(1.0-(S*F)));
        C=Math.round(255*V*(1.0-(S*(1.0-F))));
        V=Math.round(255*V); 

        switch(Math.floor(H)) {

            case 0: R=V; G=C; B=A; break;
            case 1: R=B; G=V; B=A; break;
            case 2: R=A; G=V; B=C; break;
            case 3: R=A; G=B; B=V; break;
            case 4: R=C; G=A; B=V; break;
            case 5: R=V; G=A; B=B; break;

        }

        return([R?R:0,G?G:0,B?B:0]);

    }
    else return([(V=Math.round(V*255)),V,V]);

}

/* GLOBALS */

var zINDEX=1000, hsv=[0,0,100];




function iscolour(v) {
	var ex = "^((#?([0-9a-fA-F]{6}|[0-9a-fA-F]{3})){1,1}|(rgb[(][0-9]{1,3},[0-9]{1,3},[0-9]{1,3}[)]){1,1}){1,1}$";
	v = v.match(ex);
	if (v) {
		return v[0];
	}
	else {
		return false;
	}
}
function clickautocolours() {
	if (document.getElementById('colour_details_checkbox').checked == true) {
		document.getElementById('fc').disabled = true;
		document.getElementById('lc').disabled = true;
		document.getElementById('lhc').disabled = true;
		changebgc();
	}
	else {
		document.getElementById('fc').disabled = false;
		document.getElementById('lc').disabled = false;
		document.getElementById('lhc').disabled = false;
	}
}

function noautocolours() {
	if (document.getElementById('colour_details_checkbox').checked == true) {
		document.getElementById('colour_details_checkbox').checked = false;
	}
}
function rgb(s,c) {
	if (c==null) {
		c = 1;
	}
	var r = "N";
	var g = "N";
	var b = "N";
	var i = 0;
	if (s.match("^(#?[0-9a-fA-F]{3}){1,1}$")) {
		if (s.substring(1,2)=='#') {
			i++;
		}
		r = parseInt(s.substring(i,i+1),16);
		g = parseInt(s.substring(i+1,i+2),16);
		b = parseInt(s.substring(i+2,i+3),16);
	}
	else if (s.match("^(#?[0-9a-fA-F]{6}){1,1}$")) {
		if (s.substring(1,2)=='#') {
			i++;
		}
		r = parseInt(s.substring(i,i+2),16);
		g = parseInt(s.substring(i+2,i+4),16);
		b = parseInt(s.substring(i+4,i+6),16);
	}
	else if (s.match("^(rgb[(][0-9]{1,3},[0-9]{1,3},[0-9]{1,3}[)]){1,1}$")) {
			s = s.substring(4,s.length-1);
			s = s.split(",");
			r = parseInt(s[0]);
			g = parseInt(s[1]);
			b = parseInt(s[2]);
	}
	if (r+g+b>300) {
		c = 1/c;
	}
	r = uc(r,c);
	g = uc(g,c);
	b = uc(b,c);
	if (r>g && r>b) {
		r=r+30;
	}
	else if (g>r && g>b) {
		g=g+30;
	}
	else if (b>r && b>g) {
		b=b+30;
	}
	return "rgb("+r+","+g+","+b+")";
}
function uc(n,c,m) {
	if (n==0) {
		n=1;
	}
	if (n<50) {
		n=n+(50/(n*n));
	}
	n=n+(10*(1/n));
	n = Math.round(n*c);
	if (n>255) {
		n=255;
	}
	return n;
}
function changebgc() {
	var v =document.getElementById('bgc').value;
	var bgcv = iscolour(v);
	if (bgcv) {	
		//document.getElementById('siteheader').style.backgroundColor = bgcv;
		//document.getElementById('sitemain').style.backgroundColor = bgcv;
		//document.getElementById('sitefooter').style.backgroundColor = bgcv;
		if (document.getElementById('colour_details_checkbox').checked == true) {
			document.getElementById('fc').value = 'rgb(102,102,102)';
			changefc(false);
			document.getElementById('lc').value = rgb(v,2.5);
			document.getElementById('lhc').value = rgb(v,1.5);
			changelc(false);
		}
	}
	if (bgcv == false || document.getElementById('colour_details_checkbox').checked == false) {
		changefc();
		changelc();
	}
}
function changefc(uncheck) {
	if (uncheck == true || uncheck == null) {
		noautocolours();
	}
	fc = iscolour(document.getElementById('fc').value);
	if (fc) {
		document.getElementById('content').style.color = fc;
	}
}
function changelc(uncheck) {
	if (uncheck == true || uncheck == null) {
		noautocolours();
	}
	lc = iscolour(document.getElementById('lc').value);
	lhc = iscolour(document.getElementById('lhc').value);
	if (lc && lhc) {
		setcolour(document.getElementsByTagName('a'));
		setbgcolour(document.getElementById('content').getElementsByTagName('a'));
	}
}
function setcolour(s) {
	for(var i=0;i<s.length;i++) {
		s[i].style.color = lc;
		s[i].onmouseover = function () {this.style.color = lhc};
		s[i].onmouseout = function () {this.style.color = lc};
	}
}
function setbgcolour(s) {
	for(var i=0;i<s.length;i++) {
		s[i].style.backgroundColor = lc;
		s[i].onmouseover = function () {this.style.backgroundColor = lhc};
		s[i].onmouseout = function () {this.style.backgroundColor = lc};
	}
}
