	function rollImages(thumb, start) {
		if (start == undefined || start > thumb.images.length - 1)
			start = 0;
		thumb.src = thumb.images[start].src;
		thumb.timerID = setTimeout (function() {rollImages(thumb, start + 1);}, 652);
	}
	
	function clearRoll(thumb) {
		clearTimeout(thumb.timerID);
		thumb.src = thumb.origimg;
	}
	
	function rotateNum (num, index, initial) {
		if (index < 1) {
			num.className = initial;
			return;
		}
		cur = parseInt(num.className.substring(8));
		while ((rand = Math.floor(Math.random() * 10)) == cur);
		num.className = "number n" + rand;
		setTimeout(function() {rotateNum(num, index - 1, initial);}, 400/index);
	}
	
	function report_broken( scene_id ) {
	    
	    var ie = false;
	    if( navigator.appName == "Microsoft Internet Explorer" )
	        ie = true;
	    
	    var url = "write_rate/report_broken.php?scene_id=" + scene_id;

	    var width = 560;
	    var height = 300;
	    var x = 0;
	    var y = 0;
	    
	    if( ie ){
	        x = (document.body.clientWidth - width) / 2;
	        y = (document.body.clientHeight - height) / 2;
	    }else{
	        x = (window.outerWidth - width) / 2;
	        y = (window.outerHeight - height) / 2;
	    }

	    var sOWin;
	    var sWinOpts = "align=center,location=no,menubar=no,titlebar=no,toolbar=no,height=" + height + ",width=" + width + ",left=" + Math.round(x) + ",top=" + Math.round(y) + ",resizable=no,scrollbars=yes,status=no";
	    
	    sOWin = window.open(url, "", sWinOpts);
	    
	    sOWin.blur();
	    sOWin.focus();
	    
	}
	
	function initRolls() {
		$("a img[src$=_1.jpg]").each(function() {
			if (this.origimg != undefined)
				return;
			this.images = new Array();
			this.origimg = this.src;
			if (this.origimg != "") {
				var prefix = this.origimg.replace(/(sample|promo)_[0-9]+\.[a-z]+/, "");
				for (y = 0; y < 5; y++) {
					this.images[y] = new Image();
					this.images[y].src = prefix + "promo_" + (2 + y * 2) + ".jpg";
				}
				$(this).unbind().hover(
					function() {rollImages(this);},
					function() {clearRoll(this);}
				);
			}
		});
	}
	
	$(function() {
		// Bind bookmark link
		$("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;
		});
	
	
		// Bind rating buttons
		$("div.rstars a[class^=rate_]").click(function() {
			$(this).unbind();
			var rating = this.className.substring(5);
			var id = $(this).closest("div.rstars[id]").attr("id");
			var parts = id.split("_");
			$.post("/ajax/rating.php", "item_id=" + parts[2] + "&rating=" + rating + "&type_id=" + parts[1], function(data) {
				$("#" + id + " a[class^=rate_]").remove();
				if (data.code == 0) {
					$("#" + id + " .filled").css("width", Math.round(data.avg_vote * 20) + "%").removeClass("filled").addClass("voted");
					$("#" + id).next().html(data.num_votes == 1? "(1 Vote)" : "(" + data.num_votes + " Votes)");
				}
				$.jGrowl(data.message);
			}, "json");
		});
	
		// Bind add to favorites buttons
		$("a[href*=#add_fav_]").click(function() {
			var link = $(this);
			var parts = this.href.split("#")[1].split("_");
			var data = { item_id: parts[3], type_id: parts[2], action: "add" };
			$.post("/ajax/favorites.php", data, function(data) {
				if (data.code == 2) {
					qmark = document.location.toString().indexOf("?");
					extra = qmark > -1? document.location.toString().substring(qmark) : "";
					document.location = "/join.html" + extra;
				} else {
					$.jGrowl(data.message);
					if (data.code == 0 || data.code == 4)
						link.remove();
				}
			}, "json");
			return false;
		});
		
		// Bind remove from favorites buttons
		$("a[href*=#del_fav_]").click(function() {
			var link = $(this);
			var parts = this.href.split("#")[1].split("_");
			var tab = $("li[class=active] a[href*=#tab_] span");
			var data = { action: "remove", item_id: parts[3], type_id: parts[2] };
			$.post("/ajax/favorites.php", data, function(data) {
				$.jGrowl(data.message);
				if (data.code == 0) {
					link.unbind().closest("li").fadeOut("slow", function() { link.remove(); });
					tab.text(tab.text().replace(/[0-9]+/, parseInt(tab.text().match(/[0-9]+/)) - 1));
				}
			}, "json");
			return false;
		});
		
		// Bind comments
		$("#add_comment").click(function() {
			comment = $("textarea[name=comment_body]").val();
			item_id = $("input[name=scene_id]").val();
			if ($.trim(comment).length < 3) {
				$.jGrowl("You did not enter a comment!");
				return false;
			}
			this.disabled = true;
			$.post("/ajax/comments.php", { action: "add", item_id: item_id, type_id: 5, body: comment }, function(data) {
				$.jGrowl(data.message);
				if (data.code == 0) {
					$("#comment_form").html("<h3>Thanks!</h3><p>Your comment has been posted, thank you!</p>");
				}
			}, "json");
			
		});
		$("#comments_container a[href$=#bump]").click(function() {
			span = $(this).siblings("span");
			parts = this.id.split("_");
			bump = (parts[2] == "up")? 1 : -1;
			$.post("/ajax/comments.php", { action: "bump", comment_id: parts[1], rating: bump }, function(data) {
				$.jGrowl(data.message);
				if (data.code == 0)	{
					var rating = parseInt(span.html().match(/[-]?\d+/)) + bump;
					span.html("(" + rating + " bumps)");
					span.siblings("a").remove();
				}
			}, "json");
			return false;
		});
		
	
		// Clock animation
		$("div[class^=number n]").each(function(num) {rotateNum(this, (num + 1) * 20, this.className);});
		
		// Image rolls
		initRolls();
		
		
		// Init tabs
		$("a[href*=#tab_]").click(function() {
			if (!$(this).hasClass("active")) {
				tab = this.href.split("#")[1].substring(4);
				$("ul[class^=tabs] li").removeClass("active");
				$(this).closest("li").addClass("active");
				$("div[id$=container]").hide();
				$("#" + tab + "_container").show();
			}
			return false;
		});
		
		// Init search box text toggle
		$("#search-box form input:submit").click(function() {
			var val = $.trim($(this).closest("form").find("input:text[name=q]").val()).toLowerCase();
			return val != "" && val != "search these results...";
		});
		$("#search-box form input:text[name=q]").focus(function() {
			if (this.value.toLowerCase() == "search these results...")
				this.value = "";
		}).blur(function() {
			if (this.value == "")
				this.value = "Search these results...";
		});
		
		// Init sorting suboptions
		/*
		var subsortHeight = $("div.subSort:first").height();
		var subsortsInit = function() {
			$("div.subSort").each(function() {
				var visible = $(this).is(":visible");
				if (this.visible_default && !visible) {
					$(this).slideDown("fast");
				} else if (!this.visible_default && visible) {
					$(this).slideUp("fast");
				}
			});
		};
		$("div.subSort").each(function() { this.visible_default = $(this).is(":visible"); });
		$("div.sorting").mouseleave(function() { subsortsInit(); });
		$("div.sorting>a, div.sorting>strong").mouseenter(function() {
			var rel = $(this).attr("rel");
			if (rel == undefined || rel == "") {
				subsortsInit();
			} else {
				$("#" + rel).slideDown("fast").siblings("div.subSort").slideUp("fast");
			}
		});
		*/
		$("div.subSort").each(function() { this.visible_default = $(this).is(":visible"); });
		$("div.sorting").mouseleave(function() { $("div.subSort").each(function() { $(this).toggle(this.visible_default); }); });
		$("div.sorting>a, div.sorting>strong").mouseenter(function() {
			var rel = $(this).attr("rel");
			if (rel == undefined || rel == "") {
				$("div.subSort").each(function() { $(this).toggle(this.visible_default); });
			} else {
				$("#" + rel).show().siblings("div.subSort").hide();
			}
		});
	});
