
// -------------------------------------------------------------------
function editor_setTheme(str, themeLocation)
{
	document.getElementById("theme_css").href = "themes/get_css.php?id="+themeLocation+"&force="+Math.ceil(Math.random()*9999);

	var i, j;

	editor_themeLocation   = themeLocation;
	editor_pagesLocation   = "";
	editor_blogPostFormat  = "";
	editor_blogToolColumn  = "";
	editor_secondaryTemplate = {};


	if ((i = str.indexOf("%%%")) != -1 && (j = str.indexOf("%%%", i+1)) != -1)
	{
//		try { eval( str.substr(i+3, j-i-3) ); } catch (e) { sayDebugMessage(e); }
			eval( str.substr(i+3, j-i-3) );

		str = str.substr(0, i) + str.substr(j+3);
	}

	str = str.replace(/(%loc%\/)/ig, "themes/"+themeLocation+"/");
	str = str.replace(/(\%title\%)/    , "<span id='main_title' style='cursor:pointer' onclick='editor_showSiteSettings();'>"+(editor_site_title ? editor_site_title.replace("\$", "&#36;") : "Site title")+"</span>")
			 .replace(/(\%slogan\%)/   , "<span id='main_slogan' style='cursor:pointer' onclick='editor_showSiteSettings();'>"+(editor_site_slogan ? editor_site_slogan.replace("\$", "&#36;") : "Site slogan")+"</span>")
			 .replace(/(\%sub_title\%)/, "<span id='main_subtitle' style='cursor:pointer' onclick='editor_showPagesDlg(); document.getElementById(\"editor_showP_title\").focus(); '>Page title</span>")
			 .replace(/(\%footer_code\%)/, "Create a FREE website with BlinkWeb. NOW!");


	var div2     = document.getElementById("loading-overlay");
	var main     = document.getElementById("main_Location");
	var themeCss = document.getElementById("theme_css");
	div2.appendChild(main);		main.id = "blank_old_location";

	document.getElementById("main_edit_area_holder").innerHTML = str;

	var main2 = document.getElementById("main_Location");//, ch = main.firstChild;

	while (main.firstChild)
		main2.appendChild( main.firstChild );

	editor_initMainLocation(main2);

	div2.removeChild(main);

	editor_createPageNav();
	resizeWindow();
	addShowHideAnimation("loading-overlay", false, showhideAnim_alpha);
}

// -------------------------------------------------------------------
function editor_createPageNav()
{
	for (j=0; j<editor_pagesLocation.length; j += 5)
	{
		var par    = document.getElementById(editor_pagesLocation[j]);
		var str    = editor_pagesLocation[j+1];
		var format = editor_pagesLocation[j+2], cur;
		var firstShown = 0;

		for (i=0; i<editor_pages.length; i++)
		{
			if (!editor_pages[i][3]) continue;

			if (firstShown && editor_pagesLocation[j+4]) str += editor_pagesLocation[j+4];
			firstShown = 1;

			cur = format;
			cur = cur.replace(/(\%href\%)/      , "#' onclick='editor_loadPage(\""+editor_pages[i][0]+"\");'")
					 .replace(/(\%long_title\%)/, editor_pages[i][2])
					 .replace(/(\%title\%)/     , editor_pages[i][1]);

			str += cur;
		}

		par.innerHTML = str;
	}
}

// -------------------------------------------------------------------
function editor_loadPage(id, params, force)
{
	if (id == editor_pageId && !params && !force) return;
	if (check_file_uploader_isWorking()) return;


	if (ajax1SendInterval)
		startSendajax1();

	if (typeof params != 'undefined' && params)
		id += "&" + params;
	else params = 0;

	editor_lastPageParams = params;
	addShowHideAnimation("loading-overlay", true, showhideAnim_alpha);
	ajaxComm_readPage(id);
}

