var $j=jQuery.noConflict();
var alertText = "Please be advised that you are leaving Community National Bank's website. This link is provided as a courtesy.  Community National Bank does not endorse or control the content of third party websites.";
var customFolder = "commnatlbank";
var slideSpeed = 500;
var tabEffect = "fade";	//slide, fade

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

	productTabs();

	$j(".confirm").click( function() {						   
			if (!confirmAlert($j(this).attr('href'))){
				return false;			
			}
		});
		
// new page for INMO
	$j('a.newPage').each( function(){
		this.target = "_blank";
	});

// FancyBox 
	$j("a#ssnWhy").fancybox({
		'frameWidth': 300,
		'frameHeight': 190
	});

	$j('#tellMeMoreButton a').addClass('iframe');
	
	$j("#tellMeMoreButton a").fancybox({
		'hideOnContentClick': false,
		'frameWidth': 540,
		'frameHeight': 360,
		'overlayOpacity': 0.8
	});
	
	$j('#shareButton a').addClass('iframe');
	
	$j("#shareButton a").fancybox({
		'hideOnContentClick': false,
		'frameWidth': 570,
		'frameHeight': 520,
		'overlayOpacity': 0.8
	});
	
	$j('#ultraTopNav li a:contains("Online Banking")').addClass('iframe');
	
	$j("#ultraTopNav li a.iframe").fancybox({
		'hideOnContentClick': false,
		'frameWidth': 230,
		'frameHeight': 190,
		'overlayOpacity': 0.8
	});
	
	$j('body.popup #downstreamOBLnav a').attr('target','_parent');
	
	$j('#financialCalculators li a').addClass('iframe');

	$j("#financialCalculators li a").fancybox({
		'hideOnContentClick': false,
		'frameWidth': 650,
		'frameHeight': 400,
		'padding': 10,
		'overlayOpacity': 0.8
	}); 
	
//	End of Fancybox jQuery
	
// add id to body
	$j(function(){
		if ($j('#primaryNav li a').hasClass("inPath")) {
			var primary = $j('#primaryNav li a.inPath').text().replace(/ /g,'');
			var secondary = $j('#leftNav li.inPath h2 a').text().replace(/ /g,'');
			$j('body').attr("id",primary);
			$j('body #wrapperAll').addClass(secondary);
		}
	});

//  Delay Drop Down for Leftnav
	if ($j("#leftNav li ul.nthTier-2").length){
	   // animate sub menu
	   $j("#leftNav li ul.nthTier-2").hide().delay(300).slideDown(400);
	 };

//	Left Nav - h3 tags	
	if($j('#leftNav').length > 0){
		$j('#leftNav li li a').not('#leftNav li li.inPath a').hover(
			function(){
				$j(this).stop().animate({ paddingLeft : '34px', backgroundPosition : '26px 4px'}, 500);
			},function(){
				$j(this).stop().animate({ paddingLeft : '26px', backgroundPosition : '16px 4px'}, 300);
			}
		);
	}
//	Left Nav - long title issue
	$j('#leftNav li h2 a').each(function() {
		if ($j(this).text().length > 22) {
			$j(this).parent().addClass('longTitle');
		}
	});

//	Overlabel 
	$j("#username label").overlabel();
	$j("#password label").overlabel();
	
//  Clickable Container Query  
	$j('#homeFeaturedProducts li, div#productCategory li, #search li').not("#productCategory li li").click(function(){
		  window.location = $j(this).find("a").attr("href"); return false;
	});

	
