/* Gallery for More than Words */
(function() {

var jsPath = '';

if(typeof document.scripts != 'undefined') {
	jsPath = document.scripts[document.scripts.length-1].src;
} else {
	var srcArr = $('head script');
	jsPath = srcArr[srcArr.length-1].src;
}

if(jsPath.lastIndexOf('/')!=-1) {
  jsPath = jsPath.substring(0, jsPath.lastIndexOf('/')+1);
}

jQuery.fn.picasaSlideshow = function(params) {

	var SIZES=[72,144,200,320,400,512,576,640,720,800, 912,1024,1152,1280,1440,1600];

	if(typeof params.picasaId != 'undefined' && typeof params.albumId != 'undefined') {
		if(typeof params.height == 'undefined') {
			params.height = 320;
		}
		
		if(typeof params.width == 'undefined') {
			params.width = 400;
		}
		
		if(typeof params.time == 'undefined') {
			params.time = 7000;
		}
		
		if(typeof params.fadeSpeed == 'undefined') {
			params.fadeSpeed=4000;
		}
		
		if(typeof params.slideshow == 'undefined') {
			params.slideshow=true;
		}

		if(typeof params.thumbWidth == 'undefined') {
			params.thumbWidth = 100;
		}
		
		if(typeof params.thumbHeight == 'undefined') {
			params.thumbHeight = 100;
		}

		var landscapeSize;
		var portraitSize;
		var landscapeThumbSize;
		var portraitThumbSize;

		for(var s=0; s<SIZES.length; s++) {
			if(SIZES[s]<=params.width) {
				landscapeSize = SIZES[s];
			}
			if(SIZES[s]<=params.height) {
				portraitSize = SIZES[s];
			}
			if(SIZES[s]<=params.thumbWidth) {
				landscapeThumbSize = SIZES[s];
			}
			if(SIZES[s]<=params.thumbHeight) {
				portraitThumbSize = SIZES[s];
			}
			
			if(SIZES[s]>params.width && SIZES[s]>params.height &&
			   	SIZES[s]>params.thumbWidth && SIZES[s]>params.thumbHeight) {
				break;
			}
		}

		var THIS = this;
		$(this).empty();
		
		$(this).append("<p style='text-align:center;'><img src='"+jsPath+"/images/ajax-loader.gif' class='picasaSlideshow_loading' /></p>");

		var url = 'http://picasaweb.google.com/data/feed/api/user/' + params.picasaId + '/albumid/'+params.albumId+'?alt=json-in-script&callback=?';
		jQuery.ajaxSettings.cache = true;
		$.getJSON(url, function(data) {
			$(THIS).empty();
			var urls  =[];
			var thumbs=[];
			for(var i=0; i< data.feed.entry.length; i++) {

			var url=data.feed.entry[i].content.src;
				var gphotoWidth=parseInt(data.feed.entry[i].gphoto$width.$t,10);
				var gphotoHeight=parseInt(data.feed.entry[i].gphoto$height.$t,10);
				var lastSlash = url.lastIndexOf('/');
				
				var isPortrait = (gphotoHeight>gphotoWidth || (gphotoHeight==gphotoWidth && params.width>params.height));
				var size = (isPortrait?portraitSize:landscapeSize);
				var thumbSize = (isPortrait?portraitThumbSize:landscapeThumbSize);
				var adjustedUrl = url.substring(0, lastSlash+1) + 's' + size + url.substring(lastSlash);
				
				if(isPortrait) {
					var h = (params.height-portraitSize)/2;
					var ratio=portraitSize/gphotoHeight;
					var width=gphotoWidth*ratio;
					var w=(params.width-width)/2;
					var info = {'pv':Math.round(h), 'ph':Math.round(w), 'url':adjustedUrl, 'w':width, 'h':portraitSize};
					urls.push(info);
				} else {
					var w = (params.width-landscapeSize)/2;
					var ratio=landscapeSize/gphotoWidth;
					var height=gphotoHeight*ratio;
					var h=(params.height-height)/2;
					var info = {'pv':Math.round(h), 'ph':Math.round(w), 'url':adjustedUrl, 'w':landscapeSize, 'h':height};
					urls.push(info);
				}
				
				if(params.slideshow==false) {
					var thumbUrl = url.substring(0, lastSlash+1) + 's' + thumbSize + url.substring(lastSlash);
					if(isPortrait) {
						var h = (params.thumbHeight-portraitThumbSize)/2;
						var ratio=portraitThumbSize/gphotoHeight;
						var width=gphotoWidth*ratio;
						var w=(params.thumbWidth-width)/2;
						var info = {'pv':Math.round(h), 'ph':Math.round(w), 'url':thumbUrl, 'w':width, 'h':portraitThumbSize};
						thumbs.push(info);
					} else {

						var w = (params.thumbWidth-landscapeThumbSize)/2;
						var ratio=landscapeThumbSize/gphotoWidth;
						var height=gphotoHeight*ratio;
						var h=(params.thumbHeight-height)/2;
						var info = {'pv':Math.round(h), 'ph':Math.round(w), 'url':thumbUrl, 'w':landscapeThumbSize, 'h':height};
						thumbs.push(info);
					}
				}
			}

			
			
			if(params.slideshow==true) {
				THIS.each(function() {
					var pictureNo=0;
					
					
					
					$(this).attr('style', 'width:'+params.width+'px;height:'+params.height+'px;overflow:hidden;border:1px solid black;background:black;');
					$(this).empty();
					$(this).append('<div class="slideshow_inner" style="width:'+params.width+'px;height:'+params.height+'px;overflow:hidden; position: relative;"></div>');
			
					var html_a = '<div style="width:'+params.width+'px;height:'+params.height+'px;overflow:hidden;position:absolute;top:0; left:0;';
					var first=true;
			
					for(var i=0; i<urls.length; i++) {
						var html;
						if(first) {
							html=html_a;
							first=false;
						} else {
							html=html_a+'display:none;';
						}

						html=html+'"><img src="'+urls[i].url+'" style="padding:'+urls[i].pv+'px '+urls[i].ph+'px;"/></div>';
						$('div.slideshow_inner',this).append(html);
					}
					
					var pics = $('div.slideshow_inner div',this);
					
					window.setInterval(function() {
					
						var currentPic = pics[pictureNo];
						var nextPicNo = pictureNo+1;
						if(nextPicNo==pics.length) nextPicNo=0;
						var nextPic = pics[nextPicNo];
						pictureNo=nextPicNo;
						$(currentPic).fadeOut(params.fadeSpeed);
						$(nextPic).fadeIn(params.fadeSpeed);
					
					}, params.time);
					
					
				});
			} else {
				if(typeof Shadowbox != 'undefined') {
					Shadowbox.clearCache();
				} 
				
				THIS.each(function() {
					for(var i=0; i<urls.length; i++) {
						var html = '<a href="'+urls[i].url+'" style="margin:10px; width:'+(params.thumbWidth+10)+'px;height:'+(params.thumbHeight+10)+'px; display:block; float:left; overflow:hidden;"><img src="'+thumbs[i].url+'" alt="" class="gallery" width="'+thumbs[i].w+'" height="'+thumbs[i].h+'" style="border:5px solid #fff; padding:'+thumbs[i].pv+'px '+thumbs[i].ph+'px;"/></a>';
						$(this).append(html);
					}
					
					if(typeof Shadowbox != 'undefined') {
						Shadowbox.setup(['a', this], 
										{
											player: "img",
											gallery: "Gallery"
										});
					}
				});
				
				if(typeof Shadowbox == 'undefined' && typeof jQuery.slimbox != 'undefined') {
					$('a', THIS).slimbox();
				}
			
			}
			
		});
		
		jQuery.ajaxSettings.cache = false;
		return jQuery;
		
	}

}


})();