// -------------------------------------------------------------------
function editor_setPage(code, pageId)
{
	var main_area = document.getElementById("main_edit_area");

	main_area.style.visibility = "hidden";

//	try { eval(code); } catch (e) { sayDebugMessage(e); }
		eval(code);
	editor_showPagesDlg_lastId = pageId;

	var m_subtitle = document.getElementById('main_subtitle');
	if (m_subtitle)
		for (i=0; i<editor_pages.length; i++)
			if (editor_pages[i][0] == editor_showPagesDlg_lastId)
				m_subtitle.innerHTML = ((editor_pages[i][2].length) ? editor_pages[i][2] : "Page title");

	main_area.style.visibility = "visible";
	editor_pageId = pageId;

	if (editor_pageId.indexOf)
	{
		var i = editor_pageId.indexOf('&');		// clear params
		if (i != -1) editor_pageId = editor_pageId.substr(0, i);
	}

	addShowHideAnimation("loading-overlay", false, showhideAnim_alpha);
}


// -------------------------------------------------------------------
var editor_showPagesDlg_lastId = -1;
function editor_showPagesDlg()
{
	endModalDlg();
	var modalClient  = document.getElementById("modal-client");		if (modalClient.firstNode) return;

	var div0 = document.createElement("DIV"),
		div1 = document.createElement("DIV"),	div2 = document.createElement("DIV"),
		div5 = document.createElement("DIV"),	div6 = document.createElement("DIV"),
		modalHeight = 300;

	div1.className =  "modal_type0_left";
	div2.className =  "modal_type0_right";

	div0.id = "editor_showP_buttons";	
	div1.id = "editor_showP_list";						div1.m_isMainDiv = 1;		div1.m_dontResize = 1;
	div1.style.overflow = "auto";	div1.style.overflowX = "hidden";
	div0.style.width = "490px";		div0.style.height =  "55px";
	div1.style.width = "150px";		div1.style.height = "250px";
	div2.style.width = "345px";		div2.style.height = "250px";
									div5.style.height = "170px";

	div5.className = 'modal_type0_right_holder';

	div2.appendChild(div5);
	div2.appendChild(div6);

	modalClient.appendChild(div0);
	modalClient.appendChild(div1);
	modalClient.appendChild(div2);

	div5.style.textAlign = "center";
	div5.innerHTML = "<div style='padding: 15px 20px;' id='editor_showP_edit' class='modal_table'>"+
					"<div style='position:relative; overflow:auto; height: 115px'><table><tbody class='modal_table'><tr><td>Page name:</td><td width='190'>"+
							"<input type='text' id='editor_showP_name' maxlength='60' /></td></tr>"+
						"<tr><td>Page title:</td><td><input type='text' id='editor_showP_title' maxlength='180' onchange='editor_showP_updatePageURL();'/></td></tr>"+
						"<tr><td>Show in navigation*:</td><td><select id='editor_showP_vis'><option value='1'>Yes</option><option value='0'>No</option></select></td></tr>"+
						"<tr><td>Full page URL:</td><td><input type='text' id='editor_showP_fullURL' /></td></tr>"+
						"</tbody></table></div><div style='height: 10px'></div>"+
					"<div style='text-align:center'>"+
						"<span style='width: 120px'><img src='images/modal_ui/edit.gif' style='border: 0px none; vertical-align:middle; margin-right: 5px;'><a href='#' onclick='editor_showPagesDlg_saveInfo(); editor_loadPage(editor_showPagesDlg_lastId); endModalDlg()'>Edit this page</a></span>"+
						"<span style='width: 150px' id='editor_showP_del'>&nbsp;&nbsp;<img src='images/modal_ui/bord_v.gif' style='width: 1px; height: 20px; vertical-align:middle;' alt='' />&nbsp;&nbsp;<img src='images/modal_ui/delete.gif' style='border: 0px none; vertical-align:middle; margin-right: 5px;'><a href='#' onclick='editor_showP_startDelConf()'>Delete this page</a></span>"+
					"</div>"+
				"</div>"+
			"<div id='editor_showP_delConf' class='editor_showP_delConf' style='position:relative; top: 25px; display: none'>"+
					"<div style='padding: 8px'><img src='images/modal_ui/modal_del.jpg' style='float:left; margin-right: 8px; margin-bottom:8px;'><br />Are you sure you want to delete this page and all its content? </div>"+
					"<div id='editor_showP_delConfLoc' style='margin: 0px auto; width: 170px; height:40px; clear:both'></div>"
			"</div>";
	div5.onclick = editor_showPagesDlg_saveInfo;

	div6.className = "modal_table";
	div6.style.marginLeft = "13px";
	div6.style.marginRight = "13px";
	div6.innerHTML = "<br /><b>*NOTE</b>: <i>You can only have a maximum of 6 visible pages in the navigation menu. "+
							"This however does not limitate the number of total pages available in the site.</i>"; 

	var div6 = document.getElementById('editor_showP_delConfLoc');
	if (div6)
	{
		div6.appendChild( modal_createSmallButton("Yes", "editor_showPagesDlg_delpage()", 75, 'editor_showPagesDlg_delpage') );
		div6.appendChild( modal_createSmallButton("No" , "editor_showP_endDelConf()", 75, 'editor_showP_endDelConf') );
	} else return endModalDlg();


	if (typeof editor_is_allowed_saleletters != 'undefined' && editor_is_allowed_saleletters)
	{
		div0.appendChild( modal_createSmallButton("Add salesletter", "editor_showP_createPage('salespage');", 170, 'editor_showP_createPage_sales') );
		div0.appendChild( modal_createSmallButton("Add page"       , "editor_showP_createPage('page');"     , 120, 'editor_showP_createPage_page') );
		div0.appendChild( modal_createSmallButton("Add a blog"     , "editor_showP_createPage('blog');"     , 130, 'editor_showP_createPage_blog') );
		div0.style.paddingLeft = '20px';
	} else {
		div0.appendChild( modal_createSmallButton("Add page"       , "editor_showP_createPage('page');"     , 180, 'editor_showP_createPage_page') );
		div0.appendChild( modal_createSmallButton("Add a blog"     , "editor_showP_createPage('blog');"     , 180, 'editor_showP_createPage_blog') );
		div0.style.paddingLeft = '60px';
	}
	div0.style.paddingTop = "3px";

	if (editor_pageType == 'salespage')
	{
		var divx = document.createElement("DIV");

		divx.style.clear = 'both';
		divx.style.height = '36px';
		divx.style.background = "url(images/editor_ui/page_desc.gif) no-repeat 180px 0px";
		div0.style.height = '80px';
		modalHeight += 35;
		

		div0.appendChild( divx );
	}

	editor_showPagesDlg_lastId = -1;
	editor_showPagesDlg_createList();
	editor_showPagesDlg_loadInfo( editor_pageId  );
	

	addModalBtn("Save", "editor_showPagesDlg_saveInfo(1); endModalDlg()");
	startModalDlg(500, modalHeight, "Site pages", 1, 0);


	if (!m_toolTipShown.m_createP_btns && allow_tooltips)
	{
		if (typeof editor_is_allowed_saleletters != 'undefined')
			 editor_showToolTip("Click \"Add Salesletter\" to add a new salesletter OR<br/>Click \"Add Page\" to add a new page OR<br/>Click \"Add Blog\" to add a new blog to your site.",
				'editor_showP_buttons', 100, 40, 0, 500);
		else editor_showToolTip("Click \"Add Page\" to add a new page OR<br/>Click \"Add Blog\" to add a new blog to your site.",
				'editor_showP_buttons', 160, 40, 0, 500);

		m_toolTipShown.m_createP_btns = 1;
	}

}

