/**
File Name: gallery.js
Plugin Name: ajaxGallery
Plugin URI: http://www.laciudadx.com/trabajos/ajaxgallery
Description: Ajax + Picasa + Thickbox
Author: Sergio Ceron Figueroa
Version: 1.0
Author URI: http://www.laciudadx.com

Copyright (C) 2007 Sergio Ceron F. (http://laciudadx.com)
    
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

function initAlbums( root ){
	gallery.configs.albums = root;
}

function ajaxgallery( config ){
	this.configs = config;
};
ajaxgallery.prototype.showAlbums = showAlbums;
ajaxgallery.prototype.showAlbum = showAlbum;
ajaxgallery.prototype.createTable = createTable;
ajaxgallery.prototype.clean = clean;
ajaxgallery.prototype.loadscript = loadscript;
ajaxgallery.prototype.Pagination=Pagination;

function showAlbums(root){
	if( root ){	root = root;this.configs.albums = root;
	}else{root = this.configs.albums;}
	var feed = root.feed; 
	var entries = feed.entry || [];
	var pwaFetch = feed.entry.length;
	var pwaImageSize = 160;
	var div = document.getElementById( this.configs.id );
	var table = this.createTable();
	for ( i = 0, r = 0, c = 0; i < pwaFetch ; ++i){
		var entry = feed.entry[i];
		var title = entry.title.$t;
		var jsonImage = entry.media$group.media$content[0].url;
		var link = entry.link[1].href;
		var tr = table.getElementsByTagName("tr")[ r ];
		var td = tr.getElementsByTagName( "td" )[ c ];
		td.innerHTML= "<img class='wp_img' src='"+jsonImage + "?imgmax=" + pwaImageSize + "&crop=1' onclick=\"gallery.loadscript( '" + entry.id.$t + "')\">";
		td.innerHTML+= "<div class='wp_title'>" + title + "</div>";
		if( c == this.configs.cols-1 ){
			if( r >= this.configs.rows-1 )break;
			r++;
			c = 0;
		}else{
			c++;
		}
	}
	div.innerHTML  = div.innerHTML;
};

function showAlbum( root, p ){
	var feed = root.feed; 
	var entries = feed.entry || [];
	var pwaFetch = feed.entry.length;
	var pwaImageSize = 160;
	var div = document.getElementById( this.configs.id );
	var table = this.createTable();
	this.configs.album = root;
	this.configs.pag = (p?p:0);
	for ( i = (p?p:0), r = 0, c = 0; i < pwaFetch ; ++i){
		var entry = feed.entry[i];
		var title = entry.title.$t;
		var jsonImage = entry.media$group.media$content[0].url;
		var link = entry.link[1].href;
		var tr = table.getElementsByTagName("tr")[ r ];
		var td = tr.getElementsByTagName( "td" )[ c ];
		var url = jsonImage + "?imgmax=" + 640;
		var url2 = jsonImage + "?imgmax=" +  pwaImageSize;
		td.innerHTML= "<a href=\""+url+"\" class='thickbox' rel='gallery-album' title='"+entry.media$group.media$description.$t+"'><img class='wp_img' src='"+ url2 + "'></a>";
		td.innerHTML+= "<div class='wp_title'><a href='"+link+"'>" + title + "</a></div>";
		td.innerHTML+= "<div class='wp_date'>" + entry.updated.$t + "</div>";
		if( c == this.configs.cols-1 ){
			if( r >= this.configs.rows-1 )break;
			r++;
			c = 0;
		}else{
			c++;
		}
	}
	
	div.innerHTML  = div.innerHTML;
	div.innerHTML = backtoAlbums()+this.Pagination( pwaFetch, root ) + div.innerHTML;
	div.innerHTML += this.Pagination( pwaFetch, root )+backtoAlbums();
	tb_init('a.thickbox, area.thickbox, input.thickbox');
};

function backtoAlbums(){
	return "<div align='center'><a class='linkw' onclick='gallery.showAlbums();'>Retour aux albums</a></div>"
};

function Pagination( n, root ){
	var npp = this.configs.cols * this.configs.rows;
	var divn = "<div class='pagination'>";
	for( pag = 0; pag < n; pag += npp ){
		if( pag == this.configs.pag )
			divn += "<a class='pagsel'> Page " + ((pag/npp)+1) + "</a>";
		else
			divn += "<a class='pag' onclick='gallery.showAlbum( gallery.configs.album,"+pag+" )'> Page " + ((pag/npp)+1) + "</a>";
	}
	return (divn + "</div>");
};

function loadscript( source ){
	var script = document.createElement('script');
	script.type = 'text/javascript';
	script.src = source.replace( "entry", "feed" ) + "?alt=json-in-script&callback=gallery.showAlbum&kind=photo";
	document.getElementsByTagName('head')[0].appendChild(script);  
};

function createTable(){
	div = document.getElementById( this.configs.id );
	this.clean( div );
	var table = document.createElement( "table" );
	table.setAttribute( "align", this.configs.aligntable );
	for( row = 0; row < this.configs.rows; row++ )
	{
		var tr = document.createElement( "tr" );
		for( col = 0; col < this.configs.cols; col++ )
		{
			var td = document.createElement( "td" );
			tr.appendChild( td );
		}

		table.appendChild( tr );
	}
	div.appendChild( table );
	return table;
};

function clean( div ){
	div.innerHTML = "";
};
