/* =====================================================================
*
*    ページロード時の処理
*
* =================================================================== */

/* ---------------------------------
*    Twitter
*/

$(document).ready(function() {
	var date = new Date();
	$.getJSON("http://twitter.com/statuses/user_timeline/kapirabasu.json?count=4&since=Sat%2C+17+Apr+2010+12%3A00%3A" + date.getSeconds() + "+GMT&callback=?", function(data) {

		$("tweetsBox").html("");

		var ul = document.createElement('ul');
		$(ul).attr("id","ticker");
		ul.style.display = "none";
		$("#tweetsBox").append(ul);

		for (var i=0, post; post = data[i]; i++) {

			var li = document.createElement('li');
			li.className = 'small';

			ul.appendChild(li);
			li.appendChild(document.createTextNode(post.text));
		}

		$("#tweetsBox").css("background","none");
		$("#tweetsBox ul").fadeIn("slow");

		$("#ticker").newsTicker();

	});
});


/* ---------------------------------
*    Flickr - Lunch
*/

$(document).ready(function() {
	$.getJSON("http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=ff3313a50f5a26c74bea5c7fff8c0394&tags=kaplunch&sort=date-taken-desc&format=json&jsoncallback=?", function(data) {

		var item = data.photos.photo[0];
		var id=item.id;
		var title = item.title;

			$.getJSON("http://api.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key=ff3313a50f5a26c74bea5c7fff8c0394&photo_id=" + id + "&format=json&jsoncallback=?", function(data) {
				item = data.sizes.size[2];
				source = item.source;
				url = item.url.split("sizes/s/");
				height = item.height;

				a = document.createElement('a');
		
				img = document.createElement('img');
				img.src = source;
				img.alt = title;
				img.title = title;

				$("#iLunchCont").css("height", height);

				document.getElementById("iLunchCont").appendChild(a);
				a.setAttribute("href", url[0]);
				a.appendChild(img);

				$("#iLunchCont").onImagesLoad({
					callback: function() {
						$("#iLunchBox").css("background", "none");
 						$("#iLunchCont").slideDown("slow");
					}
				});
			});

			$.getJSON("http://api.flickr.com/services/rest/?method=flickr.photos.getInfo&api_key=ff3313a50f5a26c74bea5c7fff8c0394&photo_id=" + id + "&format=json&jsoncallback=?", function(data) {


				var description = data.photo.description._content;
				/*var date = data.photo.dates.taken.substr(5,5).replace("-","/");*/
				var date = data.photo.dates.taken.substr(8,2);

				var htmlText = null;
				htmlText = "<div id='iLchInnerCont' class='clearfix'>"
						+ "<div class='dateBox iepngfix'><div class='dateDay'>" + date + "</div></div>"
						+ "<div class='titleBox'>"
						+ "<h5 class='mid'>" +  "『" +  title + "』" + "</h5>"
						+ "<p class='bodyLch small'>" + description + "</p>"
						+ "</div>"
						+ "</div>";
				$("#iLchOutput").append(htmlText);
				$("#iLchOutput").css("background","none");
				$("#iLchOutput").css("display","none").fadeIn("slow");

			});

	});
});


/* ---------------------------------
*    Flickr -Gallery
*/

$(document).ready(function() {
	$.getJSON("http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=ff3313a50f5a26c74bea5c7fff8c0394&tags=kapgallery&format=json&jsoncallback=?", function(data) {

		var item = data.photos.photo[Math.floor(Math.random()*data.photos.photo.length)];
		var id=item.id;
		var title = item.title;

			$.getJSON("http://api.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key=ff3313a50f5a26c74bea5c7fff8c0394&photo_id=" + id + "&format=json&jsoncallback=?", function(data) {
				item = data.sizes.size[2];
				source = item.source;
				url = item.url.split("sizes/s/");
				height = item.height;

				a = document.createElement('a');
		
				img = document.createElement('img');
				img.src = source;
				img.alt = title;
				img.title = title;

				$("#iFlickrCont").css("height", height);

				document.getElementById("iFlickrCont").appendChild(a);
				a.setAttribute("href", url[0]);
				a.appendChild(img);

				$("#iFlickrCont").onImagesLoad({
					callback: function() {
						$("#iFlickrBox").css("background", "none");
 						$("#iFlickrCont").slideDown("slow");
					}
				});
			});

	});
});


/* ---------------------------------
*    Youtube
*/

/* お気に入り再生用 */

$(document).ready( function() {

	$.getJSON("http://gdata.youtube.com/feeds/api/users/kapirabasu/uploads?max-result=50&alt=json-in-script&callback=?", function(data) {

        var entries = data.feed.entry;
        var url = [];
        
		$.each(entries, function(i, item){
            url.push(entries[i].media$group.media$player[0].url.substr(31,11));
		});
        
        var src= url[Math.floor(Math.random()*url.length)];
        var media_url = "http://www.youtube.com/v/" + src + "&hl=ja_JP&fs=1" + "&showinfo=0&rel=0";

/*
        $("<div/>")
        .append($("<embed/>")
        .attr("src", media_url)
        .attr("wmode", "transparent")
        .attr("type", "application/x-shockwave-flash")
        .attr("allowscriptaccess", "always")
        .attr("allowfullscreen", "true")
        .attr("height", "200")
        .attr("width", "240")
        )
*/

        $("<iframe/>")
        .addClass("youtube-player")
        .attr("type", "text/html")
        .attr("height", "200")
        .attr("width", "240")
        .attr("src", media_url)
        .attr("frameborder", "0")

        .click(function(){window.open(url, null)})
        .appendTo("#iTubeCont");

		$("#iTubeBox").css("background", "none");
		$("#iTubeCont").slideDown("slow");
        
	});
});


