/**
 * Reactee Topics JS-Database and UI Library.
 * ---------------------------------------------------------------------------
 *
 * Provides JavaScript support for topics database in shirt edit.
 *
 * ---------------------------------------------------------------------------
 * @author Dan Kamins [d k a m i n s A.T r e a c t e e D.O.T. c o m]
 * @package webReactee
 * ---------------------------------------------------------------------------
 * Copyright (c) 2006, TextMarks
 * ---------------------------------------------------------------------------
 */
/** */


// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------


function rt_topics_addForWhats()
{
	var eForWhats = document.getElementById("divStepForWhatList");
	
	for (var i=0; i < rt_topics_forwhats.length; ++i)
	{
		var sForWhat = rt_topics_forwhats[i];
		var sForWhatDisplay = sForWhat;
		if (sForWhat == " ") { sForWhatDisplay = "<i>(none)</i>"; }
		var eNewForWhat = document.createElement("div");
		eNewForWhat.className = "divForWhat";
		eNewForWhat.innerHTML = "<a href=\"javascript:;\" onclick=\"rt_topics_selStepAndGoStep('ForWhat', '"+escape(sForWhat)+"', '');\">"+sForWhatDisplay+"</a>";
		eForWhats.appendChild(eNewForWhat);
	}
}

function rt_topics_addTopic(sTopic, iColNum)
{
	var eTopics = document.getElementById("divStepTopicList" + iColNum);
	var eNewTopic = document.createElement("div");
	eNewTopic.className = "divTopic";
	eNewTopic.innerHTML = "<a href=\"javascript:;\" onclick=\"rt_topics_updateSlogansForTopic('"+escape(sTopic)+"'); rt_topics_selStepAndGoStep('Topic', '"+escape(sTopic)+"', 'Slogan');\">"+sTopic+"</a>";
	if (sTopic == "Your own topic") { eNewTopic.innerHTML = "<b>" + eNewTopic.innerHTML + "</b>"; }
	eTopics.appendChild(eNewTopic);
}

/** Note: Requires gs_rt_PathToRoot. */
function rt_topics_addImages()
{
	var eImages = document.getElementById("divStepImageList");
	
	for (var i=0; i < rt_topics_images.length; ++i)
	{
		var sImage = rt_topics_images[i];
		var sImageDisplay = sImage;
		var eNewImage = document.createElement("span");
		eNewImage.className = "spanImage";
		eNewImage.innerHTML = "<a href=\"javascript:;\" onclick=\"rt_topics_setStepCurrent('Image', '"+escape(sImage)+"');\"><img src=\""+gs_rt_PathToRoot+"/images/Shirt-Image-Icons/"+sImage+"-icon.gif\" alt=\""+sImage+"\" alt=\""+sImage+"\" title=\""+sImage+"\" /></a>";
		eImages.appendChild(eNewImage);
	}
}

// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------

/** Expand step indicated. */
function rt_topics_expandStep(sStepId)
{
	document.getElementById("divStep"+sStepId+"Collapsed").style.display = "none";
	document.getElementById("divStep"+sStepId+"Expanded" ).style.display = "block";
	if (sStepId=="Slogan") { document.getElementById("inpSlogan").focus(); }
	if (sStepId=="Keyword") 
	{
		var sIdKeyword = (document.getElementById('divStepKeywordTabKeyword').style.display == "block") ? "inpKeyword" : "inpTwitter";
		document.getElementById(sIdKeyword).focus();
	}
	if (sStepId=="ForWhat") { document.getElementById("ForWhat").focus(); }
}

/** Collapse step indicated. */
function rt_topics_collapseStep(sStepId)
{
	document.getElementById("divStep"+sStepId+"Expanded").style.display = "none";
	var sVal = document.getElementById("inp"+sStepId ).value;
	document.getElementById("divStep"+sStepId+"Collapsed").style.display = "block";
}

/** Collapse all steps. */
function rt_topics_collapseAllSteps()
{
	rt_topics_collapseStep("Topic");
	rt_topics_collapseStep("Slogan");
	rt_topics_collapseStep("Keyword");
	rt_topics_collapseStep("Image");
	rt_topics_collapseStep("ForWhat");
}

/** Set current value for step indicated. */
function rt_topics_setStepCurrent(sStepId, sCurrent)
{
	sCurrent = unescape(sCurrent);
	document.getElementById("inp"+sStepId).value = sCurrent;
	document.getElementById("spnStep"+sStepId+"Current").innerHTML = "" + sCurrent + "";
}

/** Clicked edit on indicated step. */
function rt_topics_editStep(sStepId)
{
	rt_topics_collapseAllSteps();
	rt_topics_expandStep(sStepId);
}

