(function(){
	Yankee=window.Yankee || {};
	var C= document.cookie;
	var L= location.pathname;
	if(C){
		if(C.indexOf('therecanbeonlyone')){
			if(C.indexOf(encodeURIComponent(L))!= -1) alert('Yikes!') //location= 'colortable.html'// redirect if cookie has been set
		}
		document.cookie= 'therecanbeonlyone='+encodeURIComponent(L);// no path, no expires
		window.onunload= function(){
			document.cookie= 'therecanbeonlyone='; // reset on normal unload
		}		
	}
})()
String.prototype.tlclean= function(){
	return this.toLowerCase().replace(/^['"]| {2,}|['"]$/g,'');
}
String.prototype.dasher= function(){
	return this.replace(/([a-z]+)([A-Z])/g,'$1-$2').toLowerCase();
}

function check_colorcode(obj){
	if(typeof Memry!= 'object') Memry= {};
	Memry.colornames= {
		aqua:'#00ffff',black:'#000000',blue:'#0000ff',
		fuchsia:'#ff00ff',gray:'#808080',green:'#008000',lime:'#00ff00',
		maroon:'#800000',navy:'#000080',olive:'#808000',purple:'#800080',
		red:'#ff0000',silver:'#c0c0c0',teal:'#008080',white:'#ffffff',yellow:'#ffff00'
	}
	var CP= {
		isHexColor: function(){
			var c= this.tlclean();
			if(/^\#[a-f0-9]{3}$/.test(c)){
				var i= 1, A= c.split('');
				while(i<4) A[i]+= ''+A[i++];
				return A.join('');
			}
			return (/^\#[a-f0-9]{6}$/.test(c))? c: false;
		},
		isRgb: function (){
			var c= this.tlclean();
			c= /^rgb\((\d{1,3}%?,){2}\d{1,3}%?\)$/.test(c) ? c : '';
			if (/%/.test(c)){
				c= c.replace(/(\d+%?)/g,function(w){
					return Math.round(parseInt(w)*2.55)+'';
				})
			}
			return c || false;
		},
		padZero: function(len,c){			
			var s= this;
			c=c|| '0';
			while(s.length< len) s= c+s;
			return s;
		},
		toHexColor: function(){
			var c= this.tlclean();
			var tem= Memry.colornames[c] || c.isHexColor();
			if(!tem){
				c= c.isRgb();
				if(c){
					c= c.replace('rgb(','').replace(/\)$/,'');
					tem= '#'+c.replace(/(\d+,?)/g,function(w){
						return parseInt(w).toString(16).padZero(2);
					})
				}
			}
			return tem || '';
		},
		toRgb: function(){
			var c= this.tlclean();
			var tem= c.isRgb() || '';
			if(!tem) {
				var hex= c.toHexColor();
				if(!hex) return '';
				hex= hex.replace(/^#/,'0x');
				tem= [(hex>>16) & 255,(hex>> 8) & 255, hex & 255];
				return 'rgb('+tem.join(',')+')';
			}
			return tem || '';
		},
		tlclean: function(){
			return this.toLowerCase().replace(/ +/g,'').replace(/^['"]|['"]$/g,'');
		}
	}
	if(typeof obj!= 'object')obj=String.prototype;
	for(var p in CP) obj[p]= CP[p];
	return CP;
}

// the following code is for colorcode.html, using the methods defined above
function mr(hoo){
	if(typeof hoo== 'string') hoo= document.getElementById(hoo);
	if(hoo && hoo.nodeType== 1) return hoo;
	return false;
}

onload=function(){
if(!document.createTextNode || !check_colorcode()) return false;
	mr('colorinputset').style.display='block';
	
	if(typeof Memry!= 'object') Memry= {};
	if(!String.prototype.camelCase){
		String.prototype.camelCase= function(){
			return this.dasher().replace(/\-([a-z])/g, function(a, b){
				return b.toUpperCase();
			});
		}
	}
	if(typeof Run!= 'object') Run= {
		addElement: function(wot,pa,s,c,att){
			var el= document.createElement(wot);
			if(s) el.appendChild(document.createTextNode(s));
			Run.reStyle(el,c,att)
			if(pa) pa.appendChild(el);
			return el;
		},
		addEve: function(who,what,fun){
			who= mr(who);
			if(who.addEventListener){
				who.addEventListener(what,fun,false);
			}
			else if(who.attachEvent){
				who.attachEvent('on'+what,fun);
			}
		},
		alias:function(p){
			var s= p.toLowerCase();					
			switch(s){
				case 'bg': return 'background';			
				case 'bgc': return 'backgroundColor';		
				case 'c': return 'color';
				default: return p;					
			}
		},
		collect:function(hoo){
			if(!hoo) return false;
			var A= [], who, elements, L;
			if(mr(hoo)){
				A[0]= mr(hoo);
				return A;
			}
			if(typeof hoo== "string") A= hoo.split(/\W+/);
			else{
				if(hoo.list) hoo= hoo.toList();
				if(hoo.length) A= hoo;
				else if(typeof hoo== "object"){
					for(var o in hoo){
						if(mr(o)) A.push(o);
						else if(mr(hoo[o])) A.push(hoo[o]);
					}
					return A;
				}
			}
			elements= [];
			L= A.length;
			for(var i= 0; i< L; i++){
				who= mr(A[i]);
				if(who) elements.push(who);
			}
			return elements;
		},
		deepStyle: function(hoo,cssprop){
			var who= mr(hoo), val= '', str= '';
			if(!who || who.style== undefined) return '';
			str= cssprop.camelCase();
			if(who.currentStyle) val= who.currentStyle[str];
			else {
				var dv= document.defaultView || window;
				if(dv && dv.getComputedStyle){
					str= str.dasher(true);
					val= dv.getComputedStyle(who,'').getPropertyValue(str);
				}
			}
			if(!val) val= who.style[str];
			return val || '';
		},
		getClass:function(s,pa,what){
			pa= mr(pa);
			pa= pa||document.getElementsByTagName('body')[0];

			what= what || '*';
			s= RegExp('\\b'+s+'\\b');
			var i=0,tem,B=[];
			A= pa.getElementsByTagName(what);
			while(A[i]){
				tem=A[i++];
				if(s.test(tem.className))B.push(tem);
			}
			return B;
		},			
		pauseEvent:function(e){
			e= Run.Eve(e)|| '';
			if(e){
				if(e.stopPropagation) e.stopPropagation();
				if(e.cancelable && e.preventDefault) e.preventDefault();
				event.cancelBubble= true;
				if(event.returnValue) event.returnValue= false;
			}
			return false;
		},
		quitThis: function(){
			mr('input_color').value='';
		},
		randomColor: function(radx){
			var str= 'rgb(', A=[];
			for(var i= 0; i<3; i++){
				if(radx== 'safe'){
					var tem= Math.round(Math.random()*5);
					A.push( (tem*20)+ '%');
				}
				else A.push(Math.round(Math.random()*255));
			}
			str+= A.join(',')+')';
			return (radx== 'safe')? str.isRgb(): str;
		},
		reStyle:function(hoo,obj,atts){
			var i= 0,who, A= Run.collect(hoo),s;
			while(A[i]){
				who= mr(A[i++]);
				if(who){
					for(var p in obj){
						if(p.length<4)s= Run.alias(p);
						else s= p;
						who.style[s]= obj[p];
					}
					for(var p in atts)who[p]= atts[p];
				}
			}
		},
		remove:function(hoo,boo){
			var i=0,who, A=Run.collect(hoo);
			while(A[i]){
				who=mr(A[i++]);
				if(who){
					if(boo!==false) who.parentNode.removeChild(who);
					else while(who.lastChild) who.removeChild(who.lastChild);
				}
			}
		}
	}

	if(!Run.colors) Run.colors= {
		contrast: function(bg,c,boo){
			try{
				bg= bg.toRgb();
				c= c.toRgb();
			}
			catch(er){
				bg= bg|| 'rgb(255,255,255)';
				c= c|| 'rgb(0,0,0)';
			}
			var Rx= /(\d+),(\d+),(\d+)/;
			var BG= bg.match(Rx);
			var FG= c.match(Rx);
			var B1= (BG[1]*299 + BG[2]*587 + BG[3]*114)/ 1000;
			var F1= (FG[1]*299 + FG[2]*587 + FG[3]*114)/ 1000;
			var bright= Math.round(Math.abs(B1-F1));
			var diff= (Math.max(BG[1], FG[1]) - Math.min(BG[1], FG[1])) +
			(Math.max(BG[2],FG[2]) - Math.min(BG[2],FG[2])) +
			(Math.max(BG[3],FG[3]) - Math.min(BG[3],FG[3]));
			if(boo=== true)return !!(bright>125 && diff>500);
			return [bright,diff];
		},
		bcFlip: function(c,a,b){
			var x= c.toRgb();
			if(!x)return '';
			if(!a) a= 'rgb(255,255,255)';
			if(!b) b= 'rgb(0,0,0)';
			if(Run.colors.contrast(x,b,true))return b;
			if(Run.colors.contrast(x,a,true))return a;
			var bd= Run.colors.contrast(x,b);
			var cd= Run.colors.contrast(x,a);
			if((bd[0]*4+ bd[1])> (cd[0]*4+ cd[1])) return b;
			if((cd[0]*4+ cd[1])> (bd[0]*4+ bd[1])) return a;
			return (bd[0]< cd[0])? a: b;
		},
		named: function(c){
			c= c.tlclean();
			if(Memry.colornames[c]) return c;
			c= c.toHexColor();
			if(c){
				var G= Memry.colornames;
				for(var p in G){
					if(G[p]== c) return p;
				}
			}
			return '';
		}
	}
	Run.colordemo= {
		addrow: function (c){
			Run.reStyle('morecolorSpan',{bgC:'white',c:'white'});
			mr('morecolorSpan').removeAttribute('title');
			if(Memry.splashcount)Memry.splashcount= 0;
			var tem= c.tlclean();
			c= c.toRgb();
			if(!c) return;
			var bc= Run.colors.bcFlip(c);
			var who= mr('colorTable');
			var pa= Run.addElement('tr');
			var B= 'toHexColor,toRgb,getColorname'.split(',');
			var temp,j= 3,s;
			while(j){
				temp= B[--j];
				s= '';
				if(temp== 'getColorname'){
					s= {fontWeight:'bold',c:bc,bgC:c};
					temp= Run.colors.named(c) || ' ';
				}
				else temp= String(eval('"'+tem+'".'+ temp+'()'));
				Run.addElement('td',pa,temp,s);
			}
			who.insertBefore(pa,who.firstChild);
			setTimeout(function(){
				mr('input_color').select();
				mr('input_color').focus();
			},
			100);
		},
		getString: function(e){
			var who= mr('input_color');
			var c= who.value? who.value.toRgb(): '';
			var pa= who.parentNode;
			if(c.isRgb()){
				Run.reStyle(pa,{bgC:c,c: Run.colors.bcFlip(c)});
				return Run.colordemo.addrow(c);
			}
			else{
				who.value= '';
				Run.reStyle(pa,{bgC:'',c:''});
				return false;
			}
		},		
		splash:function(){
			var A= mr('colorsplash').getElementsByTagName('span');
			var L= A.length,tem,temp;
			if(--Memry.splashcount<= 0){
				Memry.splashcount=0;
				clearInterval(Memry.splashTimer);
				delete Memry.splashTimer;
				for(var i= 0;i<L;i++){
					tem= A[i];
					temp= tem.style.color;
					if(!temp){
						temp= Run.randomColor();
						Run.reStyle(tem,{c:temp,bgC:temp},{title:temp});
					}
				}
			}
			var i= Math.floor(Math.min(4,L/4));
			while(i--){
				var n= Math.floor(Math.random()*L);
				var who= A[n];
				var c= Run.randomColor();
				Run.reStyle(who,{c:c,bgC:c},{title:c});
			}
		},
		startDemo:function(){
			Run.remove(Run.getClass('noscriptCss'));
			Run.addEve('ranColorBtn','click',function(){
				var who= mr('input_color');
				who.focus();
				var diff= Math.round(Math.random()*10);
				var c= Run.randomColor();
				if(diff> 5)c= c.toHexColor();
				who.value= c;
				return Run.colordemo.getString();
			})
			Run.addEve('showcolorTableBtn','click',function(){
				for(var p in Memry.colornames){
					Run.colordemo.addrow(p);
				}
				mr('showcolorTableBtn').style.visibility='hidden';
			})
			Run.addEve('input_color','keypress',function(e){
				if(Memry.splashcount)Memry.splashcount= 0;
				if(e.keyCode== 13){				
					e= Run.Eve(e)|| '';
					var who= mr('morecolorSpan');
					var exC= who.title,x='';
					var c= mr('input_color').value.tlclean();
					try{						
						who.style.color= c;
						who.style.backgroundColor= c;
						x= Run.deepStyle(who,'color').tlclean();						
						who.title= x;
						if(x==exC  || (x!=c && !x.isRgb())) throw'Yikes!';
					}
					catch(er){
						who.removeAttribute('title');
						Run.reStyle(who,{c:'white',bgC:'white'})
					}
					Run.colordemo.getString();
					return Run.pauseEvent(e);
				}
				return true;
			})
			document.getElementsByTagName('h1')[0].ondblclick= function(){
				location.reload(true)
			}
			mr('input_color').onchange=Run.colordemo.getString;
			Run.colordemo.startSplash(100);
			mr('input_color').value='';
			setTimeout(function(){mr('input_color').focus()},100);
		},
		startSplash:function(n){
			Memry.splashcount= typeof n=='number'? n : 50;
			var t= Math.floor(1.5*n);
			Memry.splashTimer= setInterval('Run.colordemo.splash()',t);
			Run.addEve('colorsplash','click',function(e){
				Memry.splashcount= 0;
				e= Run.Eve(e)|| '';
				e= e.target || e.srcElement;
				if(e && e.title){
					mr('input_color').focus();
					mr('input_color').value=e.title;
					mr('input_color').onchange();
				}
			})
			Run.addEve('colorinputset','dblclick',function(){
				mr('input_color').value='';
				mr('input_color').onchange();				
				if(!Memry.splashTimer){
					Memry.splashcount=40;
					Memry.splashTimer= setInterval('Run.colordemo.splash()',100);
				}
				else Memry.splashcount=0;
				mr('input_color').focus();
			})
			var who= mr('colorsplash');
			var sib= who.firstChild;
			
			var s= who.offsetHeight;
			while(who.offsetHeight== s){
				var el= sib.cloneNode(true);
				who.appendChild(el);
			}
			while(who.lastChild && who.offsetHeight>s){
				who.removeChild(who.lastChild);
				who.removeChild(who.lastChild);				
			}
		}
	}
	window.onunload= Run.quitThis;
	Run.colordemo.startDemo();
}
