
var featureTotal = 0;
var featureCurrent = 0;

var moveFeatures = function(direction) {
	
	if(direction == 'prev' && featureCurrent <= 4) {
		return false;
	}
	if(direction == 'next' && featureCurrent >= featureTotal) {
		return false;
	}
	
	var height = 572;
	var width = 956;
	
	if($.browser.msie) {
		if(direction == 'prev') {
			featureCurrent -= 4;
		} else {
			featureCurrent += 4;
		}
				
		end = featureCurrent - 1;
		start = end - 4;
		$('.featuredItem').hide();
		if(start < 1) $('.featuredItem:eq(0)').show();
		$('.featuredItem:gt('+start+')').show();
		$('.featuredItem:gt('+end+')').hide();
		
		var featureCurrentLabel = (featureCurrent > featureTotal) ? featureTotal : featureCurrent;
		
		$('#featuredNavigationLabel').text(featureCurrentLabel+' of '+featureTotal);
		return false;	
	}
	
	if($('#featuredItems').hasClass('fiWide')) {
	
		if(direction == 'prev') {
			$('#featuredItems li').animate({left: "+="+width}, 300);
			featureCurrent -= 4;
			if(featureCurrent < 4) featureCurrent = 4;
		} else if(direction == 'next') {
			$('#featuredItems li').animate({left: "-="+width}, 300);
			featureCurrent += 4;
			if(featureCurrent > featureTotal) featureCurrent = featureTotal;
		}
		
	} else {
		if(direction == 'prev') {
			$('#featuredItems li').animate({top: "+="+height}, 300);
			featureCurrent -= 4;
			if(featureCurrent < 4) featureCurrent = 4;
		} else if(direction == 'next') {
			$('#featuredItems li').animate({top: "-="+height}, 300);
			featureCurrent += 4;
			if(featureCurrent > featureTotal) featureCurrent = featureTotal;
		}
	}
	
	if($.browser.msie) {
		
	}
		
	$('#featuredNavigationLabel').text(featureCurrent+' of '+featureTotal);
	
	return false;
		
}

var videosTotal = 0;
var videosCurrent = 0;

var moveVideos = function(direction) {
	
	if(direction == 'prev' && videosCurrent <= 4) {
		return false;
	}
	
	if(direction == 'next' && videosCurrent == videosTotal) {
		return false;
	}
	
	var height = 400;
	
	if(direction == 'prev') {
		$('#videoListing li').animate({top: "+="+height}, 300);
		videosCurrent -= 4;
		if(videosCurrent < 4) videosCurrent = 4;
	} else if(direction == 'next') {
		$('#videoListing li').animate({top: "-="+height}, 300);
		videosCurrent += 4;
		if(videosCurrent > videosTotal) videosCurrent = videosTotal;
	} else {
		return false;
	}
	
	
	$('#videoNavigationLabel').text(videosCurrent+' of '+videosTotal);
	
	return false;
		
}

var updateVideoNavigation = function() {
	trace('Videos: '+$('#videoListing li:visible').length);
	if($('#videoListing li:visible').length) {
		
		videosTotal = $('#videoListing li:visible').length;
		videosCurrent = (videosTotal > 4) ? 4 : videosTotal;
		$('#videoNavigation').html('<span style="width:200px"><a id="videoNavNext" href="#"><span>Next</span></a><a id="videoNavPrevious" href="#"><span>Previous</span></a><span style="margin-top: 8px; margin-right: 5px;" id="videoNavigationLabel">'+videosCurrent+' of '+videosTotal+'</span></span>');
		$('#videoNavigation').pngFix();
	}
	$('#videoListing li').css('top','0');
}

var movieNoFlash = '';

