//document.domain = "dr.dk";

function doFrontPageStuff()
{
	if (document.getElementById("spot1") != null)
	{
		fitBoxes();
		getClubNews();
		getFeed();
	}
}


function getFeed()
{
	if(document.getElementById("spot6indhold") != null)
	{
		var boxHeight = document.getElementById("spot6indhold").offsetHeight;
		amount = Math.floor(boxHeight / 15);
		
		var myXHR = new XHR({
			method: 'get',
			onSuccess: extractFeed
	  }).send('/default.ns', 'lngItemID=1318&feed=forum_all&amount=' + amount);
	}
}

function clearFeedArea()
{
	while (document.getElementById("spot6indhold").firstChild)
	{
		document.getElementById("spot6indhold").removeChild(document.getElementById("spot6indhold").firstChild);
	}
	document.getElementById("spot6indhold").parentNode.style.background = "#fff";
	
}

function appendFeedItem(name, title, url)
{
	var a = document.createElement("a");
	if (name.length > 41)
	{
		name = name.substr(0, 38) + "...";
	}
	a.appendChild(document.createTextNode(String.fromCharCode(183) + name));
	a.title = title;
	a.href = url;
	a.style.display = "block";
	a.style.textDecoration = "underline";
	a.style.lineHeight = "15px";
	document.getElementById("spot6indhold").appendChild(a);
}

function extractFeed(text, inXML)
{
	clearFeedArea();
	var root = inXML.getElementsByTagName("frontPageFeed")[0];
	var items = root.getElementsByTagName("item");
	var i = 0;
	while (i < items.length)
	{
		if (items[i].tagName == "item")
		{
			var name = items[i].getElementsByTagName("name")[0].firstChild.nodeValue;
			var title = items[i].getElementsByTagName("title")[0].firstChild.nodeValue;
			var url = items[i].getElementsByTagName("url")[0].firstChild.nodeValue;
			appendFeedItem(name, title, url);
		}
		i++;
	}
}

function getClubNews()
{
	if(document.getElementById("clubNewsSpotTop"))
	{
		var myXHR = new XHR({
			method: 'get',
			onSuccess: extractClubNews
	  }).send('/default.ns', 'lngItemID=1317'); // http://dr.dk/skum/community/xml/zyggo.drxml
	}
}

function extractClubNews(text, inXML)
{
	var root = inXML.getElementsByTagName("skumClubNews")[0];
	var clubID = root.getElementsByTagName("clubID")[0].firstChild.nodeValue;
	var clubName = root.getElementsByTagName("clubName")[0].firstChild.nodeValue;
	if (root.getElementsByTagName("clubImage")[0].firstChild != null)
	{
		var clubImage = root.getElementsByTagName("clubImage")[0].firstChild.nodeValue;
	}
	var subject = root.getElementsByTagName("subject")[0].firstChild.nodeValue;
	var body = root.getElementsByTagName("text")[0].firstChild.nodeValue;
	renderClubNewsSpot(clubID, clubName, clubImage, subject, body);
}

function renderClubNewsSpot(clubID, clubName, clubImage, subject, body)
{
	document.getElementById("spot2link").href = "http://community.dr.dk/default.ns?strItemID=club&lngClubID=" + clubID;
	document.getElementById("spot2link").title = "Nyhed fra klubben '" + clubName + "'";
	document.getElementById("spot2image").style.backgroundImage = "url(" + clubImage + ")"; // "http://community.dr.dk" +
	while(document.getElementById("spot2header").firstChild)
	{
		document.getElementById("spot2header").removeChild(document.getElementById("spot2header").firstChild);
	}
	// remove NML from subject and body
	subject = subject.replace(/\[[^\]]+\]/gi, "");
	body = body.replace(/\[[^\]]+\]/gi, "");
	// use club name as header
	var xsubject = subject;
	subject = clubName;
	var subjectMaxLength = 19;
	if (subject.length > subjectMaxLength)
	{
		subject = subject.substr(0, (subjectMaxLength - 3));
		subject = subject + "...";
	}
	document.getElementById("spot2header").appendChild(document.createTextNode(subject));
	document.getElementById("spot2header").style.textDecoration = "underline";
	while(document.getElementById("spot2text").firstChild)
	{
		document.getElementById("spot2text").removeChild(document.getElementById("spot2text").firstChild);
	}
	var bodyMaxLength = 80;
	if (body.length > bodyMaxLength)
	{
		body = body.substr(0, bodyMaxLength);
		body = body.split(" ");
		body.pop();
		body = body.join(" ");
		body = body + "...";
	}
	document.getElementById("spot2text").appendChild(document.createTextNode(body));
}

function fitBoxes()
{
	if (document.getElementById("spot4") && document.getElementById("spot6indhold"))
	{
		// move content of box 1 down
		var box = document.getElementById("spot1_tekst");
		box.style.marginTop = (140 - box.offsetHeight) + "px";
		document.getElementById("spot1indhold").style.visibility = "visible";
		
		// align box 4 and 6
		var s4 = document.getElementById("spot4");
		var s6 = document.getElementById("spot6indhold");
		s6.style.height = (s4.offsetHeight - 236) + "px";
		s6.style.overflow = "hidden";
	}
}
