initCarousel = function(){
	$('#offers_carousel').jcarousel({});
	setTimeout(function() { $('#offers_carousel').css('visibility','visible'); }, 0);
}

$(document).ready(function() {
	initCarousel();
	
	$(".jcarousel-item").unbind("hover").hover(function() {
		$(this).css("cursor", "pointer");
	}, function() {
		$(this).css("cursor", "auto");
	});

	$(".jcarousel-prev").click(close_tip);
	$(".jcarousel-next").click(close_tip);

	$(".jcarousel-item").click(get_item);	
});
	
function get_item() {
	var offer = $(this);
	offer_id = offer.attr("id");
	
	print_link = $("a", offer).attr("href");
	
	$("#detail_tooltip_text img:visible").hide();
	$(".detail_offer_tooltip:visible").hide();
	$("#detail_tooltip_text:visible").hide();
	
	$("#detail_offer_tc").hide();
    	$("#detail_offer_tc_link").css("background-image", "none");	
	
	$(".detail_offer_tooltip").css({ "left": offer.offset().left + 10, "top": offer.offset().top - 10 });
	
	$.getJSON($("a", offer).attr("href") + "&json=true", function(json) { populateOfferTooltip(json); });
	return false;
}	
	

function close_tip() {
    $(".detail_offer_tooltip").hide();
}

function populateOfferTooltip(json) {
	$("#detail_offer_desc").html(json.shortDescriptionFull);
	$("#detail_offer_content").html("<p>"+json.longDescriptionFull);
	$("#detail_offer_valid_date").html("Valid from " + json.startDate + " to " + json.endDate);
	$("#detail_offer_tc ul").html(json.additionalTerm + json.visaterms);
	$("#detail_offer_logo img").unbind("load");
	$("#detail_offer_logo img").attr("src", "/img/spacer.gif");
	$("#detail_offer_logo img").load(function() {
		$(this).show();
	});	
	$("#detail_offer_logo img").attr("src", json.offerLogo2);
	$("#detail_offer_logo img").attr("alt", "");
	
	$("<div id='detail_offer_redeem'><a href='' target='_blank'><img src='/img/other/redeem_offer.gif' alt='Redeem Offer' border='0' /></a></div>").appendTo("#detail_offer_content");
	
	if(json.redeemUrl)
	{
		// Setup redeem URL including analytics
		$("#detail_offer_redeem a").attr("href", unescape(json.redeemUrl));
		$("#detail_offer_redeem a").click(function() {
		    var request = $.ajax({ async: false, url: buildRedeemURL(json.id, json.name, json.redeemUrl) });
		    var timeout = (new Date()).getTime() + 500;

		    while (true) {
			if (request.readyState >= 2) break;
			if ((new Date()).getTime() >= timeout) break;
		    }
		});

		$("#detail_offer_redeem:hidden").show();
	}
	else
	{
		$("#detail_offer_redeem:visible").hide();
	}


	function buildRedeemURL(offerId, offerName, redeemURL) {
		var sectionPrefix = "/microsites/promo/detail/nfl";
		var url = "";

		if(offerId && offerName) {
			var tidiedOfferName = offerName.replace(/ /g, "").replace(/&/g, "");

			url += VISA.base_href;
			url += sectionPrefix;
			url += "/vro_redeem.jsp";
			url +="?offerName=" + tidiedOfferName;
			url +="&offerId=" + offerId;
		}

		return url;
	}
	

	$("#detail_offer_printable").html("<a href='" + print_link + "&printable=yes' target='_blank'><img border='0' alt='Printable Page' src='/img/other/printable_page.gif' style='display: inline; cursor: auto;'/></a>");

	
	$("#detail_offer_printable img:hidden, #detail_offer_closewindow img:hidden, #detail_offer_related_link img:hidden, #detail_offer_more_link img:hidden, #detail_offer_redeem img:hidden").show();
	
	$("#detail_offer_tc_link").css("background-image", "url('/img/other/sm_arrow_right.gif')");
	$("#detail_tooltip_text:hidden").show();
	
	$("#detail_offer_printable img").unbind("hover").hover(function() {
		$(this).css("cursor", "pointer");
		}, function() {
		$(this).css("cursor", "auto");
	});
	
	$("#detail_offer_closewindow img").unbind("hover").hover(function() {
		$(this).css("cursor", "pointer");
		}, function() {
		$(this).css("cursor", "auto");
	});
	
	$("#detail_offer_closewindow img").unbind("click").click(function() {
		$(".detail_offer_tooltip").hide();
		$(".dropBenefits").removeClass("hide");
		$("#card").removeClass("hide");
		$(".detail_share_overlay").hide();
	});	
	
	$("#detail_offer_tc_link").unbind("click").click(function() {
        if($("#detail_offer_tc").is(":hidden")) {
            $("#detail_offer_tc").show();
            $(this).css({ "background-image": "url('/img/other/sm_arrow_down.gif')", "padding-bottom": "10px" });
        } else {
            $("#detail_offer_tc").hide();
            $(this).css({ "background-image": "url('/img/other/sm_arrow_right.gif')", "padding-bottom": "0" });
        }
	});

	$("#detail_offer_tc_link").unbind("hover").hover(function() {
		$(this).css({ "cursor": "pointer", "text-decoration": "underline" });
    	}, function() {
		$(this).css({ "cursor": "auto", "text-decoration": "none" });
	});	
	
	$(".detail_offer_tooltip").show();
	
	$("head").append("<!--[if IE 6]><script>DD_belatedPNG.fix('.div.solid, div.detail_header, div.detail_footer');</scr" + "ipt><![endif]-->");
}