var app = function(){
	var urlPath = window.location.pathname;
	var regexAdmin = /^\/admin\//i;
	var regexInternal = /^\/internal\//i;
	if(regexAdmin.test(urlPath)){return {name: 'Admin', path: '../'};}
	else if(regexInternal.test(urlPath)){return {name: 'Internal', path: '../'};}
	else{return {name: 'Service', path: '../'};}
}();

var renderWidgets = function(){
	if(!document.all){
		var articles = window.document.getElementById('newsletter').getElementsByTagName('div');

		for(var i=0; i<articles.length; i+=1){
			if(articles[i].className === 'widget'){
				var txt = articles[i].childNodes[0].data;
				var d = window.document.createElement('div');
				d.innerHTML = txt;
				articles[i].replaceChild(d, articles[i].childNodes[0]);
			}
		}
	}
};

var setAnchorTargets = function(){
	var anchors = window.document.getElementById('newsletter').getElementsByTagName('a');
	var suffix = '';
	if(app.name === 'Admin'){suffix = '&accountID=' + nl.acctID;}
	for(var i=0; i<anchors.length; i+=1){
	anchors[i].onclick = function(){
	return function(){
	var articleWindow = window.open(this.href + suffix, 'article', 'width=800,height=500,scrollbars=yes');
	articleWindow.focus();
	return false;};}();}
};

var setNewsletterStyle = function(acctID){
	var setStyle = function(http){
		var nlDivs = window.document.getElementById('nlBody').getElementsByTagName('div');
		var styleProperties = JSON.parse(http.responseText);
		window.document.getElementById('nlBody').style.backgroundColor = '#' + styleProperties.bgColor;
		window.document.getElementById('toolBar').style.backgroundColor = '#' + styleProperties.footerColor;;
		window.document.getElementById('footer').style.backgroundColor = '#' + styleProperties.footerColor;
		window.document.getElementById('banner').src = app.path + 'Images/Accounts/' + acctID + '/EFYI.jpg';
		for(var i=0; i<nlDivs.length; i+=1){
			var regTest = /^title/;
			if(regTest.test(nlDivs[i].className)){nlDivs[i].style.color = '#' + styleProperties.headlineColor;}
		}
	};

	var firstArticle = window.document.getElementById('firstArticleDiv').getElementsByTagName('div')[0];
	if(firstArticle.offsetHeight < 120){
	var y = (120 - firstArticle.offsetHeight) / 2;
	firstArticle.style.paddingTop = y + 'px';
	firstArticle.style.paddingBottom = y + 'px';}

	if(cookies.get('av') !== null){
		req.getReqExe('action=getNLStyleProperties&acctID=' + acctID, setStyle, app.path + 'Newsletters.ashx');
	}
};

var nl = {
	xmlDoc: null,
	xsltDoc: null,
	container: null,
	template: null,
	acctID: parseInt(cookies.get('aID'), 10),
	getNewsletter: function(volumeID, channel){
		var vol = volumeID.match(/\d+/);
		var ch = channel.match(/\d+/);
		var qstring = 'action=getNewsletter&vol=' + vol + '&ch=' + ch;

		var setXmlDoc = function(http){
			nl.xmlDoc = xml.parse(http.responseText);

			var render = function(){
				xml.transform(nl.xmlDoc, nl.xsltDoc, nl.container);
				renderWidgets();
				setAnchorTargets();
				setNewsletterStyle(nl.acctID);
			};

			if(nl.xsltDoc === null){
				var setXsltDoc = function(http){
					nl.xsltDoc = xml.parse(http.responseText);
					render();
				};

				req.getReqExe('action=getTemplate&type=' + nl.template, setXsltDoc, app.path + 'Newsletters.ashx');
			}
			else {
				render();
			}
		};

		req.getReqExe(qstring, setXmlDoc, app.path + 'Newsletters.ashx');
	}
};

var volCtl = function(ddlCtl, isPublished){
    var acctID = cookies.get('nlaID');

	var buildDropDown = function(http){
		var buildLabel = function(){
		var ddlLabel = window.document.createElement('label');
		ddlLabel.htmlFor = 'publishedList';
		if(isPublished){ddlLabel.innerHTML = 'Past Newsletters';}
		else{ddlLabel.innerHTML = 'Upcoming Newsletters';}
		return ddlLabel;};
		ddlCtl.appendChild(buildLabel());
		ddlCtl.innerHTML += http.responseText;
		var volumeList = ddlCtl.getElementsByTagName('select')[0];
		volumeList.id = 'publishedList';
		if(volumeList.options[0].value !== 'empty'){renderNewsleter(volumeList);}	// This function should reside in the individual pages
		volumeList.onchange = function(){
		var opt = volumeList.options[volumeList.selectedIndex];
		if(opt.value !== 'default'){
		if(app.name === 'Admin'){
		nl.acctID = parseInt(opt.parentNode.id.match(/\d+/), 10);
		window.document.getElementById('acctName').innerHTML = opt.parentNode.label;};
		nl.getNewsletter(opt.id, opt.value);}};
	};

	var qstring = '&accountID=' + acctID + '&usage=' + app.name;

	if(querySet.volumeid){
		if(isPublished){req.getReqExe('action=getPublishedList&vol=' + querySet.volumeid + qstring, buildDropDown, app.path + 'Newsletters.ashx');}
		else{req.getReqExe('action=getUnpublishedList&vol=' + querySet.volumeid + qstring, buildDropDown, app.path + 'Newsletters.ashx');}
	}
	else{
		if(isPublished){ req.getReqExe('action=getPublishedList' + qstring, buildDropDown, app.path + 'Newsletters.ashx'); }
		else{ req.getReqExe('action=getUnpublishedList' + qstring, buildDropDown, app.path + 'Newsletters.ashx'); }
	}
};