var server_name = 'http://www.publishersweekly.com';
var preview_server_name = 'https://secure.publishersweekly.com';


function setupAjax() {
	var http_request = null;
	
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		http_request = new XMLHttpRequest();
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		alert('Browser unable to make AJAX request');
	}

	return http_request;
}

function getXmlFirstChildData(tag,xmldoc) {

	var node = xmldoc.getElementsByTagName(tag).item(0);

	if(node && node.firstChild) { 
		return unescape(node.firstChild.data);
	}
	else {
		return "";
	}
}

function getQueryStringParams() {
	var params = new Array;
	
	var queryString = String (document.location).split ('?')[1];
	if (!queryString) return false;
	
	var pairs = queryString.split('&');
	
	for (var i = 0 ; i < pairs.length; i++) {
		var kv = pairs[i].split('=');
		params[kv[0]] = kv[1];
	}
	
	return params;
}

function initJavascript() {
	if(getCookie('credentials_sponsored')) {
		setCookie('credentials_sponsored','');
		setCookie('credentials','');
	}

	if(getCookie('credentials')) {
		_gaq.push(['_setCustomVar', 1, 'subscriber', 'yes', 1]);
	}
	else {
		_gaq.push(['_setCustomVar', 1, 'subscriber', 'no', 1]);
	}
	
	_gaq.push(['_trackPageview']);

	if(!getCookie('validated')){
		if(getCookie('credentials')){
			ajaxValidateLogin();
		}
	}

	logInChanged();
	
	var params = getQueryStringParams();
	if(params['validate']) {
		popin('/pw/popins/validate.html?validate=' + params['validate']);
	}

	if(params['utm_campaign']) {
		if(params['utm_campaign'] == 'testing') {
			getNewsletterSidebar(params['utm_campaign']);
		}
	}
	
}

function popinLinkSet(path,destination) {
	destination = preview_server_name + destination;
	promoPopInSetCloseDestination(destination);
	var div = dialogBoxShowWindow('popin','','',660,900,'',path , true, false);
}

function popin(path,destination) {
	if(destination) {	
		destination = server_name + destination;
		promoPopInSetCloseDestination(destination);
		path = path + '?destination=' + destination;
	}
	
	var div = dialogBoxShowWindow('popin','','',660,900,'',path , true, false);
}

function popinRemote(path) {
    $.ajax({
        type: "GET",
        url: path,
        dataType: "html",
        success: function(html) {
        	var obj = document.getElementById('popin_remote_content');
        	obj.innerHTML = html;
        	var div = dialogBox('popin_remote',500,'',true);
        },
        onFailure: function() {
            alert('popinRemote could not contact server');
        }
    });
}


function privacypolicy() {
	var w = window.open("/pw/privacy.html", 'PopUp', 'scrollbars=yes,toolbar=no,location=no,directories=no,menubar=no,width=400,height=400');
}

function popinReview(row) {
	popin('/pw/popins/review.html?review=' + row);
}

function popinBestsellerStats(row) {
	popinRemote('/pw/bestseller-stats.html?book=' + row);
}

function logOut() {
	setCookie('credentials','');
	document.location.href = server_name + '/pw/home/index.html';
}

function logInChanged() {
	conditionalCookiePane('logged_in_pane', 'logged_out_pane', 'credentials');	
	var name = document.getElementById('welcome_name');
	if(name) {
		name.innerHTML = 'Welcome Back' + ' | ';
	}
}

function registerInOrGo(destination) {
	if(destination) {
		destination = server_name + destination;
		promoPopInSetCloseDestination(destination);
		path += '?destination=' + destination
	}
	var path = '/pw/popins/register.html';
	if(getCookie('account')) {
		document.location.href = destination;
	}
	else {
		var div = dialogBoxShowWindow('popin','','',660,900,'',path, true, false);
	}
}