function editor_showP_updatePageURL()
{
	var el, val;
	el = document.getElementById('editor_showP_name'); if (!el) return;



	if (editor_pages[0][0] == editor_showPagesDlg_lastId)
		 val = "";
	else val = el.value.toLowerCase().replace(/[^a-z0-9]+/ig, "-") + ".html";

	     if (editor_subdomain != "") val = "http://"+editor_subdomain+".blinkweb.com/"+val;
	else if (editor_domain    != "") val = "http://"+editor_domain+"/"+val;
	else val = "site is not published yet";

	el = document.getElementById('editor_showP_fullURL');	if (el) el.value = val;
}

function editor_showP_createPage(type)
{
	editor_showPagesDlg_saveInfo();
	editor_showPagesDlg_lastId = -1;
	
	addShowHideAnimation('loading-overlay', true, showhideAnim_alpha);
	sendMessage("add_page('"+type+"')", 1);
	

	if (!m_toolTipShown.m_showPageTitle && allow_tooltips)
	{
		editor_showToolTip("Title your page here.  Once you've done that, click \"Save\" below", "editor_showP_title", 30, 0, 2, 1000);
	
		m_toolTipShown.m_showPageTitle = 1;
	}

	return 1;
}

function editor_showP_startDelConf()
{
	document.getElementById('editor_showP_edit'   ).style.display = 'none';
	document.getElementById('editor_showP_delConf').style.display = 'block';
}

