/* --------------------------------- */
/*          SOPHIAKNOWS v3.0         */
/* --------------------------------- */
/*            BROADSHEET             */
/* --------------------------------- */
/*  Created:  2001-01-01             */
/*  Issued:   2001-01-01             */
/*  Modified: 2005-08-03             */
/*  Component: RSS INTRO PAGES       */
/*  Copyright (c) 2001-2005          */
/*  Tony Pisarra, SophiaKnows        */
/* --------------------------------- */
/* --------------------------------- */



function cssobj (display,face,size,weight,style,color) {
	this.display=display;
	this.face=face;
	this.size=size;
	this.weight=weight;
	this.style=style;
	this.color=color;
	}

// I. FORMS: INIT
function initform() {
	if(mode=="editor") {showItem(ItemsCount);}
	if(mode=="settings") {initsettings();}
	if(mode=="page" || mode=="import") {
		document.forms['vcr'].icount.value=Items.length;
		document.forms['vcr'].acount.value=Items.length;
		}
	if(mode=="export") {
		for(e=0;e<document.forms['state'].length;e++) {
			document.forms['export'].elements[e].value=document.forms['state'].elements[e].value;
			}
		}
	}

// I. SETTINGS: INIT
function initsettings() {
	document.forms['metadata'].title.value=channel_title;
	document.forms['metadata'].description.value=channel_description;
	document.forms['metadata'].pubDate.value=((channel_pubDate)?channel_pubDate:new Date().toGMTString());
	document.forms['metadata'].copyright.value=((channel_copyright)?channel_copyright:"");
	if(document.getElementById) document.getElementById('pagecolor').style.background=pagecolor;
	if(document.getElementById) document.getElementById('linkcolor').style.background=linkcolor;
	ResetControls(document.forms['channel'],'channel_title');
	}


// A. SWITCHBOARD

// A.1 SWITCHBOARD
function showItem(id) {
	selected=id;
	document.forms['vcr'].icount.value=parseInt(id)+1;
	document.forms['vcr'].acount.value=Items.length;
	f=document.forms['editor'];
	f.title.value=Items[id].title;
	f.pubDate.value=new Date(Items[id].pubDate).toGMTString();
	f.description.value=DecodeParas(Items[id].description);
	f.author.value=Items[id].author;
	}

// A.2 SWITCHBOARD
function SaveItem() {
	f=document.forms['editor'];
	Items[selected].title=f.title.value;
	Items[selected].pubDate=f.pubDate.value;
	Items[selected].description=RecodeParas(f.description.value);
	itemviewer.document.location.href="items.2.html";
	}

// A.3 SWITCHBOARD
function AddItem() {
	f=document.forms['editor'];
	xdate=new Date();
	selected=Items.length;
	Items[Items.length]=new Item("","","","","","","");
	f.title.value="";
	f.pubDate.value=xdate.toGMTString();
	f.description.value="";
	}

// A.4 SWITCHBOARD
function PreviewItem() {
	var f=document.forms['editor'];
	phtml=mk_xhtmlcss();
	phtml+="<body>";
//	phtml+=mk_xhtmlheading(channel_title,channel_description,channel_pubDate,channel_managingEditor,channel_copyright);	
	phtml+=mk_xhtmlitem("",f.title.value,RecodeParas(f.description.value),f.pubDate.value,"",f.author.value,"","");
	phtml+="<\/body>";
	previewWin=window.open("","","width=550,height=450,scrobars=yes,resizable=yes");
	previewWin.document.write(phtml);
	previewWin.document.close();
	}

// A.B SWITCHBOARD/ SETTINGS
function PreviewAll() {
	var f=document.forms['editor'];
	var toc="";
	var ihtml="";
	var phtml=mk_xhtmlcss();
	phtml+="<body>";
	phtml+=mk_xhtmlheading(channel_title,channel_description,channel_pubDate,channel_managingEditor,channel_copyright);	
	for(i=Items.length-1;i>=0;i--) {
		toc+="<li><a href=#item_"+i+">"+Items[i].title+"<\/a> ("+Items[i].pubDate+")<\/li>";
		ihtml+="<a name=item_"+i+"><\/a>"+mk_xhtmlitem("",Items[i].title,RecodeParas(Items[i].description),Items[i].pubDate,"",Items[i].author,"","");
		}
	phtml+="<div style='float:right;padding:40px 30px 10px 10px;width:200px;' class=description><ul>"+toc+"<\/ul><\/div>";
	phtml+=ihtml;
	phtml+="<\/body>";
	previewWin=window.open("","","width=550,height=450,scrobars=yes,resizable=yes");
	previewWin.document.write(phtml);
	previewWin.document.close();
	}

// A.B.1 SWITCHBOARD VCR

