/*
	TinySite.XML
	-----------------------------
	TinySite.XML homepage: http://acidmartin.wemakesites.net/?pageId=TinySiteXml
	
	Created By: Martin Ivanov
	
	Website:
		- http://acidmartin.com/
		- http://acidmartin.wemakesites.net
		- http://wemakesites.net
	
	Contact: acid_martin@yahoo.com
	
	IMPORTANT REGULATIONS:
	
	1. IF YOU ARE USING THE OBFUSCATED VERSION OF TINYSITE.XML, IT IS FORBIDDEN TO DEOBFUSCATE AND MODIFY THIS SRCIPT WITHOUT AUTHOR'S WRITTEN PERMISSION. OTHERWISE SUCH ACTIONS WILL BE CONSIDERED VIOLATIONS OF THE COPYRIGHT LAWS AND LEGAL STEPS WILL BE TAKEN.
	
	2. IF YOU HAVE PURCHASED THE SOURCE CODE OF TINYSITE.XML, YOU CANNOT USE THIS SCRIPT ON WEBPAGES WITHOUT
	OBFUSCATING IT. OTHERWISE SUCH ACTIONS WILL BE CONSIDERED VIOLATIONS OF THE COPYRIGHT LAWS AND LEGAL STEPS WILL BE TAKEN.
*/

var tsVersion = '1.0';
var tsErrorMessage = 'TinySite.XML ' + tsVersion + ' Error Message:\n\n';
var renderingStopped = '\n\nRendering stopped.';
var loadingPanelTimeOut = '1000';

function initTinySite()
{
	if (window!= top)
	top.location.href=location.href;
	loadConfigFile();
	if (!document.getElementsByTagName) return; 
	var externalLinks = document.getElementsByTagName("a"); 
	for (var i=0; i<externalLinks.length; i++) 
		{ 
			var externalLink = externalLinks[i]; 
			if (externalLink.getAttribute("href") && externalLink.getAttribute("rel") == "external")
		{
			externalLink.target = "_blank";
		}
	}
	renderLoadingPanel();
	toggleLoadingPanel();
	pageScriptsFolder = 'Scripts';
	loadAnnouncement('0');
	radioButtonXmlScriptsFolder = 'Scripts';
	renderRadioButtonList('Scripts/RadioButton.XML/RadioButtonLists/VotingPoll.xml', 'VotingPollLoader');
	var pageBody = document.getElementsByTagName("body")[0];
	var allElementsOnPageBody = pageBody.getElementsByTagName("*");
	for (var j=0; j<allElementsOnPageBody.length; j++) 
	{
		allElementsOnPageBody[j].setAttribute('unselectable', 'on');
	}
	replaceButtons();
}

function loadConfigFile()
{
	var tinySiteConfigFile = 'TinySite.XML/Config.xml';
	if(window.ActiveXObject)
	{
		tinysiteconfig = new ActiveXObject("Microsoft.XMLDOM");
		tinysiteconfig.async = false;
		tinysiteconfig.load(tinySiteConfigFile);
	}
	else if(document.implementation && document.implementation.createDocument)
	{
		tinysiteconfig = document.implementation.createDocument("","",null);
		tinysiteconfig.async = false;
		tinysiteconfig.load(tinySiteConfigFile);
	}
	var tinySiteEnabled = tinysiteconfig.getElementsByTagName('config')[0].getAttribute('enabled');
	if(tinySiteEnabled == 'true')
	{
		try
		{
			loadLayout();
			loadDataFile();
			loadDefaultHomePage();
		}
		catch(err)
		{
			
		}
	}
	else if(tinySiteEnabled != 'true' && tinySiteEnabled != 'false')
	{
		alert(tsErrorMessage + 'The value of the "enabled" property should be either "true" or "false".' + renderingStopped);
	}
}

