var $j=jQuery.noConflict(),
	alertText = "Please be advised that you are leaving CP Federal Credit Union's website. This link is provided as a courtesy. CP Federal Credit Union does not endorse or control the content of third party websites.",
	customFolder = "_templateSECONDBranch",
	slideSpeed = 500,
	tabEffect = "fade",
	addthis_config = {ui_click: true};


$j(document).ready( function() {

	// cufon text replacements
	Cufon.replace('#productPage h1, .individualPage h1, #productCategory h1, h2#thankyou, #regE h1',
		{fontFamily: 'Just Me Again Down Here', fontSize: '56px'});
	
	// cufon text replacements
	Cufon.replace('#primaryNav',
			{fontFamily: 'Arial Narrow', fontSize: '18px'}); 
	
	 
	Cufon.replace('#leftNav h2 a',
		{fontSize: '19px', hover: true, fontFamily: 'TradeGothic'});
			
	Cufon.replace('#financials h3',
			{fontSize: '18px', hover: true, fontFamily: 'TradeGothic'});
			 
	Cufon.replace('body.popup h1',
		{fontSize: '19px', fontFamily: 'TradeGothic'});
	
	Cufon.replace('#thankYou h1',
		{fontFamily: 'Just Me Again Down Here', fontSize: '84px'});
				
	Cufon.replace('#rates th',
		{fontFamily: 'TradeGothic'});
	
	if($j('body#home').length) {
		Cufon.replace('.bottom h2, .bottom h3',
			{fontSize: '18px', fontFamily: 'TradeGothic'});
		Cufon.replace('#obLogin h2',
			{fontSize: '19px', fontFamily: 'TradeGothic'});
	}
	 
	
 	addthis_config = {ui_click: true};
	// replace body .no-js with .js 
	$j('body').removeClass('no-js').addClass('js');
	if( $j('#productTabs').length){
		productTabs();
	}

	//	Overlabel - place a div with the class 'overLabelWrapper' around whatever will be using this code. Make sure that the label is set to display block
	if($j('.overLabelWrapper').length){
		var labelFor = $j('.overLabelWrapper form label[for]'),
		 	inputFor = $j('.overLabelWrapper form input').attr('id');
		if (labelFor !== inputFor) {
			labelFor.addClass('overLabel');
			$j('.overLabelWrapper input[type=text]').addClass('overLabel');
			$j('.overLabelWrapper input[type=password]').addClass('overLabel');
			$j('.overLabelWrapper input.overLabel').focus(function(){
				$j(this).prev().css({
					'text-indent': '-9999px'
				});
			});
			$j('.overLabelWrapper input.overLabel').blur(function(){
				if($j(this).val() == ''){
					$j(this).prev().css({
						'text-indent': '0'
					});
				}
			});	
		}
	}
	
	
	$j(".confirm").click( function() {						   
		if (!confirmAlert($j(this).attr('href'))){
			return false;			
		}
	});
	
	clickableContainer($j("#productCategory li"));
	if('body#home') {
		clickableContainer($j("#features .feature"));
		clickableContainer($j("#features .feature"));
	}
	
	// IE only 
	if ( $j.browser.msie ) {
    	var hash = window.location.hash;
		//if(hash == '#shadow') {
			
			$j('.pageImage').before('<div class="ieShadow">&nbsp;</div>');
		//}
 	} 
	else {
    
		
 	} 
	/*
	// add class to quickLinks li - use first word + first letter of second word + Ql
	// delete this is your quicklinks do not have unique icons
	if($j('#quickLinks').length) {
	     var quickLinksA = $j('#quickLinks li a');
	     $j.each(quickLinksA, function() {
	          var qlText = $j(this).text().split(/\b/)[0].toLowerCase(),
	          qlText2 = $j(this).text().split(/\b/)[2].slice(0,1).toUpperCase() || '',
	          qlClass = qlText + qlText2 + 'Ql';
	          $j(this).parent().addClass(qlClass);
	     });
	}
	*/
	/*
	// New page for INMO
	$j('a.newPage').each( function(){
		this.target = "_blank";
	});

	if($j('#newApplication').length){
		$j('#ssnWhy').click(function(e){
			$j('#ssnExplanation').slideDown(); 
			$j('#ssnCloseWrapper').slideDown();
			e.preventDefault();
		});
		$j('#ssnClose').click(function(e){
			$j('#ssnExplanation').slideUp();
			$j('#ssnCloseWrapper').slideUp();
			e.preventDefault();
		});
	}

	*/

});