function fshownext(a) {
	selected=((selected-1>0)? (selected-1) : Items.length-1);
	fshow(selected);
	}
// A.B.1b SWITCHBOARD VCR
function fshowback(a) {
	selected=((selected<Items.length-1)? (selected+1) : 0);
	a.title=Items[selected].title;
	fshow(selected);
	}

// A.B.1c PAGE VIEW
function fshow(xselected) {
	selected=xselected;
	showItem(selected);
	itemviewer.document.location.href="items.2.html#item_"+xselected;
	itemviewer.select("item_"+xselected);
	}


// B. PAGE VIEW

// B.1a PAGE VIEW

function gnext(a) {
	count=((count-1>0)? (count-1) : Items.length-1);
	gt(count);
	}
// B.1b PAGE VIEW
function gback(a) {
	count=((count<Items.length-1)? (count+1) : 0);
	a.title=Items[count].title;
	gt(count);
	}

// B.1c PAGE VIEW
function gt(xcount) {
	i=itemviewer.document;
	count=xcount;
	document.forms['vcr'].icount.value=(count+1);
	i.location.href="items.1.html#item_"+xcount;
	}


// C. SETTINGS

// C.1 SETTINGS: RESET CONTROL
function ResetControls(f,s) {
	for(i=0;i<f.size.length;i++) { if(f.size.options[i].value==Styles[s].size)f.size.selectedIndex=i;}
	for(i=0;i<f.display.length;i++) { if(f.display.options[i].value==Styles[s].display)f.display.selectedIndex=i;}
	for(i=0;i<f.face.length;i++) { if(f.face.options[i].value==Styles[s].face)f.face.selectedIndex=i;}
	for(i=0;i<f.weight.length;i++) { if(f.weight.options[i].value==Styles[s].weight)f.weight.selectedIndex=i;}
	for(i=0;i<f.style.length;i++) { if(f.style.options[i].value==Styles[s].style)f.style.selectedIndex=i;}
	if(document.getElementById) document.getElementById(tdnames[f.name]).style.background=Styles[s].color;
	}

// C.2 SETTINGS: RESET STYLE
function ResetStyle(n,s,v) {
	eval("Styles['"+s+"']."+n+"='"+v+"'");
	}

// C.3 SETTINGS: RESET COLOR
function resetcolor(f,c){
	if(document.getElementById) document.getElementById(f).style.background="#"+c;
	if(f=="pagecolor") pagecolor="#"+c;
	if(f=="linkcolor") linkcolor="#"+c;
	if(f=="vlinkcolor") vlinkcolor="#"+c;
	if(f=="i_color") Styles[document.forms['items'].element.value].color="#"+c;
	if(f=="c_color") Styles[document.forms['channel'].element.value].color="#"+c;
	}

// C.4 SETTINGS: APPLY
function ApplySettings() {
	runupdates('settings');
	}


function DecodeParas(htmlin) {
	textout=htmlin.replace(/<p>/g,"\n\n");
	textout=textout.replace(/<br ?\/?>/g,"\n");
	textout=textout.replace(/&quot;/g,"\"");
	return textout;
	}