function editor_showP_endDelConf()
{
	document.getElementById('editor_showP_edit'   ).style.display = 'block';
	document.getElementById('editor_showP_delConf').style.display = 'none';

	return 1;
}

function editor_showPagesDlg_loadInfo(id)
{
	var i, m_el;
	for (i=0; i<editor_pages.length; i++)
		if (editor_pages[i][0] == editor_showPagesDlg_lastId)
		{
			if (!editor_pages[i][3]) m_el = document.getElementById('editor_showP_hidden');
			else                     m_el = document.getElementById('editor_showP_'   + editor_showPagesDlg_lastId);
			
			if (m_el)
				m_el.className = 'modal_type0_btn';
		}

	document.getElementById("editor_showP_edit"   ).style.display = 'block';
	document.getElementById("editor_showP_delConf").style.display = "none";
	document.getElementById("editor_showP_del"    ).style.display = (editor_pages.length == 1) ? "none" : "inline";

	for (i=0; i<editor_pages.length; i++)
		if (editor_pages[i][0] == id)
		{
			if (!editor_pages[i][3])
			{
				 var j, sel = document.getElementById('editor_showP_invList');

				 if (sel)
				 for (j=0; j<sel.options.length; j++)
					if (sel.options[j].value == id)
					{
						sel.selectedIndex = j;
						break;
					}


				   m_el = document.getElementById('editor_showP_hidden');
			} else m_el = document.getElementById('editor_showP_' + id);
			
			if (m_el)
				m_el.className = 'modal_type0_btn modal_type0_btn_selected';

			document.getElementById('editor_showP_name' ).value = editor_pages[i][1];
			document.getElementById('editor_showP_title').value = editor_pages[i][2];
			document.getElementById('editor_showP_vis'  ).selectedIndex = ((editor_pages[i][3]) ? 0 : 1);
			var div = document.getElementById("editor_showP_"+editor_pages[i][0]+"_move");		if (div) div.style.display = 'block';
		} else if (editor_pages[i][3]) {
			var div = document.getElementById("editor_showP_"+editor_pages[i][0]+"_move");		if (div) div.style.display = 'none';
		}

	editor_showPagesDlg_lastId = id;
	editor_showP_updatePageURL();
}

