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

$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 popup on applications
	$j("a#ssnWhy").fancybox({
		'frameWidth': 300,
		'frameHeight': 190
	});
	
	// Tell Me More, calculator, and online banking login popups provided by FancyBox 
	$j('#questions a, #downstreamObl a:contains("Online Banking"), #financialCalculators li a').addClass('iframe');
	
	$j("#questions a").fancybox({
		'hideOnContentClick': false,
		'frameWidth': 540,
		'frameHeight': 340,
		'overlayOpacity': 0.8
	});
	
	$j("#downstreamObl a").fancybox({
		'hideOnContentClick': false,
		'frameWidth': 400,
		'frameHeight': 100,
		'overlayOpacity': 0.8
	});

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

// Make links in online banking popup open in parent
	$j('#poppers #obNav li a').attr('target','_parent');
	
// 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 #wrapper').addClass(secondary);
		}
	});


//	Search Overlabel	
	var initialText = "Search";
	
	$j("#roi_input").val(initialText);
	
	$j("#roi_input").focus(function () {
		if($j(this).val() == initialText) {						 
			$j(this).val("");
		}
	});
	
	$j("#roi_input").blur(function () {
		if($j(this).val() == "") {						 
			$j(this).val(initialText);
		}
	});
	
	

//	Overlabel
	var labelFor = $j('#obLogin form label[for]');
	var inputFor = $j('#obLogin form input').attr('id');
	if (labelFor !== inputFor) {
/*
		if ( inputFor.has().text() ) {
			$j('#obLogin label.overLabel').css({
				'text-indent': '-9999px'
			} else
			$j('#obLogin label.overLabel').css({
				'text-indent': '0'
			});
		}
*/
		labelFor.addClass('overLabel');
		$j('#obLogin input').focus(function(){
			$j('#obLogin label.overLabel').css({
				'text-indent': '-9999px'
			});
		});
		$j('#obLogin input').blur(function(){
			$j('#obLogin label.overLabel').css({
				'text-indent': '0'
			});
		});	
	}

/*
	var searchValue = $j('input[value=Submit]');
	if ( searchValue ) {
		$j(this).focus(function(){
			$j(this).css({
				'text-indent': '-9999px'
			});
		});
		$j(this).blur(function(){
			$j(this).css({
				'text-indent': '-9999px'
			});
		});
	}	
*/
	
	/* Active, clickable container */
	$j("#homeFeaturedProducts ul li, #featuredProduct, #categoryPage ul li, #searchResults ul li, div#homeFeaturedProducts dl dd, #productIntro .imageClicker, #productIntro .imageClickerOpen, #productIntro .imageClickerApply, div#newsInformation li").click(function(){
		window.location=$j(this).find("a").attr("href");
	});

	$j("#homeFeaturedProducts ul li, #featuredProduct, #categoryPage ul li, #locations h2, #searchResults ul li").hover(
		function(){
			$j(this).addClass("active").css("cursor", "pointer");
		},
		function(){
			$j(this).removeClass("active");
		}
	);
	
