function album_shareurl(url)
{
	$('album_share_url_value').value="http://"+window.location.host+"/album/"+url+"/";

	var m=new Control.Modal($('album_share_url_modal'),{
		image:false,
		modal:true,
		iframeshim:false,
		opacity:0.5,
		width:700
	});
	m.open();
}

function album_import_facebook(aid)
{
	new Ajax.Request("/albums/ajax_import_facebook.php?aid="+aid,{
		method:'get',
		onSuccess:function(t){
			$('import_facebook_modal_content').update(t.responseText);

			var m = new Control.Modal( $('import_facebook_modal'), {
				image:false,
				modal:false,
				width:700,
				opacity:0.5
			});
			m.open();
		}
	});
}

function album_picturepage(albumid,page,flag)
{
	
	new Ajax.Request("/albums/pictures.php?ajax=1&aid="+albumid+"&p="+page+"&f="+flag,{
		method:'get',
		onSuccess:function(t){
			var json = t.responseJSON;
			
			var ptpl = new Template("<a class=\"img\" href=\"/photos/view.php?aid=#{albumid}#pid=#{photoid}&se=photo\"><img src=\"#{url}\" width=\"#{width}\" height=\"#{height}\" border=\"0\" /></a>");
						
			if(json.pictures.length>0)
			{
				
				var pager = showJsonPager(json.pager,"album_picturepage('"+albumid+"','#{page}','"+flag+"');return false;");
				var content = pager;
				
				for(i=0;i<json.pictures.length;i++)
				{
					content += ptpl.evaluate(json.pictures[i]);
					if( ((i+1)%5)==0)
					{
						content+='<div style="clear:both;"></div>';	
					}
				}
				content+='<div style="clear:both;"></div>';	
				content+=pager;
				
				$('albumpicturesdiv').update(content);
			}
			else
			{
				$('albumpicturesdiv').update('<p>Sorry, but no albums were found.</p>');
			}
		}
	});

}

function album_grid(albumid,page)
{
	new Ajax.Request("/albums/ajax_photogrid.php?aid="+albumid+"&p="+page+"&prevheight="+$('albpic').getHeight(),{
		method:'get',
		onSuccess:function(t){
			var json = t.responseJSON;
			var ptpl = new Template('<a href="/photos/view.php?aid=#{albumid}#pid=#{photoid}&se=photo"><img src="#{url}" width="#{width}" height="#{height}" border="0" /></a>');
			if(json.pictures.length>0)
			{
				$('albumgridpager').update(showJsonPager(json.pager,"album_grid('"+albumid+"','#{page}');return false;"));
				var content = "";
				for(i=0;i<json.pictures.length;i++)
				{
					content += ptpl.evaluate(json.pictures[i]);
				}
				$('albumgriddiv').update(content);
			}
			else
			{
				$('albumgriddiv').update('<p>Sorry, but there are no pictures in this album.</p>');
			}
		}
	});
}




function albumOut(id) {
    $('album'+id+'delete').style.visibility = 'hidden';
}
function albumOver(id) {
    $('album'+id+'delete').style.visibility = 'visible';
    //$('album'+id+'delete').style.display = 'none';
    //new Effect.Appear('album'+id+'delete');
}

function loadAlbumIndexGrid(type,part2,page)
{
	new Ajax.Request("/albums/ajax_index_list.php?type="+encodeURIComponent(type)+"&extra="+encodeURIComponent(part2)+"&p="+page,{
		method:'get',
		onSuccess:function(t){
			var json = t.responseJSON;
			
			var albumtpl = new Template('<div class="album" >'
			+'<a href="/albums/view.php?aid=#{albumid}&se=photo"><img src="#{photourl}" width="#{width}" height="#{height}" border="0" /></a> '
			+'<span class="name"><a href="view.php?aid=#{albumid}">#{name}</a></span><span class="owner">by #{uname}</span></div>');

			var smarttpl = new Template('<div class="album">'
			+'<a href="/photos/view.php?sid=#{smartid}#pid=#{photoid}&se=photo"><img src="#{photourl}" width="#{width}" height="#{height}" border="0" /></a> '
			+'<span class="name"><a href="/photos/view.php?sid=#{smartid}#pid=#{photoid}">#{name}</a></span></div>');
			
			if(json.albums.length>0)
			{
				var pager = showJsonPager(json.pager,"loadAlbumIndexGrid('"+type+"','"+part2+"','#{page}');return false;");
				var content = pager;
				content += '<div class="albumrow clearfix">';
				for(i=0;i<json.albums.length;i++)
				{
					content += albumtpl.evaluate(json.albums[i]);
				}
				content+=pager;
				content+="</div>";
				$('myalbumsarea').update(content);
			}
			else if(json.smartalbums.length>0)
			{
				var content = '<div class="albumrow clearfix">';
				
				for(i=0;i<json.smartalbums.length;i++)
				{
					content += smarttpl.evaluate(json.smartalbums[i]);
					if((i+1)%4==0)
					{
						content += '</div><div class="albumrow clearfix">';
					}
				}
				content+="</div>";
				$('myalbumsarea').update(content);
			}
			else
			{
				$('myalbumsarea').update('<p>Sorry, but no albums were found.</p>');
			}
		}
	});
}
function loadAlbumIndexPhotoGrid(type,part2,page)
{
	show_advert('alb_advert',160,600);
	new Ajax.Request("/photos/ajax_photo_grid.php?type="+encodeURIComponent(type)+"&extra="+encodeURIComponent(part2)+"&p="+page,{
		method:'get',
		onSuccess:function(t){
			var json = t.responseJSON;
			
			if(type=="smartalbum")
			{
				var picturetpl = new Template('<div class="album"><img src="/static/images/delete_album.png" class="album-delete-image" /><a href="/photos/view.php?sid='+part2+'#pid=#{photoid}"><img src="#{photourl}" width="#{width}" height="#{height}" border="0" /></a></div>');
			}
			else
			{
				var picturetpl = new Template('<div class="album"><a href="/photos/view.php?pid=#{photoid}"><img src="#{photourl}" width="#{width}" height="#{height}" border="0" /></a></div>');
			}
			
			
			if(json.photos.length>0)
			{
				
				var pager = showJsonPager(json.pager,"loadAlbumIndexPhotoGrid('"+type+"','"+part2+"','#{page}');return false;");
				var content = pager;
				content += '<div class="albumrow clearfix">';
				
				for(i=0;i<json.photos.length;i++)
				{
					content += picturetpl.evaluate(json.photos[i]);
					if((i+1)%4==0)
					{
						content += '</div><div class="albumrow clearfix">';
					}
				}
				
				content+=pager;
				
				content+="</div>";
				
				$('myalbumsarea').innerHTML = content;
			}
			else
			{
				$('myalbumsarea').innerHTML = '<p>Sorry, but no albums were found.</p>';
			}
		}
	});
}
function createAlbumAndUpload(albname)
{
	new Ajax.Request('/albums/indexcreate.php',{
		method:'post',
		postBody:'name='+encodeURIComponent(albname),
		onSuccess:function(t){
			var json = t.responseJSON;
			if(json.error=="0")
			{
				goURI('/upload/upload.php?aid='+json.albumid);
			}
			else
			{
				alert("There was a problem creating your album");
			}
		}
	});
}
function doAlbumSearch()
{
	$$('input.albselradio').each(function(e){
		$(e).checked = false;
	});
	
	loadAlbumIndexGrid('search',$('searchquery').value,1);
}