function logInOrGo(destination) {
	openLoginDialog();

/*
	if(destination) {
		destination = server_name + destination;
		promoPopInSetCloseDestination(destination);
		path += '?destination=' + destination
	}
	var path = '/pw/popins/login.html';
	if(getCookie('account')) {
		document.location.href = destination;
	}
	else {
		var div = dialogBoxShowWindow('popin','','',660,900,'',path, true, false);
	}
*/
}

function getElementsByName_iefix(tag, name) {
	 var elem = document.getElementsByTagName(tag);
	 var arr = new Array();
	 for(i = 0,iarr = 0; i < elem.length; i++) {
		  att = elem[i].getAttribute("name");
		  if(att == name) {
			   arr[iarr] = elem[i];
			   iarr++;
		  }
	 }

	 return arr;
}

function getPanesByName(name) {
	var objs = getElementsByName_iefix('span',name);
	return objs.concat(getElementsByName_iefix('div',name));
}


function conditionalCookiePane(true_pane, false_pane, cookie) {

	var c = getCookie(cookie);
	
	var pane = null;
	
	var t = getPanesByName(true_pane);
	var f = getPanesByName(false_pane);
	
	if(c) {
		for(var i = 0; i < t.length; i++) {
			pane = t[i];
			pane.style.display = '';
		}
		for(var i = 0; i < f.length; i++) {
			pane = f[i]
			pane.style.display = 'none';
		}
	}
	else {
		for(var i = 0; i < f.length; i++) {
			pane = f[i]
			pane.style.display = '';
		}
		for(var i = 0; i < t.length; i++) {
			pane = t[i];
			pane.style.display = 'none';
		}
	}
}

function setCookie(name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	
	var cookie;
	cookie = name + "=" + escape(value);
	cookie += ((expiredays==null) ? "" : ";expires=" + exdate.toGMTString());
	cookie += ';path=/';
	cookie += ';domain=.publishersweekly.com';
	
	document.cookie = cookie;
}

function deleteCookie(name) {
	setCookie(name, "", "");
}

function getCookie(name) {

	var value = '';

	if(document.cookie.length > 0) {
	
		var pairs = document.cookie.split(';');
		
		for(var i = 0; i < pairs.length; i++) {
			var kv = pairs[i].split('=');
			
			var n = kv[0].replace(/^\s+|\s+$/g, '');
			
			if(n == name) {
				if(kv.length > 1) {
					value =  unescape(kv[1].replace(/^\s+|\s+$/g, ''));
					
					return value;
				}
				else {
					return '';
				}
			}
		}
	}
	
	return '';
}

function showBlindPane(pane_id) {
	var pane = document.getElementById(pane_id);	
	pane.style.display = '';
	// Effect.BlindDown(pane_id,{duration:.3});
}

function hideBlindPane(pane_id) {
	var pane = document.getElementById(pane_id);
	pane.style.display = 'none';
	// Effect.BlindUp(pane_id,{duration:.3});
}

function toggleBlindPane(pane_id) {
	var pane = document.getElementById(pane_id);

	if(pane.style.display == 'none') {
		pane.style.display = '';
		// Effect.BlindDown(pane_id,{duration:.3});
	}
	else {
		pane.style.display = 'none';
		// Effect.BlindUp(pane_id,{duration:.3});
	}
}

/*
function launchArticleEmbeddedSlideShow(photo) {
	popin('/pw/popins/slideshow.html?photo=' + photo);
}
*/

// article slideshow

var gArticleSlides = null;
var gArticleSlidesCurrentIndex = -1;
var gArticleSlidesTimer = null;

function pauseArticleSlides() {
	if(gArticleSlidesTimer) {
		gArticleSlidesTimer = window.clearTimeout(gArticleSlidesTimer);
		document.getElementById('article_slideshow_pause_button').style.display = 'none';
		document.getElementById('article_slideshow_play_button').style.display = '';
	}
}