//	HOME PAGE 	
if ( $j('body#home').length > 0 ) {

//	home featured
	if ( $j('#homeFeaturedProducts').length > 0 ) {
		$j('div#homeFeaturedProducts li').hover(function(){
			$j('h1', this).css({
				color: '#0099FF'
			});
			$j('a', this).css({
				textDecoration: 'underline'
			});
			$j(this).css({
				cursor: 'pointer'
			});
		}, function(){
			$j('h1', this).css({
				color: '#003987'
			});
			$j('a', this).css({
				textDecoration: 'none'
			});
			$j(this).css({
				cursor: 'auto'
			});
		});
	}
//	news and information	
	if ( $j('#newsInformation').length > 0 ) {
		$j('#newsInformation li').hover(function(){
			$j('a').attr('target', '_self');
			$j(this).css({
				cursor: 'pointer',
				color: '#333333'
			});
			$j('a', this).css({
				textDecoration: 'underline'
			});
		}, function(){
			$j('a').attr('target', '_self');
			$j(this).css({
				cursor: 'auto',
				color: '#666666'
			});
			$j('a', this).css({
				textDecoration: 'none'
			});
		});
	}
// cycle photos
	$j('#featuredContainer').after('<div id="featuredNav">').cycle({
		fx: 'fade',
		speed:  1000, 
		timeout: 8000, 
		delay: 1000,
		pager:  '#featuredNav'
	});

	//  nthChild-i++ for a tag  
	var elements = $j('#featuredNav a');                                          	
	var indexA = 1;
	$j.each(elements, function(){
		$j(this).attr('id', 'nthChild-' + indexA);
		indexA++;
	});        
}
	
	if ( $j('#productIntro').length > 0 ) {
		$j('#productIntro .imageClicker').hover(function(){
			$j(this).css({
				cursor: 'pointer'
			});
			$j('h3 a').css({
				backgroundPosition: '0 -129px'
			});
		}, function(){
			$j(this).css({
				cursor: 'auto'
			});
			$j('h3 a').css({
				backgroundPosition: '0 -100px'
			});
		});
	}
	
	if ( $j('#productIntro').length > 0 ) {
		$j('#productIntro .imageClickerOpen').hover(function(){
			$j(this).css({
				cursor: 'pointer'
			});
			$j('h3 a').css({
				backgroundPosition: '0 -229px'
			});
		}, function(){
			$j(this).css({
				cursor: 'auto'
			});
			$j('h3 a').css({
				backgroundPosition: '0 -200px'
			});
		});
	}
	
	if ( $j('#productIntro').length > 0 ) {
		$j('#productIntro .imageClickerApply').hover(function(){
			$j(this).css({
				cursor: 'pointer'
			});
			$j('h3 a').css({
				backgroundPosition: '0 -465px'
			});
		}, function(){
			$j(this).css({
				cursor: 'auto'
			});
			$j('h3 a').css({
				backgroundPosition: '0 -436px'
			});
		});
	}

	// Add name to category title
	var categoryName = $j('#primaryNav li.inPath a').text() + " ";
	var subCategory = $j('#leftNav li h2 a.inPath').text();
	//console.log(subCategory);
	if (!(subCategory.indexOf("Investments") >= 0)) {
		$j("#categoryPage h1").prepend(categoryName);
	}

	// Add class to longer titles
	if ((url.indexOf("business/checking") != -1) || (url.indexOf("terms-and-conditions.html") != -1) || (url.indexOf("lost-or-stolen-card.html") != -1) || (url.indexOf("online-banking-faq.html") != -1)) {
		$j("body").addClass("longTitle");
	}

	/* target="_blank" */
	$j("a[href*='http://']:not([href*='"+location.hostname+"']),[href*='https://']:not([href*='"+location.hostname+"'])").attr("target","_blank").attr("title","Opens new window");
	
	if ( url.indexOf("/services/online-banking.html") != -1) {
		$j('#productPage #productDetails dl dd h3 a').addClass('videoHere');
	}
	
	if ( url.indexOf("/services/online-banking.html") != -1 ) {
		$j('dl dt:contains("FAQs")').attr('id', 'faqIndex');
	}
	
	
});

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

Cufon.set("selector", jQuery);
Cufon.replace("#categoryPage h1, .individualPage h1, #locations h1", {textShadow: "rgba(0,0,0,0.65) 0 -2px 2px"});

function setAction()	{

	//Fill in the Routing number below.
	var rtNum = "022309611";
	var x = document.forms.Remote;

	if(x.loginTo[0].submit()) {
		x.action = 'https://www.aldenstate.net/pbi_pbi1961/pbi1961.asp?WCI=RemoteLogin&Rt=022309611&LogonBy=Connect3&PRMAccess=Account'; 
					
		} else {
			x.nmUID.value = x.AccessID.value;
			x.nmRTN.value = rtNum;
			x.action = 'https://www.aldenstate.net/pbi_pbi1961/pbi1961.asp?WCI=RemoteLogin&Rt=022309611&LogonBy=Connect3&PRMAccess=Account';
		}
	}


	function doLoginRefresh()	{
		var x = document.forms.Remote;
		if (x.AccessIDVisible.value != "") {
			x.AccessID.value = x.AccessIDVisible.value;
			x.AccessIDVisible.value = "";
				return true;
			} else {
				alert("Please Enter a valid Access ID.  Thank you!"); 
			return false;
		} 
	}