function loadLayout()
{
	try
	{
		var tinySiteLayoutName = tinysiteconfig.getElementsByTagName('config')[0].getAttribute('layoutfile');
		var tinySiteSkinName = tinysiteconfig.getElementsByTagName('config')[0].getAttribute('theme');
		var nocss = tinysiteconfig.getElementsByTagName('config')[0].getAttribute('nocss');
		var tinySiteLayoutFile = 'TinySite.XML/Layouts/' + tinySiteLayoutName + '/Layout.xml';
		var tinySiteLayoutCssFile = document.createElement('link');
		tinySiteLayoutCssFile.setAttribute('type', 'text/css');
		tinySiteLayoutCssFile.setAttribute('rel', 'stylesheet');
		tinySiteLayoutCssFile.setAttribute('href', 'TinySite.XML/Layouts/' + tinySiteLayoutName + '/Layout.css');
		tinySiteSkin = document.createElement('link');
		tinySiteSkin.setAttribute('type', 'text/css');
		tinySiteSkin.setAttribute('rel', 'stylesheet');
		tinySiteSkin.setAttribute('href', 'TinySite.XML/Layouts/' + tinySiteLayoutName + '/Themes/' + tinySiteSkinName + '/Styles.css');
		if(nocss == 'false')
		{
			document.getElementsByTagName('head')[0].appendChild(tinySiteLayoutCssFile);
			document.getElementsByTagName('head')[0].appendChild(tinySiteSkin);
		}
		if(window.ActiveXObject)
		{
			tinysitelayout = new ActiveXObject("Microsoft.XMLDOM");
			tinysitelayout.async = false;
			tinysitelayout.load(tinySiteLayoutFile);
		}
		else if(document.implementation && document.implementation.createDocument)
		{
			tinysitelayout = document.implementation.createDocument("","",null);
			tinysitelayout.async = false;
			tinysitelayout.load(tinySiteLayoutFile);
		}
		document.getElementsByTagName('body')[0].innerHTML = tinysitelayout.getElementsByTagName('html')[0].childNodes[0].nodeValue;
	}
	catch(err)
	{
		alert(tsErrorMessage + 'Layout (' + tinySiteLayoutFile + ') file not found.' + renderingStopped);
	}
	contextMenuScriptsRootFolder = 'Scripts';
}

