//Global objects definition
var topFrames = new Array();
topFrames.push(new FrameStruct("titre_top", "vincent"));

var focus1 = "titre_etudemulti"; //First editor
var focus2 = "titre_fiche"; //second editor

var divColor = null;

//Objects
function FrameStruct(frame, hidden)
{
	this.frame = frame;
	this.hidden = hidden;
}

//Global functions

function setFocus(obj)
{
	focus1 = obj;
}

function setFocus2(obj)
{
	focus2 = obj;
}

function getHtml(frame)
{
	if (navigator.appName == "Netscape")
		return document.getElementById(frame).contentWindow.document.body.innerHTML;
	else
		return document.getElementById(frame).innerHTML;
}

function ApplyStyle(frames, style, value)
{
	if (navigator.appName == "Netscape")
	{
		for (var i = 0; i < frames.length; i++)
		{
			var frame = document.getElementById(frames[i].frame);

				frame.contentWindow.document.execCommand(style, false, value);
				frame.contentWindow.focus();

		}
	}
	else
	{
		for (var i = 0; i < frames.length; i++)
		{
			var frame = document.getElementById(frames[i].frame);
			if ((frame.id == focus1) || (frame.id == focus2))
			{
				frame.document.execCommand(style, "false", value);
				frame.document.focus();
			}
		}
	}
}

function UrlFor(array, prefix, message)
{
	if (navigator.appName == "Netscape")
	{
		var szURL = prompt(message, prefix);
		if ((szURL != null) && (szURL != '')) {
			ApplyStyle(array, 'CreateLink', szURL);
		}
	}
	else
	{
		ApplyStyle(array, 'CreateLink', "mailto:");
	}

}

function init_page(frames)
{
	if (navigator.appName == "Netscape")
	{
		for (var i = 0; i < frames.length; i++)
		{
			document.getElementById(frames[i].frame).contentWindow.document.designMode = "on";
			document.getElementById(frames[i].frame).contentWindow.document.body.innerHTML = unescape(document.getElementById(frames[i].hidden).value);
		}
	}
	else
	{
		for (var i = 0; i < frames.length; i++)
		{		
			var iFrame = document.getElementById(frames[i].frame);
			var span = document.createElement("DIV");
			span.style.width = "430px";
			span.style.height = iFrame.style.height;
			span.style.border = iFrame.style.border;
			span.style.overflowY = "auto";
			span.contentEditable = true;
			
			span.id = frames[i].frame;
			span.onclick = function (e)
			{
				setFocus(this.id);
				setFocus2(this.id);
				
			};
			span.innerHTML = unescape(document.getElementById(frames[i].hidden).value);
			iFrame.parentElement.replaceChild(span, iFrame);
		}
	}
}

function ResetAll(frames)
{
	for (var i = 0; i < frames.length; i++)
	{
		if (navigator.appName == "Netscape")
		{
			document.getElementById(frames[i].frame).contentWindow.document.body.innerHTML = "";
		}
		else
		{
			document.getElementById(frames[i].frame).innerHTML = "";
		}	
	}
}

function save(form, action)
{
	//document.getElementById('ActionToDo').value = action;
	//document.getElementById('Langage').value = document.getElementById('cblang').value;
	document.getElementById(form).submit();
}

function BackupFields(frames)
{
	for (var i = 0; i < frames.length; i++)
	{
		document.getElementById(frames[i].hidden).value = escape(getHtml(frames[i].frame));
	}
}

function ForceFocus(frames)
{
	if (navigator.appName == "Netscape")
	{
		for (var i = 0; i < frames.length; i++)
		{
			var frame = document.getElementById(frames[i].frame);
			if ((frame.id == focus1) || (frame.id == focus2))
			{
				frame.contentWindow.focus();
			}
		}
	}
	else
	{
		for (var i = 0; i < frames.length; i++)
		{
			var frame = document.getElementById(frames[i].frame);
			if ((frame.id == focus1) || (frame.id == focus2))
			{
				frame.document.focus();
			}
		}
	}
}

