$(function() {
	$("#results, .bottom .toolkit").hide();
	$("a.learn-more, #twitter a").live("click", function() {
		window.open($(this).attr("href"));
		return false;
	});
	$(".product, #email").fancybox({
		overlayColor : '#000'
	});
	$(".landingPageApp ul li a").live("click", function() {
		var $this = $(this);
		$("#notoriously ul li a").removeClass("active");
		$this.addClass("active");
		$("#landingDropdown ul").fadeOut("normal");
		$(".text-landing-notoriousFor").css('background', 'none');
		$(".middle").animate({
			height : "215px"
		}, 1000, "swing");
		$("#results").css("display", "block");
		$("#results .textEssentials").attr("id", this.id);
		$(".bottom .toolkit").delay(1000).fadeIn("normal");
		$("#results .badge").attr("id", this.id);
		$('#twitter').each(function() {
			if (this.twitter)
				this.twitter.clear();
		}).liveTwitter('#' + $this.attr("rel"), {
			limit : 1,
			rate : 5000,
			lang : _lang
		});
		var _href = $this.attr("href").split("_");
		var _id = _href[1];
		var _hash = _href[0];
		$("#description").hide();
		$("#bodyWrap").attr("class", "").addClass($this.attr("id"));
		$.ajax({
			data : {
				id : _id,
				hash : _hash,
				task : "get-notorious",
				lang : _lang
			},
			success : s,
			url : "ajax.php"
		});
		return false;
	});
	$(".notoriously-good-product").live("mouseover", function() {
		$(this).addClass("active");
	}).live("mouseout", function() {
		$(this).removeClass("active");
	});
});

var s = function(data) {
	var i = new Array();
	for ( var it in data) {
		var prod = data[it];
		if (prod && prod.name) {
			x = parseInt(it) + 1;
			$("#product-" + x + " img").attr("src", prod.image);
			$("#product-" + x + " img").attr("alt", prod.name);
			$("#product-" + x + " span").html(prod.name);
			$("#product-content-" + x + " h3").html(prod.name);
			$("#product-content-" + x + " .description").html(prod.description);
			// the following line takes the value of learn-more button text from
			// db. not needed since text is all the same
			if (prod.link)
				$("#product-content-" + x + " a.learn-more").attr("href",
						prod.link).show();
			else
				$("#product-content-" + x + " a.learn-more").hide();
			$("#product-content-" + x + " .product-image img").attr({
				"alt" : prod.name,
				"src" : prod.image,
				"height" : prod.height,
				"width" : prod.width
			});
			$("#facts").fadeIn();
			i[it] = prod.id;
		}
	}
	$("#facts p").html(data.trivia.text);
	$("#facts img").attr("src", data.trivia.image);
	$(".toolkit p span.gold").text(data.trivia.description_gold);
	$(".toolkit p span.normal").text(data.trivia.description);
	// $(".badge img").attr("scr", data.image);
	var link = $("#email").attr("href").split("?")[0] + "?lang=" + _lang;
	if (i.length > 0) {
		link += "&id=" + i.join("-");
	}
	$("#email").attr("href", link);
};
