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

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

	productTabs();
	
	// test for ie6
	var IE6 = (navigator.userAgent.indexOf("MSIE 6")>=0) ? true : false;
	var IE7 = (navigator.userAgent.indexOf("MSIE 7")>=0) ? true : false;
	
	// style the obl select list
	if(!IE6){
		$j('.oblSelect').customStyle();
	}

	// add button text to Express e-Statements
	var eStatements = /\/express-e-statements/.test(window.location);
	if(eStatements) {
		$j('body').attr('id','estatements');
		$j('#applyNow a').text('e-Statements Login or Signup').addClass('longButton');
	}
	
	// add button text to online banking
	var enrollOnlineBanking = (/\/georgia-express-online-banking/).test(window.location);
	var eBizExpress = (/\/ebiz-express-online-banking/).test(window.location);
	if(enrollOnlineBanking || eBizExpress) {
		$j('#applyNow').attr('class','enroll');
		$j('#applyNow a').text('Enroll Now');
	}
	
	// clickable containers
	$j('#featured li, #productCategory li').click(function(){
		window.location = $j(this).find('a').attr('href');
	});
	
	// home featured rollovers
	if($j('#home').length && !IE6) {
		var featuredLi = $j('#featured li');
		featuredLi.hover(function(){
			$j(this).find('.image').stop(false,true).fadeOut(200);
			$j(this).find('.overButton').stop().hide();
			$j(this).find('.over').stop().animate({bottom:'-45px'}, 300);
			$j(this).find('h3').removeClass('featureH3');
		}, function(){
			$j(this).find('.image').stop(false,true).delay(200).fadeIn(200);
			$j(this).find('.over').stop().animate({bottom:'-152px'}, 200);
			$j(this).find('.overButton').stop().show();
			$j(this).find('h3').addClass('featureH3');
		});
	}
	
	// add class to quickLinks li - use first word + Ql
	if($j('#quickLinks').length) {
		var quickLinksA = $j('#quickLinks li a');
		$j.each(quickLinksA, function() {
			var qlText = $j(this).text().split(/\b/)[0].toLowerCase(),
				qlClass = qlText + 'Ql';
			$j(this).parent().addClass(qlClass);
		});
	}
	
	// add countries table to t for Visa Check Card
	if ($j('#visaTable').length) {
		var visaTable = $j('#visaTable').css({display:'block'}),
			visaTarget = $j('#productTabs dd:eq(2)');
		visaTarget.append(visaTable);
	}
	
	// faqs in the tabs		
	if ($j('#productTabs dt:contains("FAQ")').length && !IE7){
		
		var faqDd = $j('#productTabs dt:contains("FAQ") + dd'),
			faqP = $j('#productTabs dt:contains("FAQ") + dd p'),
			faqQuestion = $j('#productTabs dt:contains("FAQ") + dd p strong');

		// hide all the paragraphs
		faqP.hide();
		// show the paragraphs containing strong
		$j(faqP).filter(function(index) {
			return $j('strong', this).length;
		}).show().addClass('question');
		
		faqQuestion.click(function(){
			if($j(this).closest('p').hasClass('open')){
				$j(this).closest('p')
					.removeClass('open')
					.nextUntil('.question').hide();
			}
			else {
				$j(this).closest('p')
					.addClass('open')
					.nextUntil('.question').show();
			}
			// recalculate dl#productTabs height
			var faqDdHeight = faqDd.height() + 30;
			$j('#productTabs').animate({height: faqDdHeight}, 150);
		});
	}
	
	// rotate tagline 
	function rotateTagline () {
		var holdThis = $j('#tagline span:visible');
		if(holdThis.next('span').length){
			holdThis.fadeOut(200, function(){
				holdThis.next().fadeIn(400);
			});
		}
		else{
			holdThis.fadeOut(200, function(){
				$j('#tag1').fadeIn(500);
			});
		}
	};
	var rotateAuto = setInterval(rotateTagline, 4000);
	
	// productCategory rollovers
    $j("#productCategory ul li").not("#productCategory ul li li")
    	.hover(function(){
       		$j(this).stop().addClass('hover');
			$j(this).find('h4 a').stop().css({color:'#CC3300', backgroundPosition:'right -288px'});
       	}, function(){
       		$j(this).stop().removeClass('hover');
			$j(this).find('h4 a').stop().css({color:'#FF6600', backgroundPosition:'right -251px'});
       	}
    );
	
	// locations
	if($j('#locations').length) {
  	  $j("#locations .locationInfo").hide();
	    $j("#locations li h2").bind('click', function () {
	        var thisLocation = $j(this).next("div");
	        if (thisLocation.is(":hidden")) {
	            $j("#locations h2 a").removeClass("open").parent().parent().find(".locationInfo").hide();
	            $j(this).find("a").addClass("open");
	            thisLocation.slideDown(300);
	        } else {
	            $j("#locations h2 a").removeClass("open").parent().parent().find(".locationInfo").slideUp(200);
	            thisLocation.hide();
	            $j(this).find("a").removeClass("open");
	        }
	    });
		$j("#locations li:first h2").trigger('click');
	}

	$j(".confirm").click( function() {						   
		if (!confirmAlert($j(this).attr('href'))){
			return false;			
		}
	});
	
	// search page rollovers
	$j('#froisearch-results li').live('click', function(){
        window.location=$j(this).find("a").attr("href");
	});
	if($j("#search").length){
		var searchLi = $j('#search li');
		$j(searchLi).live('mouseover', function(){
			$j(this).css('background','#F6FCE5');
			$j(this).find("h2 a").css('color','#FF6600');
		});
		$j(searchLi).live('mouseout', function(){
			$j(this).css('background','none');
			$j(this).find("h2 a").css('color','#333');
		});
	} // END search page rollovers
	
	// online banking tabs
	var oblTabLinks = $j('#tabList li'),
		obNav = $j('#obNav'),
		eBizNav = $j('#eBizNav'),
		oblTakemeto = $j('#oblTakemeto');
	
	$j('#obLogin').data('eBizLogin',false);
	oblTabLinks.click(function(e){
		oblTabLinks.removeClass('selected');
		$j(this).addClass('selected');
		$j('.oblWarning').hide();
		// toggle the tabs
		if($j(this).attr('id') === 'tabOnline') {
			obNav.show();
			oblTakemeto.show();
			eBizNav.hide();
			$j('#obLogin').data('eBizLogin',false);
		} else {
			obNav.hide();
			oblTakemeto.hide();
			eBizNav.show();
			$j('#obLogin').data('eBizLogin',true);
		}
		e.preventDefault;
	});
	
	// hide select list in ie6 and remove overlabel
	if(IE6){
		$j('#obLogin select').hide();
		$j('#obLogin .overLabelWrapper').removeClass('overLabelWrapper');
	}

	//	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 label[for]');
		var inputFor = $j('.overLabelWrapper 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'
					});
				}
			});	
		}
	}

	// 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, #ultraTopNav li a:contains("Online Banking"), #financialCalculators li a, .interested a').addClass('iframe');
	
	$j("#questions a").fancybox({
		'hideOnContentClick': false,
		'frameWidth': 540,
		'frameHeight': 340,
		'overlayOpacity': 0.8,
		'padding':0
	});
	$j(".interested a").fancybox({
		'hideOnContentClick': false,
		'frameWidth': 540,
		'frameHeight': 340,
		'overlayOpacity': 0.8,
		'padding':0
	});
	$j("#ultraTopNav li a.iframe").fancybox({
		'hideOnContentClick': false,
		'frameWidth': 250,
		'frameHeight': 200,
		'overlayOpacity': 0.8,
		'padding':0
	});

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

}); // END doc ready

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