var showMovie = function() {
	if(movieNoFlash != '') {
		$('#movie').html(movieNoFlash);
	}
	$('#movie').show();	
	/*var watched = readCookie('watchedVideo');
	if(!watched) 
	{
		$('#movie').show();
	} else {
		$('#movie').slideDown();
	}*/
	
	$('#wrapper').hide();
	
	$('#movie').height($(document).height());
		
	var movieVars = {};
	var params = {
		menu: "false",
		wmode: "opaque",
		allowscriptaccess: "always"
	};
	var attributes = {};
	$('#movie').css("height","375px");
	if($.browser.msie) $('#movie').css('height','100%');
	swfobject.embedSWF("/swf/video_splash.swf", "moviePlayer", '100%', '100%', "9.0.0", null, movieVars, params, attributes);
	swffit.configure({target:"movie", minWid:705, minHei:375});	
	movieNoFlash = $('#movie').html();
	return false;
}

var closeMovie = function() {
	
	
	window.setTimeout("$('#movie').hide()", 500);
	window.setTimeout("$('#wrapper').show()", 550);
	return false;
}

var selectProfileView = function(section) {
	
	$('#tabs li a').removeClass('active');
	$('ul.recent-actions').children().hide();
	
	if(section == 'all') {
		$('#profileAllActivity').addClass('active');
		$('ul.recent-actions').children().show();
		$('ul.recent-actions li.info').hide();
	} else if(section == 'comments') {
		$('#profileComments').addClass('active');
		$('ul.recent-actions li.comment').show();
	} else if(section == 'posts') {
		$('#profilePosts').addClass('active');
		$('ul.recent-actions li.entry').show();
	} else if(section == 'bookmarks') {
		$('#profileBookmarks').addClass('active');
		$('ul.recent-actions li.bookmark').show();
	}
	
	return false;	 
			
}

var validateRegForm = function() {
	var frm = document.getElementById('frmSignUp');
	
	if(frm.username.value == '') { alert('You must enter a username.'); frm.username.focus(); return false; }
	if(frm.nickname.value == '') { alert('You must enter a display name.'); frm.nickname.focus(); return false; }
	if(frm.password.value == '') { alert('You must enter a password.'); frm.password.focus(); return false; }
	if(frm.password.value != frm.pass_verify.value) { alert('Your passwords do not match.'); frm.password.focus(); return false; }
	if(frm.email.value == '') { alert('You must enter an email address.'); frm.email.focus(); return false; }
	if(frm.terms.checked == false) { alert('You must agree to the terms.'); frm.terms.focus(); return false; }
	
	return true;
	
}

var trace = function(msg) { try { console.log(msg) } catch(e) {} }

