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;
ajaxgallery.prototype.backtoAlbums=backtoAlbums;
ajaxgallery.prototype.echoAlbumTitle=echoAlbumTitle;

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();
	
	var urlAlbum,id_begin,id_end,idAlbum,jsonImage,title,entry,strHtml;
	
	var contador=1;
	for ( i = 0, r = 0, c = 0; i < pwaFetch ; ++i){
		
		//obtenim item
		entry = feed.entry[i];
			
		//obtenim el nom de l'album
		title = entry.title.$t;
		
		//url imatge
		jsonImage = entry.media$group.media$content[0].url;
		
		//id album
		id_begin = entry.id.$t.indexOf('albumid/')+8;
  		id_end = entry.id.$t.indexOf('?');
  		idAlbum = entry.id.$t.slice(id_begin, id_end);
  		
  		//url album
  		urlAlbum=this.configs.pagAlbum+idAlbum;
		
  		//generem el tag del link i de la imatge
		strHtml = "<a href=\""+urlAlbum+"\" title=\""+title+"\"><img class='wp_img' src='"+jsonImage + "?imgmax=" + pwaImageSize + "&amp;crop=1' alt=\""+title+"\"></a>";
		strHtml+= "<div class='wp_title'><a href=\""+urlAlbum+"\">" + title + "</a></div>";
		
		//fixem el contingut de la cel.la
		document.getElementById('cella_'+contador).innerHTML = strHtml;
		contador++;
		
		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 p = (typeof(p)=='undefined') ? 0 : 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;
	var entry,jsonImage,title,url,url2,tr,td,strHtml;
	
	var contador = 1;
	for ( i = p , r = 0, c = 0; i < pwaFetch ; ++i){
		
		//obtenim l'item
		entry = feed.entry[i];
		
		//obtenim el titol de la imatge
		title = entry.title.$t;
		
		//obtenim la url de la imatge
		jsonImage = entry.media$group.media$content[0].url;
		
		url = jsonImage + "?imgmax=" + 640;
		url2 = jsonImage + "?imgmax=" +  pwaImageSize+"&amp;crop=1";
		
		//generem el tag del link i de la imatge
		strHtml ="<a href=\""+url+"\" class='thickbox' rel='gallery-album' title='"+title+"'><img class='wp_img' src='"+ url2 + "' alt=\""+title+"\"></a>";
		strHtml+="<div class='wp_title'>" + title + "</div>";
		
		//fixem el contingut de la cel.la
		document.getElementById('cella_'+contador).innerHTML = strHtml;
		contador++;
		
		if( c == this.configs.cols-1 ){
			if( r >= this.configs.rows-1 )break;
			r++;
			c = 0;
		}else{
			c++;
		}
	}
	
	div.innerHTML  = div.innerHTML;
	div.innerHTML = this.echoAlbumTitle(feed.title.$t)+this.Pagination( pwaFetch, root ) + div.innerHTML;
	div.innerHTML += this.Pagination( pwaFetch, root )+this.backtoAlbums();
	tb_init('a.thickbox');
}


function backtoAlbums(){
	return '<a href="'+this.configs.pagAlbums+'" class="linkAlbums">Ver albums</a>';
};

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'>" + ((pag/npp)+1) + "</a>";
		else
			divn += "<a class='pag' onclick='gallery.showAlbum( gallery.configs.album,"+pag+" )'>" + ((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 );
	
	var contador=1;
	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" );
			td.id='cella_'+contador;
			tr.appendChild( td );
			contador++;
		}

		table.appendChild( tr );
	}
	div.appendChild( table );
	return table;
};

function clean( div ){
	div.innerHTML = "";
};

function echoAlbumTitle(strTitol){
	return '<h3 class="albumTitle">'+strTitol+'</h3>';
}


function removeChildNodes(obj){
	while (obj.hasChildNodes()){
		obj.removeChild(obj.firstChild);
	}
}
