
	var core					= { local: { is: 'ICICLE:local', document: document, window: window } };
		core.dom				= {};
		core.dom.getById		= function( id, doc ){ if( ! doc ) doc = document; return doc.getElementById( id ); }
		core.dom.getTag			= function( name, doc, index ){ if( ! doc ) doc = document; var a = doc.getElementsByTagName( name ); if( typeof index == 'undefined' ){ return a[0]; }else if( index === false ){ return a; }else{ return a[index]; } }
		core.dom.getByFlash		= function( id, local ){if(navigator.appName.indexOf("Microsoft") != -1){return local.window[id];}else{return local.document[id];}}
		core.dom.createElement	= function( type, local ){ if( ! local ) local = core.local; return local.document.createElement( type ); }
		core.dom.insertElement	= function( src, tar ){ if( ! src ) return; if( ! tar ){ tar = core.dom.getTag( 'BODY', document ); }else if( tar.is == 'ICICLE:layer' ){ if( tar.holder ) tar = tar.holder; }else if( tar.is == 'ICICLE:local' ){ if( tar.element ){ tar = tar.element; }else{ tar = core.dom.getTag( 'BODY', tar.document ); } } tar.appendChild( src ); }
		core.dom.removeNode		= function( node ){ if( node.removeNode ){ node.removeNode( true ); }else if( node.parentNode && node.removeChild ){ node.parentNode.removeChild( node ); } }
		core.exception			= function( type, text ){ return { type: type, description: text }; }

		core.flash = {};
		core.flash.components = [];
		core.flash.addParams = function( code, params ){ var hIE = '', hMO = ''; for( var i in params ){ hIE += '<param name="'+i+'" value="'+params[i]+'" />'; hMO += ' '+i+'="'+params[i]+'"'; } return code.replace(/<embed/i, hIE+'<embed'+hMO ); }
		core.flash.insertAndReturn = function( id, flashCode, local, flashParams ){

			if( core.flash.components[ id ] ){

				return core.flash.components[ id ];

			}else{

				var div =	core.dom.createElement( 'DIV', local );
							core.dom.insertElement( div, local );

					if( flashParams ) flashCode = core.flash.addParams( flashCode, flashParams );

					div.innerHTML = flashCode;

				var wrapper			= {};
					wrapper.div		= div;
					wrapper.movie	= core.dom.getByFlash( id, local );

					core.flash.components[ id ] = wrapper;

					return wrapper;

			}

		}

		core.events = {};
		core.events.addEventListener = function( object, eventname, listener, third ){

			if( object ){

				if( object.addEventListener ){

					object.addEventListener( eventname, listener, third )

				}else if( object.attachEvent ){

					object.attachEvent( 'on'+eventname, listener );

				}

			}

		}

		core.flash.fireFunction = function( object, movie, functionName, args ){

			try{

				if( movie && functionName && movie[ functionName ] && args ){

					var res;
					var code = 'res = movie[functionName](', i;
					var lof = args.length;
					for( i = 0; i < lof; i++ ){ code += 'args['+i+'],'; }

					eval( code = code.substring(0,code.length-1) + ');' );

					return res;

				}

			}catch(ee){

				object.error( 'javascript:firefunction', ee );

			}

		}

	var polygraph			= {};
		polygraph.instances = [];
		polygraph.onEvent	= function( o ){

			var p;
			var id		= o.id;
			var evn		= o.event;
			var args	= o.args;
			
			if( ( p = polygraph.instances[ id ] ) && p.ready ){

				setTimeout( function(){ p.fireEvent( evn, args ) }, 1 );

			}

		}

		polygraph.onError = function( poly, ee ){

			alert( ee.description );

		}

		polygraph.ready		= function( id, count ){

			var p;
			
			if( p = polygraph.instances[ id ] ){

				if( p.ready ){
			
					p.start();

				}else{

					if( ! count ) count = 0;
					if( count > 10 ) return;
					setTimeout( function(){ polygraph.ready( id, count+1 ); }, 10 );

				}
				
			}
				
		}

		polygraph.instance	= function( id, target, eventEar ){

			if( ! id ) return;
			if( typeof target == 'string' ){ target = core.dom.getById( target ); }

			if( target ){

				polygraph.instances[ id ] = ( this );

				this.getMovie = function(){ return movie; }

				this.error = function( type, ee ){

					if( eventEar && eventEar.onError ){

						eventEar.onError( myself, core.exception( type, ( ee && ee.description ? ee.description : ''+ee ) ) );

					}else{

						polygraph.onError( myself, core.exception( type, ( ee && ee.description ? ee.description : ''+ee ) ) );

					}

				}

				this.start = function(){
				
					var ee;
					
					try{

						var obj = movie.getSetup(); eval( 'obj.start = '+obj.start );
							obj.start( obj, myself ); if( eventEar ) myself.addListener( eventEar, 'object' );
							myself.fireEvent( 'onReady' );

					}catch( ee ){

						myself.error( 'javascript:startcomponent', ee );
					
					}

				}

				this.getSize = function(){

					return { width: w, height: h };

				}

				this.kill = function(){

					delete polygraph.instances[ id ];

					if( eventEar ) myself.removeListener( eventEar, 'object' );

					core.dom.removeNode( movie );
					core.dom.removeNode( wrapper.div );

				}

				var dest					= { is: 'ICICLE:local', document: document, window: window, element: target };
				var path					= '/polygraph/plugin.swf?nc='+( new Date().getTime() );///?unique='+escape(id);
				var myself					= this;
				var w						= 600;
				var h						= 400;

				var flashstring				= '';
					flashstring				+= '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+w+'" height="'+h+'" id="'+id+'" align="middle">';
					flashstring				+= '<param name="allowScriptAccess" value="always" />';
					flashstring				+= '<param name="movie" value="'+path+'" />';
					flashstring				+= '<param name="quality" value="high" />';
					flashstring				+= '<param name="bgcolor" value="#ffffff" />';
					flashstring				+= '<embed src="'+path+'" quality="high" bgcolor="#ffffff" width="'+w+'" height="'+h+'" name="'+id+'" align="middle" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
					flashstring				+= '</object>';

				var wrapper					= core.flash.insertAndReturn( id, flashstring, dest );
				var movie					= wrapper.movie;
					myself.ready			= true;

				this.movie = movie;

			}

		}

	var InfoItem = function( scope, parent, id, title, link, text, classes, styler ){

		this.id				= id;
		this.scope			= scope;
		this.holder			= core.dom.createElement( 'DIV' );
		this.symbol			= core.dom.createElement( 'DIV' );
		this.symboltext		= document.createTextNode('A');
		var table			= core.dom.createElement( 'TABLE' );
		var tbody			= core.dom.createElement( 'TBODY' );
		var row				= core.dom.createElement( 'TR' );
		var c1				= core.dom.createElement( 'TD' );
		var c2				= core.dom.createElement( 'TD' );

		parent.appendChild( this.holder );
		this.holder.appendChild( table );
		table.appendChild( tbody )
		tbody.appendChild( row );
		row.appendChild( c1 );
		row.appendChild( c2 );
		c1.appendChild( this.symbol );
		this.symbol.appendChild( this.symboltext );
		c2.innerHTML = '<b>'+title+'</b><p>'+text+'</p>';

		if( classes ){

			this.classes = classes;

			if( classes['table'] ) table.className	= classes['table'];
			if( classes['holder'] ) this.holder.className	= classes['holder'];
			if( classes['symbol'] ) this.symbol.className	= classes['symbol'];
			if( classes['left'] )	c1.className			= classes['left'];
			if( classes['right'] )	c2.className			= classes['right'];

		}

		if( styler ) this.styler = styler;

		this.interaction();

	}

	InfoItem.prototype.interaction = function(){

		var infoitem = this;

		this.onclick = function(){ infoitem.onClick(); }

		if( this.symbol.attachEvent ){
			this.symbol.attachEvent( 'onclick', this.onclick );
		}else if( this.symbol.addEventListener ){
			this.symbol.addEventListener( 'click', this.onclick, true );
		}

	}

	InfoItem.prototype.onClick = function(){

		this.scope.polygraph.tellFlash('onClickInfoItem', { id: this.id } );
		this.scope.area.clearHighlights();
		this.highlight( true );

	}

	InfoItem.prototype.setSymbol = function( symbol ){

		this.symboltext.nodeValue = symbol;

	}

	InfoItem.prototype.hide = function(){

		this.holder.style.display = 'none';

	}

	InfoItem.prototype.show = function(){

		this.holder.style.display = '';

	}

	InfoItem.prototype.scrollVisible = function(){

		this.scope.area.scrollTo( 0, -this.holder.offsetTop );

	}

	InfoItem.prototype.highlight = function( on ){

		if( this.classes ){

			if( on ){

				if( this.styler ){

					this.styler( this, 'highlight' );
					this.scope.area.highlights[ this.id ] = true;

				}

			}else{

				if( this.styler ){

					this.styler( this, 'normal' );
					this.scope.area.highlights[ this.id ] = false;
					delete this.scope.area.highlights[ this.id ];

				}


			}

		}

	}

	var SimplePolygraph = function(  ){

		var simple						= this;
			simple.area					= {};
			simple.area.highlights		= {};
			simple.area.initialised		= false;
			simple.area.scrollTo		= function( x,y ){

				if( ! simple.area.ready ) return;

				if( simple.area.scrollTo.arguments.length ){

					var h1 = simple.area.holder.offsetHeight;
					var h2 = simple.area.inner.scrollHeight;

					if( h2 > h1 ){

						if( ( h2 + y ) < h1 ){

							y = -( h2 - h1 );

						}

					}else{

						y = 0;

					}

					simple.area.inner.style.left	= x+'px';
					simple.area.inner.style.top		= y+'px';

				}else{

					simple.area.inner.style.left	= '0px';
					simple.area.inner.style.top		= '0px';

				}

			}
			simple.area.clearHighlights = function(){

				var i, infoitem;

				for( i in simple.area.highlights ){

					if( ! simple.area.highlights[i] ) continue;

					if( infoitem = simple.info.items[i] ){

						infoitem.obj.highlight( false );

					}

				}

				simple.area.highlights = {};

			}

			simple.create = function( name, target, infoarea ){

				simple.area.holder = core.dom.getById( infoarea );
				simple.area.inner = core.dom.createElement( 'DIV' );
				simple.area.holder.appendChild( simple.area.inner );

				simple.area.holder.style.position = 'relative';
				//simple.area.holder.style.overflow = 'hidden';

				simple.area.inner.style.position	= 'absolute';
				simple.area.inner.style.left		= '0px';
				simple.area.inner.style.top			= '0px';

				simple.polygraph = new polygraph.instance( name, target, ear );

			}

		var ear	= simple.ear = {};

			ear.onReady = function( poly ){

				if( simple.onReady ) simple.onReady( poly );

			}

			ear.onInfoReorder = function( poly, list ){

				var i, infoitem, lof = simple.info.items.length;

				if( ! simple.info || ! simple.info.ready ){

					alert('here');

				}else{

					simple.area.clearHighlights();
					simple.area.ready = false;

					for( i = lof-1; i > 0; i-- ){

						if( ( infoitem = simple.info.items[i] ) && ( infoitem.obj ) && ( list[ infoitem.obj.id ] ) ){

							infoitem.obj.setSymbol( list[ infoitem.obj.id ] );
							infoitem.obj.show();

						}else{

							infoitem.obj.hide();

						}

					}

					simple.area.ready = true;
					simple.area.initialised = true;
					simple.area.scrollTo();

				}

			}

			ear.onInfoParsed = function( poly, info ){

				if( info ){

					simple.info = info;
					simple.info.ready = false;

					var i, infoitem, lof = simple.info.items.length;
					var parent = simple.area.inner;

					var classes				= {};
						classes.holder		= 'infoitem';
						classes.table		= 'pgii-table';
						classes.symbol		= 'pgii-symbol';
						classes.left		= 'pgii-left';
						classes.right		= 'pgii-right';

					var styler = function( obj, type ){

						switch( type ){
						case 'highlight':

							obj.symbol.style.color = 'red';
							obj.symbol.style.borderColor = 'red';

						break;
						case 'normal':

							obj.symbol.style.color = '';
							obj.symbol.style.borderColor = '';

						break;
						}

					}

					for( i = lof-1; i > 0; i-- ){

						if( infoitem = simple.info.items[i] ){

							infoitem.obj = new InfoItem( simple, parent, i, infoitem.title, infoitem.link, infoitem.text, classes, styler );
							infoitem.obj.hide();

						}

					}

					simple.info.ready = true;

				}

			}

			ear.onInfoItemClipSlip = function( poly, o ){

				if( o && o.id && simple.info && simple.info.items ){

					var infoitem;

					if( infoitem = simple.info.items[ o.id ] ){

						simple.area.clearHighlights();
						///infoitem.obj.setSymbol( o.symbol );
						infoitem.obj.highlight( true );
						infoitem.obj.scrollVisible();

					}

				}

			}

			ear.onInfoItemClick = function( poly, o ){

				setTimeout( function(){ ear.onInfoItemClipSlip( poly, o ) }, 1 );

			}

			ear.onTrace = function( poly, msg ){

				if( simple.onTrace ){

					simple.onTrace( poly, msg );

				}

			}

			ear.onSetPref = function( poly, o ){

				if( simple.onSetPref ){

					simple.onSetPref( poly, o.name, o.value );

				}

			}

			ear.onXML = function( poly, success ){

				if( simple.onXML ){

					simple.onXML( poly, success );

				}else{

					if( success ){

						poly.setRange( -20, 20 );

					}

				}

			}

			ear.onError = function( poly, err ){

				if( simple.onError ){

					simple.onError( poly, err );

				}else{

					alert( err.description );

				}

			}

	}

