var is_index = false

function init(){
	$(".rating_thing li").click(vote_for_thing)
	$(".rating_thing").each(show_rating)
	if(is_index){
		$(".categories_drop").click(open_categories) 
		//$(".grid_item .gallery img:last").load(do_grid)
		$("#grid").gridLayout('.grid_item', {col_width:304, min_cols:3});  
		//$("#grid").gridLayout.placeAtEnd("#grid #footer");
		$("#grid").removeClass("hide")
		$("#grid").gridlayout.placeAtEnd("#grid_footer")
	}else{
		$(".header a").click(function(){
			$("#flash").html("");
			$("#flash").addClass("outNow");
		});
	}
}                         
//
function do_grid(){
	$("#grid").removeClass("hide")
	$("#grid").gridLayout('.grid_item', {col_width:309, min_cols:3});  
	//$(".grid_item").click(goto_entry)  	  
}
//
function goto_entry(){                 
	var new_location = "/entry/"+$(this).children(".info").children(".id").html()+"/"+$(this).children(".info").children(".slug").html()+"/"
	window.location = new_location
	//window.location = "/entry/"+$(this).parent().children(".info").children(".id").html()+"/"+$(this).parent().children(".info").children(".slug").html()+"/"
}
//
function vote_for_thing(e){
	var vote = Number($(this).attr("class").replace("vote_", ""));
	var id = $(this).parent().parent().attr("id").replace("vote_", "");
	
	//alert(id)
	//alert(vote)
	//
	$.post("/entry/vote/", {
		id:id,
		vote:vote
	}, function(d){
		rating = Number(d.replace(".0",""));
		update_rating(id, rating);
	});
}
//
function update_rating(id, rating){
	for(var i=1; i<6; i++){
		if(i > rating){
			//$("#"+id+" .rating_thing .vote_"+i).remove()
			$("#"+id).children().find(".vote_"+i).fadeTo(0, .3);
		}else{
			$("#"+id).children().find(".vote_"+i).fadeTo(0, 1);
		}
	}
}
//
function show_rating(){
	var id = $(this).attr("id").replace("vote_", "");
	var rating = Number($("#"+id+" .info .rating").text())
	for(var i=1; i<6; i++){
		if(i > rating){
			//$("#"+id+" .rating_thing .vote_"+i).remove()
			$(this).children().find(".vote_"+i).fadeTo(0, .3);
		}else{
			$(this).children().find(".vote_"+i).fadeTo(0, 1);
		}
	}
}
//    
function open_categories(){
	$(".categories_drop ul").removeClass("hide")
	$(".categories_drop").addClass("drop_open")
	$(".categories_drop").unbind("click", open_categories)
	setTimeout(setup_list, 500)
}
//
function setup_list(){
	$("html").mousedown(close_categories)
}
//
function close_categories(){
	setTimeout(reset_categories, 500)
}
//
function reset_categories(){
	$(".categories_drop ul").addClass("hide")   
    $(".categories_drop").removeClass("drop_open")
	$(".categories_drop").click(open_categories) 
	$("html").unbind("mousedown", close_categories)
}
//
$(document).ready(init)