function RecodeParas(textin) {
	htmlout=textin.replace(/\n\n/g,"<p>");
	htmlout=htmlout.replace(/\n/g,"<br \/>");
	htmlout=htmlout.replace(/\"/g,"&quot;");
	return htmlout;
	}

function mk_xhtmlitem(xlink,xtitle,xdescription,xpubDate,xcategory,xauthor,xguid,xenclosure,xcount) {
	var xhtml="<div class=item>";
	xhtml+=((xcount)?"<a name='item_"+xcount+"'<\/a>":"");
	xhtml+=((xtitle)?"<div class=title>"+xtitle+"<\/div>":"");
	xhtml+=((xpubDate)?"<div class=pubDate>"+xpubDate.toUpperCase()+"<\/div>":"");
	xhtml+=((xauthor)?"<div class=author>POSTED BY: "+xauthor.toUpperCase()+"<\/div>":"");
	xhtml+=((xdescription)?"<div class=description>"+RecodeParas(xdescription).replace(/([\s>])(http:\/\/[^\s]+)([\s<])/g,"$1<a href='$2'>$2<\/a>$3")+"<\/div>":"");
	xhtml+="<div class=link><a href='"+xlink+"'>PERMALINK<\/a> | <a href=#top>BACK TO TOP<\/a> ^<\/div>";
	xhtml+="<\/div>";
	return xhtml;
	}
function mk_i_xhtmlitem(xlink,xtitle,xdescription,xpubDate,xcategory,xauthor,xguid,xenclosure,xcount) {
	var xhtml="<div class=item>";
	xhtml+=((xcount)?"<a name='item_"+xcount+"'<\/a>":"");
	xhtml+=((xtitle)?"<div class=title>"+xtitle+"<\/div>":"");
	xhtml+=((xpubDate)?"<div class=pubDate>"+xpubDate.toUpperCase()+"<\/div>":"");
	xhtml+=((xauthor)?"<div class=author>POSTED BY: "+xauthor.toUpperCase()+"<\/div>":"");
	xhtml+=((xdescription)?"<div class=description>"+RecodeParas(xdescription).replace(/([\s>])(http:\/\/[^\s]+)([\s<])/g,"$1<a href='$2'>$2<\/a>$3")+"<\/div>":"");
	xhtml+="<div class=link><a href='"+xlink+"'>PERMALINK<\/a> | <a href=ieditor.html?itemno="+xcount+">EDIT ITEM<\/a><\/div>";
	xhtml+="<\/div>";
	return xhtml;
	}
function mk_xhtmlheading(channel_title,channel_description,channel_pubDate,channel_managingEditor,channel_copyright) {
	var hhtml="<div class=heading>";
	hhtml+=((channel_title)?"<div class=channel_title>"+channel_title+"<\/div>":"");
	hhtml+=((channel_description)?"<div class=channel_description>"+channel_description+"<\/div>":"");
	hhtml+=((channel_pubDate)?"<div class=channel_pubDate>UPDATED: "+channel_pubDate+"<\/div>":"");
	hhtml+=((channel_copyright)?"<div class=channel_copyright>"+channel_copyright+"<\/div>":"");
	hhtml+="<\/div>";
	return hhtml;
	}
function mk_xhtmlcss() {
	var css="<style>";
	css+="body { background:"+pagecolor+";}";
	css+="a:link { color:"+linkcolor+";}";
	css+="a:visited { color:"+linkcolor+";}";
	css+="div.heading { padding:10px 0px 15px 0px;margin:0 20px 0 40px;border-bottom:solid 1px #000;}";
	css+="div.channel_title{"+mkcss_statement('channel_title')+"padding-bottom:5px;letter-spacing:2px;}";
	css+="div.channel_description{"+mkcss_statement('channel_description')+"padding-top:0px;padding-bottom:5px;}";
	css+="div.channel_pubDate{"+mkcss_statement('channel_pubDate')+"padding-top:0px;padding-bottom:5px;}";
	css+="div.channel_copyright{"+mkcss_statement('channel_copyright')+"padding-top:0px;padding-bottom:5px;}";
	css+="div.item { padding:20px 60px 20px 40px;}";
	css+="div.title { "+mkcss_statement('title')+"letter-spacing:1px;}";
	css+="div.author { "+mkcss_statement('author')+"padding-top:12px;}";
	css+="div.pubDate { "+mkcss_statement('pubDate')+"padding-top:12px;}";
	css+="div.description { "+mkcss_statement('description')+"padding-top:12px;}";
	css+="div.link { "+mkcss_statement('author')+"padding-top:12px;}";
	css+="<\/style>";
	return css;
	}

function runupdates(xmode) {
	document.forms["state"].update.value=escape(updaterss());
	document.forms["state"].css.value=escape(updatecss());;
	document.forms["state"].pagecolor.value=pagecolor;
	document.forms["state"].linkcolor.value=linkcolor;
	document.forms["state"].channel_title.value=channel_title;
	document.forms["state"].channel_description.value=channel_description;
	document.forms["state"].channel_pubDate.value=channel_pubDate;
	document.forms["state"].channel_copyright.value=channel_copyright;
	document.forms["state"].channel_managingEditor.value=channel_managingEditor;
	document.forms["state"].mode.value=xmode;
	document.forms["state"].submit();
	}

function updatecss() {
	var newcss="";
	for(keys in Styles) {
		newcss+="."+keys+"{";
		newcss+="display:"+Styles[keys].display+";";
		newcss+="font-family:"+Styles[keys].face+";";
		newcss+="font-size:"+Styles[keys].size+";";
		newcss+="font-weight:"+Styles[keys].weight+";";
		newcss+="color:"+Styles[keys].color+";";
		newcss+="}";
		}
	return newcss;
	}

function updaterss() {
	var newdbstr="";
	for(i=0;i<Items.length;i++) {
		newdbstr+=Items[i].link+"\t"+Items[i].title+"\t"+Items[i].description+"\t"+Items[i].pubDate+"\t"+Items[i].category+"\t"+Items[i].author+"\t"+Items[i].guid+"\t"+Items[i].enclosure+"\n";
		}
	return newdbstr;
	}

function mkcss_statement(xselector) {
	var css_statement="font-family:"+Styles[xselector].face+";";
	css_statement+="font-size:"+Styles[xselector].size+";";
	css_statement+="color:"+Styles[xselector].color+";";
	css_statement+="font-weight:"+Styles[xselector].weight+";";
	return css_statement;
	    }


