$(function(){
    
    // Events Category page - hide more events section if more than 4 events listed
    if(($(".sig_specific_event .sig_event").length) > 4){
		$("#sig_more_events").hide();
	};
	
	// Events Category page - hide more events section if more than one specific category is shown
	if(($(".sig_specific_event").length) > 1){
		$("#sig_more_events").hide();
	};
	
	//Events - hide 'more events' section added 9-28-09
	if(($(".sig_more_events .sig_event").length) <= 0){
		$("#sig_more_events").hide();
	};	
	
	
	// Merchant Dropdown - Character limit
	$("#merchant option").each(function(){
		var optionText = $(this).text();
		if (optionText.length > 25){
			optionText = optionText.substr(0,25)
			$(this).text(optionText) + $(this).text(optionText).append("...") 
		}
	});
	
	// Remove extra <br> tags in offers
	$(".landing_offer strong").each(function(){
		var numOfBR = $(this).children("br").length
		if (numOfBR > 0){
			$(this).children("br").remove();
		}
	});
	
	// Adjust spacing for Left Nav first child link
	if ($("div#sig_navlist li#parentlink ul li:first").attr("id") == "uberlink"){
		$("div#sig_navlist li#parentlink ul li#uberlink").addClass("first")
	}
	
	// Hide Destinations or Categories if not enough items
    if ($(".sig_bottom_links:contains('Destinations')").children("a").length < 2){
    	$(".sig_bottom_links:contains('Destinations')").hide();
    }
    if ($(".sig_bottom_links:contains('Categories')").children("a").length < 2){
    	$(".sig_bottom_links:contains('Categories')").hide();
    }
    
    // Remove extra <br> tags in Destinations or Categories
	$(".sig_bottom_links br").remove();
	
	// Remove extra <br> tags in Features
	$(".sig_featured p br").remove();
	
	// merchant drop down list on shopping passion page and offer category page(s)
	// also used for category list on signature landing page
	$("#merchantList, #categoryList").change(function(e) {
		var redirectTo = $(this).val();
		if ($(this).val()) {
			VISA.Analytics.redirect(redirectTo);
		}
	});
});


function faqListWide() {
	$('div.faqListWide h3').click(function() {
		$(this).next().slideToggle('fast');
		if ($(this).hasClass('arrow-up')) {
			$(this).removeClass('arrow-up').addClass('arrow-down');
		}
		else  {
			$(this).removeClass('arrow-down').addClass('arrow-up');
		}
	});

	$('span#expand_all_wide').click(function() {
		if ($('div.faqListWide h3').hasClass('arrow-up')) {
			$('div.faqListWide h3').removeClass('arrow-up').addClass('arrow-down').next().slideDown('fast');
			var htmlStr = "Contract All";
			$('span#expand_all_wide').text(htmlStr);
		}
		else {
			$('div.faqListWide h3').addClass('arrow-up').removeClass('arrow-down').next().slideUp('fast');
			var htmlStr = "Expand All";
			$('span#expand_all_wide').text(htmlStr);

		}
	});
}

function faqList() {
	$('div.faqList h3').click(function() {
		$(this).next().slideToggle('fast');
		if ($(this).hasClass('arrow-up')) {
			$(this).removeClass('arrow-up').addClass('arrow-down');
		}
		else  {
			$(this).removeClass('arrow-down').addClass('arrow-up');
		}
	});
	
	$('span#expand_all').click(function() {
		if ($('div.faqList h3').hasClass('arrow-up')) {
			$('div.faqList h3').removeClass('arrow-up').addClass('arrow-down').next().slideDown('fast');
			var htmlStr = "Contract All";
			$('span#expand_all').text(htmlStr);
		}
		else {
			$('div.faqList h3').addClass('arrow-up').removeClass('arrow-down').next().slideUp('fast');
			var htmlStr = "Expand All";
			$('span#expand_all').text(htmlStr);

		}
	});	
}



/***************************************************
EVENTS TOOLTIP BEGINS
***************************************************/
/* -- START toolip code -------------- */
//	olangan@aa-rf.com 2006.08.22
//	this is used on the home page
var tooltip_timeout;

function tooltip(e, div) {
	if (document.getElementById(div) == null) return;
	var targ = (e.target ? e.target : e.srcElement);
	var source = document.getElementById(div);
	clearTimeout(tooltip_timeout);
	targ.onmouseout = function() { clearTimeout(tooltip_timeout); tooltip_timeout = setTimeout("hide_tooltip();",500);}
	//var title = (targ.text ? "<h3>" + targ.text + "<\/h3>" : "<h3>" + targ.innerText + "<\/h3>");
	document.getElementById("tooltip_text").innerHTML = source.innerHTML;//title + 
	var tooltip = document.getElementById("tooltip");
//	tooltip.style.top = (getAbsoluteTop(targ) - getAbsoluteTop(document.getElementById("content"))+ 20) + "px";
//	tooltip.style.left = (getAbsoluteLeft(targ) - getAbsoluteLeft(document.getElementById("content")) + targ.offsetWidth + 120) + "px";
	tooltip.style.top = (getAbsoluteTop(targ) + 20) + "px";
	tooltip.style.left = (getAbsoluteLeft(targ) + targ.offsetWidth - 20) + "px";
	tooltip.style.display = "block";
	tooltip.onmouseover = function (){clearTimeout(tooltip_timeout);}
	tooltip.onmouseout = function (){tooltip_timeout = setTimeout("hide_tooltip();",500);}
	
	// Remove extra space in event tooltip
	$("#tooltip_text h4").each(function(){
		var numOfP = $(this).children("a").children("p").length
		var pContent = $(this).children("a").children("p").text()
		if (numOfP > 0){
			$(this).children("a").children("p").remove();
			$(this).children("a").text(pContent)
		}
	});
	
	// Remove carousel styles from <ul> and <li>
	$("#tooltip_text ul, #tooltip_text li").each(function(){
		$(this).removeAttr("style")
	});
	
}

function hide_tooltip() {
	document.getElementById("tooltip").style.display = "none";
	clearTimeout(tooltip_timeout);
}

function getAbsoluteTop(element) {
	var top = element.offsetTop;
	while (element.offsetParent != null) {
		top += element.offsetParent.offsetTop;
		element = element.offsetParent;
	}
	return top;
}

function getAbsoluteLeft(element) {
	var left = element.offsetLeft;
	while (element.offsetParent != null) {
		left += element.offsetParent.offsetLeft;
		element = element.offsetParent;
	}
	return left;
}

/* -- END   toolip code -------------- */
VISA.include_js("/js/jquery/jquery.bgiframe.min.js");

$(document).ready(function() 
{
	// stop dropdowns showing through the tooltip on IE6
	$("#tooltip").bgiframe();
	faqListWide();
	faqList();
});
/***************************************************
EVENTS TOOLTIP ENDS
***************************************************/