

$(document).ready(function(){

    $(".user_name").each(function(){
	$(this).attr("title","User:" + $(this).text() );
    });

    $(".button_big").hover(function(){
	$(this).css({ backgroundColor : "gray" });
    } ,function(){
	$(this).css({ backgroundColor : "white" });
    });


    $("#paginate a").hover(function(){
	$(this).css({ backgroundColor : "gray",color: "white" });
    } ,function(){
	$(this).css({ backgroundColor : "white",color: "blue"});
    });



    var original_tag_color = $(".dash_list div.desc a").eq(0).css("color");
    var original_noedit_tag_color = $(".dash_list div.desc_noedit a").eq(0).css("color");
    $(".dash_list").hover(function(){
	$(this).find(".desc").css({ color : "#000" });
	$(this).find("div.desc a").css("color","#0000ff")
	$(this).find("div.desc_noedit a").css("color","#0000ff");
	$(this).find(".date").show().css({opacity : 0.5 });
    } ,function(){
	$(this).find(".desc").css({ color : "#666" });
	$(this).find("div.desc a").css("color", original_tag_color);
	$(this).find("div.desc_noedit a").css("color", original_noedit_tag_color);
	$(this).find(".date").hide();
    });

    var colors = ["label_yellow","label_pink","label_blue","label_green","label_purple"];

    $(".dash_list .color_changer").each(function(){
	var id = $(this).find("._id_").eq(0).text();
	var hide = function(){
	    $(".color_changer_options").remove();  
	};
	var $parent = $(this).parent();
	$(this).hover(function(){
	    var $that = $(this);
	    for(var i = 0; i < colors.length ; i ++){
		(function(){
		    var color = colors[i];
		    $("<span></span>").addClass("color_changer_options").html("&nbsp;")
		    .addClass(color).appendTo($that)
		    .click(function(){
			var $loading = $("<img></img>").attr("src","/images/loading.gif").insertAfter($(this));
			$.each(colors,function(){
			    $parent.removeClass(this);
			});
			$parent.addClass(color);
			$.get("/photos/update_color/" + id,
			      { "color" : color },
			      function(){  $loading.remove(); });
			hide();
		    });
		})();
	    }
	    
	    // to remove
	    $("<span></span>").addClass("color_changer_options").html("&nbsp;")
	    .css({ border: "solid 1px #eee"}).appendTo($that)
	    .click(function(){
		var $loading = $("<img></img>").attr("src","/images/loading.gif").insertAfter($(this));
		$.each(colors,function(){ $parent.removeClass(this); });
		$.get("/photos/update_color/" + id,
		      { "color" : '' },
		      function(){  $loading.remove(); });
		hide();
	    });
	    
	},function(){
	    hide();
	});
    });
    
    $(".label_box").click(function(){
	var $that = $(this);
	$.each(colors,function(){
	    var color = this;
	    if( $that.hasClass(color)){
		$(".dash_list").each(function(){
		    if($(this).hasClass(color) ){
			$(this).show();
		    }else {
			$(this).fadeOut("slow");
		    }
		});
	    }
	});
	$(".label_all").css("display","inline");
	$(".label_box").css("border",null);
	$(".label_name").css("font-weight",null);
	$(this).css("border","solid 2px black").next().css("font-weight","bold");
    });

    $(".label_all").click(function(){
	$(".dash_list").show();
	$(".label_box").css("border",null);
	$(".label_name").css("font-weight",null);
	$(this).hide();
    });

    $(".label_name").click(function(){
	var original_text = $(this).text();
	var color = $(this).attr("id").match(/\w+\[(\w+)]/)[1]
	$(this).text("");
	var $that = $(this);
	var def_val = '';
	if(original_text != '...'){
	    def_val = original_text;
	}
	$("<input type='text' size='20'></input>").val(def_val).appendTo($(this)).focus()
	.blur(function(){
	    var val = $(this).val();
	    if(val.length > 0 && val != original_text){
		$that.text(val);
		var $loading = $("<img></img>").attr("src","/images/loading.gif").insertAfter($that);
		$.get("/users/update_color_label/" + color,
		      { label : val },
		      function(){ $loading.remove() });
	    } else {
		$that.text(original_text);
	    }
	    $(this).remove();
	});
    });

		  
    $("#magnify_plus").click(function(){
	$(".dash_list img").each(function(){ $(this).width( $(this).width() * 1.2 )});
	$(".dash_list div.square").each(function(){ $(this).width( $(this).width() * 1.2 )});
	$(".dash_list").each(function(){ 
	    $(this).width( $(this).width() * 1.2 )
	    $(this).height( $(this).height() * 1.2 )
	});
    });

    $("#magnify_minus").click(function(){
	$(".dash_list img").each(function(){ $(this).width( $(this).width() * 0.8 )});
	$(".dash_list div.square").each(function(){ $(this).width( $(this).width() * 0.2 )});
	$(".dash_list").each(function(){ 
	    $(this).width( $(this).width() * 0.8 )
	    $(this).height( $(this).height() * 0.8 )
	});
    });

});