if(!energyblog){
	var energyblog = {};	
}

energyblog.webroot = "/mnt/stor2-wc1-dfw1/392877/394778/blog.energytomorrow.org/";
energyblog.customroot= energyblog.webroot+"web/content/custom/";

energyblog.twitter = {	
		data:"",					
		init: function(){
			
			
			
			
			$.getJSON("/custom/twitter.php",function(data){
				energyblog.twitter.data = data;
				if(data["error"]){
					$("#twitters").html(data["errorMessage"]);
				}else{					
					$("#twitters").html(data["html"]+"<a class='snap_noshots' href='http://twitter.com/energytomorrow'>More from @EnergyTomorrow</a><br /><a class='snap_noshots' href='http://twitter.com/janevanryan'>More from @JaneVanRyan</a>");				
				}
			});				
		}
}	
energyblog.mostpopular = {
	init: function(){
		$.getJSON("/custom/mostpopular.php",function(data){
			if(data["error"]){
				$("#mostpopular").html("Most popular entries cannot be shown at the moment.");
			}else{				
				$("#mostpopular").html(data["html"]);				
			}
		});	
	}
}	
energyblog.youtube = {	
	data:[],
	init: function(){
		energyblog.youtube.loadFromDb();
		$(".tubeLinkOne a").click(function(){			
			energyblog.youtube.showVideoOne();
		});
		$(".tubeLinkTwo a").click(function(){			
			energyblog.youtube.showVideoTwo();
		});				
	},
	showVideoOne: function(){		

		$("#tubeVid2").css("display","none");
		$("#tubeVid1").css("display","block");	
 		
		$(".tubeLinkTwo a").removeClass("active");
		$(".tubeLinkOne a").addClass("active");			
		
		$("#youtubeInner p a").html("More from the Energy Channel");
		$("#youtubeInner p a").attr("href","http://www.youtube.com/energy");
			
		
	},
	showVideoTwo: function(){

		$("#tubeVid1").css("display","none")
 		$("#tubeVid2").css("display","block");

		$(".tubeLinkOne a").removeClass("active");
		$(".tubeLinkTwo a").addClass("active");			

		$("#youtubeInner p a").html("More from the Energy Citizens Channel");
		$("#youtubeInner p a").attr("href","http://www.youtube.com/energycitizens");

	},
	loadFromDb: function(){
		$.getJSON("/custom/youtube.php",function(data){
			energyblog.youtube.data = data;			
			$(data).each(function(i,item){	
				i++; // the value of i is always one lower than we need - so increment it!			
				$("#tubeVid"+i).html(item["html"]);
			});
			$("#tubeVid2").hide(0);			
		});				
	}			
}

energyblog.flickr = {
	init: function() {
		var ulFlickr = $('ul#flickr-images');
		//if(ulFlickr.length<1) return;
		ulFlickr.append("<li>Loading...</li>");
		$.getJSON("/custom/flickr.php", function(data) {
			var tpl = "<li class='flickr-feed-listitem' style='{style}'><a href='{pageurl}'><img src='{imageurl}'/></a></li>";
			ulFlickr.empty();
			$(data).each(function(i,item){
				img = tpl.replace('{pageurl}', item.pageUrl);
				img = img.replace('{imageurl}', item.imageUrl);
				if(i%2==1)
				{
					img = img.replace('{style}','');
				} else 
				{
					img = img.replace('{style}','clear:both;');
				}
				ulFlickr.append(img);
			});
			
		});
	}
}

		

energyblog.comments ={
	init: function(){
		if (commenter_name &&
			(!commenter_id ||
			commenter_blog_ids.indexOf("'" + blog_id + "'") > -1)) 
		{
			if($('#comments-form').length<1)
			{
				return;
			}

			$('#comment-author').val(commenter_name);
			$('#comments-open-data').hide();
			$('#comments-form').show();

		} else 
		{
		}
	}
}

energyblog.login ={
	init: function() {
	}
}

/* The voting system works off the basis of css classes.
 * The main CSS class is called scoring.  Once the scores
 * have been loaded by the mtUpdateScores function, additional
 * css classes are added:
 * 		scoring-scorable
 * 		scoring-scored
 * 
 * If the vote link has the css class "scoring", but NO OTHER
 * class, then the user is not signed in.  It is this that
 * we look for when determining whether to show the tooltip
 * or not.
 */
energyblog.voting = {
	
	xOffset: 10,
	yOffset: 20,		
	
	init: function() {		
		$("a.scoring").hover(
			energyblog.voting.showTip,
			energyblog.voting.hideTip
		);	
		$("a.scoring").mousemove(
			energyblog.voting.moveTip
		);
	},
	
	showTip: function(e) {		
	
		if($(this).hasClass("scoring-scorable") ||
			$(this).hasClass("scoring-scored"))
			{
				return;
			}
								  
		$("body").append("<div id='voting-tooltip'>You must sign-in to vote.</div>");
		$("#voting-tooltip")
			.css("top",(e.pageY - energyblog.voting.xOffset) + "px")
			.css("left",(e.pageX + energyblog.voting.yOffset) + "px")
			.fadeIn("fast");
	},

	hideTip: function(e) {
		if($(this).hasClass("scoring-scorable") ||
			$(this).hasClass("scoring-scored"))
			{
				return;
			}
		$("#voting-tooltip").remove();
	},
	
	moveTip: function(e) {
		if($(this).hasClass("scoring-scorable") ||
			$(this).hasClass("scoring-scored"))
			{
				return;
			}
		$("#voting-tooltip")
			.css("top",(e.pageY - energyblog.voting.xOffset) + "px")
			.css("left",(e.pageX + energyblog.voting.yOffset) + "px");
	}

}

$().ready(function(){
	energyblog.flickr.init();
	energyblog.voting.init();
});