function doArticleSlides(increment, auto) {

	gArticleSlidesTimer = null
	
	if(gArticleSlidesCurrentIndex > -1) {
		$('#article_slideshow_slide_' + gArticleSlidesCurrentIndex).fadeOut('fast');
	}
	
	gArticleSlidesCurrentIndex += increment;
	
	if(gArticleSlidesCurrentIndex >= gArticleSlides.length) {
		gArticleSlidesCurrentIndex = 0;
	}

	if(gArticleSlidesCurrentIndex < 0) {
		gArticleSlidesCurrentIndex = gArticleSlides.length - 1;
	}

	$('#article_slideshow_slide_' + gArticleSlidesCurrentIndex).fadeIn('fast');
	
	if(auto) {
		gArticleSlidesTimer = window.setTimeout("doArticleSlides(1, true)",5000);
		document.getElementById('article_slideshow_pause_button').style.display = '';
		document.getElementById('article_slideshow_play_button').style.display = 'none';
	}
}

function launchArticleEmbeddedSlideShow(photo) {

	var screen = document.getElementById('article_slideshow_screen');
	if(screen) {
		screen.innerHTML = 'loading...';
	}

	var header = document.getElementById('article_slideshow_header');
	if(header) {
		header.innerHTML = 'loading...';
	}
	
    var div = dialogBox('slideshow_dialog',800,0,true);

	$.ajax({
		type: "POST",
		url: ajaxURL,
		data: jQuery.param({ proc: 'load_photo_slides', photo: photo }),
        success: function(xml) {
        	var status = getXmlFirstChildData('status',xml);
            if(status == 'ok') {
            	gArticleSlidesCurrentIndex = -1;
            	gArticleSlides = xml.getElementsByTagName('slide');
				var headline = getXmlFirstChildData('headline',xml);
				var info = getXmlFirstChildData('info',xml);
				header.innerHTML = "<div class=article_slideshow_headline>" + headline + "</div>" + "<div class=article_slideshow_info>" + info + "</div>";
            	var html = '';
            	for(var i = 0; i < gArticleSlides.length; i++) {
					var src 	= getXmlFirstChildData('src',gArticleSlides[i]);
					var width 	= getXmlFirstChildData('width',gArticleSlides[i]);
					var height 	= getXmlFirstChildData('height',gArticleSlides[i]);
					var title 	= getXmlFirstChildData('title',gArticleSlides[i]);
					var caption = getXmlFirstChildData('caption',gArticleSlides[i]);
					var credit 	= getXmlFirstChildData('credit',gArticleSlides[i]);
					
					var left = Math.round((798 - width) / 2) + 'px';
					
            		html += '<div class=article_slideshow_slide id="article_slideshow_slide_' + i + '" style="width:' + width + 'px;display:none;left:' + left + '">';
            		html += '<img class=article_slideshow_image src="' + src + '" width="' + width + '" height="' + height + '">';
            		if(credit) {
            			html += "<div class=article_slideshow_credit>credit: " + credit + "</div>";
            		}
            		if(title) {
            			html += "<div class=article_slideshow_title>" + title + "</div>";
            		}
            		if(caption) {
            			html += "<div class=article_slideshow_caption>" + caption + "</div>";
            		}
            		html += "</div>";
            	}
            	
            	screen.innerHTML = html;
				gArticleSlidesTimer = window.setTimeout("doArticleSlides(1, true)",1000);
            }
            else {
                alert('slideshow error ' + status);
                dialogBoxClose();
            }
        },
        onFailure: function() {
            alert('slideshow could not contact server');
            dialogBoxClose();
        }
    });
}


// polls

function embedPoll(poll) {
	var http_request 	= setupAjax()
	var url 			= '/pw/ajax.xml?proc=embed_poll&poll=' + poll;
	http_request.onreadystatechange = function() { embedPollDone(http_request); };
	http_request.open('GET', url, true);
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http_request.send(null);
}

