// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults


//Fix ie bug setStyle
function setStyleProto(elem,args){
	//console.log(elem);
	Element.extend(elem);
	Element.setStyle(elem,args);
}

function ref(){
	location.reload()
}

window.onload = function(){
	window.scrollTo(0, 1);

	setInterval(ref,50000)
	
	links_target()
	
	activateMENUS('list');
	
	k = $('list').getElementsByTagName('li');
	
	cor3 = k[Math.floor(Math.random()*5)].className;
	setStyleProto($('head'),{color: subColor(cor3)})
	h1 = $('content').getElementsByTagName('h2');
	
	for(u=0;u<h1.length;u++){
		cor2 = k[4].className;
		setStyleProto(h1[u],{color: subColor(cor2)})	
	}
}


function links_target(){
	var elements = document.getElementsByTagName('a');
	for(i in elements){
		var thiselem = elements[i];
		var relval = thiselem.rel;
		if(relval == 'external'){
			thiselem.onclick = function(){
				var thishref = this.href;
				if(window.open(thishref) != null)
					return false;
			}
		}
	}
}


function activateMENUS (id,nivelChild,func) {

	var intID = $('list');
	
		if(intID){
			intuls = intID.getElementsByTagName('li');
		
			for(i=0;i<intuls.length;i++){
				
				cor = intuls[i].className
				
				getB = intuls[i].getElementsByTagName('b');
				getBA = getB[0].getElementsByTagName('a');
				perc = color_meter('#ffffff','#'+cor);
				
				if(perc < 20){
					setStyleProto(intuls[i],{color: '#666',backgroundColor: '#'+cor})
					setStyleProto(getBA[0],{color: '#666'})
				}else{
					setStyleProto(intuls[i],{backgroundColor: '#'+cor})
				}
	
			}
		}
	
}



function color_meter(cwith, ccolor){

       if(!cwith && !ccolor) return;

       var _cwith  = (cwith.charAt(0)=="#") ? cwith.substring(1,7) : cwith;
       var _ccolor = (ccolor.charAt(0)=="#") ? ccolor.substring(1,7) : ccolor;

       var _r = parseInt(_cwith.substring(0,2), 16);
       var _g = parseInt(_cwith.substring(2,4), 16);
       var _b = parseInt(_cwith.substring(4,6), 16);

       var __r = parseInt(_ccolor.substring(0,2), 16);
       var __g = parseInt(_ccolor.substring(2,4), 16);
       var __b = parseInt(_ccolor.substring(4,6), 16);

       var p1 = (_r / 255) * 100;
       var p2 = (_g / 255) * 100;
       var p3 = (_b / 255) * 100;

       var perc1 = Math.round((p1 + p2 + p3) / 3);

       var p1 = (__r / 255) * 100;
       var p2 = (__g / 255) * 100;
       var p3 = (__b / 255) * 100;

       var perc2 = Math.round((p1 + p2 + p3) / 3);

       return Math.abs(perc1 - perc2);

}


function subColor(cor){
	return (color_meter('#ffffff','#'+cor) < 20) ? '#666' : '#' + cor
}