// online banking login
function setAction() {
    if($j('#obLogin').data('eBizLogin') === true) {
    document.Remote.nmUID.value = document.Remote.AccessID.value;
    document.Remote.action = 'https://ws4.bankbyweb.net/EBC_EBC1961/EBC1961.ASP?WCI=Process&WCE=RemoteLogon&IRL=T';
    }
}

function doLoginRefresh() {
	var oblWarning = $j('.oblWarning');
    if (document.Remote.AccessID.value !== "") {
		$j(oblWarning).css('display','none');
        setAction();
        setTimeout('document.Remote.reset()', 20000);
        document.Remote.AccessID.value=document.Remote.AccessID.value;
        return true;
        }
    else {
		$j(oblWarning).fadeIn(200);
        return false;
        }    
}

jQuery.extend( jQuery.easing,
{
	easeOutCirc: function (x, t, b, c, d) {
		return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
	},
	easeInOutCirc: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
		return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
	},
	easeInElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
	},
	easeOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	},
	easeInOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
		return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
	}
});

// style the select list
// http://www.adamcoulombe.info/lab/jquery/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(){
				// selectBoxSpanInner.text($(this).val()).parent().addClass('changed');   This was not ideal
			selectBoxSpanInner.text($(this).find(':selected').text()).parent().addClass('changed');
				// Thanks to Juarez Filho & PaddyMurphy
			});
			
	  });
	  }
	}
 });
})(jQuery);