function embedPollDone(http_request) {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			var xmldoc = http_request.responseXML;
			var status = getXmlFirstChildData('status', xmldoc);
			if(status == 'ok') {
				var html 		= '';
				var id 			= getXmlFirstChildData('id', xmldoc);
				var question 	= getXmlFirstChildData('question', xmldoc);
				var total_votes = getXmlFirstChildData('total_votes', xmldoc);
				var options 	= xmldoc.getElementsByTagName('record');
				
				html = question;
				
				for(var i = 0; i < options.length; i++) {
					var row			= getXmlFirstChildData('row',options[i]);
					var sequence	= getXmlFirstChildData('sequence',options[i]);
					var option		= getXmlFirstChildData('option',options[i]);
					var vote_count	= getXmlFirstChildData('vote_count',options[i]);
					
					html += '<br><input type=radio name=poll_' + id + ' value=' + row + ' onChange="pollVote(' + id + ',' + row + ');"> ' + option;
				}
				
				var div = document.getElementById('poll_' + id);
				if(div) {
					div.innerHTML = html;
				}
			}
			else {
				alert('could embed poll ' + status);
			}
		}
	}
}

function pollVote(poll,option) {
	var http_request 	= setupAjax()
	var url 			= '/pw/ajax.xml?proc=embed_poll_vote&poll=' + poll + '&option=' + option;
	http_request.onreadystatechange = function() { pollVoteDone(http_request); };
	http_request.open('GET', url, true);
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http_request.send(null);
}


function pollVoteDone(http_request) {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			var xmldoc = http_request.responseXML;
			var status = getXmlFirstChildData('status', xmldoc);
			if(status == 'ok') {
				var html 		= '';
				var id 			= getXmlFirstChildData('id', xmldoc);
				var question 	= getXmlFirstChildData('question', xmldoc);
				var total_votes = getXmlFirstChildData('total_votes', xmldoc);
				var options 	= xmldoc.getElementsByTagName('record');
				
				html = question;
				
				for(var i = 0; i < options.length; i++) {
					var row			= getXmlFirstChildData('row',options[i]);
					var sequence	= getXmlFirstChildData('sequence',options[i]);
					var option		= getXmlFirstChildData('option',options[i]);
					var vote_count	= getXmlFirstChildData('vote_count',options[i]);
					
					var percentage;
					
					if(total_votes) {
						percentage = sprintf('%02d', vote_count / total_votes * 100);
					}
					
					html += '<br>' + option + ' ' + percentage + '%';
				}
				
				var div = document.getElementById('poll_' + id);
				if(div) {
					div.innerHTML = html;
				}
			}
			else {
				alert('could embed poll ' + status);
			}
		}
	}
}

// article comments

var commentOnArticle = '';
var comments = new Array();

function loadComments(parent) {
	if(parent) {
		commentOnArticle    = parent;
		var http_request 	= setupAjax()
		var url 			= '/pw/ajax.xml?proc=load_comments&parent=' + commentOnArticle;
		http_request.onreadystatechange = function() { loadCommentsDone(http_request); };
		http_request.open('GET', url, true);
		http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http_request.send(null);
	}
}

function loadCommentsDone(http_request) {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			var xmldoc = http_request.responseXML;
			var status = getXmlFirstChildData('status', xmldoc);
			if(status == 'ok') {
				comments 	=  xmldoc.getElementsByTagName('comment');
				var count 	= comments.length;
				
				var html = "Be the first to comment.";
				
				if(comments.length) {
					html 	= '<div class=article_comments><strong>Reader Comments:</strong>';
					
					for(var i = 0; i < comments.length; i++) {
						var row			= getXmlFirstChildData('row',comments[i]);
						var author 		= getXmlFirstChildData('author',comments[i]);
						var body		= unescape(getXmlFirstChildData('body',comments[i]));
						var timestamp	= getXmlFirstChildData('timestamp',comments[i]);
						html += '<p>' + body + '<br><span class=article_comments_author>--by: ' + author + ' @ ' + timestamp + '</p>';
					}
					
					html += '</div>';
				}
				
				var div = document.getElementById('comments_pane');
				if(div) {
					div.innerHTML = html;
				}
				
				var counts = getPanesByName('comment_count');
				for(var i = 0; i < counts.length; i++) {
					counts[i].innerHTML = count;
				}
			}
			else {
				alert('could not load article comments ' + status);
			}
		}
	}
}