function editor_showPagesDlg_saveInfo(m_final)
{
	for (i=0; i<editor_pages.length; i++)
		if (editor_pages[i][0] == editor_showPagesDlg_lastId)
		{
			var name  =   document.getElementById('editor_showP_name' ).value;
			var title =   document.getElementById('editor_showP_title').value;
			var vis   = ((document.getElementById('editor_showP_vis'  ).selectedIndex==0) ? 1 : 0);
			var changed = false;

			if (name != editor_pages[i][1])
			{
				sendMessage("set_page('"+editor_pages[i][0]+"', 'name', '"+name+"')");
				editor_pages[i][1] = name;
				changed = true;
			}
			if (title != editor_pages[i][2])
			{
				sendMessage("set_page('"+editor_pages[i][0]+"', 'title', '"+title+"')");
				editor_pages[i][2] = title;
				changed = true;
			}
			if (vis != editor_pages[i][3])
			{
				sendMessage("set_page('"+editor_pages[i][0]+"', 'visible', '"+vis+"')");
				editor_pages[i][3] = vis;
				changed = true;
			}

			if (changed)
			{
				var m_subtitle = document.getElementById('main_subtitle');
				if (m_subtitle) 
					m_subtitle.innerHTML = ((editor_pages[i][2].length) ? editor_pages[i][2] : "Page title");

				editor_createPageNav();
				editor_showPagesDlg_createList();
			}
		}

	if (m_final && !m_toolTipShown.m_createdPages && allow_tooltips)
	{
		editor_showToolTip("Great job.  After adding any other content or widgets to your pages, click here to publish your site onto the web!",
					'img_publish_btn', 0, 0, 1, 1000);

		m_toolTipShown.m_createdPages = 1;
	}
}

var editor_showPagesDlg_createList_dropLocation = new Array('editor_showP_listPages');
function editor_showPagesDlg_createList()
{
	var ul     = document.getElementById('editor_showP_listPages');
	var divHid = document.getElementById('editor_showP_hidden');
	var selHid = document.getElementById('editor_showP_invList');
	if (!ul)
	{
		var leftList = document.getElementById('editor_showP_list');
		div = document.createElement("DIV");
		div.style.width = 'auto';
		div.style.height = '3px';
		leftList.appendChild(div);

		ul = document.createElement("UL");
		ul.id = editor_showPagesDlg_createList_dropLocation[0];
		ul.className = 'modal_type0_ul';
		ul.onmousedown = onStartDrag;
		leftList.onmousemove = onMoveDrag;
//		leftList.m_centerObjOnMouse = 1;

 		leftList.appendChild(ul);

		divHid = div = document.createElement("DIV");
		div.id  = 'editor_showP_hidden';
		div.className = 'modal_type0_btn';
		div.style.height = "50px";
		div.style.marginTop = "10px";
			div2 = document.createElement("DIV");
			div2.style.margin = "5px";
			div2.width = "140px";
			div2.innerHTML = "Hidden pages*:<br />";
				selHid = sel = document.createElement("SELECT");
				sel.id = 'editor_showP_invList';
				sel.style.width = "130px";
				sel.onclick = function () { editor_showPagesDlg_saveInfo(); editor_showPagesDlg_loadInfo(this.options[this.selectedIndex].value); }
				div2.appendChild(sel);
			div.appendChild(div2);
		leftList.appendChild(div);
	}


	while (ul    .firstChild) ul    .removeChild(ul    .firstChild);
	while (selHid.firstChild) selHid.removeChild(selHid.firstChild);

	document.getElementById('editor_showP_list').style.background = '';
	ul.style.visibility = 'visible';

	defineSortableList(ul.id, false, editor_showPagesDlg_createList_dropLocation, editor_showPagesDlg_finishedLiDrag);


	var divHidVis = "none";
	for (i=0; i<editor_pages.length; i++)
	{
		if (editor_pages[i][3])
		{
			var li = document.createElement("LI");
			li.id  = "editor_showP_"+editor_pages[i][0];
			li.className   = 'modal_type0_btn';
			li.m_page_id   = editor_pages[i][0];
			li.onmouseup   = editor_showPagesDlg_li_clicked;
			li.onmouseover = editor_showPagesDlg_li_over;
			li.onmouseout  = editor_showPagesDlg_li_out;
			
				div = document.createElement("DIV");
				div.id = li.id + "_move";
				div.className = 'modal_type0_btn_mover';
				li.appendChild(div);
				makeSortableListItem(div, ul.id, false, editor_showPagesDlg_createList_dropLocation, editor_showPagesDlg_finishedLiDrag);


				div = document.createElement("DIV");
				div.style.margin = "5px 0px 0px 5px";
				div.style.width  = "117px";
				div.style.height = "21px";
				div.style.lineHeight = "17px";
				div.style.overflow = "hidden";
				div.appendChild( document.createTextNode( editor_pages[i][1] ));
				li.appendChild(div);

			ul.appendChild(li);
		} else {
			var opt = document.createElement("OPTION");
			opt.value = editor_pages[i][0];
			opt.innerHTML = editor_pages[i][1];
			selHid.appendChild(opt);

			divHidVis = "block";
		}
	}
	divHid.style.display = divHidVis;


	editor_createPageNav();

	editor_showPagesDlg_saveInfo();
	editor_showPagesDlg_loadInfo(editor_showPagesDlg_lastId);
}

