﻿var is_index = false;

function init(){
	$(".res").each(function(){
		$(this).html($(this).text().replace("x", "<span class=\"x\">x</span>"));
	});
	//
	if(is_index){
		$(".thumb").click(gotoImage);
		//$("#grid .thumb .image").fadeTo(0, 0);
		//$("#grid .thumb .image img").load(fade_in);
		$(".my_resolution").click(my_resolution);
		$(".thumb").each(show_rating);
		var loc = ""+window.location+""
		loc = loc.substring(loc.lastIndexOf("/")+1, loc.length);
		if(loc == "" || loc == "?o="){
			$("#menu .latest").addClass("selected");
		}else if(loc == "?o=down"){
			$("#menu .downloads").addClass("selected");
		}else if(loc == "?o=vote"){
			$("#menu .votes").addClass("selected");
		}else if(loc == "?o=view"){
			$("#menu .views").addClass("selected");
		}else if(loc.indexOf("?res=") >= 0){
			$("#menu .my_resolution").addClass("selected");
		}
	}else{
		$(".download_link").click(download_image);
		$(".return_gallery").click(go_back);
		$("#large .image img").load(large_loaded);
		$(".star").mouseover(mouseover_rating);
		$("#large .ctrls .right").mouseout(return_default_rating);
		$(".star").click(rate_image);
		$(".share_bro").click(share);
		return_default_rating();
	}
	$(".nav").mouseover(over_nav);
}
//
function large_loaded(){
	/*
	var speed = 300;
	//$(this).fadeTo(0, 0);
	$(this).width(1000);
	$(this).removeClass("hide");
	$//(this).fadeTo(speed, 1);
	$(".image").animate({
		height:$(this).height()
	}, speed);
	$(".image").click(download_image);
	*/
}
//
function gotoImage(){
	window.location = "image.php?id="+$(this).children(".details").children(".id").html()
}
//
function my_resolution(){
	var res = ""+screen.width+'x'+screen.height+""
	window.location = "?res="+res;
	return false
}
//
function go_back(){
	history.go(-1) 
	return false;
}
//
function download_image(){
	window.location = "./download.php?id="+$(".info .id").text();
	return false;
}
//
function fade_in(){
	//$(this).parent().fadeTo(50, 1);
}
//
function mouseover_rating(){
	var n = Number($(this).attr("id").replace("star_", ""));
	for(var i = 1; i < 6; i++){
		if(i > n){
			//$("#star_"+i).fadeTo(0, .3);
			$("#star_"+i).addClass("faded_star");
		}else{
			//$("#star_"+i).fadeTo(0, 1);
			$("#star_"+i).removeClass("faded_star");
		}
	}
}
//
function return_default_rating(){
	var rating = Number($(".info .rating").text())
	//alert(rating);
	for(var i = 0; i<6; i++){
		if(i > rating){
			$("#star_"+i).addClass("faded_star");
		}
	}
}
//
function rate_image(){
	var rating = $(this).attr("id").replace("star_","");
	$(".rating_machine").remove();
	$(".right .rating").text("Thanks for rating.");
	$.post("../vote.php", {
		aid:$(".info .id").text(),
		v:rating
		}, function(d){
			//window.location.reload(true);
			//		
	})
	return false;
}
//
function show_rating(){
	var rating = Number($(this).children(".details").children(".rating").html());
	var id = $(this).attr("id").replace("thumb_", "");
	if(is_index){
		for(var i = 1; i<6; i++){
			var star = $("#thumb_"+id+" .star_"+i);
			if(i > rating){
				star.addClass("faded_index");
			}
		}
	}
}
//
function share(){
	window.location = "http://www.facebook.com/share.php?u="+urlencode(window.location);
	return false;
}
//
function urlencode(str) {
	return escape(str).replace('+', '%2B').replace('%20', '+').replace('*', '%2A').replace('/', '%2F').replace('@', '%40');
}
//
function over_nav(){
	trace("?");
	//$(".nav").unbind("mouseover", over_nav);
	//$(".nav").mouseout(out_nav);
	//out_nav();
}
//
function out_nav(){
	trace("out_nav");
	$(".menu").fadeTo(50, 0);
}
//
$(document).ready(init);