//	Home Featured Products - Hoverintent: backSide and frontSide function
	function backSide(){
		var divTier = $j('div.fadeout', this);
		// on hovering over, find the element we want to fade *up*
	    var fade = $j('> div', divTier);
	    // if the element is currently being animated (to a fadeOut)...
	    if (fade.is(':animated')) {
	      // ...take it's current opacity back up to 1
	      fade.stop().fadeTo(200, 1);
	    } else {
	      // fade in quickly
	      fade.fadeIn(200);
	    }

		$j('h4 a', this).stop().fadeIn('slow', function() {
			$j(this).css('color', '#FEB913');
			$j(this).stop().css('background-position', '70px -20px');
		});                                                
	};

	function frontSide(){
		var divTier = $j('div.fadeout', this);
		var fade = $j('> div', divTier);
	    if (fade.is(':animated')) {
	      fade.stop().fadeTo(200, 0);
	    } else {
	      // fade away slowly
	      fade.fadeOut(200);
	    }		

		$j('h4 a', this).stop().fadeIn('slow', function() {
			$j(this).css('color', '#C19C36');
			$j(this).stop().css('background-position', '70px 0');
		});
	};

	var homeFeaturedProducts = {    
		sensitivity: 70, // number = sensitivity threshold (must be 1 or higher)    
		interval: 100, // number = milliseconds for onMouseOver polling interval    
		over: backSide, // function = onMouseOver callback (required)    
		timeout: 100, // number = milliseconds delay before onMouseOut    
		out: frontSide // function = onMouseOut callback (required)    
	};

	$j("#homeFeaturedProducts li").hoverIntent(homeFeaturedProducts);

//	Category Pages
if($j('#productCategory').length > 0){
	$j('#productCategory li').css('cursor', 'pointer');
	$j("#productCategory li").not("#productCategory li li").hover(function(){
		$j('h3 a', this).stop().css("color", "#FDB813");
		$j('h4 a', this).stop().css('background-position', '0 -42px');		
	}, function() {
		$j('h3 a', this).stop().css("color", "#005900");
		$j('h4 a', this).stop().css('background-position', '0 0');		
	});
}

//	Search Page
if($j('#search').length > 0){
	$j('#search li').css('cursor', 'pointer');
	$j("#search li").not("#search li li").hover(function(){
		$j('h3 a', this).stop().css("color", "#FDB813");
		$j('h4 a', this).stop().css('background-position', '0 -42px');		
	}, function() {
		$j('h3 a', this).stop().css("color", "#005900");
		$j('h4 a', this).stop().css('background-position', '0 0');		
	});
}

//  Articles	
	$j('#article h3').eq(0).addClass("active");
	$j('#article .stories').hide();            
	$j('#article h3').css('cursor', 'pointer');	       
	
	$j("#article h3").click(function(){	
		$j(this).next("div.stories").slideToggle("slow").siblings("div.stories:visible").slideUp("slow");
		$j(this).toggleClass("active");
		$j(this).siblings("h3").removeClass("active");
	});
	
// homepage custom select box
$j('body#home .oblSelect').customStyle();


}); // END doc ready

// custom select box
(function($){
 $.fn.extend({
 
 	customStyle : function(options) {
	  if(!$.browser.msie || ($.browser.msie&&$.browser.version>6)){
	  return this.each(function() {
	  
			var currentSelected = $(this).find(':selected');
			$(this).after('<span class="customStyleSelectBox"><span class="customStyleSelectBoxInner">'+currentSelected.text()+'</span></span>').css({position:'absolute', opacity:0,fontSize:$(this).next().css('font-size')});
			var selectBoxSpan = $(this).next();
			var selectBoxWidth = parseInt($(this).width()) - parseInt(selectBoxSpan.css('padding-left')) -parseInt(selectBoxSpan.css('padding-right'));			
			var selectBoxSpanInner = selectBoxSpan.find(':first-child');
			selectBoxSpan.css({display:'inline-block'});
			selectBoxSpanInner.css({width:selectBoxWidth, display:'inline-block'});
			var selectBoxHeight = parseInt(selectBoxSpan.height()) + parseInt(selectBoxSpan.css('padding-top')) + parseInt(selectBoxSpan.css('padding-bottom'));
			$(this).height(selectBoxHeight).change(function(){
				// added ($('option:selected',this).text() so that the text does not change to the value
				selectBoxSpanInner.text($('option:selected',this).text()).parent().addClass('changed');
			});
			
	  });
	  }
	}
 });
})(jQuery);
// END custom select box

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 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').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);									 
					});
				}
			}
		});
	}
}

