/*
STATUS: PENDING
TODO: Test Richmedia (just integrated)
*/

/*
	This file contains all the javascripts that are used by the modules section in the welt online site
*/

/*
	Javascript functions for the poll forum
*/
function ShowHide( id)
{
  try{
	var hideId = "vote-"+id;
	var showId = "voted-"+id;
	var hideElm = document.getElementById( hideId);
	var showElm = document.getElementById( showId);

	if ( hideElm != null && showElm != null) {
		hideElm.style.display = "none";
		showElm.style.display = "block";
	}
	var hideId = "vote-"+id+"-art";
	var showId = "voted-"+id+"-art";

	var hideElm = document.getElementById( hideId);
	var showElm = document.getElementById( showId);

	if ( hideElm != null && showElm != null) {
		hideElm.style.display = "none";
		showElm.style.display = "block";
	}

  }catch (erm){
	//alert("peiling.ShowHide()"+erm);
  }
}
function initialHide( id)
{
  try
  {
	//hide poll Q&A and show Results
	var mentometer = readCookie("mentometer");
	if ( mentometer != null )
	{
	  var mentometer_array = mentometer.split("M");
	  for ( i = 0; i < mentometer_array.length; i++ )
	  {
		if ( mentometer_array[i] != 'null' )
		{
		  if ( mentometer_array[i].match(id) )
		  {
			  ShowHide(""+id);
		  }
		}
	  }
	}
  }
  catch ( erm )
  {
	alert("displayPoll" + erm);
  }
}
/*
	end Javascript functions for the poll forum
*/



/*
 *	Start function for tabs
 */



var TabHash = {};

function showTab(tabgroup, panelid, islasttab)
{
	/* First check if there's 1 tab selected (the first time its the 1st tab probably) */
	if (TabHash[tabgroup])
	{
		/* If the selected tab is the lasttab */
		if (TabHash[tabgroup][2]){
			/* Set a different deselect-style */
			TabHash[tabgroup][0].className="unselectedtablast";
		}else{
			/* If not, set the default deselect-style */
			TabHash[tabgroup][0].className="unselectedtab";
		}
		TabHash[tabgroup][1].style.display= "none";
	}

	/* Then remember the new selected Tab and remember if the tab is the lasttab (islasttab boolean) */
	TabHash[tabgroup] = [document.getElementById(tabgroup + "_" + panelid + "_tab"), document.getElementById(tabgroup + "_" + panelid), islasttab];
	TabHash[tabgroup][0].className="selected";
	TabHash[tabgroup][1].style.display = "block";
	/* If select tab is the last tab, show 'padding-line' */
	if(islasttab){
		document.getElementById('tablastend').style.visibility = 'visible';
	}else{
		document.getElementById('tablastend').style.visibility = 'hidden';
	}
}

/*
 *	End function for tabs
 */

/*
 *	Start function for slideshows
 */


var SlideHash = {};

function slide(slidegroup,delta)
{
	i=0;
	if (SlideHash[slidegroup])
	{
	if (!delta) return true;
		i = SlideHash[slidegroup][0] + (delta>0 ? 1 : -1);
		SlideHash[slidegroup][1].style.display="none";
	}

	//check to see if there if buttons still needs to be displayed
	if (document.getElementById(slidegroup + "_" + (i+1)) != null) document.getElementById(slidegroup + "_forward").style.visibility = "visible";
	else document.getElementById(slidegroup + "_forward").style.visibility = "hidden";

	if (document.getElementById(slidegroup + "_" + (i-1)) != null) document.getElementById(slidegroup + "_back").style.visibility = "visible";
	else document.getElementById(slidegroup + "_back").style.visibility = "hidden";

	SlideHash[slidegroup] = [i,document.getElementById(slidegroup + "_" + i)];
	SlideHash[slidegroup][1].style.display="block";
	return true;
}

/*
 *	End function for slideshows
 */


/*
 *	TabEx used for richmedia box
 */

var LOADED = false;
var hash_tab_ex = {};

function setTabInfo(groupid, nrtabs,cn_notsel, cn_sel, cn_first, cn_last, cn_stop, cn_stop_br)
{
        hash_tab_ex[groupid] = {};
        hash_tab_ex[groupid]["classNotSelected"] = cn_notsel;
        hash_tab_ex[groupid]["classSelected"] = cn_sel;
        hash_tab_ex[groupid]["classFirst"] = cn_first;
        hash_tab_ex[groupid]["classLast"] = cn_last;
        hash_tab_ex[groupid]["classStop"] = cn_stop;
        hash_tab_ex[groupid]["classStopBr"] = cn_stop_br;

        hash_tab_ex[groupid]["nrTabs"] = nrtabs;
        hash_tab_ex[groupid]["currentTabNr"] = -1;
}

