var $j=jQuery.noConflict();
var alertText = "Please be advised that you are leaving BankFirst Financial Service's website. This link is provided as a courtesy. BankFirst does not endorse or control the content of third party websites.";
var slideSpeed = 500;
var tabEffect = "fade";	// slide, fade
var url = window.location.pathname;

function productTabs(){
	if( $j('#productTabs').length > 0 ){
		var offset = 95;
		$j('#productTabs dt').addClass('label');
		$j('#productTabs dt:eq(0)').addClass('selected');
		$j('#productTabs dd').addClass('pane');
		$j('#productTabs dd').css('position','absolute');
		$j('#productTabs dd').not("dd:eq(0)").hide();
		$j('#productTabs').css('height', $j('#productTabs dd:eq(0)').height() + offset);
		$j('#productTabs dt').click( function(){
			var holdThis = $j(this);
			if( !holdThis.hasClass('selected') ){
				$j('#productTabs dt').removeClass('selected');
				holdThis.addClass('selected');
				if(tabEffect == "slide"){
					$j('#productTabs dd').slideUp(slideSpeed);
					$j('#productTabs').animate({ height : holdThis.next().height() + offset }, slideSpeed, function(){
						holdThis.next().slideDown(slideSpeed);									 
					});
				}
				else{
					$j('#productTabs dd').fadeOut(slideSpeed);
					$j('#productTabs').animate({ height : holdThis.next().height() + offset }, slideSpeed, function(){
						holdThis.next().fadeIn(slideSpeed);									 
					});
				}
			}
		});
	}
}

function confirmAlert(url){
		jConfirm(alertText, "Confirm", function(r) {
			if( r ){
				window.open(url);
			} else {
			return false;
		}
	});
	return false;
}

function confirmAlert2(url){
	if (!confirmAlert(url)) {
		return false; 
	}
}

