
var is_index = false

function init(){
	if(is_index){
		getPhotosetImages()
		getVideoThumbs()
		$(window).scroll(update);
	}
}

function getPhotosetImages(){
	$(".post_thumb .photoset").each(function(){
		var ID = $(this).parent().attr("id").replace("thumb_", "")
		$.getJSON("/api/read/json?id="+ID+"&callback=?", {}, function(D){
			var photos = D["posts"][0]["photos"]
				var thumb = photos[0]["photo-url-250"]
				$("#thumb_"+ID).html('<img src="' + thumb + '"/>');
				
		})
	})
}

function getVideoThumbs(){
	$(".post_thumb .video").each(function(){
		var swf = "" 
		var ID = $(this).parent().attr("id").replace("thumb_", "")
		
		
		$(this).children("object").children("param").each(function(){
			if($(this).attr("name") == "movie"){
				swf = $(this).attr("value")
			}
		})
		
		if(swf.indexOf("youtube.com") >= 0){        
			// get youtube thumb
			swf = swf.replace("http://www.youtube.com")
			swf = swf.replace("http://youtube.com")
			var youtube_id = swf.substring(swf.indexOf("/v/")+3, swf.indexOf("&"))
			var thumb_url = "http://i.ytimg.com/vi/"+youtube_id+"/0.jpg"
			$("#thumb_"+ID).html('<img src="' + thumb_url + '" width="250"/>');
		}else if(swf.indexOf("vimeo.com") >= 0){
			// get vimeo json, then thumb (SO ANNOYING!)
			swf = swf.replace("http://vimeo.com/moogaloop.swf?")
			swf = swf.replace("http://www.vimeo.com/moogaloop.swf?")      
			var vimeo_id = swf.substring(swf.indexOf("clip_id=")+8, swf.indexOf("&"));
			$.getJSON("http://vimeo.com/api/clip/"+vimeo_id+".json?callback=?", function(d){
				var thumb_url = d[0].thumbnail_large
				$("#thumb_"+ID).html('<img src="' + thumb_url + '" width="250"/>');
			})
		}
		
	})
}

function update(){
	getVideoThumbs()
	getPhotosetImages();
}

$(document).ready(init)
