/////////////////////////////////////////////////////////////////////////////
//
// Gladiator Components 
//
// http://www.gladiatorweb.com
//
// (c) 2006 by Edward H. Trager .  All Rights Reserved
//
// NAME: gladiatorIndicator.js
//
// AUTHOR: Edward H. Trager
//
// LAST UPDATE: 2006.11.01
//
// NOTE: SEE "LICENSE" FILE FOR LICENSING TERMS
//
//////////////////////////////////////////////////////////////////////////////

//
// 
//
var GLADIATOR_INDICATOR_INCLUDED=true;
if(typeof GLADIATOR_CORE_INCLUDED == 'undefined') alert('NOTA BENE: "gladiatorIndicator.js" REQUIRES "gladiatorCore.js"!');
// DISABLE CHECKING SVG -- THIS IS AN IE WORK-AROUND:
//if(typeof GLADIATOR_SVG_INCLUDED == 'undefined') alert('NOTA BENE: "gladiatorIndicator.js" REQUIRES "gladiatorSVG.js"!');

function gWaitIndicator(parentNode){
	
	// Explicitly  initialize the window dimesions for browsers like Opera:
	DIMENSIONS.init();	
	var _self = this;
	
	// CREATE THE INDICATOR CANVAS:
	var _canvas = document.createElementNS(NS.xhtml,"div");
	_canvas.setAttribute("class","gWaitIndicator");
	
	var _width  = 64;
	var _height = 64;
	var _padding= 12;
	var _animationInterval = 70;
	var _spokeCount=11;
	
	//
	// POSITION is a global convenience enumeration object in gladiatorCore.js:
	//
	var _horizontalPosition=POSITION.center;
	var _verticalPosition  =POSITION.center;
	
	//
	// ADD SVG:
	//
	var _svg = new gSVG();
	_svg.drawHeader(_width,_height);
	_svg.addContent('<path class="Disc" d="M 32.593750,8.6250000 C 19.880500,8.6250000 9.5625000,18.943000 9.5625000,31.656250 C 9.5625000,44.369500 19.880500,54.687498 32.593750,54.687500 C 45.307000,54.687500 55.625000,44.369500 55.625000,31.656250 C 55.625000,18.943000 45.307000,8.6250000 32.593750,8.6250000 z M 32.593750,27.375000 C 34.956998,27.375001 36.875000,29.293002 36.875000,31.656250 C 36.875001,34.019498 34.956998,35.937500 32.593750,35.937500 C 30.230502,35.937498 28.312500,34.019498 28.312500,31.656250 C 28.312500,29.293001 30.230502,27.375000 32.593750,27.375000 z " />');
	_svg.addContent('<path class="LargeRing" d="M 32.558494,10.578434 C 20.915428,10.578434 11.437575,20.056291 11.437573,31.699357 C 11.437573,43.342423 20.915428,52.820283 32.558494,52.820278 C 44.201560,52.820278 53.679413,43.342423 53.679419,31.699357 C 53.679419,20.056291 44.201560,10.578430 32.558494,10.578434 z M 32.558494,13.487270 C 42.629078,13.487267 50.776379,21.628776 50.776379,31.699357 C 50.776374,41.769939 42.629078,49.917240 32.558494,49.917240 C 22.487914,49.917242 14.346408,41.769939 14.346408,31.699357 C 14.346408,21.628776 22.487914,13.487270 32.558494,13.487270 z " />');
	_svg.addContent('<path class="SmallRing" d="M 32.557405,24.025844 C 28.333080,24.025844 24.888568,27.470352 24.888573,31.694676 C 24.888573,35.919001 28.333080,39.374222 32.557405,39.374219 C 36.781728,39.374219 40.236948,35.919001 40.236948,31.694676 C 40.236948,27.470352 36.781728,24.025842 32.557405,24.025844 z M 32.557405,26.146555 C 35.630527,26.146553 38.105524,28.621552 38.105524,31.694676 C 38.105529,34.767798 35.630527,37.242798 32.557405,37.242798 C 29.484283,37.242800 27.009281,34.767798 27.009281,31.694676 C 27.009281,28.621552 29.484283,26.146555 32.557405,26.146555 z " />');
	_svg.addContent('<path class="GC" d="M 30.929242,14.782494 C 22.327417,15.605847 15.566573,22.880931 15.566578,31.692837 C 15.566578,40.504743 22.327431,47.786254 30.929242,48.610312 L 30.929242,47.775849 L 30.936374,47.775849 L 30.936374,40.686486 L 30.900714,40.686486 L 30.900714,40.180103 C 27.482825,39.522205 24.810459,36.872008 24.103769,33.475876 L 20.801580,33.475876 C 21.414613,37.519021 24.072812,40.880093 27.676979,42.519450 L 27.676979,44.188375 C 22.664559,42.233347 19.104127,37.409357 19.104127,31.692837 C 19.104123,24.796067 24.253453,19.142771 30.929242,18.341440 L 30.929242,14.782494 z M 34.188638,14.782494 L 34.188638,18.341440 C 40.863786,19.143353 46.013753,24.796514 46.013753,31.692837 C 46.013753,38.589087 40.863683,44.248506 34.188638,45.051366 L 34.188638,48.610312 C 42.787101,47.782961 49.551309,40.502339 49.551302,31.692837 C 49.551302,22.883334 42.787106,15.609142 34.188638,14.782494 z " />');
	_svg.addContent('<path class="color0" id="rot10" d="M 22.531216,5.4383545 C 24.414077,4.7283369 26.351625,4.2058193 28.421985,3.8942392 C 29.234739,5.6771209 29.961556,7.3271900 31.157123,8.6960092 C 31.875616,8.6178842 32.312860,8.6022592 33.054792,8.6335092 C 33.044375,9.2644316 33.065208,9.8641041 33.054792,10.495027 C 28.746127,10.468115 25.304649,11.409953 21.777234,13.461167 C 21.472190,12.918633 21.159333,12.391725 20.854288,11.849191 C 21.437518,11.502480 21.966061,11.226081 22.525854,10.965307 C 22.863579,9.3104895 22.716929,7.6869220 22.531216,5.4383545 z " />');
	_svg.addContent('<path class="color0" id="rot09" d="M 9.9444730,15.029408 C 11.144573,13.414151 12.492048,11.927063 14.065294,10.545624 C 15.712925,11.606072 17.216458,12.601252 18.962273,13.106404 C 19.524471,12.652235 19.883857,12.402698 20.524905,12.027868 C 20.857244,12.564266 21.198978,13.057480 21.531318,13.593878 C 17.892086,15.900679 15.506126,18.553608 13.647644,22.186265 C 13.097708,21.894775 12.549648,21.620655 11.999712,21.329166 C 12.302910,20.722176 12.598116,20.203902 12.928059,19.681878 C 12.317510,18.107168 11.316373,16.820621 9.9444730,15.029408 z " />');
	_svg.addContent('<path class="color0" id="rot08" d="M 4.5411234,29.902839 C 4.6774373,27.895176 5.0070251,25.915659 5.5836605,23.902958 C 7.5430569,23.904287 9.3459433,23.928615 11.087721,23.409717 C 11.315129,22.723698 11.482554,22.319476 11.819190,21.657573 C 12.388769,21.929143 12.942905,22.159306 13.512486,22.430875 C 11.698122,26.338995 11.125206,29.860727 11.525715,33.921482 C 10.905489,33.973583 10.296231,34.039282 9.6760044,34.091384 C 9.6029074,33.416830 9.5710507,32.821230 9.5663888,32.203695 C 8.2014099,31.209054 6.6636403,30.667997 4.5411234,29.902839 z " />');
	_svg.addContent('<path class="color0" id="rot07" d="M 8.0367195,45.336430 C 7.0659698,43.573781 6.2730288,41.730317 5.6699769,39.725374 C 7.3190436,38.667162 8.8488797,37.712914 10.033619,36.334713 C 9.8540365,35.634652 9.7763441,35.204082 9.7016880,34.465255 C 10.327671,34.385776 10.918274,34.279813 11.544257,34.200333 C 12.130807,38.468970 13.552832,41.741380 16.085171,44.940970 C 15.591572,45.320120 15.114551,45.704780 14.620952,46.083930 C 14.194768,45.555979 13.845963,45.072152 13.508176,44.555169 C 11.822140,44.456387 10.235969,44.832602 8.0367195,45.336430 z " />');
	_svg.addContent('<path class="color0" id="rot06" d="M 19.321416,56.430110 C 17.551809,55.472102 15.888092,54.349979 14.296818,52.989348 C 15.111988,51.207570 15.883062,49.577713 16.134616,47.777778 C 15.605060,47.285939 15.306917,46.965724 14.844672,46.384546 C 15.328312,45.979252 15.767872,45.570805 16.251512,45.165510 C 19.052750,48.439403 22.018233,50.423524 25.878402,51.746104 C 25.668144,52.331925 25.474812,52.913418 25.264554,53.499240 C 24.620593,53.285512 24.065583,53.067069 23.501917,52.814776 C 22.030127,53.643217 20.899153,54.817258 19.321416,56.430110 z " />');
	_svg.addContent('<path class="color0" id="rot05" d="M 34.812414,59.661732 C 32.805788,59.812527 30.799514,59.768010 28.725236,59.483682 C 28.447699,57.544041 28.215201,55.756043 27.453703,54.105841 C 26.742303,53.978379 26.318369,53.870186 25.615294,53.631176 C 25.803040,53.028745 25.951998,52.447494 26.139743,51.845062 C 30.266295,53.084773 33.833715,53.150661 37.796138,52.176321 C 37.935976,52.782819 38.087714,53.376525 38.227553,53.983024 C 37.570268,54.151376 36.985264,54.267672 36.374679,54.360171 C 35.584419,55.852809 35.267718,57.451926 34.812414,59.661732 z " />');
	_svg.addContent('<path class="color0" id="rot04" d="M 49.591412,54.005274 C 47.984855,55.216995 46.273002,56.264218 44.374289,57.146466 C 43.092160,55.664783 41.929906,54.286322 40.397126,53.309780 C 39.729747,53.587163 39.314617,53.725341 38.593934,53.904384 C 38.426177,53.296084 38.237241,52.726571 38.069483,52.118271 C 42.211199,50.930200 45.247926,49.056935 48.054561,46.095020 C 48.500099,46.529637 48.948730,46.947059 49.394269,47.381675 C 48.932343,47.878657 48.503081,48.292768 48.039432,48.700691 C 48.181604,50.383625 48.779726,51.900111 49.591412,54.005274 z " />');
	_svg.addContent('<path class="color0" id="rot03" d="M 58.966189,41.256620 C 58.269773,43.144555 57.395841,44.951034 56.275519,46.719750 C 54.395865,46.166449 52.672862,45.635175 50.855446,45.642338 C 50.443976,46.236501 50.169451,46.577180 49.659971,47.117431 C 49.189973,46.696391 48.723128,46.319434 48.253129,45.898395 C 51.095045,42.659745 52.636940,39.442075 53.396702,35.432971 C 54.006483,35.557719 54.609572,35.666328 55.219354,35.791075 C 55.099446,36.458899 54.962213,37.039348 54.792706,37.633182 C 55.822171,38.972093 57.145218,39.924474 58.966189,41.256620 z " />');
	_svg.addContent('<path class="color0" id="rot02" d="M 59.960317,25.463375 C 60.395149,27.428118 60.636606,29.420308 60.650372,31.513938 C 58.769969,32.064689 57.033257,32.549279 55.508223,33.537874 C 55.483300,34.260173 55.436540,34.695190 55.300020,35.425123 C 54.677002,35.325023 54.080468,35.260301 53.457449,35.160202 C 54.097274,30.899220 53.654796,27.358732 52.126464,23.575300 C 52.706888,23.350572 53.272958,23.115886 53.853382,22.891156 C 54.113562,23.517793 54.311928,24.080292 54.490381,24.671499 C 56.080292,25.241292 57.708206,25.327193 59.960317,25.463375 z " />');
	_svg.addContent('<path class="color0" id="rot01" d="M 52.258158,11.639792 C 53.686183,13.057552 54.966368,14.602948 56.109850,16.356781 C 54.825713,17.836725 53.626686,19.183325 52.878219,20.839480 C 53.247757,21.460591 53.443608,21.851831 53.723392,22.539699 C 53.145156,22.792318 52.608329,23.060381 52.030094,23.313001 C 50.264689,19.382518 47.978321,16.643289 44.647128,14.286741 C 45.013915,13.783887 45.363242,13.280416 45.730029,12.777560 C 46.287691,13.164057 46.758677,13.530016 47.228433,13.930893 C 48.874005,13.550663 50.289935,12.742811 52.258158,11.639792 z " />');
	_svg.addContent('<path class="color0" id="rot00" d="M 38.305093,4.1747392 C 40.272922,4.5953877 42.185387,5.2033376 44.095539,6.0605431 C 43.815371,7.9998084 43.534712,9.7808835 43.800447,11.578782 C 44.447119,11.901506 44.823400,12.124754 45.430658,12.552163 C 45.080792,13.077297 44.774110,13.593037 44.424244,14.118172 C 40.814112,11.766087 37.409757,10.697805 33.333333,10.516328 C 33.370030,9.8949994 33.391707,9.2825923 33.428403,8.6612633 C 34.106494,8.6849099 34.700566,8.7381407 35.312479,8.8214111 C 36.491256,7.6118776 37.245655,6.1667598 38.305093,4.1747392 z " />');
	_svg.drawFooter();
	_svg.render(_canvas);
	
	//
	// set position based on DIMENSIONS:
	//
	_canvas.style.width  = _width  + "px";
	_canvas.style.height = _height + "px";
	//
	// Place in center:
	//
	_position();
	
	
	//
	// add to DOM:
	//
	if(parentNode){
		parentNode.appendChild(_canvas);
	}else{
		var bodyRef = document.getElementsByTagName("body").item(0);
		bodyRef.appendChild(_canvas);
	}
	
	var _rotArray = new Array();
	for(var i=0;i<_spokeCount;i++){
		_rotArray[i] = document.getElementById("rot"+padWithZeros(i,2));
	}
	
	var _index=0;
	
	////////////////////
	//
	// PRIVATE METHODS
	//
	////////////////////
	
	//
	// position()
	//
	function _position(){
		if( _horizontalPosition > 0 ){
			//
			// If the horizontal pos. is greater than 
			// zero (POSITION.center==0), then
			// interpret as a fixed position:
			//
			_canvas.style.left = _horizontalPosition + "px";
		}else{
			//
			// Treat as a symbolic position:
			//
			switch(_horizontalPosition){
			case POSITION.center:
				_canvas.style.left = DIMENSIONS.horizontalCenter - 0.5*_width + "px";
				break;
			case POSITION.left:
				_canvas.style.left = _padding + "px";
				break;
			case POSITION.right:
				_canvas.style.left = DIMENSIONS.width - _padding - _width + "px";
				break;
			}
		}
		if( _verticalPosition > 0 ){
			//
			// If the vertical pos. is greater than 
			// zero (POSITION.center==0), then
			// interpret as a fixed position:
			//
			_canvas.style.top = _verticalPosition + "px";
		}else{
			//
			// Treat as a symbolic position:
			//
			switch(_verticalPosition){
			case POSITION.center:
				_canvas.style.top  = DIMENSIONS.verticalCenter   - 0.5*_height + "px";
				break;
			case POSITION.top:
				_canvas.style.top  = _padding + "px";
				break;
			case POSITION.bottom:
				_canvas.style.top  = DIMENSIONS.height - _padding - _height + "px";
				break;
			}
		}
	}
	
	//
	// paint:
	//
	function _paint(){
		
		// Paint rot spokes:
		var i=_index;
		_rotArray[i].setAttribute("class","color0");
		i++; if(i>=_spokeCount) i=0;
		_rotArray[i].setAttribute("class","colorA");
		i++; if(i>=_spokeCount) i=0;
		_rotArray[i].setAttribute("class","colorB");
		i++; if(i>=_spokeCount) i=0;
		_rotArray[i].setAttribute("class","colorC");
		i++; if(i>=_spokeCount) i=0;
		_rotArray[i].setAttribute("class","colorD");
		//i++; if(i>=_spokeCount) i=0;
		// Increment index:
		_index++;
		if(_index>=_spokeCount) _index=0;
	}
	
	var _intervalId=false;
	
	//
	// animate:
	//
	var _animate=false;
	this.animate = function(animateOrNot){
		// No state change?
		if(animateOrNot == _animate) return;
		// Get here if state change:
		_animate = animateOrNot;
		if(_animate){
			_intervalId = window.setInterval(_paint,_animationInterval);
		}else{
			if(_intervalId) window.clearInterval(_intervalId);
			_intervalId=false;
		}
	}
	
	////////////////////
	//
	// PUBLIC METHODS
	//
	////////////////////
	
	var _show;
	//
	// show:
	//
	this.show = function(showIt){
		// No state change?
		if(showIt == _show) return;
		// Get here if state change:
		_show=showIt;
		if(_show){
			_canvas.style.display="block";
			_self.animate(true);
		}else{
			_self.animate(false);
			_canvas.style.display="none";
		}
	}
	
	//
	// setPosition():
	// 
	// => If horizontal and vertical are symbolic constants
	//    from the POSITION enumeration object, then the relative position
	//    is set, possibly using _padding.
	// => Otherwise, if horizontal and vertical are numerals greater than
	//    zero ( POSITION.center == 0 ), then the position is set to a fixed
	//    position in pixels.
	//
	this.setPosition = function(horizontal,vertical){
		// Set the symbolic positioning:
		_horizontalPosition = horizontal;
		_verticalPosition   = vertical;
		// Make it take effect:
		_position();
	}
	
	//
	// setPadding():
	//
	this.setPadding = function(padding){
		_padding = padding;
	}
	
	//
	// Initially hide from view:
	//
	_self.show(false);
	
	///////////////////
	//
	// EVENT HANDLERS:
	//
	///////////////////
	
	//
	// reposition the indicator whenver the window is resized:
	//
	window.addEventListener("resize",_position,false);
	
}

