	function rate(rating, scene_id) {
		$("#rating_" + scene_id + " a").remove();
		$.ajax({
			type: "POST",
			url: "/ajax/rating/",
			data: "id=" + scene_id + "&rating=" + rating,
			success: function (msg) {
				msg = jQuery.trim(msg);
				if (msg == "already_voted")
					message = "You have already voted!";
				else
					message = "Your vote has been recorded, thanks!";
				$("#rating_" + scene_id + " .filled").
					css("width", Math.round(parseInt(msg)) + "%").
					removeClass("filled").
					addClass("voted");
				
				var text = $("#rating_" + scene_id).next();
				var numb = text.html().match(/\d+/);
				if (text.html() == "(Not Rated)")
					text.html("(1 Vote)");
				else
					text.html("(" + (parseInt(numb) + 1) + " Votes)");
				alert(message);
			}
		});
	}
	
	function rollImages(thumb, start) {
		if (start == undefined || start > thumb.images.length - 1)
			start = 0;
		$(thumb).attr("src", thumb.images[start].src);
		thumb.timerID = setTimeout (function() {rollImages(thumb, start + 1);}, 652);
	}
	
	function clearRoll(thumb) {
		clearTimeout(thumb.timerID);
		$(thumb).attr("src", thumb.origimg);
	}
	
	$(function() {
		$("div.tube ul li img[src*=_1.jpg], ul.thumbs li img[src$=_1.jpg]").each(function() {
			if (!$(this).attr("src").match(/sample|promo/) || this.origimg != undefined || $(this).closest("div.ads1").size() > 0) {
				return;
			}
			this.images = new Array();
			this.origimg = $(this).attr("src");
			if (this.origimg != '') {
				var prefix = this.origimg.replace(/(sample|promo)_[0-9]+\.[a-z]+/, "").replace(/302x201/, "222x150");
				for (y = 0; y < 4; y++) {
					this.images[y] = new Image();
					this.images[y].src = prefix + "promo_" + (1 + y * 2) + ".jpg";
				}
				$(this).unbind().hover(
					function() {rollImages(this);},
					function() {clearRoll(this);}
				);
			}
		});
		
		$("input[name=bookmark],a[href*=#bookmark]").click(function() {
			url = document.location.href;
			title = document.title;
			if (window.sidebar) // firefox
				window.sidebar.addPanel(title, url, "");
				else if(window.opera && window.print){ // opera
				var elem = document.createElement('a');
				elem.setAttribute('href',url);
				elem.setAttribute('title',title);
				elem.setAttribute('rel','sidebar');
				elem.click();
			} else if(document.all)		// ie
				window.external.AddFavorite(url, title);
			return false;
		});
		
		// Init tabs
		$("a[href*=#tab_]").click(function() {
			if (!$(this).closest("li").hasClass("active")) {
				tab = this.href.split("#")[1].substring(4);
				$(this).closest("li").siblings().removeClass("active");
				$(this).closest("li").addClass("active");
				$("div[id$=_container]").hide();
				$("#" + tab + "_container").show();
			}
			return false;
		});
		if ($("li.active a[href*=#tab_]").size() == 0)
			$("a[href*=#tab_]:first").click();
		else $("li.active a[href*=#tab_]:first").click();
		
		// Init comments
		$("#comment_form input[type=button]").click(function() {
			form = $("#comment_form");
			scene_id =	$.trim($("input[name=item_id]", form).val());
			text =		$.trim($("textarea[name=comment]", form).val());
			captcha =	$.trim($("input[name=captcha]", form).val());
			name =		$.trim($("input[name=nickname]", form).val());
			post_data = {scene_id: scene_id, message: text, captcha: captcha, username: name};
			
			
			if (text.length > 3 && captcha.length > 0 && name.length > 0) {
				$.post("/ajax/comment/", post_data, function(data) {
						if (data.code == 1)
							form.html("Comment posted, thanks!");
						alert(data.message);
					}, "json");
			} else {
				alert('All the fields must be entered');
				return false;
			}
		});
	});