/** Select item for this step and go to new step. */
function rt_topics_selStepAndGoStep(sStepId, sNewVal, sGoStepId)
{
	rt_topics_setStepCurrent(sStepId, sNewVal);
	rt_topics_collapseStep(sStepId);
	if (sGoStepId != '')
	{
		rt_topics_expandStep(sGoStepId);
	}
}


// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------

function rt_topics_updateSlogansForTopic(sTopic)
{
	sTopic = unescape(sTopic);
	
	// Find and clear slogans list:
	var eSlogans = document.getElementById("divStepSloganList");
	eSlogans.innerHTML = "";
	
	// Add slogans for topic:
	for (var i=0; i < rt_topics_topics[sTopic].length; ++i)
	{
		sSlogan = rt_topics_topics[sTopic][i].s;
		var eNewSlogan = document.createElement("div");
		eNewSlogan.className = "divSlogan";
		eNewSlogan.innerHTML = "<a href=\"javascript:;\" onclick=\"rt_topics_updateAllForSlogan('"+escape(sTopic)+"', '"+escape(sSlogan)+"', true); rt_topics_selStepAndGoStep('Slogan', '"+escape(sSlogan)+"', 'Keyword');\">"+sSlogan+"</a>";
		eSlogans.appendChild(eNewSlogan);
	}
}

function rt_topics_updateKeywordsForSlogan(sTopic, sSlogan, bSetCurrent)
{
	sTopic = unescape(sTopic);
	sSlogan = unescape(sSlogan);

	// Find and clear keywords list:
	var eKeywords = document.getElementById("divStepKeywordList");
	eKeywords.innerHTML = "";

	// Find topic entry for slogan:
	var oEntry = "";
	for (var i=0; i < rt_topics_topics[sTopic].length; ++i)
	{
		if (rt_topics_topics[sTopic][i].s == sSlogan) { oEntry = rt_topics_topics[sTopic][i]; break; }
	}
	if (oEntry == "") { return; }
	
	// Add Keywords for topic/slogan:
	var sKeywordFirst = "";
	for (var i=0; i < oEntry.k.length; ++i)
	{
		sKeyword = oEntry.k[i];
		if (sKeywordFirst == "") { sKeywordFirst = sKeyword; }
		var eNewKeyword = document.createElement("div");
		eNewKeyword.className = "divKeyword";
		eNewKeyword.innerHTML = "<a href=\"javascript:;\" onclick=\"rt_topics_selStepAndGoStep('Keyword', '"+escape(sKeyword)+"', 'Image'); \">"+sKeyword+"</a>";
		//eNewKeyword.innerHTML = "<a href=\"javascript:;\" onclick=\"rt_topics_setStepCurrent('Keyword', '"+escape(sKeyword)+"'); \">"+sKeyword+"</a>";
		eKeywords.appendChild(eNewKeyword);
	}
	
	// Set first keyword:
	if (bSetCurrent) { rt_topics_setStepCurrent("Keyword", escape(sKeywordFirst)); }
}

function rt_topics_updateForWhatsForSlogan(sTopic, sSlogan, bSetCurrent)
{
	sTopic = unescape(sTopic);
	sSlogan = unescape(sSlogan);

	// Find topic entry for slogan:
	var oEntry = "";
	for (var i=0; i < rt_topics_topics[sTopic].length; ++i)
	{
		if (rt_topics_topics[sTopic][i].s == sSlogan) { oEntry = rt_topics_topics[sTopic][i]; break; }
	}
	if (oEntry == "") { return; }

	// Set forWhat:
	if (bSetCurrent) 
	{
		var sForWhat = escape(oEntry.f);
		if (sForWhat == "") { sForWhat = " "; }
		rt_topics_setStepCurrent("ForWhat", sForWhat);
	}
}

function rt_topics_updateResponseForSlogan(sTopic, sSlogan, bSetCurrent)
{
	sTopic = unescape(sTopic);
	sSlogan = unescape(sSlogan);

	// Find topic entry for slogan:
	var oEntry = "";
	for (var i=0; i < rt_topics_topics[sTopic].length; ++i)
	{
		if (rt_topics_topics[sTopic][i].s == sSlogan) { oEntry = rt_topics_topics[sTopic][i]; break; }
	}
	if (oEntry == "") { return; }

	// Set response:
	if (bSetCurrent) { document.getElementById("inpResponse").value = oEntry.r; }
}

function rt_topics_updateAllForSlogan(sTopic, sSlogan, bSetCurrent)
{
	rt_topics_updateKeywordsForSlogan(sTopic, sSlogan, bSetCurrent);
	rt_topics_updateForWhatsForSlogan(sTopic, sSlogan, bSetCurrent);
	rt_topics_updateResponseForSlogan(sTopic, sSlogan, bSetCurrent);
}
