var $j=jQuery.noConflict();
var alertText = "Links to external websites are provided by Solvay Bank for your convenience. Use of such links on our website are not an endorsement or warranty by Solvay Bank of the information, products or services provided by such external websites. Solvay Bank does not control such websites. You should review the linked sites privacy and security policies as they may be different from Solvay Bank's.";
var slideSpeed = 500;
var tabEffect = "fade";

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

	$j(".confirm").click( function() {
		if (!confirmAlert($j(this).attr('href'))){
			return false;			
		}
	});
	
	if( $j('body.popup').length <= 0 )
	{	// New popups provided by FancyBox 
		$j("a#ssnWhy").fancybox({
			'frameWidth': 300,
			'frameHeight': 190
		});
	
			$j('#notifyMeButton a').addClass('iframe');

			$j("#notifyMeButton a").fancybox({
				'hideOnContentClick': false,
				'frameWidth': 540,
				'frameHeight': 340,
				'overlayOpacity': 0.8
			});
			
		$j('#tellMeMoreButton a').addClass('iframe');
	
		$j("#tellMeMoreButton a").fancybox({
			'hideOnContentClick': false,
			'frameWidth': 540,
			'frameHeight': 340,
			'overlayOpacity': 0.8
		});
		
		$j('a#downstreamObl').addClass('iframe');
		
		$j("a#downstreamObl").fancybox({
			'hideOnContentClick': false,
			'frameWidth': 250,
			'frameHeight': 280,
			'overlayOpacity': 0.8
		});
		
		$j('#financialCalculators li a').addClass('iframe');
	
		$j("#financialCalculators li a").fancybox({
			'hideOnContentClick': false,
			'frameWidth': 660,
			'frameHeight': 404,
			'padding': 10,
			'overlayOpacity': 0.8
		});

	}


	/* Click and hover functionality */
	$j("#featuredProduct, #secondaryProducts ul li, #locations h2").hover(
		function(){
			$j(this).addClass("active").css("cursor", "pointer");
		},
		function(){
			$j(this).removeClass("active");
		}
	);

	/* target="_blank" */
	if($j('#wrapper').find('a').attr('src') == 'http://mortgages.solvaybank.com/') {
		return false;
	} else {
		$j("a[href*='http://']:not([href*='"+location.hostname+"']),[href*='https://']:not([href*='"+location.hostname+"'])").attr("target","_blank").attr("title","Opens new window");
	}

	//home featured
	$j("#homeFeaturedProducts li").click(function(){
		
        window.location=$j(this).find("a").attr("href");
	});
	$j("#homeFeaturedProducts li").hover(function(){
        $j(this).addClass('hover');
	}, function(){
		$j(this).removeClass('hover');
	});
	
	//Category pages
	$j('#productCategory li').click(function(){
			window.location = $j(this).find('h3 a').attr('href');
		});
	
	$j("#productCategory li").hover(function(){
		$j(this).addClass('hover');
	}, function(){
		$j(this).removeClass('hover');
	});
	
	$j("#productCategory li:first").css('border', '0px');

	//Community news
	if( $j('#homeCommunityText').length > 0 ){
		if( $j('#homeCommunityText p').size() > 1 ){
			$j('#homeCommunityText p').hide();
			$j('#homeCommunityText p:eq(0)').show().addClass('visible');
			
			$j('#homeCommunityText').before('<span id="prevP">Previous</span>').after('<span id="nextP">Next</span>');
			
			$j('#prevP').live( 'click', function(){
				var holdThis = $j('#homeCommunityText p.visible');
				if( $j(holdThis).prev().length > 0 ){
					$j(holdThis).hide().removeClass('visible');
					$j(holdThis).prev().fadeIn().addClass('visible');
				}
				else{
					$j(holdThis).hide().removeClass('visible');
					$j('#homeCommunityText p:last').fadeIn().addClass('visible');
				}
			});
			$j('#nextP').live( 'click', function(){
				var holdThis = $j('#homeCommunityText p.visible');
				if( $j(holdThis).next().length > 0 ){
					$j(holdThis).hide().removeClass('visible');
					$j(holdThis).next().fadeIn().addClass('visible');
				}
				else{
					$j(holdThis).hide().removeClass('visible');
					$j('#homeCommunityText p:first').fadeIn().addClass('visible');
				}
			});
		}
	}
	
	if (document.referrer.indexOf("campus-connection-account") != -1) {
		$j('#productTabs').addClass("student");
	}

	//OLB
	if( $j('#obLoginWrapper').length > 0 ){
		$j('#obLoginWrapper ul').css('display', 'block');
		$j('#obLoginWrapper h4').hide();
		$j('#obLoginWrapper ul li:eq(0)').addClass('selected');
		$j('#obLoginWrapper form:eq(1)').hide();
		
		$j('#obLoginWrapper ul li:eq(0)').click( function(){
			if( !$j(this).hasClass('selected') ){
				$j('#obLoginWrapper ul li:eq(1)').removeClass('selected');
				$j(this).addClass('selected');
				$j('#obLoginWrapper form:eq(1)').hide();
				$j('#obLoginWrapper form:eq(0)').fadeIn();
			}
		});
		$j('#obLoginWrapper ul li:eq(1)').click( function(){
			if( !$j(this).hasClass('selected') ){
				$j('#obLoginWrapper ul li:eq(0)').removeClass('selected');
				$j(this).addClass('selected');
				$j('#obLoginWrapper form:eq(0)').hide();
				$j('#obLoginWrapper form:eq(1)').fadeIn();
			}
		});
	}
	
	//Product Page
	$j('#tellMeMoreButton img').hover( function(){
		$j(this).attr('src', '/custom/solvaybank/image/questionsButtonHover.png');
	}, function(){
		$j(this).attr('src', '/custom/solvaybank/image/questionsButton.png');
	});
	
	//New Online Bill Pay
	$j('#notifyMeButton img').hover( function(){
		$j(this).attr('src', '/custom/solvaybank/image/notify-me-hover.png');
	}, function(){
		$j(this).attr('src', '/custom/solvaybank/image/notify-me.png');
	});
	
	//Meh
	/*if( $j('#leftNav li.selected').length > 0 ){
		console.log('asdf');
		if( !($j(this).children('ul').length > 0) ){
			console.log('asdf');
			$j(this).css('padding-bottom', '5px');
		}
	}*/
	
	//Locations Page
	if( $j('#locList').length > 0 ){
		$j('.accordionContent').hide();
		$j('.accordionContent:first').show().parent().addClass('open');
		$j('#locList h2').click( function(){
			if( $j(this).parent().hasClass('open') ){
				$j(this).parent().removeClass('open');
				$j(this).next().slideUp();
			}
			else{
				$j(this).parent().addClass('open');
				$j(this).next().slideDown();
			}
		});
	}
	
	productTabs();
});

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 ){
		$j('#productTabs').addClass('on');
		var offset = 80;
		$j('#productTabs dt').addClass('label');
		
		if( $j('#productTabs').hasClass('student') && $j('#productTabs dd:eq(2)').length > 0 ){
			$j('#productTabs dt:eq(2)').addClass('selected');
			$j('#productTabs dd').not("dd:eq(2)").hide();
		}
		else{
			$j('#productTabs dt:eq(0)').addClass('selected');
			$j('#productTabs dd').not("dd:eq(0)").hide();
		}
		
		$j('#productTabs dd').addClass('pane');
		$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);									 
					});
				}
			}
		});
		
		
		$j('#productTabs dt').each( function(){
			if( $j(this).text().indexOf('FAQ') == 0 ){
				var holdThis = $j(this);
				holdThis.next().children('.textVariant1').css('cursor', 'pointer');
				holdThis.next().children('.textVariant1').each( function(){
					$j(this).nextUntil('.textVariant1').hide();
				});
				holdThis.next().children('.textVariant1:eq(0)').nextUntil('.textVariant1').show();
				
				holdThis.next().children('.textVariant1').hover( function(){
					$j(this).css('text-decoration', 'underline');
				}, function(){
					$j(this).css('text-decoration', 'none');
				});
				holdThis.next().children('.textVariant1').click( function(){
					holdThis.next().children('.textVariant1').each( function(){
						$j(this).nextUntil('.textVariant1').slideUp();
					});
					$j(this).nextUntil('.textVariant1').slideDown();
				});
			}
		});
	}
}