$j(window).load(function(){
	$j('#questions a, #header a.obl, #financialCalculators li a').addClass('iframe');
	$j("#header a.obl").fancybox({
		'hideOnContentClick': false,
		'frameWidth': 240,
		'frameHeight': 227,
		'padding': 0,
		'overlayOpacity': 0.8,
		'overlayColor': '#000'
	});
	$j("#questions a").fancybox({
		'hideOnContentClick': false,
		'frameWidth': 471,
		'frameHeight': 525,
		'padding': 0,
		'overlayOpacity': 0.8,
		'overlayColor': '#000'
	});
	$j("#financialCalculators li a").fancybox({
		'hideOnContentClick': false,
		'frameWidth': 650,
		'frameHeight': 394,
		'padding': 10,
		'overlayOpacity': 0.8,
		'overlayColor': '#000'
	});
	
	if( $j('#search').length ){     // search page clickable
	    $j('#froisearch-results ul').addClass('clickable');
        $j('#froisearch-results li').live('click', function(){
            window.location = $j(this).find('a:first').attr('href');
        });
    }
	
	$j(".slideToggler").click(function(){
		$j(this).toggleClass('active');
		$j(this).next().slideToggle();
	});
	
	$j("a.viewTips").click(function(){
		$j(".slide").slideToggle();
		return false;
	});
	
	$j("a.warn").click(function(){
		var href = $j(this).attr("href");
		if(!href)
			return true;
			
		window.open(href);
		
		return false;
	});
	
	$j('#memberList .memberList h2').click(function(){
		$j(this).next().slideToggle();
		$j(this).toggleClass('on');
	});
	
	$j('#memberList .memberMultiList h2').click(function(){
		$j(this).parent().toggleClass('active');
		$j(this).toggleClass('on');
		$j(this).next().slideToggle();
	});
	
	$j('.alphaNav p').click(function(){
		
		var index = $j('.alphaNav p').index(this);
		$j('.memberMultiList .list').hide();
		$j('.memberMultiList .list:eq('+index+')').show();
		$j('.alphaNav p').removeClass('on');
		$j(this).addClass('on');
	});
	
	$j('#locList li').click(function(){
		  $j('html, body').animate({scrollTop:0}, 'slow');
	});
	
	$j('#locList li').click(function(){
		var i = gMap.getInfoWindow();
		var p = i.getPoint();
		gMap.setCenter(p, 13);
		
	});
	
	$j('#rates td.ge a').click(function(){
		window.location = $j(this).attr('href') + '#ge';
		return false;
	});
	
	if($j('#productPage').length){
		
		if(window.location.hash == '#ge') {
			$j('#productTabs dt').each(function(index, elem){
				
				var html = $j(this).html().toLowerCase();
				if(html == 'great expectations') {
					
					$j(elem).click();
				}
			});
		}
	}
	
	if(window.location.pathname =='/financial-education/fraud-protection.html') {
		$j('#leftNav').addClass('noChildren');
	}
	
});

// clickable container
function clickableContainer(target){
	target
		
		.click( function(){
			var href = $j(this).find("a").attr("href");
			if(!href)
				return true;
				
			if( href.indexOf('.pdf') >= 0 )
				window.open(href);
			else
				window.location = href;
			return false;
			});
		
}

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(){
	
		var offset = 27,
		productTabsContainer = $j('#productTabs'),
		productTabsDT = $j('#productTabs dt'),
		productTabsDD = $j('#productTabs dd');

		productTabsDT.addClass('label');
		$j('#productTabs dt:eq(0)').addClass('selected');
		productTabsDD.addClass('pane').wrapInner('<div class="tabDiv"></div>');
		productTabsDD.css('position','absolute');
		productTabsDD.not("dd:eq(0)").hide();
		productTabsContainer.css('height', $j('#productTabs dd:eq(0)').innerHeight() + offset);

		productTabsDT.click( function(){
			var holdThis = $j(this);
			if( !holdThis.hasClass('selected') ){
				productTabsDT.removeClass('selected');
				holdThis.addClass('selected');
				if(tabEffect === "slide"){
					productTabsDD.slideUp(slideSpeed);
					productTabsContainer.animate({ height : holdThis.next().innerHeight() + offset }, slideSpeed, function(){
						holdThis.next().slideDown(slideSpeed);									 
					});
				}
				else{
					productTabsDD.fadeOut(slideSpeed);
					productTabsContainer.animate({ height : holdThis.next().innerHeight() + offset }, slideSpeed, function(){
						holdThis.next().fadeIn(slideSpeed);									 
					});
				}
			}
		});
	
}