function loadDataFile()
{
	var tinySiteDataFile = 'TinySite.XML/Data.xml';
	var liteVersion = false;
	try
	{
		if(window.ActiveXObject)
		{
			tinysitedata = new ActiveXObject("Microsoft.XMLDOM");
			tinysitedata.async = false;
			tinysitedata.load(tinySiteDataFile);
		}
		else if(document.implementation && document.implementation.createDocument)
		{
			tinysitedata = document.implementation.createDocument("","",null);
			tinysitedata.async = false;
			tinysitedata.load(tinySiteDataFile);
		}
		var navigationLinksHolder = document.createElement('ul');
		navigationLinksHolder.setAttribute('id', 'TinySiteNavigation_NavigationList');
		$('TinySiteNavigation').appendChild(navigationLinksHolder);
		var navigationLinks = tinysitedata.getElementsByTagName('page');
		for (i = 0; i < navigationLinks.length; i ++)
		{
			var navigationLinkItem = document.createElement('li');
			var linkText = tinysitedata.getElementsByTagName('page')[i].getAttribute('linkname');
			var ExcludedLink = tinysitedata.getElementsByTagName('page')[i].getAttribute('navigable');
			var onClientClick = tinysitedata.getElementsByTagName('page')[i].getAttribute('onclientclick');
			var navigable = '';
			if(ExcludedLink == 'true')
			{
				navigable = '';
			}
			if(ExcludedLink == 'false')
			{
				navigable = 'display: none';
			}
			else if(ExcludedLink != 'false' && ExcludedLink != 'true')
			{
				alert(tsErrorMessage + '"' + ExcludedLink + '" is not a valid value of the "navigable" property. It should be either "true" or "false".' + renderingStopped);
			}
			navigationLinkItem.setAttribute('id', 'TinySiteNavigation_NavigationListItem_' + i );
	navigationLinkItem.innerHTML = '<a href="javascript:void(0);" id="TinySiteNavigation_NavigationListItem_Link_' + i + '" title="' + linkText + '" onclick="loadContent(\'' + i + '\'); resetLinkState(\'TinySiteNavigation_NavigationList\', this.id, \'TinySiteSelectedLink\'); ' + onClientClick + ';" style="' + navigable + '"><span class="tsxOuterSpan"><span class="tsxInnerSpan">' + linkText + '</span></span></a>'
			$('TinySiteNavigation_NavigationList').appendChild(navigationLinkItem);
		}
		var debugmode = tinysiteconfig.getElementsByTagName('config')[0].getAttribute('debugmode');
		var mailinglistform = tinysiteconfig.getElementsByTagName('config')[0].getAttribute('mailinglistform');
		var searchform = tinysiteconfig.getElementsByTagName('config')[0].getAttribute('searchform');
		var signinform = tinysiteconfig.getElementsByTagName('config')[0].getAttribute('signinform');
		var customtextboxleft = tinysiteconfig.getElementsByTagName('config')[0].getAttribute('customtextboxleft');
		var customtextboxright = tinysiteconfig.getElementsByTagName('config')[0].getAttribute('customtextboxright');
		var blogroll = tinysiteconfig.getElementsByTagName('config')[0].getAttribute('blogroll');
		var votingpoll = tinysiteconfig.getElementsByTagName('config')[0].getAttribute('votingpoll');
		var footer = tinysiteconfig.getElementsByTagName('config')[0].getAttribute('footer');
		var rssbutton = tinysiteconfig.getElementsByTagName('config')[0].getAttribute('rssbutton');
		var rsslink = tinysiteconfig.getElementsByTagName('config')[0].getAttribute('rsslink');
		var latestnews = tinysiteconfig.getElementsByTagName('config')[0].getAttribute('latestnews');
		if(latestnews == 'true')
		{
			loadComponent('LatestNews.xml', 'LatestNews');
		}
		if(mailinglistform == 'true')
		{
			loadComponent('MailingList.xml', 'MailingList');
		}
		if(mailinglistform != 'true' && mailinglistform != 'false')
		{
			alert(tsErrorMessage + '"' + mailinglistform + '" is not a valid value of the "mailinglistform" property. It should be either "true" or "false".' + renderingStopped);
		}
		if(searchform == 'true')
		{
			loadComponent('Search.xml', 'Search');
		}
		if(searchform != 'true' && searchform != 'false')
		{
			alert(tsErrorMessage + '"' + searchform + '" is not a valid value of the "searchform" property. It should be either "true" or "false".' + renderingStopped);
		}
		if(signinform == 'true')
		{
			loadComponent('SignIn.xml', 'SignIn');
		}
		if(signinform != 'true' && signinform != 'false')
		{
			alert(tsErrorMessage + '"' + signinform + '" is not a valid value of the "signinform" property. It should be either "true" or "false".' + renderingStopped);
		}
		if(customtextboxleft == 'true')
		{
			loadComponent('CustomTextLeft.xml', 'CustomTextLeft');
		}
		if(customtextboxleft != 'true' && customtextboxleft != 'false')
		{
			alert(tsErrorMessage + '"' + customtextboxleft + '" is not a valid value of the "customtextboxleft" property. It should be either "true" or "false".' + renderingStopped);
		}
		if(customtextboxright == 'true')
		{
			loadComponent('CustomTextRight.xml', 'CustomTextRight');
		}
		if(customtextboxright != 'true' && customtextboxright != 'false')
		{
			alert(tsErrorMessage + '"' + customtextboxright + '" is not a valid value of the "customtextboxright" property. It should be either "true" or "false".' + renderingStopped);
		}
		if(blogroll == 'true')
		{
			loadComponent('BlogRoll.xml', 'BlogRoll');
		}
		if(blogroll != 'true' && blogroll != 'false')
		{
			alert(tsErrorMessage + '"' + blogroll + '" is not a valid value of the "blogroll" property. It should be either "true" or "false".' + renderingStopped);
		}
		if(votingpoll == 'true')
		{
			loadComponent('VotingPoll.xml', 'VotingPoll');
		}
		if(votingpoll != 'true' && votingpoll != 'false')
		{
			alert(tsErrorMessage + '"' + votingpoll + '" is not a valid value of the "votingpoll" property. It should be either "true" or "false".' + renderingStopped);
		}
		if(footer == 'true' || liteVersion == true)
		{
			loadComponent('Footer.xml', 'TinySiteFooter');
		}
		if(footer != 'true' && footer != 'false')
		{
			alert(tsErrorMessage + '"' + footer + '" is not a valid value of the "footer" property. It should be either "true" or "false".' + renderingStopped);
		}
		if(debugmode == 'on')
		{
			f = window.open('', 'SourceCode', '');
			f.document.write('<html><head><title>TinySite.XML ' + tsVersion + ' Source Viewer</title></head><body style="overflow: hidden; padding: 0; margin: 0;"><form><textarea style="border: 0; display: block; width: 100%; height: 800px; text-transform: lowercase; color: blue; font: normal 11px \'Courier New\';"><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">\n<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">\n' + document.documentElement.innerHTML + '</form></body></html></textarea></html>');
		}
		if(debugmode != 'on' && debugmode != 'off')
		{
			alert(tsErrorMessage + '"' + debugmode + '" is not a valid value of the "debugmode" property. It should be either "on" or "off".' + renderingStopped);
		}
		if(rssbutton == 'true')
		{
			var rssLink = document.createElement('a');
			rssLink.className = 'rssbutton';
			rssLink.innerHTML = 'RSS Feed';
			rssLink.setAttribute('title', 'RSS Feed');
			rssLink.setAttribute('href', rsslink);
			rssLink.setAttribute('rel', 'external');
			$('TinySiteHeader').appendChild(rssLink);
		}
		if(rssbutton != 'true' && rssbutton != 'false')
		{
			alert(tsErrorMessage + '"' + rssbutton + '" is not a valid value of the "rssbutton" property. It should be either "true" or "false".' + renderingStopped);
		}
		if(liteVersion)
		{
			$('TinySiteFooter').childNodes[0].innerHTML = '<div style="font-size: 12px; text-align: right; padding-right: 8px;">Created with <a href="http://acidmartin.wemakesites.net/?pageId=TinySiteXml" title="TinySite.XML Lite" target="_blank">TinySite.XML Lite</a></div>';
		}
		attachDomEvent('RssFeedLink', 'click', function(e)
			{
				window.open(rsslink, '_blank');
			}, true);
		attachDomEvent('RssFooterLink', 'click', function(e)
			{
				window.open(rsslink, '_blank');
			}, true);
		// customize message box through these variables
		skinnableBrowserDialogsRootFolder = 'Scripts'; // root folder of the 'SkinnableBrowserDialogs' directory
		messageBoxEnabled = true; // "true" or "false" - enable or disable the rendering of the Skinnable Browser Dialogs
		messageBoxSkin = 'AcidMachine'; // message box skin
		messageBoxWidth = '250'; // width of the message box
		OkButtonText = 'OK'; // "OK" button text
		CancelButtonText = 'Cancel'; // "Cancel" button text
		initSkinnableBrowserDialogs();
		renderContextMenu();
		initContextMenuXml();
	}
	catch(err)
	{
		alert(tsErrorMessage + 'Datafile not found.' + renderingStopped);
		return false;
	}
}

