/**
 * cvi_busy_lib.js 1.02 (12-Aug-2010)
 * (c) by Christian Effenberger 
 * All Rights Reserved
 * Source: dev.netzgesta.de
 * Distributed under Netzgestade Software License Agreement
 * http://www.netzgesta.de/cvi/LICENSE.txt
 * License permits free of charge
 * use on non-commercial and 
 * private web sites only 
 * syntax:
	OBJECT = getBusyOverlay(parent, olcolor, olopacity, oltext, txstyle/txclass, arguments);
	OBJECT.remove();
	parent		== element to add the overlay (a.k.a. document.getElementById(id))
	olcolor 	== STR 'black' or '#000000' or 'rgb(0,0,0)' Default: 'white'
	olopacity   == FLOAT 0.0 - 1.0  Default: 0.0
	oltext   	== STR e.g. "loading" Default: ''
	txstyle   	== STR e.g. "color: black;" Default: ''
	txclass   	== STR e.g. "my_text_class" Default: ''
	arguments 	== from getBusy() color, size, iradius, weight, type, count, speed, minopac
 * syntax:	
	OBJECT = getBusy(context, color, size, iradius, weight, type, count, speed, minopac);
	OBJECT = getBusyIE(object, color, size, iradius, weight, type, count, speed, minopac);
	OBJECT.start();
	OBJECT.pause();
	OBJECT.stop();
	object 	== object (a.k.a. document.getElementById(id))
	context == context (a.k.a. canvas.getContext("2d"))
	color   == STR '#000000' - '#ffffff' or '#000' - '#fff' Default: '#000'
	size	== INT 16 - 512 (pixel) Default: 32
	iradius == INT 6 - 254 (pixel) Default: 8
	weight  == INT 1 - 254 (pixel) Default: 3
	type	== STR 'circle|oval|polygon|rectangle|tube' or 'c|o|p|r|t' Default: 'tube'
	count   == INT 5 - 36 (rays) Default: 12
	speed   == INT 30 - 1000 (millisec) Default: 96
	minopac == FLOAT 0.0 - 0.5  Default: 0.25
 *
**/

