	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();
			rating = this.className.substring(5);
			id = $(this).closest("div.rstars[id]").attr("id");
			parts = id.split("_");
			$.post("/ajax/rating.php", "id=" + parts[2] + "&rating=" + rating + "&type=" + parts[1], function(data) {
				$("#" + id + " a[class^=rate_]").remove();
				if (data.code == 1) {
					$("#" + 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)");
					// at some point we will replace 3 lines below with 1 line above :)
					var text = $("#" + id).next();
					var numb = text.html().match(/\d+/);
					text.html(text.html() == "(Not Rated)"? "(1 Vote)" : "(" + (parseInt(numb) + 1) + " Votes)");
					//
				}
				$.jGrowl(data.message);
			}, "json");
		});
	
		// Bind add to favorites buttons
		$("a[href*=#add_fav_]").live("click", function() {
			link = $(this);
			parts = this.href.split("#")[1].split("_");
			$.post("/ajax/favorites.php",  "id=" + parts[3] + "&type=" + parts[2] + "&action=add", function(msg) {
				msg = jQuery.trim(msg);
				qmark = document.location.toString().indexOf("?");
				extra = qmark > -1? document.location.toString().substring(qmark) : "";
				if (msg.match(/not authorized/i)) {
					document.location = "/join.html" + extra;
					return;
				}
				if (msg == "1" || msg.match(/already added/i))
					link.remove();
				if (msg == "1") {
					$.jGrowl("Favorite successfully added!");
					return;
				}
				$.jGrowl(msg);
			});
			return false;
		});
		
		// Bind remove from favorites buttons
		$("a[id^=del_fav_]").live("click", function() {
			link = $(this);
			tab = $("li[class=active] a[href*=#tab_] span");
			data = "favorite_id=" + this.id.split("_")[2] + "&action=remove";
			$.post("/ajax/favorites.php", data, function(msg) {
				msg = jQuery.trim(msg);
				if (msg == 1) {
					$.jGrowl("Favorite item deleted!");
					link.unbind().closest("li").fadeOut("slow", function() {link.remove();});
					tab.text(tab.text().replace(/[0-9]+/, parseInt(tab.text().match(/[0-9]+/)) - 1));
				} else
					$.jGrowl("Cannot delete favorite item! Please try again later.");
			});
		});
		
		// 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 + "&comment=" + comment, function(msg) {
				$("#comment_form").html("<h3>Thanks!</h3><p>Your comment has been posted, thank you!</p>");
				$.jGrowl(msg == "1"?
					"Your comment has been posted, thank you!" :
					"There was a problem posting your comment, please try again later!");
			});
			
		});
		$("#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] + "&bump=" + bump, function(msg) {
				message = "Problem bumping a comment... Try again later";
				if (msg == 3)		message = "You have already rated this comment"
				else if (msg == 1)	{
					message = "Comment bumped. Thanks!";
					var rating = parseInt(span.html().match(/[-]?\d+/)) + bump;
					span.html("(" + rating + " bumps)");
					span.siblings("a").remove();
				}
				$.jGrowl(message);
			});
			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();
			}
		});
	});