function editor_showPagesDlg_li_clicked()
{
	if (this.m_page_id)
	{
		editor_showPagesDlg_saveInfo();
		editor_showPagesDlg_loadInfo(this.m_page_id);
	}
}

var editor_showPagesDlg_li_over_timer = 0;
function editor_showPagesDlg_li_over()
{
	if (editor_showPagesDlg_li_over_timer != 0) editor_showPagesDlg_li_out();

	if (!m_toolTipShown.m_pageOrder && allow_tooltips)
	{
		editor_showPagesDlg_li_over_timer = setTimeout("editor_showToolTip('You can change the order of your pages by "+
					"dragging them up or down, and then "+
					"dropping them to the placement of your choice.', '"+this.id+"', 0, 0, 3);", 1500);
	
		m_toolTipShown.m_pageOrder = 1;
	}
}

function editor_showPagesDlg_li_out()
{
	if (editor_showPagesDlg_li_over_timer)
		clearTimeout(editor_showPagesDlg_li_over_timer);

	editor_showPagesDlg_li_over_timer = 0;
}

function editor_showPagesDlg_delpage()
{
	addShowHideAnimation('loading-overlay', true, showhideAnim_alpha);
	sendMessage("rem_page('"+editor_showPagesDlg_lastId+"')", 1);

	
	return true;
}

function editor_showPagesDlg_finishedLiDrag(listParentId, newLocation, menuPalette_id)
{
	var insertBefore =  ((newLocation.previousSibling && newLocation.previousSibling.m_page_id) ? newLocation.previousSibling.m_page_id : "first")
	var m_id = document.getElementById(menuPalette_id).m_page_id;

	addShowHideAnimation('loading-overlay', true, showhideAnim_alpha);
	sendMessage("set_page('"+m_id+"', 'after', '"+insertBefore+"')", 1);

	
	document.getElementById('editor_showP_listPages').style.visibility = 'hidden';
	document.getElementById('editor_showP_list'     ).style.background = 'url(images/blink.gif) no-repeat center 20px';

	return true;
}

// -------------------------------------------------------------------
function editor_swapPages(a, b)
{
	var i, j, k;		onEndDrag();

	for (i=0, j=k=-1; i<editor_pages.length; i++)
		if (editor_pages[i][0] == a) j = i; else
		if (editor_pages[i][0] == b) k = i;

	if (j != -1 && (k != -1 || b == "first"))
	{
		var arr1 = editor_pages[j];

		editor_pages.splice(j, 1);		if (j < k) k--;
		editor_pages.splice(k+1, 0, arr1);
	}	

	editor_showPagesDlg_createList();
	editor_createPageNav();
}