function loadContent(PageIndex)
{
	toggleLoadingPanel();
	var pageContent = tinysitedata.getElementsByTagName('page'); 
	var pageTitle = pageContent[PageIndex].getAttribute('title');
	var tinySiteName = tinysiteconfig.getElementsByTagName('config')[0].getAttribute('sitename');
	var tinySiteTitleDelimiter = tinysiteconfig.getElementsByTagName('config')[0].getAttribute('pagedelimiter');
	$('TinySiteMiddleColumn').innerHTML = pageContent[PageIndex].childNodes[0].nodeValue;
	$('TinySitePageName').innerHTML = pageTitle;
	$('TinySiteHeading').innerHTML = tinySiteName;
	document.title = tinySiteName + tinySiteTitleDelimiter + pageTitle;
}

function toggleLoadingPanel()
{
	displayLoadingPanel();
	setTimeout('hideLoadingPanel()', loadingPanelTimeOut);
}

function loadDefaultHomePage()
{
	var defaultPageIndex = tinysiteconfig.getElementsByTagName('config')[0].getAttribute('defaultpageindex');
	var pageContent = tinysitedata.getElementsByTagName('page'); 
	var pageTitle = pageContent[defaultPageIndex].getAttribute('title');
	var tinySiteName = tinysiteconfig.getElementsByTagName('config')[0].getAttribute('sitename');
	var tinySiteTitleDelimiter = tinysiteconfig.getElementsByTagName('config')[0].getAttribute('pagedelimiter');
	$('TinySiteMiddleColumn').innerHTML = pageContent[defaultPageIndex].childNodes[0].nodeValue;
	$('TinySitePageName').innerHTML = pageTitle;
	$('TinySiteHeading').innerHTML = tinySiteName;
	$('TinySiteHeading').setAttribute('title', tinySiteName + ' ' + tinySiteTitleDelimiter + ' Home');

	attachDomEvent('TinySiteHeading', 'click', function(e)
	{
		loadContent(defaultPageIndex);
		resetLinkState('TinySiteNavigation_NavigationList', 'TinySiteNavigation_NavigationListItem_Link_' + defaultPageIndex, 'TinySiteSelectedLink');
	}
	, true);

	document.title = tinySiteName + tinySiteTitleDelimiter + pageTitle;
	resetLinkState('TinySiteNavigation_NavigationList', 'TinySiteNavigation_NavigationListItem_Link_' + defaultPageIndex, 'TinySiteSelectedLink');
}