function ajaxSaveComment() {
	var comment_body = document.getElementById('comment_body');
	if(comment_body && comment_body.value) {
		var http_request 	= setupAjax()
		var url 			= '/pw/ajax.xml';
		var request_body 	= 'proc=save_comment';

		request_body += '&parent=' + commentOnArticle;
		request_body += '&comment=' + escape(comment_body.value);

		http_request.onreadystatechange = function() { ajaxSaveCommentDone(http_request); };
		http_request.open('POST', url, true);
		http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http_request.send(request_body);

		var div = document.getElementById('comment_status');
		if(div) {
			div.innerHTML = 'Saving comment...';
		}
		
		comment_body.value = '';
	}
	else {
		alert('Please type your comment in the box then click on submit');
	}
	return false;
}

function ajaxSaveCommentDone(http_request) {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			var xmldoc = http_request.responseXML;
			var status = getXmlFirstChildData('status', xmldoc);
			if(status == 'ok') {
				var comment 				= xmldoc.getElementsByTagName('comment');
				comments[comments.length] 	= comment;
				var count 					= comments.length;
				var html 					= '';
				
				for(var i = 0; i < comment.length; i++) {
					var row			= getXmlFirstChildData('row',comment[i]);
					var author 		= getXmlFirstChildData('author',comment[i]);
					var body		= unescape(getXmlFirstChildData('body',comment[i]));
					var timestamp	= getXmlFirstChildData('timestamp',comment[i]);
					html += '<p>' + body + '<br>--by: ' + author + ' @ ' + timestamp;
				}
				
				var div = document.getElementById('comments_pane');
				if(div) {
					div.innerHTML += html;
				}

				var counts = getPanesByName('comment_count');
				for(var i = 0; i < counts.length; i++) {
					counts[i].innerHTML = count;
				}

				var div = document.getElementById('comment_status');
				if(div) {
					div.innerHTML = '';
				}
			}
			else {
				alert('Could not save comment error:' + status);
			}
		}
		else {
			alert('Could not save comment error:' + http_request.status);
		}
	}
}

// popular

function markPopular(article) {
	var http_request 	= setupAjax()
	var url 			= '/pw/ajax.xml';
	//http_request.onreadystatechange = function() { loadCommentsDone(http_request); };
	http_request.open('POST', url, true);
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http_request.send('proc=mark_popular&article=' + article);
}
 // link sets
 
 function doTodaysDate() {
	var div = document.getElementById('currentdatedisplay');
	if(div) {
		var dayNames = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
		var monthNames = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
		var now = new Date();
		// var buffer = dayNames[now.getDay()] + ", " + monthNames[now.getMonth()] + " " + now.getDate();
		var buffer = monthNames[now.getMonth()] + " " + now.getDate();
		div.innerHTML = buffer;
	}
}

function clearDefault(obj) {
	obj.value='';
}


function openPopUp(url,width,height) {
	w = window.open(url, 'PopUp', 'scrollbars=yes,toolbar=no,location=no,directories=no,menubar=no,width=' + width + ',height=' + height);
}

var gTabbedPane 	= null;
var gTabbedButton 	= null;