var static_popup = null;
function PopupColor(frames, property, target)
{
	ForceFocus(frames);
	static_popup = this;
	
	this.array = frames;
	this.property = property;
	
	if (divColor == null)
	{
		divColor = document.getElementById("DivColor");
		divColor.style.position = "absolute";
		divColor.style.zIndex = 2000;
		divColor.style.backgroundColor="#D4D0C8";
		//divColor.style.width = 100;
		//divColor.style.height = 100;

		/*divColor.onmouseout = function (e)
		{
			static_popup.Close();
		};*/

		divColor.onmousedown = function(e)
		{
		ForceFocus(frames);
		if (navigator.appName == "Netscape")
		{
			ApplyStyle(static_popup.array, static_popup.property, e.target.style.backgroundColor);
		}
		else
		{
			ApplyStyle(static_popup.array, static_popup.property, event.srcElement.style.backgroundColor);
		}
			static_popup.Close();
		};
	}

		divColor.style.top = FindTopEdge(target);
		divColor.style.left = FindLeftEdge(target);
	

	var str = '<table width="100" height="100" border="0" cellpadding="0" cellspacing="3" style="background-color:D4D0C8">';
  	str += '<tr>';
    	str += '<td style="background-color:#000000" width="5px" height ="5px"><img src="images_moduletexte/t.gif" style="background-color:#000000" width="5px" height="5px"></td>';
   	 str += '<td style="background-color:#333333" width="5px" height ="5px"><img src="images_moduletexte/t.gif" style="background-color:#333333" width="5px" height="5px"></td>';
    	str += '<td style="background-color:#666666" width="5px" height ="5px"><img src="images_moduletexte/t.gif" style="background-color:#666666" width="5px" height="5px"></td>';
   	 str += '<td style="background-color:#999999" width="5px" height ="5px"><img src="images_moduletexte/t.gif" style="background-color:#999999" width="5px" height="5px"></td>';
    	str += '<td style="background-color:#CCCCCC" width="5px" height ="5px"><img src="images_moduletexte/t.gif" style="background-color:#CCCCCC" width="5px" height="5px"></td>';
    	str += '<td style="background-color:#FFFFFF" width="5px" height ="5px"><img src="images_moduletexte/t.gif" style="background-color:#FFFFFF" width="5px" height="5px"></td>';
  	str += '</tr>';
  	str += '<tr> ';
    	str += '<td style="background-color:990033" width="5px" height ="5px"><img src="images_moduletexte/t.gif" style="background-color:#990033" width="5px" height="5px"></td>';
    	str += '<td style="background-color:#CC0033" width="5px" height ="5px"><img src="images_moduletexte/t.gif" style="background-color:#CC0033" width="5px" height="5px"></td>';
    	str += '<td style="background-color:#D1470A" width="5px" height ="5px"><img src="images_moduletexte/t.gif" style="background-color:#D1470A" width="5px" height="5px"></td>';
    	str += '<td style="background-color:#FF9900" width="5px" height ="5px"><img src="images_moduletexte/t.gif" style="background-color:#FF9900" width="5px" height="5px"></td>';
    	str += '<td style="background-color:#FFCC00" width="5px" height ="5px"><img src="images_moduletexte/t.gif" style="background-color:#FFCC00" width="5px" height="5px"></td>';
  	str += '</tr>';
  	str += '<tr> ';
    	str += '<td style="background-color:#CC0000" width="5px" height ="5px"><img src="images_moduletexte/t.gif" style="background-color:#CC0000" width="5px" height="5px"></td>';
    	str += '<td style="background-color:#CC0033" width="5px" height ="5px"><img src="images_moduletexte/t.gif" style="background-color:#CC0033" width="5px" height="5px"></td>';
    	str += '<td style="background-color:#FF0033" width="5px" height ="5px"><img src="images_moduletexte/t.gif" style="background-color:#FF0033" width="5px" height="5px"></td>';
    	str += '<td style="background-color:#CC0066" width="5px" height ="5px"><img src="images_moduletexte/t.gif" style="background-color:#CC0066" width="5px" height="5px"></td>';
    	str += '<td style="background-color:#FF9966" width="5px" height ="5px"><img src="images_moduletexte/t.gif" style="background-color:#FF9966" width="5px" height="5px"></td>';
    	str += '<td style="background-color:#FFCC66" width="5px" height ="5px"><img src="images_moduletexte/t.gif" style="background-color:#FFCC66" width="5px" height="5px"></td>';
  	str += '</tr>';
  	str += '<tr> ';
    	str += '<td style="background-color:#330066" width="5px" height ="5px"><img src="images_moduletexte/t.gif" style="background-color:#330066" width="5px" height="5px"></td>';
    	str += '<td style="background-color:#003366" width="5px" height ="5px"><img src="images_moduletexte/t.gif" style="background-color:#003366" width="5px" height="5px"></td>';
    	str += '<td style="background-color:#333399" width="5px" height ="5px"><img src="images_moduletexte/t.gif" style="background-color:#333399" width="5px" height="5px"></td>';
    	str += '<td style="background-color:6782AF" width="5px" height ="5px"><img src="images_moduletexte/t.gif" style="background-color:#6782AF" width="5px" height="5px"></td>';
    	str += '<td style="background-color:#333399" width="5px" height ="5px"><img src="images_moduletexte/t.gif" style="background-color:#333399" width="5px" height="5px"></td>';
    	str += '<td style="background-color:#6666CC" width="5px" height ="5px"><img src="images_moduletexte/t.gif" style="background-color:#6666CC" width="5px" height="5px"></td>';
  	str += '</tr>';
  	str += '<tr> ';
    	str += '<td style="background-color:#052E38" width="5px" height ="5px"><img src="images_moduletexte/t.gif" style="background-color:#052E38" width="5px" height="5px"></td>';
    	str += '<td style="background-color:#1A5969" width="5px" height ="5px"><img src="images_moduletexte/t.gif" style="background-color:#1A5969" width="5px" height="5px"></td>';
    	str += '<td style="background-color:#6BA9B4" width="5px" height ="5px"><img src="images_moduletexte/t.gif" style="background-color:#6BA9B4" width="5px" height="5px"></td>';
    	str += '<td style="background-color:#6DA5B1" width="5px" height ="5px"><img src="images_moduletexte/t.gif" style="background-color:#6DA5B1" width="5px" height="5px"></td>';
    	str += '<td style="background-color:#85BAC2" width="5px" height ="5px"><img src="images_moduletexte/t.gif" style="background-color:#85BAC2" width="5px" height="5px"></td>';
    	str += '<td style="background-color:#D8F2F6" width="5px" height ="5px"><img src="images_moduletexte/t.gif" style="background-color:#D8F2F6" width="5px" height="5px"></td>';
  	str += '</tr>';
  	str += '<tr> ';
    	str += '<td style="background-color:#333300" width="5px" height ="5px"><img src="images_moduletexte/t.gif" style="background-color:#333300" width="5px" height="5px"></td>';
    	str += '<td style="background-color:#336600" width="5px" height ="5px"><img src="images_moduletexte/t.gif" style="background-color:#336600" width="5px" height="5px"></td>';
    	str += '<td style="background-color:#339900" width="5px" height ="5px"><img src="images_moduletexte/t.gif" style="background-color:#339900" width="5px" height="5px"></td>';
    	str += '<td style="background-color:#33CC00" width="5px" height ="5px"><img src="images_moduletexte/t.gif" style="background-color:#33CC00" width="5px" height="5px"></td>';
    	str += '<td style="background-color:#33CC66" width="5px" height ="5px"><img src="images_moduletexte/t.gif" style="background-color:#33CC66" width="5px" height="5px"></td>';
    	str += '<td style="background-color:#33FF99" width="5px" height ="5px"><img src="images_moduletexte/t.gif" style="background-color:#33FF99" width="5px" height="5px"></td>';
  	str += '</tr>';
	str += '</table>';
	
	divColor.innerHTML = str;
	ForceFocus(frames);
}

PopupColor.prototype =
{
	Close:function()
	{
		divColor.innerHTML = "";
	}
};

function FindLeftEdge(img) 
{
	xPos = img.offsetLeft;
    tempEl = img.offsetParent;
    while (tempEl != null) 
	{
    	xPos += tempEl.offsetLeft;
        tempEl = tempEl.offsetParent;
    }
        
	return xPos;
}

function FindTopEdge(img) 
{
	yPos = img.offsetTop;
    tempEl = img.offsetParent;
    while (tempEl != null) 
	{
    	yPos += tempEl.offsetTop;
        tempEl = tempEl.offsetParent;
    }
    
	return yPos;
}