WSPA = {
};

WSPA.CONSTS = {
	ajaxUrl:	'/wp-admin/admin-ajax.php?' + new Date().getTime(),
	themeUrl:	'http://notinmycuppa.loc/wp-content/themes/notinmycuppaTwo'
};

$(function() {
});

if (jQuery.cookie('intercept')!='1') {
	if (window.location.pathname=='/') {
		if (jQuery.cookie('intercept-shown-this-visit')!='1') {
			jQuery.cookie('intercept-shown-this-visit', 1);
			window.location = window.location.origin + '/intercept';
		}
	}
}

$(document).ready(function() {

if( jQuery.cookie('home_sign_was') == 'email' ) { jQuery('#takepart-online-signup-tab').addClass("hidden"); jQuery('#facebookLink').addClass("active"); jQuery('#signUpLink').removeClass("active"); jQuery('#takepart-facebook-tab').removeClass("hidden"); }

	WSPA.LIVEFEED.init();
	WSPA.NEWSHOME.init();
	
	WSPA.FORMS.init();
	WSPA.PAGE.init();
	
	WSPA.TABS.init();
	
	WSPA.TAKEPART.init();
	
	LIBRARY.addTargetAttribute('targetBlank', '_blank', '');
	
});

WSPA.NEWSHOME = {

	totalRecordset: 	0,
	countRecordset: 	0,
	recsPerPage: 		2,
	loadTimestamp: 		'',
	currentFirstRec: 	0,
	recIdsArr: 			[],
	
	prevLinkId: 		'prevHomeNewsLink',
	nextLinkId: 		'nextHomeNewsLink',
	baseRecId: 			'newshome-',
	
	nextPageClicked:	false,
	prevPageClicked:	false,
		
	init: function() {
		$("#" + WSPA.NEWSHOME.prevLinkId).bind('click', function() {
			WSPA.NEWSHOME.prevPage();
			return false;
		});
		$("#" + WSPA.NEWSHOME.nextLinkId).bind('click', function() {
			WSPA.NEWSHOME.nextPage();
			return false;
		});
	},

	nextPage: function() {
		
		if (WSPA.NEWSHOME.nextPageClicked==true) {
			return;
		}
		
		WSPA.NEWSHOME.nextPageClicked = true;

		if (WSPA.NEWSHOME.countRecordset < WSPA.NEWSHOME.currentFirstRec + (WSPA.NEWSHOME.recsPerPage * 2)) {
			WSPA.NEWSHOME.getRecs();
		} else {
			WSPA.NEWSHOME.currentFirstRec = WSPA.NEWSHOME.currentFirstRec + WSPA.NEWSHOME.recsPerPage;
			for (c=0; c<WSPA.NEWSHOME.recsPerPage; c++) {
				id = WSPA.NEWSHOME.currentFirstRec + c - WSPA.NEWSHOME.recsPerPage;
				$("#" + WSPA.NEWSHOME.baseRecId + (WSPA.NEWSHOME.recIdsArr[id])).addClass("hidden"); 
				$("#" + WSPA.NEWSHOME.baseRecId + (WSPA.NEWSHOME.recIdsArr[id])).removeClass("layout");
				id = WSPA.NEWSHOME.currentFirstRec + c;
				$("#" + WSPA.NEWSHOME.baseRecId + (WSPA.NEWSHOME.recIdsArr[id])).removeClass("hidden");
				$("#" + WSPA.NEWSHOME.baseRecId + (WSPA.NEWSHOME.recIdsArr[id])).addClass("layout");
			}
			WSPA.NEWSHOME.updatePagingInfo();
			WSPA.NEWSHOME.nextPageClicked = false;
		}
		
		
		
	},

	prevPage: function() {
		
		if (WSPA.NEWSHOME.prevPageClicked==true) {
			return;
		}
		
		WSPA.NEWSHOME.prevPageClicked = true;
		
		$("#" + WSPA.NEWSHOME.nextLinkId).removeClass("notvisible");
		
		if (WSPA.NEWSHOME.currentFirstRec==0) {
			return;
		}
		WSPA.NEWSHOME.currentFirstRec = WSPA.NEWSHOME.currentFirstRec - WSPA.NEWSHOME.recsPerPage;
		
		for (c=0; c<WSPA.NEWSHOME.recsPerPage; c++) {
			id = WSPA.NEWSHOME.currentFirstRec + c + WSPA.NEWSHOME.recsPerPage;
			$("#" + WSPA.NEWSHOME.baseRecId + (WSPA.NEWSHOME.recIdsArr[id])).addClass("hidden"); 
			$("#" + WSPA.NEWSHOME.baseRecId + (WSPA.NEWSHOME.recIdsArr[id])).removeClass("layout");
			id = WSPA.NEWSHOME.currentFirstRec + c;
			$("#" + WSPA.NEWSHOME.baseRecId + (WSPA.NEWSHOME.recIdsArr[id])).removeClass("hidden");
			$("#" + WSPA.NEWSHOME.baseRecId + (WSPA.NEWSHOME.recIdsArr[id])).addClass("layout");
		}
		WSPA.NEWSHOME.updatePagingInfo();
		
		WSPA.NEWSHOME.prevPageClicked = false;
		
	},
	
	getRecs: function() {
		start = WSPA.NEWSHOME.currentFirstRec + WSPA.NEWSHOME.recsPerPage;
		recs = WSPA.NEWSHOME.recsPerPage;
		timestamp = WSPA.NEWSHOME.loadTimestamp;
		$.post(WSPA.CONSTS.ajaxUrl,{"action":"news-home", "start":start, "recs":recs, "timestamp":timestamp}, function(response){
			if (response.result=='success') {
				n = response.items.length;
				for (i=0; i<n; i++) {
					WSPA.NEWSHOME.recIdsArr.push(response.items[i].id);
					
					html = '<div class="newsItem layout hidden" id="' + WSPA.NEWSHOME.baseRecId + response.items[i].id + '">';
						html += response.items[i].image;
						html += '<h3><a href="' + response.items[i].link + '">' + response.items[i].title + '</a></h3>';
						html += '<span class="newsDate">' + response.items[i].date + '</span>';
						html += '<a href="' + response.items[i].link + '" class="commentsLink">Comments ' + response.items[i].comments + '</a>';
						html += response.items[i].content;
					html += '</div>';
					$("#latest-news-box").append(html);
				}
				
				WSPA.NEWSHOME.countRecordset = WSPA.NEWSHOME.countRecordset + n;
				WSPA.NEWSHOME.currentFirstRec = WSPA.NEWSHOME.currentFirstRec + WSPA.NEWSHOME.recsPerPage;
				for (c=0; c<WSPA.NEWSHOME.recsPerPage; c++) { 
					id = WSPA.NEWSHOME.currentFirstRec + c - WSPA.NEWSHOME.recsPerPage;
					$("#" + WSPA.NEWSHOME.baseRecId + (WSPA.NEWSHOME.recIdsArr[id])).addClass("hidden");
					$("#" + WSPA.NEWSHOME.baseRecId + (WSPA.NEWSHOME.recIdsArr[id])).removeClass("layout");
					id = WSPA.NEWSHOME.currentFirstRec + c;
					$("#" + WSPA.NEWSHOME.baseRecId + (WSPA.NEWSHOME.recIdsArr[id])).removeClass("hidden");
					$("#" + WSPA.NEWSHOME.baseRecId + (WSPA.NEWSHOME.recIdsArr[id])).addClass("layout");
				}
				WSPA.NEWSHOME.updatePagingInfo();
				
				WSPA.PAGE.setTimeAgo();
				WSPA.NEWSHOME.nextPageClicked = false;
				
			} else {
				// no data returned
				WSPA.NEWSHOME.nextPageClicked = false;
			}
		}, 'json');
	},
	
	updatePagingInfo: function() {
		currentPage = (WSPA.NEWSHOME.currentFirstRec + WSPA.NEWSHOME.recsPerPage) / WSPA.NEWSHOME.recsPerPage;
		nPages = Math.ceil(WSPA.NEWSHOME.totalRecordset/2);
		$("#newsPaging").html('' + currentPage + ' of ' + nPages);
		WSPA.NEWSHOME.hideShowPrevNextLinks();
	},
	
	hideShowPrevNextLinks: function() {
		if (WSPA.NEWSHOME.currentFirstRec<=0) {
			WSPA.NEWSHOME.currentFirstRec = 0;
			$("#" + WSPA.NEWSHOME.prevLinkId).addClass("notvisible");
		} else {
			$("#" + WSPA.NEWSHOME.prevLinkId).removeClass("notvisible");
		}
		if (WSPA.NEWSHOME.totalRecordset>0) {
			if (WSPA.NEWSHOME.totalRecordset <= (WSPA.NEWSHOME.currentFirstRec + WSPA.NEWSHOME.recsPerPage)) {
				$("#" + WSPA.NEWSHOME.nextLinkId).addClass("notvisible");
			}
		}		
	}
	
};

