// JavaScript Document

var articleParent = null;
var articleCurrentTag = null;

article_init();

function article_init()
{
	var num = 0;
	
	els = document.getElementsByTagName("DIV");	
	for( i=0; i<els.length; i++ ) {
		if( els[i].getAttribute('contenteditable') == "true" && els[i].id == 'Article' ) {
			articleParent = els[i];
		}
	}
	
	els = document.getElementsByTagName('P');
	for( i=0; i<els.length; i++ ) {
		if(els[i].parentNode) {
			if( els[i].parentNode.getAttribute('contenteditable') == 'true' ) 
				els[i].onclick = article_selecttextnode;
		}
	}
	
	els = document.getElementsByTagName('H1');
	for( i=0; i<els.length; i++ ) {
		if(els[i].parentNode) {
			if( els[i].parentNode.getAttribute('contenteditable') == 'true' ) 
				els[i].onclick = article_selecttextnode;
		}
	}
	
	els = document.getElementsByTagName('H2');
	for( i=0; i<els.length; i++ ) {
		if(els[i].parentNode) {
			if( els[i].parentNode.getAttribute('contenteditable') == 'true' ) 
				els[i].onclick = article_selecttextnode;
		}
	}
}

function article_selecttextnode()
{
	e = gettarget(window.event);
	if( e ) {
		articleCurrentTag = e;
	}
}

function article_showimageoptions()
{
	e = gettarget(window.event);
}

function article_hideimageoptions()
{
	e = gettarget(window.event);
}

function article_doneediting(id)
{
	if( articleParent )
	{
		var request = "widgets/articles.php?ajax=savepart&id="+id;
		callAjaxFunction( request, true, cb_article_doneediting, articleParent.id, articleParent.innerHTML );
	}
}

function cb_article_doneediting( response, id )
{
	if( articleParent ) {
		articleParent.innerHTML = response;
	}
	
	article_init();
}

function article_format( type )
{
	if( articleCurrentTag ) {
		el = document.createElement(type);
		if( el ) {
			el.innerHTML = articleCurrentTag.innerHTML;
			el.onclick = article_selecttextnode;
		
			if( articleCurrentTag.parentNode ) {
				articleCurrentTag.style.color = "#FF0000";
				articleCurrentTag.parentNode.insertBefore( el, articleCurrentTag );
				articleCurrentTag.removeNode(true);
				articleCurrentTag = el;
			}
		}
	}
}

function article_insertimage()
{
	if( articleParent ) {
		el = document.createElement('image');
		if( el ) {
	//		el.src = "widgets\articles.php?newimage=1";
			el.src = "images/articles/00000001.jpg";
			el.className = "articleimage";
			articleParent.appendChild( el );
		}
	}
}

function article_additionalphotoupload(id)
{
	el = document.getElementById(id);
	if( el ) {
		row = el.insertRow(el.rows.length-1);
		cell1 = document.createElement("TD");
		cell2 = document.createElement("TD");
		filebox = document.createElement("INPUT");
		
		filebox.setAttribute("type","file");
		filebox.setAttribute("name","photos[]");
		filebox.setAttribute("size","50");
		
		cell1.innerHTML = "Image "+(el.rows.length-1)+":";
		cell2.appendChild( filebox );
		cell2.colSpan = 3;
		
		row.appendChild( cell1 );
		row.appendChild( cell2 );
	}
}