function addFirstLast(groupid,sel)
{
        result = "";
        prefix = "";

        if (sel) prefix = hash_tab_ex[groupid]["classSelected"] + "_";
        else prefix = hash_tab_ex[groupid]["classNotSelected"] + "_";

        if (hash_tab_ex[groupid]["currentTabNr"] == 0) return " " + prefix + hash_tab_ex[groupid]["classFirst"];
        if (hash_tab_ex[groupid]["nrTabs"] == hash_tab_ex[groupid]["currentTabNr"]+1) return  " " + prefix + hash_tab_ex[groupid]["classLast"];
        return "";
}

function isLastTab(groupid)
{
        return (hash_tab_ex[groupid]["nrTabs"] == hash_tab_ex[groupid]["currentTabNr"]+1);
}

function showTabEx(groupid, tabid, tabnr)
{
        if (!document.getElementById(groupid+"_"+tabid)) return false;;
        if (!hash_tab_ex[groupid]) return false; // not initialized;

        if (hash_tab_ex[groupid]["currentTabNr"] != -1)
        {
                unsel = hash_tab_ex[groupid]["classNotSelected"] + addFirstLast(groupid,false);
                hash_tab_ex[groupid]["currentTabObj"].className = unsel;
                hash_tab_ex[groupid]["currentDivObj"].style.display = "none";
        }

        tabobj = document.getElementById(groupid + "_" + tabid + "_tab");
        divobj = document.getElementById(groupid + "_" + tabid);

        hash_tab_ex[groupid]["currentTabNr"]  = tabnr;
        hash_tab_ex[groupid]["currentTabObj"] = tabobj;
        hash_tab_ex[groupid]["currentDivObj"] = divobj;

        sel = hash_tab_ex[groupid]["classSelected"] + " " + addFirstLast(groupid,true);

        tabobj.className = sel;
        divobj.style.display = "block";

        stop = document.getElementById(groupid + "_stop_tab");
        if (stop) stop.className = (isLastTab(groupid) ? hash_tab_ex[groupid]["classStopBr"] : hash_tab_ex[groupid]["classStop"]);

        return false;
}

function startupRichmedia(divarray, tabhash)
{
	setTabInfo("rmtab", divarray.length, "tab", "tab_selected","firsttab","lasttab","rest","rest break");
	tabhash = tabhash.substring(1);
	var index = -1;

	//hide all tabs and find the id of the to be selected tab
	for (i=0; i<activeTabs.length; i++)
	{
		document.getElementById("rmtab_" + activeTabs[i]).style.display = "none";
		if (tabhash && (activeTabs[i] == tabhash)) index = i;
	}

	//select a tab
	showTabEx("rmtab",activeTabs[0],0);
	if (index >=0)
	{
		showTabEx("rmtab",tabhash,index);
	}
}

/*
 *	End of TabEx
 */

currElem = null;
currElem2 = null;
function showArticleInteraction(id)
{

        if (currElem != null)
        {
                currElem.style.display = "none";
        }
        if (currElem2 != null)
        {
                currElem2.style.display = "none";
        }

        if (id == "comment")
        {
        	if (document.getElementById("readcomments")) {
                	currElem2 = document.getElementById("readcomments");
                	currElem2.style.display = "block";
                }
        }
        if (!document.getElementById(id)) return true;

		if (captcha = document.getElementById(id + "_captcha_img"))
		{
			captcha.src="/captcha/captcha.jpg?no_cache=" + Math.floor(Math.random() * 9000000 + 1000000);
		}

        currElem = document.getElementById(id);
        currElem.style.display = "block";
        return true;
}

function popupImage(id)
{
        window.open("?service=ImagePopup&id=" + id, "", "scrollbars=no, addressbar=no,statusbar=no, menubar=no");
}