WSPA.LIVEFEED = {

	feed:				'',
	totalRecordset: 	0,
	loadTimestamp: 		'', 
	newTimestamp: 		'', // timestamp of the first record loaded
	itemsCount:			0,
	recIdsArr: 			[],
	currentFirstRec:	0,
	
	recsPerPage: 		10,
	
	prevLinkId: 		'prev-livefeed-link',
	nextLinkId: 		'next-livefeed-link',
	currentView:		'',
	
	loadNew:			false,
	
	nextPageClicked:	false,
	prevPageClicked:	false,
		
	init: function() {
		
		WSPA.LIVEFEED.loadNew = true;
		WSPA.LIVEFEED.resetDefaultValues();
		WSPA.LIVEFEED.getTotal();
		WSPA.LIVEFEED.getRecs(0, true);
		
		$('#'+WSPA.LIVEFEED.prevLinkId).bind('click', function() {
			WSPA.LIVEFEED.prevPage();
			return false;
		});
		$('#'+WSPA.LIVEFEED.nextLinkId).bind('click', function() {
			WSPA.LIVEFEED.nextPage();
			return false;
		});
		$('#show-livefeed-signup-items').bind('click', function() {
			WSPA.LIVEFEED.setView('signup');
			return false;
		});
		$('#show-livefeed-facebook-items').bind('click', function() {
			WSPA.LIVEFEED.setView('facebook');
			return false;
		});
		$('#show-livefeed-twitter-items').bind('click', function() {
			WSPA.LIVEFEED.setView('twitter');
			return false;
		});
		$('#show-livefeed-youtube-items').bind('click', function() {
			WSPA.LIVEFEED.setView('youtube');
			return false;
		});
		$('#show-livefeed-flickr-items').bind('click', function() {
			WSPA.LIVEFEED.setView('flickr');
			return false;
		});
		$('#show-livefeed-celebrity-items').bind('click', function() {
			WSPA.LIVEFEED.setView('celebrity-support');
			return false;
		});
		$('#show-livefeed-mpsupport-items').bind('click', function() {
			WSPA.LIVEFEED.setView('mp-support');
			return false;
		});
	},

	getTotal: function() {
		$.post(WSPA.CONSTS.ajaxUrl,{"action":"total"}, function(response){
			if (response.result=='success') {
				$(".count").html(response.total);
			} else {
				// no data returnedse;
			}
		}, 'json');
	},
	
	setView: function(view) {
		if (view!=WSPA.LIVEFEED.currentView) {
			// switch the view
			WSPA.LIVEFEED.feed = view;
			WSPA.LIVEFEED.loadNew = true;
			
			$("#"+WSPA.LIVEFEED.prevLinkId).addClass("notvisible");
			$("#"+WSPA.LIVEFEED.nextLinkId).removeClass("notvisible");
			
			WSPA.LIVEFEED.resetDefaultValues();
			$("#live-feed-box").html("");
			WSPA.LIVEFEED.getRecs(0, true);
		}
		WSPA.LIVEFEED.currentView = view;
	},
	
	resetDefaultValues: function() {
		WSPA.LIVEFEED.totalRecordset = '0';
		WSPA.LIVEFEED.loadTimestamp = '';
		WSPA.LIVEFEED.newTimestamp = '';
		WSPA.LIVEFEED.itemsCount = 0;
		WSPA.LIVEFEED.recIdsArr = [];
		WSPA.LIVEFEED.currentFirstRec = 0;	
	},

	nextPage: function() {
		
		if (WSPA.LIVEFEED.nextPageClicked==true) {
			return;
		}
		
		$('#' + WSPA.LIVEFEED.prevLinkId).removeClass("notvisible");
		
		WSPA.LIVEFEED.nextPageClicked = true;
			
		if (WSPA.LIVEFEED.totalRecordset-WSPA.LIVEFEED.recsPerPage > WSPA.LIVEFEED.currentFirstRec) {
			WSPA.LIVEFEED.getRecs();
		} else {
			WSPA.LIVEFEED.currentFirstRec = WSPA.LIVEFEED.currentFirstRec + WSPA.LIVEFEED.recsPerPage;
			for (c=0; c<WSPA.LIVEFEED.recsPerPage; c++) {
				$("#item-" + (WSPA.LIVEFEED.recIdsArr[WSPA.LIVEFEED.currentFirstRec+c-WSPA.LIVEFEED.recsPerPage])).addClass("hidden"); 
				$("#item-" + (WSPA.LIVEFEED.recIdsArr[WSPA.LIVEFEED.currentFirstRec+c])).removeClass("hidden");
			}
			
			if (WSPA.LIVEFEED.totalRecordset-WSPA.LIVEFEED.recsPerPage <= WSPA.LIVEFEED.currentFirstRec) {
				$('#' + WSPA.LIVEFEED.nextLinkId).addClass("notvisible");
			}
			
			WSPA.LIVEFEED.updatePagingInfo();
			WSPA.LIVEFEED.nextPageClicked = false;
		}
		
	},

	prevPage: function() {
		
		if (WSPA.LIVEFEED.prevPageClicked==true) {
			return;
		}
		
		WSPA.LIVEFEED.prevPageClicked = true;
		
		$('#' + WSPA.LIVEFEED.nextLinkId).removeClass("notvisible");
		
		if (WSPA.LIVEFEED.currentFirstRec==0) {
			return;
		}
		WSPA.LIVEFEED.currentFirstRec = WSPA.LIVEFEED.currentFirstRec - WSPA.LIVEFEED.recsPerPage;

		if (WSPA.LIVEFEED.currentFirstRec<=0) {
			WSPA.LIVEFEED.currentFirstRec = 0;
			$('#' + WSPA.LIVEFEED.prevLinkId).addClass("notvisible");
		}
		
		for (c=0; c<WSPA.LIVEFEED.recsPerPage; c++) {
			$("#item-" + (WSPA.LIVEFEED.recIdsArr[WSPA.LIVEFEED.currentFirstRec+c+WSPA.LIVEFEED.recsPerPage])).addClass("hidden"); 
			$("#item-" + (WSPA.LIVEFEED.recIdsArr[WSPA.LIVEFEED.currentFirstRec+c])).removeClass("hidden");
		}
		WSPA.LIVEFEED.updatePagingInfo();
		
		WSPA.LIVEFEED.prevPageClicked = false;

	},

	defaultHtml: function(item) {
		'';
	},
	
	twitterHtml: function(item) {
		html = '<div class="thumbnail">';
			html += '<a href="http://www.twitter.com/' + item.additional.twitter_username + '" class="targetBlank">';
			html += '<img src="' + item.additional.twitter_profile_image + '" alt="' + item.additional.twitter_username + ' picture" height="48" width="48" />';
			html += '</a>';
		html += '</div>';
		html += '<p class="name">' + item.additional.twitter_username + '</p>';
		html += '<p class="timeago"><span class="timeago">' + item.date + '</span></p>';
		html += '<div class="content">' + item.content + '</div>';
		return html;
	},

	facebookHtml: function(item) {
		html = '<div class="thumbnail">';
			html += '<a href="http://www.facebook.com/profile.php?id=' + item.additional.facebook_userid + '" class="targetBlank">';
			html += '<img src="http://graph.facebook.com/' + item.additional.facebook_userid + '/picture" alt="' + item.additional.facebook_username + ' picture" />';
			html += '</a>';
		html += '</div>';	
		html += '<p class="name">' + item.additional.facebook_username + '</p>';
		html += '<p class="timeago"><span class="timeago">' + item.date + '</span></p>';
		html += '<div class="content">' + item.content + '</div>';
		return html;
	},

	lettersHtml: function(item) {
		html = '<p class="name">' + item.additional.letter_author + '</p>';
		html += '<p class="timeago"><span class="timeago">' + item.date + '</span></p>';
		html += '<div class="content">' + item.content + '</div>';
		return html;
	},

	textsHtml: function(item) {
		html = '<p class="name">' + item.additional.sms_author + '</p>';
		html += '<p class="timeago"><span class="timeago">' + item.date + '</span></p>';
		html += '<div class="content">NOTINMYCUPPA</div>';
		return html;
	},

	youtubeHtml: function(item) {
		html = '<div class="thumbnail">';
			html += '<img src="http://img.youtube.com/vi/' + item.additional.youtube_videoid + '/default.jpg" alt="video" width="50" />';
		html += '</div>';
		html += '<p class="name">' + item.additional.youtube_username + '</p>';
		html += '<p class="timeago"><span class="timeago">' + item.date + '</span></p>';
		html += '<div class="content">' + item.excerpt + ' <a href="/youtube.php?id=' + item.additional.youtube_videoid + '" rel="videoPopup">view video</a></div>';
		return html;
	},

	flickrHtml: function(item) {
		html = '<div class="thumbnail">';
			html += '<img src="' + item.additional.flickr_t_url + '" width="50" />';
		html += '</div>';
		html += '<p class="name">' + item.additional.flickr_author_name + '</p>';
		html += '<p class="timeago"><span class="timeago">' + item.date + '</span></p>';
		html += '<div class="content">' + item.title + ' <a href="' + item.additional.flickr_l_url + '" rel="videoPopup">view image</a></div>';
		return html;
	},
	
	celebrityHtml: function(item) {
		html = '<div class="thumbnail">';
			html += '<img src="' + item.additional.thumb + '" height="' + item.additional.thumb_height + '" width="' + item.additional.thumb_width + '" alt="' + item.title + '" />';
		html += '</div>';
		html += '<p class="name">' + item.title + '</p>';
		html += '<p class="timeago"><span class="timeago">' + item.date + '</span></p>';
		html += '<div class="content">' + item.content + ' <a href="/category/celebritysupport">view celebrities</a></div>';
		return html;
	},
	
	mpHtml: function(item) {
		
		if (item.additional.youtube_videoid != null) {
			html = '<div class="thumbnail">';
				html += '<img src="http://img.youtube.com/vi/' + item.additional.youtube_videoid + '/default.jpg" alt="video" width="50" />';
			html += '</div>';
		} else if (item.additional.flickr_t_url != null) {
			html = '<div class="thumbnail">';
				html += '<img src="' + item.additional.flickr_t_url + '" width="50" />';
			html += '</div>';
		}
		
		html += '<p class="name">' + item.title + '</p>';
		html += '<p class="timeago"><span class="timeago">' + item.date + '</span></p>';
		if (item.additional.youtube_videoid != null) {
			html += '<div class="content">' + item.content + ' <a href="/category/mp-support">view MP supporters</a></div>';
		}
		else if (item.additional.flickr_t_url != null) {
			html += '<div class="content"><a href="/category/mp-support">view MP supporters</a></div>';
		}
		return html;
	},
	
	
	getRecs: function(start, setLoad) {
		if (start==undefined) {
			start = WSPA.LIVEFEED.currentFirstRec + WSPA.LIVEFEED.recsPerPage;
		}
		timestamp = WSPA.LIVEFEED.loadTimestamp;
		feed = WSPA.LIVEFEED.feed;
		total = WSPA.LIVEFEED.totalRecordset;
		last = WSPA.LIVEFEED.recIdsArr[(WSPA.LIVEFEED.itemsCount-1)];
		if (last===undefined) {
			last = -1;
		}
		$.post(WSPA.CONSTS.ajaxUrl,{"action":"items", "method":"next-recordset", "feed":feed, "total":total, "start":start, "last":last, "timestamp":timestamp}, function(response){
			if (response.result=='success') {
				$('#loading-live-feed').addClass('hidden');
				WSPA.LIVEFEED.totalRecordset = response.total;
				n = response.items.length;
				for (i=0; i<n; i++) {
					if (i==0 && WSPA.LIVEFEED.recIdsArr.length<1) {
						WSPA.LIVEFEED.recIdsArr = [response.items[i].id];
						
					} else {
						WSPA.LIVEFEED.recIdsArr.push(response.items[i].id);
					}
					if (i==0 && setLoad==true) {
						WSPA.LIVEFEED.loadTimestamp = response.items[i].date;
					}
					
					typeFix = response.items[i].type.toLowerCase().replace(' ','-');
					html = '<div class="liveFeed ' + typeFix + ' hidden" id="item-' + response.items[i].id + '">';
						if (typeFix=='twitter') {
							html += WSPA.LIVEFEED.twitterHtml(response.items[i]);
						} else if (typeFix=='facebook') {
							html += WSPA.LIVEFEED.facebookHtml(response.items[i]);
						} else if (typeFix=='letters') {
							html += WSPA.LIVEFEED.lettersHtml(response.items[i]);
						} else if (typeFix=='sms') {
							html += WSPA.LIVEFEED.textsHtml(response.items[i]);
						} else if (typeFix=='youtube') {
							html += WSPA.LIVEFEED.youtubeHtml(response.items[i]);
						} else if (typeFix=='flickr') {
							html += WSPA.LIVEFEED.flickrHtml(response.items[i]);
						} else if (typeFix=='celebrity-support') {
							html += WSPA.LIVEFEED.celebrityHtml(response.items[i]);
						} else if (typeFix=='mp-support') {
							html += WSPA.LIVEFEED.mpHtml(response.items[i]);
						} else {
							html += WSPA.LIVEFEED.defaultHtml(response.items[i]);
						}
					html += '</div>';
					$("#live-feed-box").append(html);
				}
				WSPA.LIVEFEED.itemsCount = WSPA.LIVEFEED.itemsCount + n; // increment the number of items
				if (WSPA.LIVEFEED.loadNew==true) {
					WSPA.LIVEFEED.loadNew = false;
				} else {
					WSPA.LIVEFEED.currentFirstRec = WSPA.LIVEFEED.currentFirstRec + WSPA.LIVEFEED.recsPerPage;
				}
				for (c=0; c<WSPA.LIVEFEED.recsPerPage; c++) {
					$("#item-" + (WSPA.LIVEFEED.recIdsArr[WSPA.LIVEFEED.currentFirstRec+c-WSPA.LIVEFEED.recsPerPage])).addClass("hidden"); 
					$("#item-" + (WSPA.LIVEFEED.recIdsArr[WSPA.LIVEFEED.currentFirstRec+c])).removeClass("hidden");
				}
				
				if (WSPA.LIVEFEED.totalRecordset-WSPA.LIVEFEED.recsPerPage <= WSPA.LIVEFEED.currentFirstRec) {
					$('#' + WSPA.LIVEFEED.nextLinkId).addClass("notvisible");
				}
				
				WSPA.LIVEFEED.updateValues();
				WSPA.LIVEFEED.updatePagingInfo();
				LIBRARY.addTargetAttribute('targetBlank', '_blank', '');
				WSPA.LIVEFEED.nextPageClicked = false;
			} else {
				// no data returned
				WSPA.LIVEFEED.nextPageClicked = false;
			}
		}, 'json');
	},

	getNewRecs: function() {
		timestamp = WSPA.LIVEFEED.newTimestamp;
		if (timestamp==0) {
			timestamp = WSPA.LIVEFEED.loadTimestamp;
		}
		feed = WSPA.LIVEFEED.feed;
		$.post(WSPA.CONSTS.ajaxUrl,{"action":"items", "method":"new-records", "feed":feed, "timestamp":timestamp}, function(response){
			if (response.result=='success') {
				n = response.items.length;
				if (n==0) {
					return;
				}
				html = '';
				tempArr = [];
				
				for (i=0;i<n;i++) {
					
					if (i==0) {
						WSPA.LIVEFEED.newTimestamp = response.items[i].date;
					}
					tempArr.push(response.items[i].id);
					typeLowercase = response.items[i].type.toLowerCase();
					html += '<div class="testBox ' + typeLowercase + ' hidden" id="item-' + response.items[i].id + '">';
						if (typeLowercase=='twitter') {
							html += WSPA.LIVEFEED.twitterHtml(response.items[i]);
						} else if (typeLowercase=='facebook') {
							html += WSPA.LIVEFEED.facebookHtml(response.items[i]);
						} else if (typeLowercase=='letters') {
							html += WSPA.LIVEFEED.lettersHtml(response.items[i]);
						} else if (typeLowercase=='texts') {
							html += WSPA.LIVEFEED.textsHtml(response.items[i]);
						} else if (typeLowercase=='youtube') {
							html += WSPA.LIVEFEED.youtubeHtml(response.items[i]);
						} else if (typeLowercase=='flickr') {
							html += WSPA.LIVEFEED.flickrHtml(response.items[i]);
						} else {
							html += WSPA.LIVEFEED.defaultHtml(response.items[i]);
						}
					html += '</div>';
					
				}
				tempArr = tempArr.reverse();
				for (i=0; i<tempArr.length; i++) {
					WSPA.LIVEFEED.recIdsArr.unshift(tempArr[i]);
				}			
				
				$("#live-feed-box").prepend(html);
				WSPA.LIVEFEED.itemsCount = WSPA.LIVEFEED.itemsCount + n;
				
				if (WSPA.LIVEFEED.currentFirstRec==0) {
					for (c=0;c<n;c++) {
						$("#item-" + (WSPA.LIVEFEED.recIdsArr[WSPA.LIVEFEED.currentFirstRec+c+WSPA.LIVEFEED.recsPerPage])).addClass("hidden"); 
						$("#item-" + (WSPA.LIVEFEED.recIdsArr[WSPA.LIVEFEED.currentFirstRec+c])).removeClass("hidden");
					}
				} else {
					WSPA.LIVEFEED.currentFirstRec = WSPA.LIVEFEED.currentFirstRec + n;
				} 
				
				WSPA.LIVEFEED.updateValues();
				LIBRARY.addTargetAttribute('targetBlank', '_blank', '');
			}
			else {
				// no data returned
			}
		});
	},

	updatePagingInfo: function() {
		currentPage = (WSPA.LIVEFEED.currentFirstRec + WSPA.LIVEFEED.recsPerPage) / WSPA.LIVEFEED.recsPerPage;
		nPages = Math.ceil(WSPA.LIVEFEED.totalRecordset/WSPA.LIVEFEED.recsPerPage);
		start = WSPA.LIVEFEED.currentFirstRec + 1;
		end = WSPA.LIVEFEED.currentFirstRec + WSPA.LIVEFEED.recsPerPage;
		if (end>WSPA.LIVEFEED.totalRecordset) {
			end = WSPA.LIVEFEED.totalRecordset;
		}
		$("#livefeed-paging").html('Showing feed entries ' + start + ' to ' + end + '<br />on page ' + currentPage + ' of ' + nPages);
		WSPA.PAGE.scrollToTop();

	},

	updateValues: function() {
		
		WSPA.PAGE.setTimeAgo();
		WSPA.PAGE.setColorbox();
		
		html = '<ul>';
		for (i=0; i<WSPA.LIVEFEED.recIdsArr.length; i++) {
			html += '<li>' + i + ' - ' + WSPA.LIVEFEED.recIdsArr[i] + '</li>'; 
		}
		html += '</ul>';
		html += '<p>' + WSPA.LIVEFEED.currentFirstRec + ' ' + WSPA.LIVEFEED.loadTimestamp + '</p>';
		// $("#devValues").html(html);
	}
		
};

