
// Config data
TWITTER_USER = "CAConeZones";
FLICKR_USER_ID = "53946733@N03";
FLICKR_API_KEY = "467f4fc8c980977cb6286ba13db0c744";
YOUTUBE_USER = "CAConeZone";

// Global data
gFlickr = null;
gYouTube = null;

$(document).ready(function(){
	
	// Slidy stuff
		$(".ico-cone").click(function(){
		      $("#hero-parent").animate(
				{ top: 0, left: 0 }, {
					duration: 'slow',
					easing: 'easeInOutCirc'
				})	
			activeScreen = 0;
			$('.promo-list li a').removeClass("SelectedItem");
			$(this).children(":first-child").addClass("SelectedItem");
		    });
		$(".ico-reward").click(function(){
		      $("#hero-parent").animate(
				{ top: 0, left: -600 }, {
					duration: 'slow',
					easing: 'easeInOutCirc'
				})
			activeScreen = 1;
			$('.promo-list li a').removeClass("SelectedItem");
			$(this).children(":first-child").addClass("SelectedItem");
			 });
		$(".ico-zone").click(function(){
		      $("#hero-parent").animate(
				{ top: 0, left: -1200 }, {
					duration: 'slow',
					easing: 'easeInOutCirc'
				})
			activeScreen = 2;
			$('.promo-list li a').removeClass("SelectedItem");
			$(this).children(":first-child").addClass("SelectedItem");
			 });
		$(".ico-fines").click(function(){
		      $("#hero-parent").animate(
				{ top: 0, left: -1200 }, {
					duration: 'slow',
					easing: 'easeInOutCirc'
				})
			activeScreen = 3;
			$('.promo-list li a').removeClass("SelectedItem");
			$(this).children(":first-child").addClass("SelectedItem");
			 });
			
			
	// Twitter
	$.getJSON("http://twitter.com/status/user_timeline/"+TWITTER_USER+".json?count=4&callback=?", function(data) {
		$(".info p").each(function(index) {
			if (index >= data.length) {
				return false;
			}
			$(this).html('<a href="http://www.twitter.com/'+TWITTER_USER+'" target="_blank"><strong>'+data[index]['user']['name']+':</strong></a> ' + data[index]['text']);
		});
	});
	
	// Flickr
	var url = "http://api.flickr.com/services/rest/?method=flickr.people.getPublicPhotos&api_key="+FLICKR_API_KEY+"&user_id="+FLICKR_USER_ID+"&per_page=5&format=json&jsoncallback=?";
	$.getJSON(url, function(data) {
		if (data["stat"] == "ok") {
			gFlickr = data["photos"]["photo"];
			startFlickr();
		}
	});
	
	// YouTube
	$.getJSON("http://gdata.youtube.com/feeds/api/users/" + YOUTUBE_USER + "/uploads?v=2&alt=jsonc&callback=?", function(data) {
		if (data && data["data"] && data["data"]["totalItems"] > 0) {
			gYouTube = data["data"];
			startYouTube();
		}
	});
});

function startFlickr() {
	for (var i=0; i < gFlickr.length; i++) {
		var photo = gFlickr[i];
		var img_url = 'http://farm' + photo['farm'] + '.static.flickr.com/' + photo['server'] + '/' + photo['id'] + '_' + photo['secret'] + '_m.jpg';
		$(".block-photo img").eq(i).attr("src", img_url);
		if (i > 0) {
			// Set up click handler for thumbnails
			$(".block-photo img").eq(i).bind('click', photo, function(evt) {
				$(".block-photo img").eq(0).attr("src", this.src);
				$(".block-photo a").eq(0).attr("href", "http://www.flickr.com/photos/"+FLICKR_USER_ID+"/"+evt.data["id"]+"/");
			});
		} else {
			// Set up Flickr landing page for default image
			$(".block-photo a").eq(0).attr("href", "http://www.flickr.com/photos/"+FLICKR_USER_ID+"/"+photo["id"]+"/");
		}
	}
}

function startYouTube() {
	// Set up player
	var width = 265;
	var height = 148;
	var url = "http://www.youtube.com/v/" + gYouTube["items"][0]["id"] + "?rel=0&enablejsapi=1&fs=1&showinfo=0&version=3";
	$("#ytplayer").html('' +
		'<object width="'+width+'" height="'+height+'" id="ytplayerswf">' +
		'  <param name="movie" value="'+url+'"></param>' +
		'  <param name="allowFullScreen" value="true"></param>' +
		'  <param name="allowScriptAccess" value="always"></param>' +
		'  <embed src="'+url+'" id="ytplayerswf"' +
		'    type="application/x-shockwave-flash"' +
		'    allowscriptaccess="always"' +
		'    width="'+width+'" height="'+height+'" ' +
		'    allowfullscreen="true"></embed>' +
		'</object>');
	
	// Set up thumbnails
	for (var i=0; i < gYouTube["items"].length; i++) {
		var html = '<li><a href="#"><img src="'+ gYouTube["items"][i]['thumbnail']['sqDefault'] +'" width="80" /></a></li>';
		$($(html).appendTo('.gallery ul'), 'a').bind('click', gYouTube["items"][i], function(evt) {
			console.info(evt.data);
			var swf = $("#ytplayerswf").get(0);
			if (swf) {
				swf.cueVideoById(evt.data["id"]);
			}
		});
	}

	// Set up the scrolling
	$('.gallery').galleryScroll({
		btPrev: 'a.link-prev',
		btNext: 'a.link-next',
		holderList: 'div',
		scrollElParent: 'ul',
		scrollEl: 'li',
		slideNum: false,
		duration : 300,
		step: 1,
		circleSlide: true,
		disableClass: 'disable',
		funcOnclick: null
	});
}

function onYouTubePlayerReady() {
}
