	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 seekSafe (player, seconds) {
		if (seconds < 0)
			return;
		if (player.getState() > 2) {
			player.seek(seconds);
			return;
		}
		setTimeout(function(){seekSafe(player, seconds);}, 100);
	}
	
	function initRolls() {
		$("li 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;
		});
		
		
		// Image rolls
		initRolls();
		
		// Search button
		$("form input[name=q]").
			click(function() {if ($.trim(this.value) == "Search This Site...") this.value = "";}).
			blur(function() {if ($.trim(this.value) == "") this.value = "Search This Site...";});
		
		// Init carousel
	    $("#slide").jcarousel({
			easing: "BounceEaseOut",
			wrap: "both",
			auto: 5,
			scroll: 1,
			animation: 900
	    });
	    
	    // Init tabs
		$("a[href*=#tab_]").click(function() {
			if (!$(this).closest("li").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;
		});
		if ($("li.active a[href*=#tab_]").size() == 0) {
			$("a[href*=#tab_]:first").click();
		} else {
			tmp = $("li.active a[href*=#tab_]:first");
			tmp.closest("li").removeClass("active");
			tmp.click();
		}
		// Bind add to favorites buttons
		$("a[href*=#add_fav_]").live("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/favor", 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;
					return;
				}
				$.jGrowl(data.message);
				link.remove();
			}, "json");
			return false;
		});
		
		// Bind comments
		$("#comment_form").submit(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;
			}
			$("input[type=submit]", this).attr("disabled", true);
			$.post("/ajax/comment", { action: "add", type_id: 5, item_id: item_id, 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");
			return false;
		});
		
		// Bind comment bumps
		$("#comments_container a[href$=#bump]").click(function() {
			span = $(this).siblings("span");
			parts = this.id.split("_");
			bump = parts[2] == "up"? 1 : -1;
			$.post("/ajax/comment", { 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;
		});
		
		// Bind favorite remove buttons
		$("#favorites a.favorite_rem").click(function() {
			link = $(this);
			parts = this.href.split("#")[1].split("_");
			data = { action: "remove", item_id: parts[1], type_id: parts[0] };
			tab = $("li[class=active] a[href*=#tab_] span");
			$.post("/ajax/favor", 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 rating buttons
		$("a[href*=#rate_]").click(function() {
			var div = $(this).closest("div");			
			var parts = this.href.split("_");
			//div.children("ul").unbind();
			$.post("/ajax/rating", { item_id: parts[2], rating: parts[3], type_id: parts[1] }, function(data) {
				if(data.code == 1) {
					var pp = Math.round(data.avg_vote * 20);
					$("#percent"+parts[2]).text(pp + "%");
					div.children("ul").fadeOut(function() { $(this).remove(); });
				}
				$.jGrowl(data.message);
			}, "json");
			return false;
		});
		
		
		// Bind rating buttons
		$("div.rstars a[class^=rate_]").click(function() {
			var rating = this.className.substring(5);
			var id = $(this).closest("div.rstars[id]").attr("id");
			var parts = id.split("_");
			$.post("/ajax/rating", "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");
		});
		
	});
	
	function report_broken( scene_id ){
	    
	    var ie = false;
	    if( navigator.appName == "Microsoft Internet Explorer" )
	        ie = true;
	    
	    var url = "/report/"+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();
	    
	}