WSPA.FORMS = {
	
	init: function() {
		// prepopulate fields with data
		LIBRARY.prepopulateFieldsText('fieldPrepopulate_');
		// set required fields
		LIBRARY.bindFieldsRequired('fieldPrepopulate_', 'fieldRequired', '#f5762b', '#666666');
		
		$('#takepart-online-sign-up-submit').bind('click', function() {
			WSPA.FORMS.takepartOnlineSignUp();
			return false;
		});
		
		$('#postcodeFindBtn').bind('click', function(){
			if ($('#fieldPostcode').val() != 'Post Code') {
				WSPA.POSTCODE.postcodeLookup();
			}
			return false;
		});
		
		var notinmy = $('#notinmy').val();
		$('.notInMy select').change(function() {
			$('.notInMy select').val( $(this).val() );
			notinmy = $(this).val(); 
		});
		
		$('.donate-twitter').click(function() {
			WSPA.GA.pageView('/takepart/donatetweet/clicked');
			window.location = this.href + '&notinmy=' + notinmy;
			return false;
		});
		
		$('.donate-facebook').bind('click', function(e) { 
			e.preventDefault();
			WSPA.GA.pageView('/takepart/donatefacebookpost/clicked');
			var url = this.href;
			FB.login(function(response) {
				if(response.authResponse) {
					window.location = url + '?notinmy=' + notinmy;
				} else {
				// user is not logged in
				}
			}, {scope:'publish_stream,read_stream'});
			return false;
		});
		
		$('#feed-post-form').submit(function() {
			$('#feed-post-submit').attr('disabled', 'disabled');
			var data = $(this).serializeArray();
			$.post('/wp-admin/admin-ajax.php', data, function(response) {
				if(response.error) {
					
					if ($('.template-donate.facebook').length) {
						WSPA.GA.pageView('/takepart/donatefacebookpost/submiterror');
					}
					if ($('.template-donate.twitter').length) {
						WSPA.GA.pageView('/takepart/donatetweet/submiterror');
					}
					
					$('#feed-post-submit').removeAttr('disabled');
					alert(response.error);
				}
				else {
					
					if ($('.template-donate.facebook').length) {
						WSPA.GA.pageView('/takepart/donatetfacebookpost/submitted');
					}
					if ($('.template-donate.twitter').length) {
					}
					
//					$('.step1').fadeOut(function(){$('.step2').fadeIn()});
                    jQuery.cookie('home_sign_was', 'likeOrTweet');
					WSPA.FORMS.setInterceptCookie();
                    $.get('/thanks');
                    window.location.href = "/thanks";
										
				}
			},'json');
			return false;
		});
		
	},
	
	setInterceptCookie: function() {
		var date = new Date();
		date.setTime(date.getTime()+(365*24*60*60*1000));						
		jQuery.cookie('intercept', '1', {expires: date});
	},

	
	takepartOnlineSignUp: function() {
		var data = $('#join-us-for-the-next-stage').serializeArray();
		data.push({'name': 'action', 'value': 'callback'});
		$.post('/wp-admin/admin-ajax.php', data, function(response) {
			if (response.valid===true) {
				if ($('.template-donate.facebook').length) {
					WSPA.GA.pageView('/takepart/donatefacebookpost/contactform/sent');
				}
				else {
					WSPA.GA.pageView('/takepart/contactform/sent');
				}

				$('#join-us-for-the-next-stage').html(response.message);

				WSPA.POSTCODE.clearStep2SubClasses();
				
				$('.step2').addClass('step2Thanks');
                jQuery.cookie('home_sign_was', 'email');
				WSPA.FORMS.setInterceptCookie();
                $.get('/thanks');
                window.location.href = "/thanks";


			
			} else if (response.valid===false) {
				$('#join-us-for-the-next-stageResponse').html(response.message);
				$('#join-us-for-the-next-stageResponse p').addClass('textError');
				var tempError = 0;
				for(fieldname in response.errors) {
					tempError = 1;
					var mydiv = $('#'+[fieldname]).closest('div');
					if (mydiv.hasClass('field159')) {
						mydiv.addClass('field159Error');
					}
					else if (mydiv.hasClass('field322')) {
						mydiv.addClass('field322Error');
					}
					else if (mydiv.hasClass('field100')) {
						mydiv.addClass('field100Error');
					}
					else if (mydiv.hasClass('fieldCheckbox')) {
						mydiv.addClass('textError');
					}
				}
				if (tempError == 1) {
					if ($('.step2a').size() > 0) {
						ClearStep2SubClasses();
						$('.step2').addClass('step2aError');
					}
					if ($('.step2b').size() > 0) {
						ClearStep2SubClasses();
						$('.step2').addClass('step2bError');
					}
				}
			}
		},'json');
		return false;		
	}
	
};