function initByHash()
{
	if (document.location.hash) {
		switch (document.location.hash)
		{
			case "#article_mailauthor":
				showArticleInteraction("mailAuthor");
				break;
			case "#article_recommend":
				showArticleInteraction("recommend");
				break;
			case "#article_comment":
				showArticleInteraction("comment");
				break;
			case "#read_comments":
				showArticleInteraction("comment");
				document.location.hash="#article_readcomments";
				break;
			case "#msg_mailAuthor":
				document.getElementById("xmsg_mailAuthor").style.display = "block";
				break;
			case "#msg_comment":
				document.getElementById("xmsg_comment").style.display = "block";
				break;
			case "#msg_recommend":
				document.getElementById("xmsg_recommend").style.display = "block";
				break;
					
			//Recommend
			case "#article_recommend_parameterError":
				showArticleInteraction("recommend");
				addNotification("recommend_form","Falscher Parameter");
				document.location.hash = "#article_recommend";
				break;
			case "#article_recommend_parameterEmpty":
				showArticleInteraction("recommend");
				addNotification("recommend_form","Falscher Parameter");
				document.location.hash = "#article_recommend";
				break;
			case "#article_recommend_captcha_failure":
				showArticleInteraction("recommend");
				addNotification("recommend_form","Bitte geben Sie den Code erneut ein");
				document.location.hash = "#article_recommend";
				break;
			case "#article_recommend_captcha_sessionError":
				showArticleInteraction("recommend");
				addNotification("recommend_form","Zeitlimit abgelaufen");
				document.location.hash = "#article_recommend";
				break;
				
			//MailAuthor
			case "#article_mailauthor_parameterError":
				showArticleInteraction("mailAuthor");
				addNotification("mailAuthor_form","Falscher Parameter");
				document.location.hash = "#article_mailauthor";
				break;
			case "#article_mailauthor_parameterEmpty":
				alert('article_mailauthor_parameterEmpty');
				showArticleInteraction("mailAuthor");
				addNotification("mailAuthor_form","Falscher Parameter");
				document.location.hash = "#article_mailauthor";
				break;
			case "#article_mailauthor_captcha_failure":
				showArticleInteraction("mailAuthor");
				addNotification("mailAuthor_form","Bitte geben Sie den Code erneut ein");
				document.location.hash = "#article_mailauthor";
				break;
			case "#article_mailauthor_captcha_sessionError":
				showArticleInteraction("mailAuthor");
				addNotification("mailAuthor_form","Zeitlimit abgelaufen");
				document.location.hash = "#article_mailauthor";
				break;
					
			//Comment
			case "#article_kommentar_parameterError":
				showArticleInteraction("comment");
				addNotification("com.escenic.forum.struts.presentation.PostingForm","Falscher Parameter");
				document.location.hash = "#article_comment";
				break;
			case "#article_kommentar_captcha_failure":
				showArticleInteraction("comment");
				addNotification("com.escenic.forum.struts.presentation.PostingForm","Bitte geben Sie den Code erneut ein");
				document.location.hash = "#article_comment";
				break;
			case "#article_kommentar_captcha_sessionError":
				showArticleInteraction("comment");
				addNotification("com.escenic.forum.struts.presentation.PostingForm","Zeitlimit abgelaufen");
				document.location.hash = "#article_comment";
				break;
			case "#article_kommentar_forum_succes":
				/* firstPostInformation is not written anymore
				try{
					document.getElementById("firstPostInformation").style.display = "block";
				}catch(err){
					// "firstPostInformation" not found, not the first post on this article
				}
				*/
				//showArticleInteraction("comment");
				showArticleInteraction("comment");
				addNotification("com.escenic.forum.struts.presentation.PostingForm","Vielen Dank für Ihren Kommentar. Er wird in wenigen Minuten unter dem Artikel erscheinen.");
				document.location.hash="#article_readcomments";
				break;
			case "#article_kommentar_forum_error":
				showArticleInteraction("comment");
				addNotification("com.escenic.forum.struts.presentation.PostingForm","Falscher Parameter");
				document.location.hash="#article_comment";
				break;
			case "#article_kommentar_forum_cancel":
				showArticleInteraction("comment");
				addNotification("com.escenic.forum.struts.presentation.PostingForm","Abgebrochen");
				document.location.hash="#article_comment";
				break;


		}
		//this is needed because when this is called the page has already scrolled to the anchor, and if the content after the page is smaller
		//after the anchor then the page it will scroll to the bottom, after which the div is displayed. The div will then not be scrolled
		//into view correctly. This will recall the anchor after the div is displayed, just like the links in the menu do.
		document.location.hash = document.location.hash;
	}

}

/*
 *	Following function is used to underline every element in a mouse over over en exklusiv item
 */



function styleChildren(obj, elem, val)
{
	par = obj;
	for (i=0; i<par.childNodes.length; i++)
	{
		v = "par.childNodes[i].style." + elem + " = \"" + val + "\"";
		try { eval(v); }
		catch (e) {}
	}
}


/*
 *	end underline styleChildren
 */
