function CalendarToggle(type, activeColor)
{
	var elements = document.getElementsByClassName(type, 'td');
	activeColor = Cal_TrueColor(activeColor);
	for (x=0; x<elements.length; x++)
	{
		var color = CalendarGetStyle(elements[x], 'background-color');
		color = Cal_TrueColor(color);
		
		if (color == activeColor)
		{
			//newstyle = new Array();
			//newstyle['background-color'] = '#ffffff';
			elements[x].style.backgroundColor = '#ffffff';
			//CalendarSetStyle(elements[x], newstyle);
		}
		else
		{
			//newstyle = new Array();
			//newstyle['background-color'] = activeColor;
			//CalendarSetStyle(elements[x], newstyle);
			
			elements[x].style.backgroundColor = '#' + activeColor;
		}
	}
}

function Cal_TrueColor(color)
{
	if (color.length == 7)
	{
		// hex value was passed
		color = color.substr(1);
		color = Cal_colorhex2dec(color);
		colors = color.split(' ');
		var r=colors[0];
		var g=colors[1];
		var b=colors[2];
	}
	else
	{
		// string RGB
		color = color.replace('rgb(','');
		color = color.replace(')','');
		colors = color.split(', ');
		
		var r=colors[0];
		var g=colors[1];
		var b=colors[2];
	}
	
	var hex = Cal_RGBtoHex(r,g,b);
	return hex;
}

function Cal_Hex2Dec(x)
{
	var y = 0, z;
	for(var i=0;i<x.length;i++)
	{
		z = x.toUpperCase().charCodeAt(i);
		y = 16*y+z-((z<58)?48:55);
	}
	return y;
}

function Cal_colorhex2dec(x)
{
	return x.replace(/(..)/g,function($1){return Cal_Hex2Dec($1)+' '})
}

function Cal_RGBtoHex(R,G,B) {return  Cal_toHex(R)+ Cal_toHex(G)+ Cal_toHex(B)}
function Cal_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 CalendarGetStyle(element, style)
{
    element = $(element);
    var value = element.style[style.camelize()];
    if (!value) {
      if (document.defaultView && document.defaultView.getComputedStyle) {
        var css = document.defaultView.getComputedStyle(element, null);
        value = css ? css.getPropertyValue(style) : null;
      } else if (element.currentStyle) {
        value = element.currentStyle[style.camelize()];
      }
    }

    if (window.opera && ['left', 'top', 'right', 'bottom'].include(style))
      if (Element.getStyle(element, 'position') == 'static') value = 'auto';

    return value == 'auto' ? null : value;
}

function CalendarSetStyle(element, style)
{
    element = $(element);
    for (name in style)
      element.style[name.camelize()] = style[name];
}

function Calendar_LoadEvent(component_id, id)
{
	new Ajax.Updater('calendar_moreinfo', '/widgets/calendar/show.php?component_id='+component_id+'&event='+id);
}

function Calendar_Load(component_id, month, year)
{
	new Ajax.Updater('calendar_main', '/widgets/calendar/view.php?component_id='+component_id+'&month='+month+'&year='+year);
}