WSPA.POSTCODE = {
	
	postcodeLookup: function() {

		var pcode = jQuery('#fieldPostcode').val();
		if (pcode == '') { alert('Please enter a postcode'); return false; }
		jQuery.get('/postcode.php?query='+escape(pcode), function(rtn) {
			if (rtn.success) {
				if (rtn.type == 'list') {
					data = rtn.data;
					jQuery('.postcode_results_content').html('');
					var HTML = '<p>Select your Address</p>';
					HTML += '<ul class="horizontalList">';
					jQuery.each(data,function(idx,row) {
						HTML += '<li';
						if (idx % 2) {
							HTML += ' class="shaded"';
						}
						HTML += '><a href="#" onclick="WSPA.POSTCODE.getAddress('+row.Id+');return false;">'+(idx+1)+', '+row.display+'</a></li>';
					});
					HTML += '</ul>';
					$('.postcode_results_content').html(HTML);
					// $('div.postcode_results').removeClass('noDisplay');
					WSPA.POSTCODE.clearStep2SubClasses();
					$('.step2').addClass('step2Addresses');
				}
			}
			else {
				$('.postcode_results_content').html("We're sorry...", "There was a problem looking up the postcode '" + pcode + "'. The error returned by the server was:<br /><br />" + rtn.message);
				$('div.postcode_results').removeClass('noDisplay');
			}
		}, 'json');
		
	},
	
	getAddress: function (id) {
		jQuery.get('/postcode.php?query='+id, function(rtn) {
			if (rtn.success) {
				
				if ($('.template-donate.facebook').length) {
					WSPA.GA.pageView('/takepart/donatefacebookpost/postcodelookup/succesful');

				}
				if ($('.template-donate.twitter').length) {
					WSPA.GA.pageView('/takepart/donatetweet/postcodelookup/succesful');
				}
				else {
					WSPA.GA.pageView('/takepart/postcodelookup/succesful');
				}
				
				if (rtn.type == 'address') {
					var data = rtn.data;
					jQuery('#fieldAddress_1').val(data.Line1);
					jQuery('#fieldAddress_2').val(data.Line2);
					jQuery('#fieldCity').val(data.PostTown);
					jQuery('#fieldPostcode').val(data.Postcode);
				}
				$('div.postcode_results').addClass('hidden');
				$('.fieldFind').addClass('notvisible');
				
				$('#join-us-for-the-next-stage').find('div.hide').each(function(){
					$(this).removeClass('hide');
					});

				WSPA.POSTCODE.clearStep2SubClasses();
				if ($('.alignRight .textError').size() > 0) {
					$('.step2').addClass('step2bError');
				} else {
					$('.step2').addClass('step2b');
				}
			}
			else {
				
				if ($('.template-donate.facebook').length) {
					_gaq.push(['_trackPageview','/takepart/donatefacebookpost/postcodelookup/failed']);
				}
				if ($('.template-donate.twitter').length) {
					_gaq.push(['_trackPageview','/takepart/donatetweet/postcodelookup/failed']);
				}
				else {
					_gaq.push(['_trackPageview','/takepart/postcodelookup/failed']);
				}
				
				$('.postcode_results_content').html("We're sorry...", "There was a problem looking up the postcode '" + pcode + "'. The error returned by the server was:<br /><br />" + rtn.message);
				$('div.postcode_results').removeClass('noDisplay');
			}
		}, 'json');
	},
	
	clearStep2SubClasses: function () {
		$('.step2').removeClass('step2a');
		$('.step2').removeClass('step2aError');
		$('.step2').removeClass('step2b');
		$('.step2').removeClass('step2bError');
		$('.step2').removeClass('step2Addresses');
		$('.step2').removeClass('step2Thanks');
	}
	
}