$(document).ready(function(){

	jQuery.fn.centerScreen = function(loaded) {
		var obj = this;
		var addTop = $(document).scrollTop();
		if(!loaded) {
			obj.css('top', ($(window).height()/2-this.height()/2)+addTop);
			obj.css('left', $(window).width()/2-this.width()/2);
			$(window).resize(function() { obj.centerScreen(!loaded); });
			$(window).scroll(function() { obj.centerScreen(!loaded); });
		} else {
			obj.stop();
			obj.animate({ top: ($(window).height()/2-this.height()/2)+addTop, left: $(window).width()/2-this.width()/2}, 200, 'linear');
		}
	}
	
	$('.closeTop').click(function(){
		$(this).parent().parent().slideUp();
	});
	
	$('#getTestedBox .textInput').blur(function(){
		if($(this).val() == '') {
			$(this).val('Enter Zip Code');
		}		
	});
	
	$('#getTestedBox .textInput').focus(function(){
		if($(this).val() == 'Enter Zip Code') {
			$(this).val('');
		}
	});
	
	$('.faqList h4').hover(function(){
		$(this).css('color', '#5e4a2a');
	}, function() {
		$(this).css('color', '#796038');
	})
	
	$('.faqList h4').click(function(){
		var open = $(this).parent().find("p:visible").length;
		
		if(!$.browser.msie) {
			$('.faqList h4 span').css('backgroundImage','url(/images/faqArrowRight.gif)');
			$('.faqList p').hide();
			if(open == 0) {
				$(this).find('span').css('backgroundImage','url(/images/faqArrowDown.gif)');
				$(this).parent().find('p').show();
			}
		} else {
			$('.faqList h4 img').attr('src','/images/faqArrowRight.gif');
			$('.faqList p').hide();
			if(open == 0) {
				$(this).parent().find('img').attr('src','/images/faqArrowDown.gif');
				$(this).parent().find('p').show();
			}
		}
	});
	
	$('.mainNavLink').parent().hover(function(){
		var current=$(this).find('.mainNavLink').css('backgroundPosition').match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
		$(this).find('.mainNavLink').animate({ backgroundPosition: "("+current[1]+current[2]+" -21px)" }, 200);
		$(this).find('.subMenu').show();
		if($(this).find('.subMenu').length && $(this).attr('setSubMenu') != 1) {
			$(this).find('li:last').addClass('nopadding');
			var thisCenter = $(this).position()['left'] + ($(this).width()/2);
			var subMenuWidth = $(this).find('ul').width();
			var subMenuLeft = thisCenter - (subMenuWidth/2);
			if(subMenuLeft + subMenuWidth > 940) subMenuLeft = 940 - subMenuWidth;
			if(subMenuLeft < 0) subMenuLeft = 0;
			$(this).find('.subMenu ul').css('left', subMenuLeft);
			$(this).attr('setSubMenu',1); // only do all of this math once...
		}
	},function() {
		var current=$(this).find('.mainNavLink').css('backgroundPosition').match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
		$(this).find('.mainNavLink').animate({ backgroundPosition: "("+current[1]+current[2]+" 0px)" }, 200);
		$(this).find('.subMenu').hide();
	});
	
	if($('#featuredItems li').length > 4) {
		
		featureTotal = $('#featuredItems li').length;
		featureCurrent = 4;

		$('#featureNavigationCell').html('<span><a id="featureNavNext" href="#"><span>Next</span></a><a id="featureNavPrevious" href="#"><span>Previous</span></a><span style="margin-top: 8px; margin-right: 5px;" id="featuredNavigationLabel">'+featureCurrent+' of '+featureTotal+'</span></span>')
		$('#featureNavigationCell').show();
		$('#featureNavigationCell').css('width','130px');
		$('#featureNavigation').pngFix();
			
	} 
	
	updateVideoNavigation();
	
	$('#featureNavPrevious').live('click', function(){ return moveFeatures('prev');});
	$('#featureNavNext').live('click', function(){ return moveFeatures('next');});
	
	$('#videoNavPrevious').live('click', function(){return moveVideos('prev');});
	$('#videoNavNext').live('click', function(){return moveVideos('next');});
	
	$('.commentLink').click(function(){
		$('#shareLinks').hide();
		$('#commentForm').slideToggle(400);
		return false;
	});
	
	$('.shareLink').click(function(){
		$('#commentForm').hide();
		$('#shareLinks').slideToggle(400);
		return false;
	});
	
	$('#topNavLoginLink').click(function(){
		$('#topSearch').hide();
		$('#topLogin').slideToggle(400);
		return false;
	});
	
	$('#topNavSearchLink').click(function(){
		$('#topLogin').hide();
		$('#topSearch').slideToggle(400);
		return false;
	});
	
	if(document.location == 'http://' + document.location.host + '/') {
		var watched = readCookie('watchedVideo');
		if(!watched) {
			showMovie();
			createCookie('watchedVideo', true, 365);
		}
	}
	
	$('.feedQuestion').click(function(){
		$(this).parent().find('p').toggle();
		if($(this).parent().find('p:first').css('display') == 'block') {
			$(this).find('img').attr('src','/images/circleArrowDown.gif');
		} else {
			$(this).find('img').attr('src','/images/circleArrowRight.gif');
		}
		return false;
	});
	
	$('.videoToggle').click(function(){
		$('.videoToggle').css('color','');
		$(this).css('color','#ffffff');
		if($(this).text() == 'REAL TALK') {
			$('.videoOther').hide();
		} else {
			$('.videoOther').show();
		}
		updateVideoNavigation();
		return false;
	});
	
	$('.videoOther').hide();
	
	if(document.location.href.indexOf('#comments') > -1) {
		$('.commentLink').click();
	}
	
	$('#profileAllActivity').click(function(){return selectProfileView('all'); });
	$('#profileComments').click(function(){return selectProfileView('comments'); });
	$('#profilePosts').click(function(){return selectProfileView('posts'); });
	$('#profileBookmarks').click(function(){return selectProfileView('bookmarks'); });
	
	if($('li.statement').length) {
		var statement = $('li.statement').text();
		var parts = statement.split(' > ');
		var newStatement = '<span style="color: #ffffff">'+parts[0] + ' &gt; </span>'+parts[1]+'';
		$('li.statement').html(newStatement);
	}
	

	
	if($('#customfield_statement').length) {
		var val = $('#customfield_statement').val();
		var html = '<select name="customfield_statement" id="#customfield_statement">'
		for(var i=0; i<statements.length; i++) {
			var sel = (val == statements[i]) ? 'selected' : '';
			html += '<option value="'+statements[i]+'" '+sel+'>'+statements[i]+'</option>';
		}
		html += '</select>';
		$('#customfield_statement').replaceWith(html);
	};
	

	$('img[hover_src]').each(function(){
		var image = new Image();
		image.src = $(this).attr('hover_src');
	});
	
	$('img[hover_src]').hover(function() {
		$(this).attr('orig_src', $(this).attr('src'));
		$(this).attr('src', $(this).attr('hover_src'));
	}, function(){
		$(this).attr('src', $(this).attr('orig_src'));
	});
	
	$('#frmSignUp').submit(function(){return validateRegForm();});
	
	$(document).pngFix(); 
	//$('div.pageHeaderWide').pngFix(); 
	
	$('#contentBody a').each(function(){$(this).attr('target','_blank');});
	
	$('.previewShare').click(function(){
		$(this).closest('.entry').find('.shareLinks').slideToggle();
	});
	
	$('.bookmarkLink').click(function(){
		u = mtGetUser();
		if(!u) {
			alert('You must be logged in to bookmark pages');
			return false;
		}
		var entry_id = $(this).attr('entry_id');
		$.post("/cgi-bin/mt/mt-cp.cgi",'__mode=vote&blog_id=1&f=scored,count&jsonp=mtScore_cb&id=' + entry_id, function() {
			if($('#bookmarkLink-'+entry_id).hasClass('white')) {
				$('#bookmarkLink-'+entry_id).replaceWith('<span style="color: #FFFFFF">BOOKMARKED</span>');
			} else {
				$('#bookmarkLink-'+entry_id).replaceWith('<span style="color: #000000">BOOKMARKED</span>');
			}
		});
		return false;
		
	});
	
	var bookmarkEntries = '';
	$('.bookmarkLink').each(function(){
		if(bookmarkEntries != '') bookmarkEntries += ',';
		bookmarkEntries += $(this).attr('entry_id');
	});
	
	if(bookmarkEntries != '') {
		$.post("/cgi-bin/mt/mt-cp.cgi",'__mode=score&blog_id=1&f=scored,count&jsonp=updateBookmarkLinks&id=' + bookmarkEntries, function(data) {
			eval(data);
		});
	}
	
	$('.closeLink').click(function(){
		if($(this).hasClass('cform')) {
			$(this).parent().parent().parent().slideToggle();
		} else {
			$(this).parent().parent().slideToggle();
		}
	});
	
	if($.browser.msie) {
		/** IE Fixes... **/
		$('.featuredItem:gt(3)').hide();
		$('.faqList h4 span').addClass('msie');
		$('.faqList h4 span').after('<img src="/images/faqArrowRight.gif" style="border: none; vertical-align: bottom; margin-left: 10px" />')
	}
		
});