// -------------------------------------------------------------------
function editor_showThemesDlg()
{
	endModalDlg();
	var modalClient  = document.getElementById("modal-client");		if (modalClient.firstNode) return;

	var i, temp = new Array('-1'), last_id = -1, last_parent = -1;
	var str = "", indent = "";

	div1 = document.createElement("DIV");
	div1.style.margin = "0px 10px";
	div1.style.width = "180px";
	div1.style.cssFloat = div1.style.styleFloat = "left";
		div2 = document.createElement("DIV");
		div2.style.marginTop = "10px";
		div2.style.cssFloat = div1.style.styleFloat = "left";
		div2.innerHTML = "<br /><p style='font-weight: bold; color: #29a411; font-size: 130%'>Template types</p>"+
						 "<p>Choose the type of category template you want to use</p><br />";
			sel = document.createElement("SELECT");
			sel.id = 'editor_showT_select';
			sel.style.width = '170px';
			sel.onchange = function () { editor_showT_load(this.options[this.selectedIndex].value) }; 

	for (i=0; i<editor_tempCat.length; i++)
	{
		if (editor_tempCat[i] == -1)
		{
			indent = indent.substr(18);
			last_id = temp.pop();
		} else {
			if (editor_tempCat[i][1] == last_id && last_id != -1)
			{
				temp.push(last_id);
				
				indent += "&nbsp;&nbsp;&nbsp;";
			}

			opt = document.createElement("OPTION");
			opt.value = editor_tempCat[i][0];
			if (editor_tempCat[i][0] == editor_lastTempCat) opt.selected = "yes";
			opt.innerHTML = indent+editor_tempCat[i][2];
			sel.appendChild(opt);

			last_id = editor_tempCat[i][0];
		}
	}
			div2.appendChild(sel);
			div2.appendChild( document.createElement("BR") );

			span = document.createElement("SPAN");
			span.id = 'editor_showT_total';
			span.style.color = '#222222';
			span.style.fontSize = '10px';
			span.innerHTML = 'Loading templates list...'
			div2.appendChild(span);
		div1.appendChild(div2);
	modalClient.appendChild(div1);

	div1 = document.createElement("DIV");
	div1.id = 'editor_showT_preview';
	div1.style.width = '550px';
	div1.style.height = '300px';
	div1.style.border = '1px solid #70a9ce';
	div1.style.overflow = 'auto';
	div1.style.marginLeft = '25px';
	div1.style.cssFloat = div1.style.styleFloat = "right";

	modalClient.appendChild(div1);	

	editor_showT_load(editor_lastTempCat);
	addModalBtn("Close", "endModalDlg()");
	startModalDlg(780, 305, "Layout / Template change", 2, 0);

	if (!m_toolTipShown.m_selectedDesign && allow_tooltips)
	{
/*		editor_showToolTip('Now, select a design category.', sel.id, 10, 0, 1, 0, 
				   "editor_showToolTip('Hover the mouse over a design to get a preview.<br/>"+
							"Then, click the design you want for your site.', '"+div1.id+"', 120, 40, 2, 500);");
*/		
		editor_showToolTip('Hover the mouse over a design to get a preview.<br/>'+
							'Then, click the design you want for your site.', div1.id, 120, 40, 2);
		m_toolTipShown.m_selectedDesign = 1;
	}
}

function editor_showT_load(id)
{
	for (i=0; i<editor_tempCat.length; i++)
		if (editor_tempCat[i][0] == id) break;

	if (i == editor_tempCat.length) return;

	
	ajaxComm_readThemeDir(id);
	editor_lastTempCat = id;

	var m_div = document.getElementById("editor_showT_preview");
	while (m_div.firstChild)
		   m_div.removeChild(m_div.firstChild);

	m_div.innerHTML = "<div style='margin-left: 150px; margin-top: 105px; width: 200px; height: 60px; "+
				"background: url(images/blink.gif) no-repeat 10px 20px; padding: 25px 0px 0px 55px; font: 18px Arial;'>"+
					"Loading... Please Wait."
				"</div>";

	str = "Total available templates: "+editor_tempCat[i][3];
	if (editor_pageType == 'salespage')
		str += "<strong style='color: red'>*</strong><br/><br/><br/><br/><br/><br/><br/><br/><strong>Tip</strong><span style='color: red'>*</span>: Looking for content style website templates? You will need to add a new page or blog from the \"Pages\" screen to be able to use them.";

	document.getElementById('editor_showT_total').innerHTML = str;
}

function editor_setThemes(code, parentID)
{
	var m_div = document.getElementById("editor_showT_preview");	if (!m_div) return;
	while (m_div.firstChild)
		   m_div.removeChild(m_div.firstChild);

//	try { eval(code); } catch (e) { sayDebugMessage(e) };
		eval(code);
}