WSPA.PAGE = {
	init: function() {
		WSPA.PAGE.setTimeAgo();
		WSPA.PAGE.setColorbox();
		WSPA.PAGE.setScrollToControls();
		WSPA.PAGE.liveFeedMenu();
	},
	
	setTimeAgo: function() {
		$('span.timeago').each(function(){
			var stamp = $(this).html();
			$(this).html($.timeago(stamp,{refreshMillis:0}));
			$(this).removeClass('timeago');
		});
	},
	
	setColorbox: function() {
		$("a[rel='videoPopup']").colorbox();
	},
	
	setScrollToControls: function() {
		
		$('#scrollUp').click(function(){
			$('#live-feed-box').scrollTo('-=100px', 200);
			return false;
		});
		$('#scrollDown').click(function(){
			$('#live-feed-box').scrollTo('+=100px', 200);
			return false;
		});
		
	},
	
	scrollToTop: function() {
		$('#live-feed-box').scrollTo( {top:'0px', left:'0px'}, 200 );
	},
	
	liveFeedMenu: function() {
		$('.feedMenuWrapper').hover(function(){
			$(this).addClass('feedMenuHover');
		},
		function(){
			$(this).removeClass('feedMenuHover');
		});
		
		$('.feedMenuWrapper').removeClass('feedMenuActive');
		
		$('.feedMenuWrapper a').click(function(){
			$('.feedMenuWrapper').removeClass('feedMenuActive');
			$(this).parent().addClass('feedMenuActive');
		});		
		
	}
	
};