$('#topNav').ready(function(){
	//trace('topNav ready');
	//window.setTimeout("mtFetchUser('getTheUser');", 20);	
});

var mtSignInOnClick = function() {
	loginOverlay();
}

var loginOverlay = function() {
	var return_to = document.URL.replace(/#.+/, '');
	var loginContent = '';
	if($('#loginOverlay').length == 0) {
		$('body').append('<div id="loginOverlay"></div><div id="loginOverlayForm"></div>');
		$.ajax({
			type: "GET",
			url: '/ajax_login.html',
			success: function(data) {
				$('#loginOverlay').css('background', '#000000').css('opacity',0.8).height($(document).height());
				data = data.replace('##return_to##', return_to);
				var closeButton = '<div style="text-align: right"><a href="javascript: void(0);" id="closeLoginOverlay">Close X</a></div>';
				$('#loginOverlayForm').html(closeButton+data);
				$('#loginOverlayForm').centerScreen();
				$('#closeLoginOverlay').click(function(){ 
					closeLoginOverlay();
				});
				FB.init('9a204454129f7d056e42f84fbc7db487', '/mt-static/plugins/FacebookCommenters/xd_receiver.html');
			},
			error: function(data) {
				document.location = 'http://www.greaterthan.org/cgi-bin/mt/mt-cp.cgi?__mode=login&blog_id=1';
			}
		})
	} else {
		$('#loginOverlay').show();
		$('#loginOverlayForm').show();
	}
		
}

var closeLoginOverlay = function () {
	$('#loginOverlay').hide();
	$('#loginOverlayForm').hide();
}

// var getTheUser = function() {
// 	
// 	var u = mtGetUser();
// 	if(u && u.name) {
// 		$('#topNavSearchLink').css('padding-right', '0').css('border', 'none');
// 		$('#topNavLoginLink').hide();
// 		user_link = '<a style="margin-right: 0px; padding-right: 0px; border: none;" href="/cgi-bin/mt/mt-cp.cgi?__mode=edit&amp;return_to=' + encodeURIComponent(document.URL) + '&amp;blog_id=1';
// 		user_link += '" >Edit</a>';
// 		user_link_view = '<a style="margin-right: 0px; padding-right: 0px; border: none;" href="/cgi-bin/mt/mt-cp.cgi?__mode=view&amp;return_to=' + encodeURIComponent(document.URL) + '&amp;blog_id=1';
// 		user_link_view += '" style="border: none;">View</a>';
// 		log_out = '<a style="margin-right: 0px; padding-right: 0px; border: none;" href="/cgi-bin/mt/mt-cp.cgi?__mode=logout;return_to=http://www.greaterthan.org" style="border: none; paddoing: none; margin: none;">Sign Out</a>';
// 		//$('#loggedInInfo').remove();
// 		$('#topNav').append('<li class="leftBorder" id="loggedInInfo" style="padding-left: 11px; display: none;">Logged In as '+u.name+' &nbsp; | &nbsp; '+user_link_view+' or '+user_link+' your profile. &nbsp; | &nbsp; '+log_out+'</li>');
// 		$('#loggedInInfo').show();
// 	} 
// }

/* <![CDATA[ */
function mtUpdateSignInWidget(u) {
	
   	var content = '';
    if (u) {
        if (u && u.is_authenticated) {
            user = u;
            mtSaveUser();
        } else {
            // user really isn't logged in; so let's do this!
            return mtSignIn();
        }
    } else {
        u = mtGetUser();
    }
    if (u && u.name) {
		$('#topNavSearchLink').css('padding-right', '0').css('border', 'none');
		$('#topNavLoginLink').hide();
		user_link = '<a style="margin-right: 0px; padding-right: 0px; border: none;" href="/cgi-bin/mt/mt-cp.cgi?__mode=edit&amp;return_to=' + encodeURIComponent(document.URL) + '&amp;blog_id=1';
		user_link += '" >Edit</a>';
		user_link_view = '<a style="margin-right: 0px; padding-right: 0px; border: none;" href="/cgi-bin/mt/mt-cp.cgi?__mode=view&amp;return_to=' + encodeURIComponent(document.URL) + '&amp;blog_id=1';
		user_link_view += '" style="border: none;">View</a>';
		log_out = '<a style="margin-right: 0px; padding-right: 0px; border: none;" href="/cgi-bin/mt/mt-cp.cgi?__mode=logout;return_to=http://www.greaterthan.org" style="border: none; paddoing: none; margin: none;">Sign Out</a>';
		//$('#loggedInInfo').remove();
		$('#topNav').append('<li class="leftBorder" id="loggedInInfo" style="padding-left: 11px; display: none;">Logged In as '+u.name+' &nbsp; | &nbsp; '+user_link_view+' or '+user_link+' your profile. &nbsp; | &nbsp; '+log_out+'</li>');
		$('#loggedInInfo').show();
    }

}

var updateBookmarkLinks = function(data) {
	for(id in data) {
		if(data[id].scored) {
			if($('#bookmarkLink-'+id).hasClass('white')) {
				$('#bookmarkLink-'+id).replaceWith('<span style="color: #FFFFFF">BOOKMARKED</span>');
			} else {
				$('#bookmarkLink-'+id).replaceWith('<span style="color: #000000">BOOKMARKED</span>');
			}	
		}
	}
}

/* -- Cookie Funcs -- */

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function cleanupActions() {
	
	var haveEntries = ($('.recent-actions .entry').length) ? true : false;
	var haveComments = ($('.recent-actions .comment').length) ? true : false;
	var haveBookmarks = ($('.recent-actions .bookmark').length) ? true : false;
	
	if(!haveEntries || !haveComments || !haveBookmarks) {
		var classN = '';
		var titles = new Array();
		var output = '';
		if(!haveEntries) {
			classN += 'entry ';
			titles.push('Posts');
			output += '<li class="entry info" style="display: none;"><h3 style="font-size: 20px; padding: 0px; margin: 10px;"> No Posts</h3></li>';
		}
		if(!haveComments) {
			classN += 'comment ';
			titles.push('Comments');
			output += '<li class="comment info" style="display: none;"><h3 style="font-size: 20px; padding: 0px; margin: 10px;"> No Comments</h3></li>';
		}
		if(!haveBookmarks) {
			classN += 'bookmark ';
			titles.push('Bookmarks');
			output += '<li class="bookmark info" style="display: none;"><h3 style="font-size: 20px; padding: 0px; margin: 10px;"> No Bookmarks</h3></li>';
		}
		
		var title = '';
		
		if(titles.length == 1) {
			title = titles[0];
		} else if(titles.length == 2) {
			title = titles[0] + ' or ' + titles[1];
		} else {
			title = titles[0] + ', ' + titles[1] + ' or ' + titles[2];
		}
				
		output += '<li class="all"><h3 style="font-size: 20px; padding: 0px; margin: 10px;"> No '+title+'</h3></li>';
		$('.recent-actions').append(output);
	}
	
}

var statements = new Array();

statements.push('Now > Never');
statements.push('Action > Apathy');
statements.push('Talking > Silence');
statements.push('Knowledge > Ignorance');
statements.push('Knowing > Doubt');
statements.push('Commitment > Indifference');
statements.push('Safe > Sorry');
statements.push('Tolerance > Prejudice');
statements.push('We > Me');
statements.push('Hope > Fear');
statements.push('Inspired > Complacent');
statements.push('Compassion > Neglect');
statements.push('Faith > Despair');
statements.push('Life > Death');
statements.push('Acceptance > Rejection');
statements.push('United > Divided');
statements.push('Love > Hate');
statements.push('Forgive > Foresake');
statements.push('Presence > Absence');
statements.push('Courage > Cowardice');
statements.push('Consideration > Disregard');
statements.push('Respect > Contempt');
statements.push('Tune In > Tune Out');


