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 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() {
		// 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...";});
		
		// 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 add to favorites buttons
		$("a[href*=#add_fav_]").live("click", function() {
			var link = $(this);
			var parts = this.href.split("#")[1].split("_");
			var data = { action: "add", item_id: parts[3], type_id: parts[2] };
			$.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;
					return;
				}
				link.closest("p").remove();
				$.jGrowl(data.message);
			}, "json");
			return false;
		});
		
		
		// Search function
		$("#search_form").submit(function() {
			query = $.trim($("input[name=q]", this).val());
			if (query.match(/search this site/i))
				return false;
			$.post("/ajax/search.php", {q: query}, function(msg) {
				$("#search_results").html(msg).show();
			});
			return false;
		});
		
		
		// Rating buttons
		$("a[href*=#vote_]").click(function() {
			parts = this.href.split("#")[1].split("_");
			$("a[href*=#vote_" + parts[1] + "_" + parts[2] + "_]").remove();
			$.post("/ajax/rating.php", "item_id=" + parts[2] + "&rating=" + parts[3] + "&type_id=" + parts[1], function(data) {
				$.jGrowl(data.message);
				if (data.code == 0) {
					hot = Math.round((data.avg_vote - 1) * 25 * 1.78);
					cold = 178 - hot;
					$("div.coldrating div.bar").width(cold);
					$("div.hotrating div.bar").width(hot);
					hot = Math.round((data.avg_vote - 1) * 25);
					cold = 100 - hot;
					$("div.coldrating div.percent").html(cold + "%");
					$("div.hotrating div.percent").html(hot + "%");
				}
			}, "json");
			return false;
		});
		
		
		// Comments
		$("form input[type=image][name=add_comment]").click(function() {
			form = $(this).closest("form");
			item_id = $("input[name=item_id]", form).val();
			message = $.trim($("textarea[name=message]", form).val());
			if (message.length < 3) {
				$.jGrowl("You must enter a message");
				return false;
			}
			$.post("/ajax/comments.php", { type_id: 5, item_id: item_id, body: message, action: "add"}, function(data) {
				$.jGrowl(data.message);
				if (data.code == 0) {
					$("p.user").html("Thanks for commenting!");
					$("div.post").remove();
				}
			}, "json");
			return false;
		});
		
		// Comment bumps
		$("div.comments a[href*=#bump_]").click(function() {
			com_div = $(this).closest("div.vote");
			parts = this.href.split("#")[1].split("_");
			rating = parts[1] == "down"? -1  : 1;
			id = parts[2];
			$.post("/ajax/comments.php", { comment_id: id, rating: rating, action: "bump"}, function(data) {
				$.jGrowl(data.message);
				if (data.code == 0) {
					bump = $("p.info span.post", com_div.closest("div.entry"));
					bump.text(bump.text() + rating);
					com_div.remove();
				}
			}, "json");
			return false;
		});
		
		// Init tabs
		$("a[href*=#tab_]").click(function() {
			if (!$(this).hasClass("active")) {
				tab = this.href.split("#")[1].substring(4);
				$("ul[class^=tabs] li a").removeClass("active");
				$(this).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();
		
		
		// Remove favs
		$("li a.favorite_rem").click(function() {
			var parts = this.href.split("#")[1].split("_");
			var data = { action: "remove", item_id: parts[1], type_id: parts[0] };
			$(this).unbind();
			$.post("/ajax/favorites.php", data, function(data) {
				$.jGrowl(data.message);
			}, "json");
			$(this).closest("li").fadeOut("slow", function() {$(this).remove();});
			return false;
		});
		
		// Report broken
		$("a.broken[href*=#report_broken_]").click(function() {
			id = this.href.split("#")[1].split("_")[2];
			url = "/ajax/broken.php?scene_id=" + id;
			x = 0;
			y = 0;
			//x = (document.body.clientWidth - 560) / 2;
			//y = (document.body.clientHeight - 300) / 2;
			x = (window.outerWidth - 560) / 2;
			y = (window.outerHeight - 300) / 2;
			sWinOpts = "align=center,location=no,menubar=no,titlebar=no,toolbar=no,height=300,width=560,left=" + Math.round(x) + ",top=" + Math.round(y) + ",resizable=no,scrollbars=yes,status=no";
			sOWin = window.open(url, "", sWinOpts);
			sOWin.blur();
			sOWin.focus();
			return false;
		});
		
		
		
});