WSPA.TABS = {
	
	init: function() {
		$('.navigationSecondary ul.tabs li#signUpTabLink a').bind('click', function() {
			WSPA.TABS.changeView('signUp');
			return false;
		});
		$('.navigationSecondary ul.tabs li#facebookTabLink a').bind('click', function() {
			WSPA.TABS.changeView('facebook');
			return false;
		});
		$('.navigationSecondary ul.tabs li#twitterTabLink a').bind('click', function() {
			WSPA.TABS.changeView('twitter');
			return false;
		});
	},
	
	changeView: function(view){
		$('.navigationSecondary .tabs li').removeClass('active');
		$('.navigationSecondary .tabs li#' + view + 'TabLink').addClass('active');
		$('.tabBox .tab_content').addClass('hide');
		$('.tabBox #' + view + 'Tab').removeClass('hide');
	}
	
};

WSPA.TAKEPART = {
	init: function() {
		$('#show-takepart-online-signup-tab').bind('click', function() {
			WSPA.TAKEPART.changeView('online-signup');
			WSPA.GA.pageView('/takepart');
			return false;
		});
		$('#show-takepart-facebook-tab').bind('click', function() {
			WSPA.TAKEPART.changeView('facebook');
			WSPA.GA.pageView('/takepart/facebook');
			return false;
		});
		$('#show-takepart-twitter-tab').bind('click', function() {
			WSPA.TAKEPART.changeView('twitter');
			WSPA.GA.pageView('/takepart/twitter');
			return false;
		});
		$('#show-takepart-youtube-tab').bind('click', function() {
			WSPA.TAKEPART.changeView('youtube');
			WSPA.GA.pageView('/takepart/youtube');
			return false;
		});
		$('#show-takepart-flickr-tab').bind('click', function() {
			WSPA.TAKEPART.changeView('flickr');
			WSPA.GA.pageView('/takepart/flickr');
			return false;
		});
		$('#show-takepart-letters-tab').bind('click', function() {
			WSPA.TAKEPART.changeView('letters');
			WSPA.GA.pageView('/takepart/letters');
			return false;
		});
	},
	
	changeView: function(view){
		$('#takePartPage .tabContent').addClass('hidden');
		$('#takepart-'+view+'-tab').removeClass('hidden');
		$('.navigationSecondary li').removeClass('active');
		$('#show-takepart-'+view+'-tab').parent().addClass('active');
	}
};


WSPA.GA = {
	pageView: function(page) {
		_gaq.push(['_trackPageview', page]);
	}
};

