// JavaScript Document
$().ready(function(){
	catching_rating();	
	catching_favorite();
	$("#favbox").css("display","block");
	catching_signin();
});

function catching_signin(){
	$("#open_signin").click(function(){
		if($("#signin_bottom_box").size() == 0){
			var cu = $("link[rel=currentpage]").attr("href");
			$.get(basepath+"process.php?action=get_signin&cu="+cu,function(data){
				$("#unregistered_advise").html(data);
				$("#unregistered_advise").hide();
				$("#unregistered_advise").fadeIn("slow");
			});			
		}
		return false;
	});
}

function catching_rating(){
	$("#your-rate a").bind("mouseover",function(){
		if($("#your-rate .current-rating").size() > 0){
			$("#your-rate .current-rating").hide();											
		}//end if
	});
	$("#your-rate a").bind("mouseout",function(){
		if($("#your-rate .current-rating").size() > 0){
			$("#your-rate .current-rating").show();											
		}//end if
	});
	$("#your-rate a").click(function(){									
		//check the class name
		var className = $(this).attr("class");
		switch(className){
			case "one-star":
				stars = 1;
				break;
			case "two-stars":
				stars = 2;
				break
			case "three-stars":
				stars = 3;
				break
			case "four-stars":
				stars = 4;
				break
			case "five-stars":
				stars = 5;
				break
		}		
		
		if($("#your-rate .current-rating").size() > 0){
			$("#your-rate .current-rating").css("width",(18*stars)+"px");
			$("#your-rate .current-rating").html('Currently '+stars+'/5 Stars.');
		} else {
			$("#your-rate li:first").before('<li class="current-rating" style="width: '+(18*stars)+'px">Currently '+stars+'/5 Stars.</li>');
		}//end if
		
		var enc_data = $("link[rel=pageinfo]").attr("href");		
		$.get(basepath+'process.php?action=send_rating&adata='+enc_data+'&vote='+stars, function(success){																									
			if(success == "ok"){
			//	alert("done");
			} else {
				//alert("problems: "+success);
			}//end if
		});
		//alert($("#your-rate li:first").html());
		return false;
	});
}

function catching_favorite(){
	$("#fav-saver").click(function(){
		//check the class name
		var className = $(this).attr("class");
		
		var enc_data = $("link[rel=pageinfo]").attr("href");
		$.get(basepath+'process.php?action=save_fav&adata='+enc_data, function(success){																								
			if(success == "ok"){
				if(className == "is-favorite"){
					$("#fav-saver").attr("class","mark-favorite");	
					$("#fav-saver").html("Save as a favorite");
				} else {
					$("#fav-saver").attr("class","is-favorite");
					$("#fav-saver").html("Remove from favorites");
				}
			} else {
				//alert("problems: "+success);
			}//end if
		});
		//alert($("#your-rate li:first").html());
		return false;
	});
}