/* 
Coder: VC
Date: June 2009
Notes: Banner JS for Breakthrough homepage. 
Requires: JQuery
 */

jQuery(document).ready(function() {

	/* What you need to know */
	jQuery('.links_accordion .heading_wrapper').hoverIntent(
		function(eventObj){
			var this_index = jQuery(".links_accordion .heading_wrapper").index(this);
			
			jQuery('.links_accordion .heading_wrapper').removeClass('default_selected selected');
			jQuery('.links_accordion .heading_wrapper:not(:eq('+this_index+')) ul').slideUp('fast');
			jQuery('.links_accordion .heading_wrapper:eq('+this_index+')').addClass("selected");
			jQuery('.links_accordion .heading_wrapper:eq('+this_index+') ul').slideDown(
				'fast', function(){
					jQuery('.links_accordion .heading_wrapper:not(:eq('+this_index+')) ul').hide();
				}
			);
		},function(eventObj){
			//do something
		}
	);
	
	/* twitter stream */
	var month=new Array(12);
	month[0]="January";
	month[1]="February";
	month[2]="March";
	month[3]="April";
	month[4]="May";
	month[5]="June";
	month[6]="July";
	month[7]="August";
	month[8]="September";
	month[9]="October";
	month[10]="November";
	month[11]="December";
	
	$("#twitter .carouselWrapper").tweet({
		join_text: "auto",
		username: "breakthroughbc",
		count: 3,
		auto_join_text_default: "we said,", 
		auto_join_text_ed: "we",
		auto_join_text_ing: "we were",
		auto_join_text_reply: "we replied",
		auto_join_text_url: "we were checking out",
		loading_text: "loading tweets..."
	});
	
	/* flickr photos*/
	$(".flickr .photos").each(function() { 
		var current_page = "1";
		
	    /* Flickr returns some hidden INPUTs within the UL:
	        0: current page
	        1: last page
	        2: photos per page
	        3: total photos
	    */
		if ($("input", this).length) {
		    // If we already have some photos find out which page we're on.
			current_page = $("input", this).get(0).value;
		}
		
		loadFlickrThumbs(this, current_page);
	});
		
	/* youtube vids */
	$.getJSON('http://gdata.youtube.com/feeds/api/users/breakthroughbc/uploads?v=2&alt=jsonc&callback=?', function(data) {
		$('#media .youtube .videos').append('<ul></ul>');
		
		var number=data.data.items.length;
		
		$.each(data.data.items, function(i, item) {
			var title = item['title'];
			var id = item['id'];
			var thumb = item['thumbnail']['sqDefault'];
			
			video ="http://www.youtube.com/watch?v="+id;
			$('#media .youtube .videos ul').append('<li><a href="'+video+'" title="'+title+'" target="_blank"><img src="'+thumb+'" alt="'+title+'" title="'+title+'" /></a></li>');
		});
		
		$('#media .youtube .videos ul').css("width",number*153);
		jQuery('#media .youtube .videos').serialScroll({
			items:'li',
			cycle:false,
			step:2,
			next:'#media .youtube .paging .next a',
			prev:'#media .youtube .paging .previous a'
		})
		
	});	

});

jQuery(window).load(function(){
	/*  news carousels */	
	$('.carouselWrapper').each(function(){
		var theId=$(this).parent().attr("id");
		$(this).before('<div class="news-scroller"><span class="up">up</span><span class="down">down</span></div>');
			
		$(this).jCarouselLite({
			btnNext:"#"+theId+" .news-scroller .down",
			btnPrev:"#"+theId+" .news-scroller .up",
			vertical: true,
			scroll: 1,
			visible:1
		});
	});
});

function loadFlickrThumbs(container, page) {
	$(container).flickr({
		api_key: "04318d1f3d0b8f3b81d5be9faad7c829",
		type: "search",
		group_id: "1492656@N24",
		per_page: 3,
		page: page,
		callback: function(el) {
			// Get the current and last page values out of the INPUTs
			var current_page = parseInt($("input", el).get(0).value);
			var last_page = parseInt($("input", el).get(1).value);
			
			// Find the paging buttons
			var prev = $(el).parents(".flickr").find(".paging .previous").show();
			var next = $(el).parents(".flickr").find(".paging .next").show();
			
			// Hide buttons that won't work
			if (current_page === 1) { prev.hide(); }
			if (current_page === last_page) { next.hide(); }
			
			// Next page
			next.click(function() {
				// Check that current page has finished loading
				if ($("input", el).length) {
					$(el).fadeOut(500, function() {
						var next_page = parseInt($("input", this).get(0).value) + 1;
						$(this).empty().show();
						loadFlickrThumbs(this, next_page);
					});
				}
				return false;
			});
			
			// Previous page
			prev.click(function() {
				
				// Check that current page has finished loading
				if ($("input", el).length) {
					$(el).fadeOut(500, function() {
						var prev_page = parseInt($("input", this).get(0).value) - 1;
						$(this).empty().show();
						loadFlickrThumbs(this, prev_page);
					});
				}
				return false;
			});
		}
	});
}
