$(function() {
	jQuery('#content').css('padding-bottom','10px');
	init_carousel();
	init_feeds();
});


/*Google Feed API*/
google.load("feeds", "1");
function init_feeds() {
	var feed = new google.feeds.Feed("http://ubcommunicators.ning.com/profiles/blog/feed");
	feed.load(function(result) {
		if (!result.error) {
			var container = document.getElementById("blog");
			attach(result.feed.entries,container);
		}
	});
	var feed = new google.feeds.Feed("http://ubcommunicators.ning.com/forum?feed=yes&xn_auth=no");
	feed.load(function(result) {
		if (!result.error) {
			var container = document.getElementById("forum");
			attach(result.feed.entries,container);
		}
	});
	var feed = new google.feeds.Feed("http://gdata.youtube.com/feeds/base/users/ubcommunications/uploads?alt=rss&v=2&orderby=published&client=ytapi-youtube-profile");
	feed.load(function(result) {
		if (!result.error) {
			var container = document.getElementById("comm_tube");
			attachVideo(result.feed.entries,container);
		}
	});
	

}
function attach(entries,container) {
	for (var i = 0; i < 1; i++) {
		var entry = entries[i];
		var date = new Date(entry.publishedDate);
		var month = date.getMonth() + 1;
		var info_text_pre = "Posted by ";
		var a_author = document.createElement("a");
		a_author.setAttribute("href","http://ubcommunicators.ning.com/profile/" + entry.author.split(' ').join(''));
		a_author.appendChild(document.createTextNode(entry.author));
		var p_title = document.createElement("p");
		var p_info = document.createElement("p");
		var a_title = document.createElement("a");
		a_title.setAttribute("href",entry.link);
		a_title.appendChild(document.createTextNode(entry.title));
		p_title.appendChild(a_title);
		p_info.appendChild(document.createTextNode("Posted by "));
		p_info.appendChild(a_author)
		p_info.appendChild(document.createTextNode(" on " + month +"/"+ date.getDate()+"/"+date.getFullYear()));
		container.appendChild(p_title);
		container.appendChild(p_info);
	}
}

function attachVideo(entries, container) {
	var entry = entries[0];//Extract the first video
	//var e_vid =document.createElement("embed");
	var d_vid =document.createElement("div");//DUmmy video o prevent wrapping in ie6
	var reg_video_id = new RegExp(/v=(.*)$/);
	var video_id = reg_video_id.exec(entry.link)[1];
	var embed_link = "http://www.youtube.com/v/" + video_id +"&hl=en&fs=1&";
	
	//e_vid.setAttribute("src",embed_link);
	//e_vid.setAttribute("quality","high");
	//e_vid.setAttribute("height","175px");//Done in css
	//e_vid.setAttribute("width","150px");//Done in css
	//e_vid.setAttribute("type","application/x-shockwave-flash");
	//e_vid.setAttribute("pluginspage","http://www.macromedia.com/go/getflashplayer");

	d_vid.appendChild(document.createTextNode("    "));
	
	//container.appendChild(e_vid);
	
	swfobject.embedSWF(embed_link, "movie", "280", "175", "10.0.0");
	container.appendChild(d_vid);//Do not remove or video will wrap in ie6.
}
/*google.setOnLoadCallback(initialize);*///This is being done in jquery function at the top.

function on_carousel_init(carousel) {
	jQuery('#left_button').bind('click', function() {
		carousel.prev();
		return false;
	});
	jQuery('#right_button').bind('click', function() {
		carousel.next();
		return false;
	});
}
function init_carousel() {
	jQuery('#new_add_carousel').jcarousel({
			scroll: 1,
			visible:1,
			wrap:"both",
			initCallback: on_carousel_init
	});
}
