var $j=jQuery.noConflict();
var alertText = "Please be advised that you are leaving First Community Bank's website. This link is provided as a courtesy. First Community Bank does not endorse or control the content of third party websites.";
var customFolder = "fcb-al";
var slideSpeed = 500;
var tabEffect = "fade";
var url = window.location.pathname;

$j(window).load( function(){
	$j('#flashDNA img').wrap('<a href="/personal/checking/first-reward-checking.html"/>');
}); // end window load

$j(document).ready( function() {
	
	productTabs();
	locations();

	$j(".confirm").click( function() {						   
		if (!confirmAlert($j(this).attr('href'))){
			return false;			
		}
	});
	
	// logo image
	$j('#logo').append('<img src=\"/custom/'+ customFolder +'/image/logo.png\" alt=\"First Community Bank\" />');

	// online banking login
	if ( $j('#obLogin').length > 0 ) {
		$j('#obLogin form label').overlabel();
		$j('#obLogin input').not('#obLogin input#oblSubmit').css({width: 155});
		$j("#obNav a").attr("target", "_blank"); 
	}
	if ( $j('body.popup #obLogin').length > 0 ) {
		$j('#obLogin label').overlabel();
		$j('.popup #obLogin input').not('.popup #obLogin input#oblSubmit').css({width: 194});
		var listLength = $j("#randomList li").length;
		var ran = Math.ceil(Math.random() * listLength);
		$j("body.popup #randomList li:nth-child(" + ran + ")").css("display", "list-item");
		$j("body.popup a").attr("target", "_parent");
	}
	
	// featured products
	if ($j.browser.msie && !($j.browser.version.substr(0,1) < 7) || !($j.browser.msie)){
		if( $j('#featuredProducts').length > 0 ){
			$j('#featured li, #currentNews li').hover(function() {
				$j(this).addClass('hover');
			}, function() {
				$j(this).removeClass('hover');
			});
		}
	}
	$j('#featuredProducts #featured li.nthChild-2 a').each( function(){
		this.target = "_blank";
	});

	// left nav
	if($j('#leftNav').length > 0){
		var prevH3A = $j('#leftNav h3 a.inPath').parent().parent().prev('li').find('h3 a');
		if(prevH3A.length > 0) {
			prevH3A.css({borderBottom: 'none'});
		}
	}
	
	if($j('#blurbWithImage').length){
		if ($j.browser.msie && $j.browser.version < 7){
			$j('#blurbWithImage p.textVariant1').removeClass('textVariant1');
		}
	}
	// custom category pages
	if($j('#categoryPage').length > 0){
		$j('#categoryPage #catBreadcrumb a').css({letterSpacing: '-5px'});
		var ulBizServWidth = $j('#catBreadcrumb .nthTier-2 a').width() * 1.61;
		if( url.indexOf("/business/business-services") != -1 ) {
			$j('#catBreadcrumb a').not('#catBreadcrumb .nthTier-2 a').hide();
			$j('#categoryPage #catBreadcrumb').css({right: '0'}).width(ulBizServWidth);
		}	if( url.indexOf("/personal/loans") != -1 ) {
			$j('#catBreadcrumb a').not('#catBreadcrumb .nthTier-2 a').hide();
			$j('#categoryPage #catBreadcrumb').width(ulBizServWidth);
		} else {
			var ulWidth = $j('#catBreadcrumb a').not('#catBreadcrumb .nthTier-2 a').width() * 1.71 + $j('#catBreadcrumb .nthTier-2 a').width() * 1.71;
			$j('#categoryPage #catBreadcrumb').width(ulWidth);
		}
	}
	
	// adding a hover class
	$j('#productCategory li, #search li').not('#productCategory li li, #home #obLogin').hover(function(){
			$j(this).addClass('hover');
		},function(){
			$j(this).removeClass('hover');
		});
	$j('#productCategory li h2 a:contains("IRAs")').parent().css({fontSize: '1.5em'});

	// clickable containers
	$j("#homeFeaturedProducts li, #featured li.nthChild-1,#featured li.nthChild-2, #productCategory li, #relatedProducts li, #search li, #currentNews li").css({cursor: "pointer"}).click(function(){
			window.location=$j(this).find("a").attr("href");
	});
	/*$j("#featured li.nthChild-2").css({cursor: "pointer"}).click(function(event){
			window.open("https://www.regulation-e.com/form/OptInConfirm_FTCB.aspx?kc=FTCB847593");
			event.preventDefault();
	});*/
	
	// footer hover popups
	$j("#footer #fdic a").parent().append("<div id=\"fdicDisclaimer\" class=\"footerPopup\"></div>");
	$j("#footer #eh a").parent().append("<div id=\"ehDisclaimer\" class=\"footerPopup\"></div>");
	$j(".footerPopup").hide();
	$j("#footer #fdic a, #footer #eh a").hover(function(){
		$j(this).parent().find(".footerPopup").show();
	}, function(){
		$j(this).parent().find(".footerPopup").hide();
	});
	
	// resize h1 for certain product pages
	if( url.indexOf("/personal/savings/certificates-of-deposit-cds.html") != -1 || url.indexOf("/business/savings/certificates-of-deposit-business-cds.html") != -1 ) {
		$j('h1').css({fontSize: "2.2em"});
	}

			
	// New page for INMO
	$j('a.newPage').each( function(){
		this.target = "_blank";
	});

	// FancyBox popup on applications
	$j("a#ssnWhy").fancybox({
		'frameWidth': 300,
		'frameHeight': 190
	});
	
	
	// Careers Page
	$j('#careers p, #careers ul').hide();
	$j('#careers h3 + p').show().addClass('alwaysOpen');
	$j('#careers h3').click(function() {
				if ($j('#careers h3 span.less')){
					$j('#careers h3 span').removeClass('less');}
				else {
					$j('#careers h3 span').addClass('less');
					}
				
				$j(this).next().nextUntil('#careers h3').slideToggle(slideSpeed);
	});	
	
	// Tell Me More, calculator, and online banking login popups provided by FancyBox 
	$j('#questionsButton a, #questionsLink a, #obLogin h2 a, #financialCalculators li a, #productPage #contactUsLink a').addClass('iframe');
	
	$j("#questionsButton a, #questionsLink a, #productPage #contactUsLink a").fancybox({
		'hideOnContentClick': false,
		'frameWidth': 540,
		'frameHeight': 340,
		'padding': 3,
		'overlayOpacity': 0.8
	});
	
	$j("#obLogin h2 a").fancybox({
		'hideOnContentClick': false,
		'frameWidth': 400,
		'frameHeight': 284,
		'padding': 3,
		'overlayOpacity': 0.8
	});

	$j("#financialCalculators li a").fancybox({
		'hideOnContentClick': false,
		'frameWidth': 650,
		'frameHeight': 400,
		'padding': 10,
		'overlayOpacity': 0.8
	}); 

});

