var iluminator;
var loading;
var preloaded_imgs = [];
var tagCloud;
var layout = 1;
preloadImages();
function initialize() {
    var content = document.getElementById("frame");
    loading = document.createElement("img");
    loading.className = "loading";
    loading.src = "images/loading.gif";
    content.appendChild(loading);
	document.getElementById('page').style.backgroundColor="#ffffff";
	iluminator=new Iluminator(200,document.getElementById('page'),40);
	iluminator.addColor('#ffffff');
	iluminator.addColor('#ffff77');	
	iluminator.addColor('#ff9966');
	iluminator.addColor('#cc99ff');		
	iluminator.addColor('#00ff77');		
	iluminator.addColor('#ffffff');
	iluminator.start();
	pngFrame();
	getLocation();
}
function getLocation() {
    if (window.location.hash)
        document.getElementById("mainwindow").src=window.location.hash.replace('#', '');
}
function showmenu(elmnt) {
    document.getElementById(elmnt).style.visibility = "visible";
}
function hidemenu(elmnt) {
    document.getElementById(elmnt).style.visibility = "hidden";
}

function preloadImages() {

    var img2 = new Image();
    img2.src = "images/pxorange.gif";
    preloaded_imgs.push(img2);
    var img3 = new Image();
    img3.src = "images/frames/frame1.png";
    preloaded_imgs.push(img3);
    var img4 = new Image();
    img4.src = "images/frames/frame2.png";
    preloaded_imgs.push(img4);
    var img5 = new Image();
    img5.src = "images/frames/frame3.png";
    preloaded_imgs.push(img5);
    var img6 = new Image();
    img6.src = "images/frames/frame1v.png";
    preloaded_imgs.push(img6);
    var img7 = new Image();
    img7.src = "images/frames/frame2v.png";
    preloaded_imgs.push(img7);
    var img8 = new Image();
    img8.src = "images/frames/frame3v.png";
    preloaded_imgs.push(img8);
}
function Iluminator(interval,element,stepsNo) {
	this.colors = [];
	this.active=false;
	this.start= function()
	{ 
		active = true;
		this.changeColor();
	}
	this.changeColor=function()
	{
		if(!active)return;
		this.currentColor=nextColor(this.currentColor,this.colors[this.colorIndex],this.colors[this.colorIndex+1],stepsNo);
		element.style.backgroundColor=this.currentColor;
		
		if(this.currentColor==this.colors[this.colorIndex+1])			{
		this.colorIndex++;
		if(this.colorIndex==this.colors.length-1)this.colorIndex=0;
		}
		setTimeout(function(o){
			return function()
			{
			o.changeColor();
			}
		}(this),interval);
	}
	this.currentColor=element.style.backgroundColor.canonizeColor();
	this.colorIndex=0;
	this.stop =  function()
	{
	}
	this.addColor = function(color)
	{
		this.colors.push(color);
	}
}

function nextColor(color,c1,c2,steps)
{
var fout=steps/Math.abs(steps);
colorHex = cutHex(color);
R= HexToR(colorHex);
G= HexToG(colorHex);
B= HexToB(colorHex);
colorHex1 = cutHex(c1);
R1= HexToR(colorHex1);
G1= HexToG(colorHex1);
B1= HexToB(colorHex1);
colorHex2 = cutHex(c2);
R2= HexToR(colorHex2);
G2= HexToG(colorHex2);
B2= HexToB(colorHex2);
R+=(R==R1 && fout<0? 0:1)*(R==R2 && fout>0? 0:1)*Math.ceil((R2-R1)/steps-(((R2-R1)/steps)<0? 1:0));
G+=(G==G1 && fout<0? 0:1)*(G==G2 && fout>0? 0:1)*Math.ceil((G2-G1)/steps-(((G2-G1)/steps)<0? 1:0));
B+=(B==B1 && fout<0? 0:1)*(B==B2 && fout>0? 0:1)*Math.ceil((B2-B1)/steps-(((B2-B1)/steps)<0? 1:0));
//document.getElementById('main').innerHTML=color+" "+Math.ceil((B2-B1)/steps-(((R2-B1)/steps)<0? 1:0)) +" "+c1+ " "+c2;
if(R>Math.max(R1,R2)) R=Math.max(R1,R2);
if(R<Math.min(R1,R2)) R=Math.min(R1,R2);
if(G>Math.max(G1,G2)) G=Math.max(G1,G2);
if(G<Math.min(G1,G2)) G=Math.min(G1,G2);
if(B>Math.max(B1,B2)) B=Math.max(B1,B2);
if(B<Math.min(B1,B2)) B=Math.min(B1,B2);
return(RGBtoHex(R,G,B));
}

function RGBtoHex(R,G,B) {return "#"+toHex(R)+toHex(G)+toHex(B)}
function toHex(N) {
 if (N==null) return "00";
 N=parseInt(N); if (N==0 || isNaN(N)) return "00";
 N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N);
 return "0123456789abcdef".charAt((N-N%16)/16)
      + "0123456789abcdef".charAt(N%16);
}
function HexToR(h) {return parseInt(h.substring(0,2),16)}
function HexToG(h) {return parseInt(h.substring(2,4),16)}
function HexToB(h) {return parseInt(h.substring(4,6),16)}
function cutHex(h) {return (h.charAt(0)=="#") ? h.substring(1,7):h
}

String.prototype.canonizeColor = function(){
    if(this.indexOf("rgb") != -1){
  	str = this.replace(/rgb\(|\)/g, "").split(",");
	return RGBtoHex(str[0],str[1],str[2]);
	}
    else{
      return this.toLowerCase();
    }
  }