function showTabbedPane(pane,class_off,class_on) {
	// do the tab
	var tab=document.getElementById(pane + '_tab');
	if(tab) {
		if(gTabbedButton != tab) {
			if(gTabbedButton) {
				gTabbedButton.className = class_off;
			}
			tab.className = class_on;
			gTabbedButton = tab;
		}
	}
	
	// do the pane
	var div=document.getElementById(pane + '_pane');
	if(div) {
		if(gTabbedPane != div) {
			if(gTabbedPane) {
				gTabbedPane.style.display = 'none';
			}
			div.style.display = '';
			gTabbedPane = div;
		}
	}
}

function issueNav(url) {
	if(url) {
		document.location.href = server_name + url;
	}
}

// Photo Of The Day

var gPhotoOfTheDayList = new Array();
var gPhotoOfTheDayCurrentIndex = -1;

function photoOfTheDayRegister(row) {
	gPhotoOfTheDayList[gPhotoOfTheDayList.length] = row;
}

function photoOfTheDayShow(i) {

	if(i < 0) { i = gPhotoOfTheDayList.length - 1; }
	if(i > gPhotoOfTheDayList.length - 1) { i = 0; }

	if(i >= 0 && i < gPhotoOfTheDayList.length) {
		if(gPhotoOfTheDayCurrentIndex != -1) {
			var olddiv = document.getElementById('photo_of_the_day-' + gPhotoOfTheDayList[gPhotoOfTheDayCurrentIndex]);
			if(olddiv) {
				olddiv.style.display = 'none';
			}
		}

		var id = 'photo_of_the_day-' + gPhotoOfTheDayList[i];
		var div = document.getElementById(id);
		if(div) {
			div.style.display = '';
		}
		gPhotoOfTheDayCurrentIndex = i;
	}
}

function photoOfTheDayShowLast() {
	photoOfTheDayShow(gPhotoOfTheDayList.length - 1);
}

function photoOfTheDayPrev() {
	photoOfTheDayShow(gPhotoOfTheDayCurrentIndex - 1);
}

function photoOfTheDayNext() {
	photoOfTheDayShow(gPhotoOfTheDayCurrentIndex + 1);
}

// Comic Of The Day

var gComicOfTheDayList = new Array();
var gComicOfTheDayCurrentIndex = -1;

function comicOfTheDayRegister(row) {
	gComicOfTheDayList[gComicOfTheDayList.length] = row;
}

function comicOfTheDayShow(i) {

	if(i < 0) { i = gComicOfTheDayList.length - 1; }
	if(i > gComicOfTheDayList.length - 1) { i = 0; }

	if(i >= 0 && i < gComicOfTheDayList.length) {
		if(gComicOfTheDayCurrentIndex != -1) {
			var olddiv = document.getElementById('comic_of_the_day-' + gComicOfTheDayList[gComicOfTheDayCurrentIndex]);
			if(olddiv) {
				olddiv.style.display = 'none';
			}
		}

		var id = 'comic_of_the_day-' + gComicOfTheDayList[i];
		var div = document.getElementById(id);
		if(div) {
			div.style.display = '';
		}
		gComicOfTheDayCurrentIndex = i;
	}
}

function comicOfTheDayShowLast() {
	comicOfTheDayShow(gComicOfTheDayList.length - 1);
}

function comicOfTheDayPrev() {
	comicOfTheDayShow(gComicOfTheDayCurrentIndex - 1);
}

function comicOfTheDayNext() {
	comicOfTheDayShow(gComicOfTheDayCurrentIndex + 1);
}


// Home slide show

var gHomeSlideShowList = new Array();
var gHomeSlideShowCurrentIndex = -1;
var gHomeSlideShowPlaying = null;

function homeSlideShowRegister(row) {
	gHomeSlideShowList[gHomeSlideShowList.length] = row;
}

