/*****************************************************************
	jQuery Picasa Gallery
	Version: 1.0
	
	Author: Michael King
	
	Requires: jquery.EmbedPicasaGallery.js
*****************************************************************/

pGalleryAutos = [];

$(document).ready(function() {
	var pGalleryCount = 0;
	$(".pGallery").each(function() {
		// run embedder
		$(this).attr('id','pGallery-'+ pGalleryCount);
		pGalleryCount++;
		jQuery(this).EmbedPicasaGallery($(this).attr('data-user'),{
			albumid: $(this).attr('data-albumid'),
			size: 64,  // thumb size (32,48,64,72,144,160)) 
		});
	});
});

/* Switches the image that is displayed */
function pGalleryActivate(thumbLink) {
	/* get URL from clicked thumb */
	var url = $(thumbLink).attr('href');
	var gallery = $(thumbLink).parents('.pGallery');
	var albumList = $(thumbLink).parents('.album-list');
	var viewer = $(gallery).children('.viewer');
	
	/* find photo and set to active, also set thumb to active and remove all other actives */
	$('li.active',viewer).fadeOut('fast', function() {
		$(viewer).find('li').removeClass('active').hide();
		$('img[src="'+url+'"]',viewer).parents('li').fadeIn('fast').addClass('active');
	});
	$(albumList).find('a').removeClass('active');
	$('a[href="'+url+'"]',albumList).addClass('active');
	pGalleryMoveAlbumList(thumbLink);
}

/* Calculates width of album list */
function pGalleryListWidth(albumList) {
	var width = 0;
	$('li', albumList).each(function() {
		width += $(this).outerWidth(true);
	});
	$('ul', albumList).css("width",width);
}

/* Slides album list left and right */
function pGalleryMoveAlbumList(thumbLink, scroll) {
	thumbnail = {};
	thumbnail.el = thumbLink;
	thumbnail.container = $(thumbnail.el).parent('li');
	thumbnail.width = $(thumbnail.container).outerWidth(true);
	thumbnail.pos = $(thumbnail.container).index();
	
	albumList = {};
	albumList.el = $(thumbnail.el).parents('ul');
	albumList.viewport = $(albumList.el).parents('.album-list').width();
	albumList.pos = parseInt( ($(albumList.el).css('left')).substring(0,($(albumList.el).css('left')).length - 2));
	
	var offset = ((thumbnail.pos + 1) * thumbnail.width) - (albumList.viewport - albumList.pos);
	var newPos = -1;
	var steps = Math.floor((thumbnail.width * thumbnail.pos)/albumList.viewport);
	var perView = Math.floor(albumList.viewport/thumbnail.width);
	
	if(scroll) {
		if(scroll == "left") {
			newPos = (albumList.pos + albumList.viewport) > 0 ? 0 : albumList.pos + albumList.viewport;
		}
		else {
			newPos = (albumList.viewport - albumList.pos) > ($(albumList.el).width() - albumList.viewport) ? (0 - ($(albumList.el).width() - albumList.viewport)) : albumList.pos - albumList.viewport;
		}	
	}
	
	else {
		if(thumbnail.pos == 0 || thumbnail.pos < perView) {
			newPos = 0;
		}
		else if($(thumbnail.container).siblings().length == thumbnail.pos || ($(thumbnail.container).siblings().length - thumbnail.pos) < perView) {
			newPos = (0 - (($(thumbnail.container).siblings('li').length + 1) * thumbnail.width) + albumList.viewport);
		}
		else if ((thumbnail.width - offset) > albumList.viewport) {
			newPos = (0 - ((thumbnail.pos + 1) * thumbnail.width)) + albumList.viewport;
		}
		else if (offset >= 0) {
			newPos = (0 - (thumbnail.pos * thumbnail.width));
		}
	}
	if(newPos != -1 && newPos != albumList.pos) {
		$(albumList.el).animate({ left: newPos }, 1000);
	}
}

/* Moves to next/prev image */
function pGalleryNextPrev(dir,albumList) {
	var active = $(albumList).find("a.active");
	var activeParent = $(active).parent('li');
	if(dir == "next") {
		if($(activeParent).next().length != 0) {
			pGalleryActivate($(activeParent).next().find('a'));
		}
		else {
			pGalleryActivate($("a:first-child",albumList));
		}
	}
	if(dir == "previous") {
		if($(activeParent).prev().length != 0) {
			pGalleryActivate($(activeParent).prev().find('a'));
		}
		else {
			pGalleryActivate($("a:last-child",albumList));
		}
	}
}

/* Plays/Pauses */
function pGalleryPlayPause(op, gallery) {
	if(op == "play") {
		clear = window.clearInterval(pGalleryAutos[$(gallery).attr('id')]);
		pGalleryAutos[$(gallery).attr('id')] = setInterval(function() {
			$('a.next',gallery).click();
		}, 4000);
		$('.playpause',gallery).find('a').removeClass('active');
		$('.playpause a.play',gallery).addClass('active');
	}
	else if(op == "pause") {
		clear = window.clearInterval(pGalleryAutos[$(gallery).attr('id')]);
		$('.playpause',gallery).find('a').removeClass('active');
		$('.playpause a.pause',gallery).addClass('active');
	}
}

function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}
