/**
 * @author Georges
 */
	function d2h(dec) { 
       return dec.toString(16);
	}
	
	function h2d(hex) { 
       return parseInt(hex,16);
	}

	function rgb2h(r,g,b) { 
         return [d2h(r),d2h(g),d2h(b)];
	}
	
	function h2rgb(h,e,x) {
        return [h2d(h),h2d(e),h2d(x)];
	}

	
	function changeBackgroundColor(name,styleelement,newcolor)
	{
		var element = document.getElementById(name);
			
		if (styleelement == "backgroundcolor") element.style.backgroundColor = newcolor;
		else if (styleelement == "bordermenu") {
			element.style.borderRightColor = newcolor;
			element.style.borderLeftColor = newcolor;
		}
		else if (styleelement == "table") {
			getFlexApp(name).changebackground(newcolor);
		}
	}
	
	
	function cssColor2rgb(color) {
     	if(color.indexOf('rgb')<=-1) {
     		return h2rgb(color.substring(1,3),color.substring(3,5),color.substring(5,7));
     	}
     	return color.substring(4,color.length-1).split(',');
	}
	
	function animateColor(name,styleelement,initialcolor,finalcolor,fps,duration) 
	{
		duration=parseFloat(duration);
  		fps=parseFloat(fps);
  		var interval = Math.ceil(1000/fps);
 		var totalframes = Math.ceil(duration/interval);
		var b = cssColor2rgb(initialcolor);
        var e  = cssColor2rgb(finalcolor);
        var changer= parseFloat((e[0]-b[0]) / totalframes);
        var changeg= parseFloat((e[1]-b[1]) / totalframes);
        var changeb= parseFloat((e[2]-b[2]) / totalframes);
		animateFrame(name,styleelement,b,e,interval,totalframes,changer,changeg,changeb);
  
	}

	function animateFrame(name,styleelement,currentcolor,finalcolor,interval,framesleft,changer,changeg,changeb)
	{
		if (framesleft==1)
		{
			changeBackgroundColor(name,styleelement,"rgb(" + parseInt(finalcolor[0]) + "," + parseInt(finalcolor[1]) + "," + parseInt(finalcolor[2]) + ")");
		}
		else
		{
			var newcolor = [parseFloat(currentcolor[0] + changer),parseFloat(currentcolor[1] + changeg),parseFloat(currentcolor[2] + changeb)];
			changeBackgroundColor(name,styleelement,"rgb(" + parseInt(newcolor[0]) + "," + parseInt(newcolor[1]) + "," + + parseInt(newcolor[2]) + ")");	
			var newfunction = "animateFrame('" + name + "','" + styleelement + "',[" + newcolor[0] + "," + newcolor[1] + "," + newcolor[2] + "],[" + finalcolor[0] + "," + finalcolor[1] + "," + finalcolor[2] + "]," + interval + "," + parseInt(framesleft - 1) + "," + changer + "," + changeg + "," + changeb + ")";
			setTimeout(newfunction, interval);
		}
	}
	
	function animatepage(bordercolor,topbarcolor,menucolor,sidebarheadercolor,sidebarelementcolor,mainpanelcolor,fps,totaltime){
		//get current colors
		animateColor("topbar", "backgroundcolor", currenttopbarcolor, topbarcolor, fps,totaltime);
		animateColor("main", "bordermenu", currentbordercolor, bordercolor, fps,totaltime);
		animateColor("topline", "backgroundcolor", currentbordercolor, bordercolor, fps,totaltime);
		animateColor("bottomline", "backgroundcolor", currentbordercolor, bordercolor, fps,totaltime);
		animateColor("menu", "backgroundcolor", currentmenucolor, menucolor, fps,totaltime);
		animateColor("sidebarheader1", "backgroundcolor", currentsidebarheadercolor, sidebarheadercolor, fps,totaltime);
		animateColor("sidebarelement1", "backgroundcolor",currentsidebarelementcolor, sidebarelementcolor, fps,totaltime);
		animateColor("sidebarheader2", "backgroundcolor", currentsidebarheadercolor, sidebarheadercolor, fps,totaltime);
		animateColor("sidebarelement2", "backgroundcolor",currentsidebarelementcolor, sidebarelementcolor, fps,totaltime);
		animateColor("sidebarheader3", "backgroundcolor", currentsidebarheadercolor, sidebarheadercolor, fps,totaltime);
		animateColor("sidebarelement3", "backgroundcolor",currentsidebarelementcolor, sidebarelementcolor, fps,totaltime);
		animateColor("sidebarheader4", "backgroundcolor", currentsidebarheadercolor, sidebarheadercolor, fps,totaltime);
		animateColor("sidebarelement4", "backgroundcolor",currentsidebarelementcolor, sidebarelementcolor, fps,totaltime);
		animateColor("sidebarheader5", "backgroundcolor", currentsidebarheadercolor, sidebarheadercolor, fps,totaltime);
		animateColor("sidebarelement5", "backgroundcolor",currentsidebarelementcolor, sidebarelementcolor, fps,totaltime);
		animateColor("sidebar", "backgroundcolor",currentsidebarelementcolor, sidebarelementcolor, fps,totaltime);
		animateColor("mainpanel", "backgroundcolor", currentmainpanelcolor, mainpanelcolor, fps,totaltime);

		currenttopbarcolor = topbarcolor;
		currentbordercolor = bordercolor;
		currentmenucolor = menucolor;
		currentsidebarheadercolor = sidebarheadercolor;
		currentsidebarelementcolor = sidebarelementcolor;
		currentmainpanelcolor = mainpanelcolor;
	}
	
	function getFlexApp(appName) {
  		if (navigator.appName.indexOf("Microsoft") != -1) {
			return window[appName];
		}
		else {
			return document[appName];
		}
  	}
	
	//animatepage("#FFCC00","#655100","#655100","#90760B","#CBA50E","#655100",10,2000);
	
	function changelink(site)
	{
		var str= location.href
		var output = str.substr(0,str.indexOf("style")) + "style=" + site;
		document.location = output;
	}
	