function make_colorbox(obj){
	if($("#photo_thumb").val()=="true"){
		var color_open=false;
		$('#associated-'+obj.id).show();
		$('.album').hide();
	} else {
		var color_open=true;
		$('#associated-'+obj.id).hide();
	}
	$("a[rel='"+obj.id+"']").colorbox({
		open:color_open,
		slideshowAuto:true,
		slideshow:true,
		title:$(obj).children(":first-child").attr("title")
	})  
	$('.hide_img').removeClass('hide_img');
}
var gallery_trail = null;
var album_trail = null;
jQuery(document).ready(function(){
	$('.hide_img').removeClass('hide_img');
	$('.gallery').click(function(){
		if($('#associated-'+this.id).length==1){
			$('#associated-'+this.id).show();
		} else {
			var temp=this; 
			obj=$("<div id=\"associated-"+$('.'+temp.id).id+"\"></div>").load('albums.php',{gallery_id:this.id.substr(8)}, 
			function () { 
				$('.'+temp.id).live('click',function () { 
					get_photos(this);
					return false;
				});
			});		
			$(obj).insertAfter(this);
		}
		$('.gallery').hide();
		gallery_trail = this.id;
		$('#bread_crumb').text("Back to Gallery");
		return false;
	});
	
	$('#bread_crumb').click(function(){
		if (album_trail != null) {
			$('.' + album_trail).show();
			$('.photos').hide();
			album_trail = null;
			if (gallery_trail != null) {
				$('#bread_crumb').text("Back to Gallery");
			} else {
				$('#bread_crumb').text("");
			}
		} else if (gallery_trail != null) {
			$('.gallery').show();
			$('#associated-' + gallery_trail).hide();
			gallery_trail = null;
			$('#bread_crumb').text("");
		}
		return false;
	});
	$('.album').click(function () {
		get_photos(this);
		return false;
	});
});

function get_photos(obj){
		if($('#associated-'+obj.id).length==1){
			make_colorbox(obj);
		} else {
			obj1=$("<div id=\"associated-"+obj.id+"\" class='photos'></div>").load('photos.php',{album_id:obj.id.substr(6), photo_thumb: $("#photo_thumb").val()},function(){ 
				make_colorbox(obj);	 
			});		
			$(obj1).insertAfter(obj);
		}
		if($("#photo_thumb").val()=="true") { 
			album_trail = obj.rel;
			$('#bread_crumb').text("Back to Album");
		}
}