function testfun(o) {
	//testfun({color:'black', opacity:0.5, text:'loading', styles:'color:#000;'}); 
	window.document.title=o.color+'/'+o.opacity+'/'+o.text+'/'+o.styles;
	//window.document.title=overlay['color']+'/'+overlay['opacity']+'/'+overlay['text']+'/'+overlay['styles'];
}
function onIEWinResize(event) {
	function parseWidth(val) {return (isNaN(parseInt(val,10))?0:parseInt(val,10));}
	if(!event) {event=window.event;}
	var i, cs, parent=this, div=parent.getElementsByTagName("div");
	if(div.length>0) {
		if(parent.currentStyle){cs=parent.currentStyle;}else if(document.defaultView&&document.defaultView.getComputedStyle){cs=document.defaultView.getComputedStyle(parent,"");}else{cs=parent.style;}
		for(i=0; i<div.length; i++) {
			if(div[i].className=='buzy_ele') {
				div[i].style.height=(parent.offsetHeight-parseWidth(cs.borderBottomWidth)-parseWidth(cs.borderTopWidth));
				div[i].style.width=(parent.offsetWidth-parseWidth(cs.borderLeftWidth)-parseWidth(cs.borderRightWidth)); 
				div[i].firstChild.style.height=div[i].style.height; div[i].firstChild.style.width=div[i].style.width; 
				break;
			}
		}
	}
}
function getBusyOverlay(parent,color,opacity,text,styles,cl,sz,ir,w,tp,ct,sp,mo) {
	if(typeof(parent)==='object') {
		function parseWidth(val) {return (isNaN(parseInt(val,10))?0:parseInt(val,10));}
		var isIE,isVL,isCV,cz,cs,type,size,inner,outer,string,canvas,context,ctrl,waiting=true;
		if(parent.currentStyle){cs=parent.currentStyle;}else if(document.defaultView&&document.defaultView.getComputedStyle){cs=document.defaultView.getComputedStyle(parent,"");}else{cs=parent.style;}
		if(cs.display.search(/block|inline-block|table|inline-table|list-item/i)>=0) {
			opacity=Math.max(0.0,Math.min(1.0,opacity||0)); color=(color==''?'white':color);
			canvas=document.createElement("canvas"); isCV=canvas.getContext?1:0; 
			isIE=navigator.appName=='Microsoft Internet Explorer'&&window.navigator.systemLanguage&&(!document.documentMode||document.documentMode<9)?1:0;
			isVL=document.all&&document.namespaces&&!window.opera&&(!document.documentMode||document.documentMode<9)?1:0; 
			outer=document.createElement('div'); 
			parent.style.position=(cs.position=='static'?'relative':cs.position);
			cz=parent.style.zIndex>=0?(parent.style.zIndex-0+2):2;		
			outer.style.position='absolute'; outer.style.overflow='hidden';
			outer.style.display='block'; outer.style.zIndex=cz; 
			outer.style.left=0+'px'; outer.style.top=0+'px';
			outer.style.width='100%'; outer.style.height='100%';
			if(isIE) {outer.className='buzy_ele'; outer.style.zoom=1; outer.style.margin='0px'; outer.style.padding='0px'; outer.style.height=(parent.offsetHeight-parseWidth(cs.borderBottomWidth)-parseWidth(cs.borderTopWidth)); outer.style.width=(parent.offsetWidth-parseWidth(cs.borderLeftWidth)-parseWidth(cs.borderRightWidth));}
			if(typeof(cs.borderRadius)=="undefined"){				
				if(typeof(cs.MozBorderRadius)!="undefined"){outer.style.MozBorderRadiusTopleft=cs.MozBorderRadiusTopleft; outer.style.MozBorderRadiusTopright=cs.MozBorderRadiusTopright; outer.style.MozBorderRadiusBottomleft=cs.MozBorderRadiusBottomleft; outer.style.MozBorderRadiusBottomright=cs.MozBorderRadiusBottomright;}else 
				if(typeof(cs.WebkitBorderRadius)!="undefined"){outer.style.WebkitBorderTopLeftRadius=cs.WebkitBorderTopLeftRadius; outer.style.WebkitBorderTopRightRadius=cs.WebkitBorderTopRightRadius; outer.style.WebkitBorderBottomLeftRadius=cs.WebkitBorderBottomLeftRadius; outer.style.WebkitBorderBottomRightRadius=cs.WebkitBorderBottomRightRadius;}
			}else {outer.style.borderTopLeftRadius=cs.borderTopLeftRadius; outer.style.borderTopRightRadius=cs.borderTopRightRadius; outer.style.borderBottomLeftRadius=cs.borderBottomLeftRadius; outer.style.borderBottomRightRadius=cs.borderBottomRightRadius;}
			parent.appendChild(outer);
			inner=document.createElement('div');
			inner.style.position='absolute'; inner.style.cursor='progress';
			inner.style.display='block'; inner.style.zIndex=(cz-1);
			inner.style.left=0+'px'; inner.style.top=0+'px';
			inner.style.width=100+'%'; inner.style.height=100+'%';
			inner.style.backgroundColor=color;
			if(isIE) {inner.style.zoom=1; inner.style.margin='0px'; inner.style.padding='0px'; inner.style.height=outer.style.height; inner.style.width=outer.style.width; }
			if(typeof(cs.borderRadius)=="undefined"){				
				if(typeof(cs.MozBorderRadius)!="undefined"){inner.style.MozBorderRadiusTopleft=cs.MozBorderRadiusTopleft; inner.style.MozBorderRadiusTopright=cs.MozBorderRadiusTopright; inner.style.MozBorderRadiusBottomleft=cs.MozBorderRadiusBottomleft; inner.style.MozBorderRadiusBottomright=cs.MozBorderRadiusBottomright;}else 
				if(typeof(cs.WebkitBorderRadius)!="undefined"){inner.style.WebkitBorderTopLeftRadius=cs.WebkitBorderTopLeftRadius; inner.style.WebkitBorderTopRightRadius=cs.WebkitBorderTopRightRadius; inner.style.WebkitBorderBottomLeftRadius=cs.WebkitBorderBottomLeftRadius; inner.style.WebkitBorderBottomRightRadius=cs.WebkitBorderBottomRightRadius;}		
			}else {inner.style.borderTopLeftRadius=cs.borderTopLeftRadius; inner.style.borderTopRightRadius=cs.borderTopRightRadius; inner.style.borderBottomLeftRadius=cs.borderBottomLeftRadius; inner.style.borderBottomRightRadius=cs.borderBottomRightRadius;}
			if(isIE) {inner.style.filter="alpha(opacity="+parseInt(opacity*100)+")";}else {inner.style.opacity=opacity;}			outer.appendChild(inner); size=Math.max(16,Math.min(512,sz||32));
			if(isVL){
				if(document.namespaces['v']==null) {
					var e=["shape","shapetype","group","background","path","formulas","handles","fill","stroke","shadow","textbox","textpath","imagedata","line","polyline","curve","roundrect","oval","rect","arc","image"],s=document.createStyleSheet(); 
					for(var i=0; i<e.length; i++) {s.addRule("v\\:"+e[i],"behavior: url(#default#VML);");} document.namespaces.add("v","urn:schemas-microsoft-com:vml");
				}
			}
			canvas.style.position='absolute'; 
			canvas.style.cursor='progress'; canvas.style.zIndex=(cz-0+1);
			canvas.style.top='50%'; canvas.style.left='50%';
			canvas.style.marginTop='-'+(size/2)+'px';
			canvas.style.marginLeft='-'+(size/2)+'px';
			canvas.width=size; canvas.height=size;
			canvas.style.width=size+"px"; canvas.style.height=size+"px";
			outer.appendChild(canvas);
			if(typeof(text)!="undefined"){				
				string=document.createElement('div');
				string.style.position='absolute'; string.style.overflow='hidden'; 
				string.style.cursor='progress'; string.style.zIndex=(cz-0+1);
				string.style.top='50%'; string.style.left='0px';
				string.style.marginTop=2+(size/2)+'px'; string.style.textAlign='center'; 
				string.style.width=100+'%'; string.style.height='auto';
				if(typeof(styles)!="undefined"){
					string.innerHTML='<span '+(styles.match(/:/i)?'style':'class')+'="'+styles+'">'+text+'</span>';
				}else {
					string.innerHTML='<span>'+text+'</span>';
				}
				outer.appendChild(string);
			}
			if(isVL){
				ctrl=getBusyIE(canvas,cl,sz,ir,w,tp,ct,sp,mo); ctrl.start();
			}else if(isCV){
				ctrl=getBusy(canvas.getContext("2d"),cl,sz,ir,w,tp,ct,sp,mo); ctrl.start();
			}
			if(isIE) { parent.onresize = onIEWinResize; }
			return {remove: function (){if(waiting){waiting=false; ctrl.stop(); delete ctrl; parent.removeChild(outer);} } };
		}
	}
}
function getBusyIE(obj,cl,sz,ir,w,tp,ct,sp,mo) {
	var running=false,os=0,al=0,f=100,c,i,h,p,t,x,y,hs,qs,hw,qw,rp,sh,fl,ele=new Array();;
	c=getHEX(cl); tp=tp||"t"; t=(tp.match(/^[coprt]/i)?tp.substr(0,1).toLowerCase():'t');
	ct=Math.max(5,Math.min(36,ct||12)); sp=Math.max(30,Math.min(1000,sp||96));
	sz=Math.max(16,Math.min(512,sz||32)); ir=Math.max(1,Math.min((sz/2)-2,ir||sz/4));
	w=Math.max(1,Math.min((sz/2)-ir,w||sz/10)); mo=Math.max(0,Math.min(0.5,mo||0.25));
	h=(sz/2)-ir; x=sz/2; y=x; al=360/ct; hs=parseInt((sz/2)*f); qs=parseInt(hs/2); 
	hw=parseInt((w/2)*f); qw=parseInt(hw/2); rp=hs-parseInt(ir*f); 
	switch(t) {
		case "c": p='m '+hs+','+(rp-hw)+' ar '+(hs-hw)+','+(rp-hw-hw)+','+(hs+hw)+','+rp+','+(hs-hw)+','+(rp-hw-hw)+','+(hs-hw)+','+(rp-hw-hw)+' e'; break;
		case "p": p='m '+(hs-qw)+',0 l '+(hs-hw)+','+rp+','+(hs+hw)+','+rp+','+(hs+qw)+',0 x e'; break;
		case "o": p='m '+hs+','+(rp-qs)+' ar '+(hs-hw)+',0,'+(hs+hw)+','+rp+','+(hs-hw)+',0,'+(hs-hw)+',0 e'; break;
		case "t": p='m '+(hs-hw)+','+rp+' l '+(hs-hw)+','+hw+' qy '+hs+',0 qx '+(hs+hw)+','+hw+' l '+(hs+hw)+','+rp+' x e'; break;
		default: p='m '+(hs-hw)+',0 l '+(hs-hw)+','+rp+','+(hs+hw)+','+rp+','+(hs+hw)+',0 x e'; break;
	} 
	for(i=0;i<ct;i++) {
		sh=document.createElement('v:shape');
		sh.setAttribute('filled','t');
		sh.setAttribute('stroked','f');
		sh.setAttribute('coordorigin','0,0');
		sh.setAttribute('coordsize',(sz*f)+','+(sz*f));
		sh.setAttribute('path',p);
		sh.style.rotation=(i*al);
		sh.style.position='absolute'; sh.style.margin='0px';
		sh.style.width=sz+'px'; sh.style.height=sz+'px';
		sh.style.top='-1px'; sh.style.left='-1px';
		obj.appendChild(sh);
		fl=document.createElement('v:fill');
		fl.setAttribute('color',c);
		fl.setAttribute('opacity',Math.min(1,Math.max(mo,1-((ct+1-i)/(ct+1)))));
		sh.appendChild(fl);
		ele[i]=fl;
	}
	function nextLoop(){
		if(!running) {return;} os=(os+1)%ct; 
		if(document.documentMode==8) {
			for(i=0;i<ct;i++) {al=((os+i)%ct); ele[al].opacity=Math.min(1,Math.max(mo,1-((ct+1-i)/(ct+1))));}
		}else {
			for(i=0;i<ct;i++) {al=((os+i)%ct); ele[al].setAttribute('opacity',Math.min(1,Math.max(mo,1-((ct+1-i)/(ct+1)))));}
		}
		setTimeout(nextLoop,sp);
	}
	function getHEX(v){
		var col=v||'#000000';
		if(!col.match(/^#[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]$/i)) {
			if(v.match(/^#[0-9a-f][0-9a-f][0-9a-f]$/i)) {col='#'+v.substr(1,1)+v.substr(1,1)+v.substr(2,1)+v.substr(2,1)+v.substr(3,1)+v.substr(3,1);}
		}return col;
	}
	nextLoop(0);
	return {		
		start: function (){if(!running){running=true; nextLoop(0);}},
		stop: function (){running=false; for(i=0;i<ct;i++) {ele[i].setAttribute('opacity',0);}},
		pause: function (){running=false; }
	};
}
function getBusy(ctx,cl,sz,ir,w,tp,ct,sp,mo) {
	var running=false,os=0,al=0,c,i,h,t,x,y;
	c=getRGB(cl); tp=tp||"t"; t=(tp.match(/^[coprt]/i)?tp.substr(0,1).toLowerCase():'t');
	ct=Math.max(5,Math.min(36,ct||12)); sp=Math.max(30,Math.min(1000,sp||96));
	sz=Math.max(16,Math.min(512,sz||32)); ir=Math.max(1,Math.min((sz/2)-2,ir||sz/4));
	w=Math.max(1,Math.min((sz/2)-ir,w||sz/10)); mo=Math.max(0,Math.min(0.5,mo||0.25));
	h=(sz/2)-ir; x=sz/2; y=x;
	function nextLoop(){
		if(!running) {return;}
		os=(os+1)%ct; ctx.clearRect(0,0,sz,sz); ctx.save(); ctx.translate(x,y);
		for(i=0;i<ct;i++){ al=2*((os+i)%ct)*Math.PI/ct; 
			ctx.save(); ctx.translate(ir*Math.sin(-al),ir*Math.cos(-al)); ctx.rotate(al);
			ctx.fillStyle='rgba('+c+','+Math.min(1,Math.max(mo,1-((ct+1-i)/(ct+1))))+')';
			switch(t) {
				case "c": drawCirc(ctx,w/2,h); break;
				case "o": drawOval(ctx,w,h); break;
				case "p": drawPoly(ctx,w,h); break;
				case "t": drawTube(ctx,w,h); break;
				default: ctx.fillRect(-w/2,0,w,h); break;
			} ctx.restore();
		} ctx.restore();
		setTimeout(nextLoop,sp);
	}
	function getRGB(v){
		function hex2dec(h){return(Math.max(0,Math.min(parseInt(h,16),255)));}
		var r=0,g=0,b=0; v = v||'#000'; if(v.match(/^#[0-9a-f][0-9a-f][0-9a-f]$/i)) {
			r=hex2dec(v.substr(1,1)+v.substr(1,1)),g=hex2dec(v.substr(2,1)+v.substr(2,1)),b=hex2dec(v.substr(3,1)+v.substr(3,1));
		}else if(v.match(/^#[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]$/i)) {
			r=hex2dec(v.substr(1,2)),g=hex2dec(v.substr(3,2)),b=hex2dec(v.substr(5,2));
		} return r+','+g+','+b;
	}
	function drawOval(ctx,w,h){ctx.beginPath(); ctx.moveTo(-w/2,h/2); ctx.quadraticCurveTo(-w/2,0,0,0); ctx.quadraticCurveTo(w/2,0,w/2,h/2); ctx.quadraticCurveTo(w/2,h,0,h); ctx.quadraticCurveTo(-w/2,h,-w/2,h/2); ctx.fill();}
	function drawTube(ctx,w,h){ctx.beginPath(); ctx.moveTo(w/2,0); ctx.lineTo(-w/2,0); ctx.lineTo(-w/2,h-(w/2)); ctx.quadraticCurveTo(-w/2,h,0,h); ctx.quadraticCurveTo(w/2,h,w/2,h-(w/2)); ctx.fill();}
	function drawPoly(ctx,w,h){ctx.beginPath(); ctx.moveTo(w/2,0); ctx.lineTo(-w/2,0); ctx.lineTo(-w/4,h); ctx.lineTo(w/4,h); ctx.fill();}
	function drawCirc(ctx,r,z){ctx.beginPath(); ctx.arc(r,r,r,0,Math.PI*2,false); ctx.fill();}  
	nextLoop(0);
	return {
		start: function (){if(!running){running=true; nextLoop(0);}},
		stop: function (){running=false; ctx.clearRect(0,0,sz,sz); },
		pause: function (){running=false; }
	};
}