Cufon.set("selector", jQuery);
Cufon.replace("#primaryNav, #obLogin h2, #leftNav li h2", { fontFamily: "Futura Std", fontWeight: "400", hover: true});
Cufon.replace("h1", { fontFamily: "Annifont", fontWeight: "400", letterSpacing: "-2px", hover: true});
Cufon.replace("#catBreadcrumb a", { fontFamily: "Annifont", textShadow: "1px 1px 1px #000", letterSpacing: '-5px', fontWeight: "400", hover: true});

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; 
}

function locations(){
	if( $j('#locations').length > 0 )	{
		$j("#locations .driveHours:not(:has('dl'))").html("<p>No Drive Thru at this location</p>");
		$j("#locations h2:contains('Montlimar')").parent().find(".driveHours").html("<p>Administrative and Commercial Lending Services</p>");
		$j('#locList li').not('li:eq(0)').find('.contactDetails').hide();
		$j('#locList li:eq(0)').find('h2').addClass('active');
		$j('.adr span').unbind('click');
		$j('#locList h2').css({cursor: 'pointer'}).click(function(){
			var thisLoc = $j(this).parent().find('.contactDetails');
			if (thisLoc.is(':visible')) {
				$j(this).removeClass('active');
				thisLoc.slideUp();
			} else {
				$j(this).addClass('active');
				thisLoc.slideDown();
			}
		});
	}
}

function productTabs(){
	if( $j('#productTabs').length > 0 ){
		var offset = 40;
		$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);									 
					});
				}
			}
		});
	}
}
