if(typeof(blippr) == "undefined") blippr = {};
blippr.root = blippr.root || "http://static1.blippr.com";
blippr.widget_css = blippr.widget_css || blippr.root + "/stylesheets/node_widget.min.css?v1";
if(blippr.widget_css != "native") {
	var link = document.createElement("link");
	link.rel = "stylesheet";
	link.type = "text/css";
	link.href = blippr.widget_css;
	document.getElementsByTagName('head')[0].appendChild(link);
}

var NOOP = function() { return false; };
var blipprWidgetReady = function($) {
	var $parent = $(".blippr-title-widget:not(.jq-handled)");
	$parent.find(".blippr-opinions input").each(function() { this.onclick = NOOP; });
	$parent.find(".blippr-opinions input").click(function() {
		var $this = $(this);
		$(this).parents(".blippr-title-widget:first").find(".blippr-blips").hide('fast');
		$this.blur().parent().addClass("blippr-selected").parent().find("span.blippr-selected").not($this.parent()).removeClass("blippr-selected");
		
		var val;
		     if($this.hasClass("blippr-value-3")) val = 2;
		else if($this.hasClass("blippr-value-2")) val = 1;
		else if($this.hasClass("blippr-value-1")) val = 0;
		else if($this.hasClass("blippr-value-0")) val = -1;
		
		var form = $this.parents(".blippr-title-widget-blip-form:first").find(".blippr-blip-entry");
		if($.browser.msie) {
			form.fadeIn('normal', function() {
				$(this).parents(".blippr-title-widget:first").find(".blippr-show-reviews").css({bottom: "0px"});
			});
		} else {
			form.show('normal', function() {
				$(this).parents(".blippr-title-widget:first").find(".blippr-show-reviews").css({bottom: "0px"});
			});
		}
		form.find(".blippr-input-bias").val(val);
		$this.parents(".blippr-inline-hover:first").find(".blippr-hover-close").fadeIn("fast");
	});
	
	var default_text = "Your review in 160 characters or less"
	$parent.find(".blippr-blip-entry textarea").keydown(function() {
		var $this = $(this);
		if($this.val().length > 160) { $this.val($this.val().substr(0, 160)); }
		var count = $this.val().length;
		$this.parent().find(".blippr-character-count").text(count + " / 160");
	}).val(default_text).addClass("blippr-usage-hint").focus(function() {
		if($(this).val() == default_text) { $(this).val("").removeClass("blippr-usage-hint"); }
		blipprElemWithFocus = this;
	}).blur(function() { 
		blipprElemWithFocus = false;
		if($(this).val() == "") { $(this).val(default_text).addClass("blippr-usage-hint"); }
	});
	
	$parent.find(".blippr-source-key").val(blippr.sourceKey);
	var $form = $parent.find("form:first");
	$form.submit(function() {
		$(this).find(".blippr-usage-hint").val("");
	});
	
	$parent.find(".blippr-show-reviews").click(function() {
		var $this = $(this);
		var span = $this.find("span");
		if($this.hasClass("blippr-expand-showing")) {
			$this.text("+");
			$this.removeClass("blippr-expand-showing");
		} else {
			$this.text("-");
			$this.addClass("blippr-expand-showing");
		}
		$this.blur();
		$this.parents(".blippr-title-widget:first").find(".blippr-title-widget-blips").toggle('fast');
	});
	$parent.addClass("jq-handled");
	$("a.blippr-widget-anchor").hide();
	
	if($.browser.msie) {
		$(".blippr-submit").hover(function() {
			$(this).addClass("blippr-submit-hover");
		}, function() {
			$(this).removeClass("blippr-submit-hover");
		});
	}
};

jQuery(document).ready(function($) {
	var blippr_hover = document.createElement("div");
	document.body.appendChild(blippr_hover);
	var $hover = $(blippr_hover);
	$hover.addClass("blippr-inline-hover").hide();
	
	var content = document.createElement("div");
	blippr_hover.appendChild(content);
	var $content = $(content);
	$content.addClass("blippr-hover-content");
	
	var close = document.createElement("a");
	close.href = "#";
	close.onclick = function() {
		$(this).parents(".blippr-inline-hover:first").fadeOut("fast");
		return false;
	}
	blippr_hover.appendChild(close);
	var $close = $(close);
	$close.text("close").addClass("blippr-hover-close");
	
	var blipprHoverCache = {};
	var blipprFadeElement;
	var blipprFadeTimer;
	var blipprLastHover;
	var blipprRequestSerial = 0;
	
	var blipprHoverFadeOut = function() {
		if($hover.find("span.blippr-selected").length > 0) return;
		$hover.fadeOut("fast");
	}
	var getRequestSerial = function() { return blipprRequestSerial; }
	var positionHover = function($this) {
		// Position after content logic; may affect width.
		var offset = $this.offset();
		var top = offset.top + $this.height() + 5;
		var left = offset.left + ($this.width() / 2) - ($hover.width() / 2);
		var MAXWIDTH = $(document).width() - 10;
		if(left < 10) left = 10;
		if(left + $hover.width() > MAXWIDTH - 10) left = (MAXWIDTH - 10) - $hover.width();
		$hover.css({
			top: top + "px",
			left: left + "px"
		});
	}
	
	var blipprHoverFadeIn = function() {
		var $this = $(blipprFadeElement);
		$hover.hide();
		
		var last = blipprLastHover;
		blipprLastHover = $this;
		var href = $this.attr("rel");
		var c1 = blipprLastHover.get(0);
		var c2 = last && last.get(0) || c1;
		
		if(blipprHoverCache[href]) {
			if(c1 != c2) {
				$content.html(blipprHoverCache[href]);
				$close.hide();
			}
			blipprWidgetReady($);
			positionHover($this);
			$hover.fadeIn("normal");
		} else {
			$content.html("");
			$close.hide();
			$hover.fadeIn("normal");
			$hover.addClass("blippr-hover-waiting");
			positionHover($this);
			blipprHoverCache[href] = "(loading data)";
			
			blipprRequestSerial = blipprRequestSerial + 1;
			var serial = blipprRequestSerial;
			
			$.getJSON(href + "?dyn=y&jsonp=?", {}, function(data) {			
				blipprHoverCache[href] = data.html;
				if(blipprRequestSerial == serial) {
					$hover.removeClass("blippr-hover-waiting");
					positionHover($this);
					$content.html(blipprHoverCache[href]);		
					blipprWidgetReady($);
				}
			});
		}
	}
	
	var $smilies = $(".blippr-inline-smiley");
	$smilies.hover(
		function() {
			clearTimeout(blipprFadeTimer);
			blipprFadeElement = this;
			blipprFadeTimer = setTimeout(blipprHoverFadeIn, 200);
		},
		function () {
			clearTimeout(blipprFadeTimer);
			blipprFadeTimer = setTimeout(blipprHoverFadeOut, 300);
		}
	);
	$smilies.find("span").remove();
	$smilies.find("img").before("<span> (</span>");
	$smilies.find("img").after("<span>)</span>");
	
	$hover.hover(
		function() {
			clearTimeout(blipprFadeTimer);
		},
		function() {
			blipprFadeTimer = setTimeout(blipprHoverFadeOut, 300);
		}
	).click(function(e) {
		e.cancelBubble = true;
		if(e.stopPropagation) e.stopPropagation();
	});
	
	$(document).click(function() {
		$hover.fadeOut("fast");
	});
});