function editor_showT_creaThumb(title, id, url, thumb, big_thumb)
{
	var m_par, par = document.createElement("DIV"), div, span;

	m_par = par;
	if (id == editor_tempID)
	{
		div = document.createElement("DIV");
		par.appendChild(div);
		par.style.backgroundImage = 'url(images/modal_ui/showT_selected.gif)';
		par.style.backgroundRepeat = 'no-repeat';
		par.style.height = '140px';
		par.style.width  = '170px';
		par.style.cssFloat = 'left';
		par.style.styleFloat = 'left';

		par = div;
	}

	div  = document.createElement("DIV");
	span = document.createElement("P");

	span.appendChild( document.createTextNode(title) );
	span.style.fontWeight = 'bold';
		
	par.className = 'editor_showT_holder';
	par.appendChild( span );

	div.className = 'editor_showT_img';
	div.id        = 'editor_showT_template_'+id;

	var img = document.createElement("IMG");
	img.src = thumb;
	img.alt = '';//'Template thumbnail';

	div.appendChild( img );
	par.appendChild( div );
	

	div.data_thumbImg    = thumb;
	div.data_bigThumbImg = big_thumb;
	div.data_title       = title;

	div.onmousemove = editor_showT_showBigThumb;
	div.onmouseout  = editor_showT_hideBigThumb;
	img.onclick     = editor_showT_setTemplate;

	return m_par;
}

function editor_showT_showBigThumb(event)
{
	var el = this;
	while (el && el.tagName != "DIV") el = el.parentNode;
	if (!el || (el.id.indexOf('editor_showT_template_') == -1 && el.className != 'demoTempl')) return;

	id = el.id.substr(22);

	var div = document.getElementById("editor_showT_bigPreview");
	if (!div)
	{
		div    = document.createElement("DIV");
		div.id = 'editor_showT_bigPreview';

		div.style.position   = 'absolute';
		div.style.background = 'white';
		div.style.left       = '0px';
		div.style.top        = '0px';
		div.style.border     = '1px solid black';
		div.style.padding    = '10px';
		div.style.zIndex     = 9999;

		document.body.appendChild(div);
	}
	
	if (div.innerHTML.indexOf(el.data_bigThumbImg) == -1)
		div.innerHTML = ((!el.data_title) ? "" : "<p style='font-weight: bold'>"+el.data_title + "</p>")+
					"<div style='width: 500px; height: 270px; background: white url(images/blink.gif) no-repeat center center'><img src='" + el.data_bigThumbImg + "' alt='' /></div>";

	if (!event)
		try { event = window.event; parseInt(event.clientX); } catch (e) { m_dragNDropObject.working = 0; return; }
	var sizeX = g_prevSize.w, sizeY = g_prevSize.h;

	x0 = event.clientX+20;		x1 = 500;
	y0 = event.clientY+20;		y1 = 270;

	if (x0 > sizeX - x1 - 20) x0 = event.clientX - x1 - 40;
	if (y0 > sizeY - y1 - 20) y0 = sizeY - y1 - 40;

	y0 += g_prevSize.s;

	div.style.left = x0 + 'px';
	div.style.top  = y0 + 'px';
	div.style.display = 'block';
}

function editor_showT_hideBigThumb()
{
	var div = document.getElementById("editor_showT_bigPreview");
	if (div)
		div.style.display = 'none';
}

function editor_showT_setTemplate()
{
	var el = this;
	while (el && el.tagName != "DIV") el = el.parentNode;
	if (!el || el.id.indexOf('editor_showT_template_') == -1) return;

	id = el.id.substr(22);

	sendMessage("set_site_template('" + id + "')", 1);
	editor_showT_hideBigThumb();

	endModalDlg();
	addShowHideAnimation("loading-overlay", true, showhideAnim_alpha);

	if (!m_toolTipShown.m_selectedTheme && allow_tooltips)
	{
		editor_showToolTip('Looks great!  Click here to add more pages or a blog.', 'tool_pages', 0, 0, 0, 4000);

		m_toolTipShown.m_selectedTheme = 1;
	}
}