function $(stringId)
{
	return document.getElementById(stringId);
}

if(window.attachEvent)
{
	window.attachEvent('onload', function(e)
		{
			initTinySite();
			// vote thank you
			if(location.href.indexOf('?pageId=Voted') > 0)
			{
				loadContent('9');
				redirectAfterSubmit();
			}
			// contact us thank you
			if(location.href.indexOf('?pageId=ContactFormSubmitted') > 0)
			{
				loadContent('10');
				redirectAfterSubmit();
			}
			// contact form
			if(location.href.indexOf('?pageId=Form') > 0)
			{
				loadContent('6');
				resetLinkState('TinySiteNavigation_NavigationList', 'TinySiteNavigation_NavigationListItem_Link_6', 'TinySiteSelectedLink');
				loadContactFormControls();
				sendThisEmailTo()
			}
			// contest
			if(location.href.indexOf('?pageId=Contest') > 0)
			{
				loadContent('12');
				resetLinkState('TinySiteNavigation_NavigationList', 'TinySiteNavigation_NavigationListItem_Link_12', 'TinySiteSelectedLink');
				initLogoContestTabStrip();
			}
		}
	);
}
else
{
	window.addEventListener('load', function(e)
		{
			initTinySite();
			if(location.href.indexOf('?pageId=Voted') > 0)
			{
				loadContent('9');
				redirectAfterSubmit();
			}
			if(location.href.indexOf('?pageId=ContactFormSubmitted') > 0)
			{
				loadContent('10');
				redirectAfterSubmit();
			}	
		}, true	
	);
}

function redirectAfterSubmit()
{
	setTimeout('window.open("/", "_self")', 3000);
}

function resetLinkState(linkContainer, linkId, selectedClassName)

{
	
	var linksToReset = $(linkContainer).getElementsByTagName('a');

	for (i = 0; i < linksToReset.length; i ++)
	
	{
		
		linksToReset[i].className = '';

	}
	
	$(linkId).className = selectedClassName;

}


function loadComponent(ComponentFile, ComponentPlaceholder)
{
	if(window.ActiveXObject)
	{
		component = new ActiveXObject("Microsoft.XMLDOM");
		component.async = false;
		component.load('TinySite.XML/Components/' + ComponentFile);
	}
	else if(document.implementation && document.implementation.createDocument)
	{
		component = document.implementation.createDocument("","",null);
		component.async = false;
		component.load('TinySite.XML/Components/' + ComponentFile);
	}
	
	$(ComponentPlaceholder).innerHTML = component.getElementsByTagName('code')[0].childNodes[0].nodeValue;

}

function attachDomEvent(ElementId, EventType, ClientFunction, UseCapture)
{
	var ClientEventType;
	if(window.attachEvent)
	{
		ClientEventType = 'on' + EventType;
		document.getElementById(ElementId).attachEvent(ClientEventType, ClientFunction)
	}
	else
	{
		document.getElementById(ElementId).addEventListener(EventType, ClientFunction, UseCapture);
	}
}

/*var message = '';
function clickIE()
{
	if (document.all) 
	{
		(message);
		return false;
	}
}
function clickNS(e) 
{
	if (document.layers ||(document.getElementById&&!document.all)) 
	{
		if (e.which==2 || e.which==3) 
		{
			(message);
			return false;
		}
	}
}
if (document.layers) 
{
	document.captureEvents(Event.MOUSEDOWN);
	document.onmousedown = clickNS;
}
else
{
	document.onmouseup = clickNS;document.oncontextmenu = clickIE;
}

document.oncontextmenu = new Function("return false")*/