$j(document).ready(function(){

	$j(".confirm").click( function() {						   
		if (!confirmAlert($j(this).attr('href'))){
			return false;			
		}
	});
	
	if( $j().overlabel )    
		$j("#obLogin form ol li label").overlabel();

	$j("#obLogin #obMore h3").toggle(
		function() {
			$j(this).addClass("open");
			$j(this).siblings("ul").slideDown("slow");
		}, function() {
			$j(this).siblings("ul").slideUp("slow",
				function() {
    	$j("#obLogin #obMore h3").removeClass("open");
  		}
  	);
  }
	);

	$j(document).click(
		function() {
			$j("#obLogin #obMore h3").siblings("ul").slideUp("slow",
				function() {
	   	$j("#obLogin #obMore h3").removeClass("open");
	 		}
	 	);
		}
	);

	if( $j().fancybox )    
	{	// FancyBox popup on apps
		$j("a#ssnWhy").fancybox({
			'frameWidth': 300,
			'frameHeight': 190
		});
		
		// New popups provided by FancyBox 
		$j('#questions a').addClass('iframe');
		
		$j("#questions a").fancybox({
			'hideOnContentClick': false,
			'frameWidth': 520,
			'frameHeight': 340,
			'overlayOpacity': 0.8
		});
	
		if ((url.indexOf("personal/mortgage") != -1)) {
			$j("#leftNav li a[href*='financial-calculator']").addClass("iframe");
		}
		
		$j('#financialCalculators li a').not('#financialCalculators li a:contains("FDIC Calculator")').addClass('iframe');
	
		$j("a[href*='mvbpp_flash_demo']").addClass("flashDemo").addClass("iframe");
	
		$j("a.flashDemo").fancybox({
			'hideOnContentClick': false,
			'frameWidth': 800,
			'frameHeight': 605,
			'padding': 10,
			'overlayOpacity': 0.8
		});
	
		$j("#financialCalculators li a, #leftNav li a.iframe").not('#financialCalculators li a:contains("FDIC Calculator")').fancybox({
			'hideOnContentClick': false,
			'frameWidth': 650,
			'frameHeight': 400,
			'overlayOpacity': 0.8
		});
		// End of FancyBox
	}

	if( $j().blend ) 
		//$j("#primaryNav li a, #obLoginLinks ul li a").blend();
	
	$j("#homeFeaturedProducts ul li, #featuredProduct, #secondaryProducts ul li").click(function(){
		window.location=$j(this).find("a").attr("href");
	});

	$j("ul#leftNav li:last").addClass("lastLastChild");

	$j("ul#leftNav li a[href*='promotions']").addClass("currentPromotions");

	if ((url.indexOf("choose-a-cd.html") != -1)) {
		$j("ul#leftNav li a[href*='certificates-of-deposit.html']").addClass("selected inPath").parent().addClass("selected inPath");
	};
	
	$j("ul#leftNav h3 a").hover(
		function() {
			$j(this).css({width: "196px", paddingLeft: "14px"});
			$j(this).stop().animate({width: "186px", paddingLeft: "24px"})
		}, function () {
			$j(this).stop().animate({width: "196px", paddingLeft: "14px"})
		}
	);
	
	if ((url.indexOf("personal/mortgage") != -1) || (url.indexOf("business/commercial-services") != -1) || (url.indexOf("services/promotions") != -1) || (url.indexOf("services/card-services") != -1)) {
		$j("#featuredProduct").attr("class","flip");
	};

	// Alphabetize gMapLocations
	if (typeof gMapLocations !== 'undefined') {
	 gMapLocations.sort(function(a, b) { return a.tags > b.tags });
	};

	// Resort locations
	$j("#locations ul#locList li").tsort("span.locality");
	var sa = [];
	var classes = [];
	var locality = [];
	$j('#locations ul#locList li').each(function() {
		thisClass = $j(this).attr('class');
		if (thisClass.indexOf(" ") != -1) {
			thisClass = thisClass.substr(0, thisClass.indexOf(" ")-1);
		}
	
		var spot = classes.length;
		classes[spot] = thisClass;
		locality[spot] = $j(this).find(".locality").text();
		
		sa.push( $(this).clone(true) );
		$j(this).remove();
	});
	sa.reverse();
	var numLocations = classes.length;
	for( var i = 0; i < numLocations; i++ ){
		if( classes[i] != classes[i-1] ){
			$j('#locations ul#locList').append('<li><ul></ul></li>');
			$j('#locations ul#locList li:last').prepend('<h2>' + locality[i] + '</h2>');
		}
		$j('#locations ul#locList ul:last').append(sa.pop());
	}
	// End resort locations

	$j('#locations ul#locList>li>ul').hide();
	$j('#locations ul#locList>li>ul:first').show().siblings("h2").addClass("open");

	$j('#locations ul#locList>li>h2').click(function () {
		var thisLocation = $j(this).next("ul");
		$j('#locations ul#locList>li>ul>li:nth-child(even)').addClass("even");
		if (thisLocation.is(":hidden")) {
			$j(this).addClass("open");
			thisLocation.slideDown("slow");
		} else {
			thisLocation.slideUp("fast");
			$j(this).removeClass("open");
		}
	});

	$j("#locations .hours dl dt:contains('No drive up window')").addClass("noDriveThrough");
	$j("#locations .hours dl dt:contains('No banking lobby')").addClass("noLobby");

	$j("#obLogin #obMore h3, #homeFeaturedProducts ul li, #featuredProduct, #secondaryProducts ul li, #locations h2").hover(
		function(){
			$j(this).addClass("active").css("cursor", "pointer");
		},
		function(){
			$j(this).removeClass("active");
		}
	);
	
	//$j("#forSale ul li img").parent().append("<img src=\"/custom/bankfirstfs/image/for-sale-background.png\" class=\"background\" alt=\"\"");
	$j("#meetOurLenders ul li img").parent().append("<img src=\"/custom/bankfirstfs/image/meet-our-lenders-background.png\" class=\"background\" alt=\"\"");

	$j('#helpfulSites h2').addClass("dontend");
	$j('#helpfulSites h2, #ourTeam ul li').addClass("dontsplit");
	$j('#helpfulSites .columnize').columnize({ columns: 2, lastNeverTallest: true });
	$j('#miscellaneousServices ul').columnize({ columns: 2, lastNeverTallest: true });
	$j('#ourTeam ul').columnize({ columns: 2, lastNeverTallest: true });

	$j("ol.applicationSteps li.active").prevAll().addClass("complete");

	/* target="_blank" */	
	$j("a[href*='http://']:not([href*='"+location.hostname+"']),[href*='https://']:not([href*='"+location.hostname+"'])").attr("target","_blank").attr("title","Opens new window");

	productTabs();
});

Cufon.set("selector", jQuery);
Cufon.replace("#homeFeaturedProducts h3", { textShadow: "#16293D 1px 0 3px" });
Cufon.replace("#productHead h1");
Cufon.replace(".individualPage h1, fieldset#newApplication div#introduction h1, .disclosures h1, body.inmo #mainContent h1", { separate: "none" });