function homeSlideShowShow(i) {

	if(i < 0) { i = gHomeSlideShowList.length - 1; }
	if(i > gHomeSlideShowList.length - 1) { i = 0; }

	if(i >= 0 && i < gHomeSlideShowList.length) {
		if(gHomeSlideShowCurrentIndex != -1) {
			var olddiv = document.getElementById('home_slideshow-' + gHomeSlideShowList[gHomeSlideShowCurrentIndex]);
			if(olddiv) {
				olddiv.style.display = 'none';
				// Effect.Fade(olddiv.id, { duration:1, from:1.0, to:0.0 });
			}

			var old_indicators = getPanesByName('home_slide_page-' + gHomeSlideShowList[gHomeSlideShowCurrentIndex]);
			for(var item = 0; item < old_indicators.length; item++) {
				old_indicators[item].className = 'slide_indicator_off';
			}
		}

		var id = 'home_slideshow-' + gHomeSlideShowList[i];
		var div = document.getElementById(id);
		if(div) {
			div.style.display = '';
        	// Effect.Appear(div.id, { duration:1, from:0.0, to:1.0 });
		}

		var indicators = getPanesByName('home_slide_page-' + gHomeSlideShowList[i]);
		if(indicators) {
			for(var item = 0; item < indicators.length; item++) {
				indicators[item].className = 'slide_indicator_on';
			}
		}

		gHomeSlideShowCurrentIndex = i;
	}
}

function homeSlideShowPause() {
	if(gHomeSlideShowPlaying) {
		window.clearTimeout(gHomeSlideShowPlaying);
		gHomeSlideShowPlaying = null;
	}
}

function homeSlideShowPlay() {
	homeSlideShowShow(gHomeSlideShowCurrentIndex + 1);
	gHomeSlideShowPlaying = window.setTimeout("homeSlideShowPlay()", 5000);
}

function homeSlideShowStart() {
	homeSlideShowShow(0);
	if(gHomeSlideShowList.length > 1) {
		gHomeSlideShowPlaying = window.setTimeout("homeSlideShowPlay()", 5000);
	}
}

function homeSlideShowUserSelect(i) {
	homeSlideShowPause();
	homeSlideShowShow(i - 1);
}

function homeSlideShowUserPrev() {
	homeSlideShowShow(gHomeSlideShowCurrentIndex - 1);
}

function homeSlideShowUserNext() {
	homeSlideShowShow(gHomeSlideShowCurrentIndex + 1);
}

// newsletter sidebar

function getNewsletterSidebar(campaign_id) {
	var http_request 	= setupAjax()
	var url 			= '/pw/ajax.xml?proc=newsletter_sidebar&campaign_id=' + campaign_id;
	http_request.onreadystatechange = function() { getNewsletterSidebarDone(http_request); };
	http_request.open('GET', url, true);
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http_request.send(null);
}

function getNewsletterSidebarDone(http_request) {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			var xmldoc = http_request.responseXML;
			var status = getXmlFirstChildData('status', xmldoc);
			if(status == 'ok') {
				var thisUrl = String (document.location).split('?')[0];	
				
				var campaign_id = getXmlFirstChildData('campaign_id',xmldoc);
				var items = xmldoc.getElementsByTagName('item');

				var html = '';
				
				for(var i = 0; i < items.length; i++) {
					var url = getXmlFirstChildData('url',items[i]);
					var link_text = getXmlFirstChildData('link_text',items[i]);
					html += "<li>";
					if(thisUrl != url) {
						html += "<a href=\"" + url + "?utm_campaign=" + campaign_id + "\">";
					}
					html += link_text;
					if(thisUrl != url) {
						html += "</a>";
					}
				}
				
				var div = document.getElementById('also_in_newsletter');
				if(div) {
					div.innerHTML = 'Also in newsletter <ol>' + html + '</ol>';
				}
			}
			else {
				alert('could load newsletter links ' + status);
			}
		}
	}
}

// clicktrack

$(document).ready(function() { 
	$('.rsslink').click(function() {
		clicktrack('rss','click', $(this).attr("href"));
	});
});

function clicktrack(category,action,description) {
	_gaq.push(['_trackEvent', category, event, description]);
}

