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

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

// add class to body
	var primary = $j('#primaryNav li a.inPath').text().replace(/\s+/g, '-').toLowerCase();
	$j('body').addClass(primary);
	
	productTabs();

	$j(".confirm").click( function() {						   
			if (!confirmAlert($j(this).attr('href'))){
				return false;			
			}
		}); 	

// add hidden logo for print stylesheet
	$j('#logo a').after('<img src="/custom/' + customFolder + '/image/logo.png" alt="Mutual 1st Federal"/>');


// new page for INMO
	$j('a.newPage').each( function(){
		this.target = "_blank";
	});

	$j('#videoLink a').addClass("confirm");
	
	// clickable container
	$j('#videoLink').click(function(){
		window.location=$j(this).find("a").attr("href");
	});

// super hovers and clickable containers
	
	$j("#homeFeatured li, #secondaryProducts li, ul#promotions li").hover(function(){
		$j(this).addClass("hover");
	},
	function(){
		$j(this).removeClass("hover");
	});
	
	$j("#homeFeatured li, #secondaryProducts li, #featuredContent, #search ul li, #locations ul#locList li, #promotions li").not("#secondaryProducts li li").not('#homeFeatured li#featured2').click(function(){
	window.location=$j(this).find("a").attr("href");
	});

	$j("#homeFeatured li, #secondaryProducts li, #featuredContent, #search ul li, #locations ul#locList li, #promotions li").css({cursor : "pointer"});
	
// secondary products
		/*if ($j("#secondaryProducts ul").not("#secondaryProducts ul ul").children().size() > 3) {
			$j("#secondaryProducts ul li").not("#secondaryProducts li li").css({borderBottom : "1px solid #CEECF2"});
		} */


// leftNav
	$j("ul#leftNav li:last").addClass("lastLastChild");
	
	
// target="_blank"
	$j("a[href*='http://']:not([href*='"+location.hostname+"']),[href*='https://']:not([href*='"+location.hostname+"'])")
	.attr("target","_blank").attr("title","Opens new window");


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


// fancybox - tell me more, downstream obl, calculators
	$j('#tellMeMoreButton a, #financialCalculators li a, li a:contains("Mortgage Calculator")').addClass('iframe');
	
	$j("#tellMeMoreButton a").fancybox({
		'hideOnContentClick': false,
		'frameWidth': 560,
		'frameHeight': 380,
		'overlayOpacity': 0.8
	});

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


// anythingSlider
	$j('#rateMeter h2#rates').hide();
	$j('.anythingSlider ul, #rateMeter .arrow').show();
	
	$j('#rateMeter').anythingSlider({
		easing			: "easeInOutExpo", // Anything other than "linear" or "swing" requires the easing plugin
		autoPlay		: true, // This turns off the entire FUNCTIONALY
		startStopped	: false, // If autoPlay is on, this can force it to start stopped
		delay			: 8000, // How long between slide transitions in AutoPlay mode
		animationTime	: 500, // How long the slide transition takes
		hashTags		: true, // Should links change the hashtag in the URL?
		buildNavigation	: true, // If true, builds and list of anchor links to link to each slide
		pauseOnHover	: true, // If true, and autoPlay is enabled, the show will pause on hover
		startText		: "Start", // Start text
		stopText		: "Stop", // Stop text
		navigationFormatter: null // Details at the top of the file on this use (advanced use)
	});
	
	$j("#slide-jump").click(function(){
	$j('.anythingSlider').anythingSlider(5);
	}); 

// add class 'active' and cursor: pointer
	$j("#obLogin #obMore h3, #homeFeaturedProducts ul li, #featuredProduct, #secondaryProducts ul li, #locations h2").hover(
		function(){
			$j(this).addClass("active").css("cursor", "pointer");
		},
		function(){
			$j(this).removeClass("active");
		}
	);

// add class according to URL - Personal, Business, About Us
	if ( (url.indexOf("request-stop-payment-check-copy.html") != -1) || (url.indexOf("e-services-request-form.html") != -1) ) {
		$j("fieldset#newApplication div#introduction").css({"min-height":"3em"});
	} 

// dollar dog
	if ( !($j.browser.msie && $j.browser.version.substr(0,1)<7) && (url.indexOf("personal/savings/youth-accounts.html") != -1) ) {
		$j("#productPage").append("<img alt=\"Dollar Dog\" class=\"dollarDog\" src=\"/custom/mutualfirst/image/layout/dollar-dog.png\">");
		$j("#productPage").append("<img alt=\"Dollar Dog\" class=\"dollarDogSelected\" src=\"/custom/mutualfirst/image/layout/dollar-dog-selected.png\">");
		$j("#productPage img.dollarDogSelected").hide();
		$j("dl#productTabs dt .tab:contains('Dollar Dog Club')").click(function(){
				$j("img.dollarDog").fadeOut();
				$j("img.dollarDogSelected").fadeIn();
		});
		$j("dl#productTabs dt .tab:contains('Kids 1st Account')").click(function(){
				$j("img.dollarDogSelected").fadeOut();
				$j("img.dollarDog").fadeIn();
		});
	}
	
// duplicate locations event handlers
	if( $j('.branches').length > 0 ){
		var extra = [];
		var j = 0;
		for( var i = 0; i < gMapLocations.length; i++ ){
			extra[j] = gMapLocations[i];
			j++;
			extra[j] = gMapLocations[i];
			j++;
		}
		gMapLocations = extra;
	}
	
// sort the gMapLocations to match the display

	if ( ($j('.atms').length > 0 ) && ($j.browser.mozilla) ) {
			gMapLocations.sort(function(a, b) { return a.tags > b.tags; });
	}
	
// Resort locations
	if ( !($j.browser.webkit) ) {
		$j('.atms ul#locList li').tsort({attr:'class'});
	}
	
	var sa = [];
	var classes = [];
	var city = [];
	$j('.atms ul#locList li').each(function() {
		thisClass = $j(this).attr('class');
		if (thisClass.indexOf(" ") != -1) {
			thisClass = thisClass.substr(0, thisClass.indexOf(" ")-1);
		}
	
		var spot = classes.length;
		classes[spot] = thisClass;
		city[spot] = $j(this).attr("class").split(/\s/).shift().replace(/-/g," ");
		
		sa.push( $(this).clone(true) );
		$j(this).remove();
	});
	sa.reverse();
	var numLocations = classes.length;
	for( var i = 0; i < numLocations; i++ ){
		if( classes[i] != classes[i-1] ){
			$j('.atms ul#locList').append('<li><ul></ul></li>');
			$j('.atms ul#locList li:last').prepend('<h2>' + city[i] + '</h2>').css({'text-transform':'capitalize'});
		}
		$j('.atms ul#locList ul:last').append(sa.pop());
	}

// End resort locations
	$j('.atms ul#locList>li>ul').not('.atms ul#locList>li>ul:first').hide();
	$j('.atms ul#locList>li>h2').text().replace(" vcard clear","").replace("-"," ").toUpperCase();
	$j('.atms ul#locList>li>ul:first').addClass('open');
	
	$j('.atms ul#locList>li>h2').click(function () {
		var thisLocation = $j(this).next("ul");
		if (thisLocation.is(":hidden")) {
			$j(this).addClass("open");
			thisLocation.slideDown("slow");
		} else {
			thisLocation.slideUp("fast");
			$j(this).removeClass("open");
		}
	});
	
	$j('.atms ul#locList>li>h2, .atms ul#locList li ul li').hover(function () {
			$j(this).addClass('hover');
		},
		function () {
			$j(this).removeClass('hover');
		}
	);

}); // END doc ready

Cufon.set("selector", jQuery);
Cufon.replace("#featuredProduct h1, .individualPage h1, body.popup h4.title, fieldset#newApplication div#introduction h1", { separate: true, hover: true});
Cufon.replace("#productPage h1, #rightSide h1");


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

/* add images for addPrintLink  */
/*$j('#logo a').after('<img src="/custom/' + customFolder + '/image/logo.png" alt="BANK NAME"/>');
$j('#footer #firstROI').after('<img class="froiLogo" src="/custom/' + customFolder + '/image/firstroi-logo.png" alt="First ROI"/>');*/

/* addPrintLink function by Roger Johansson, www.456bereastreet.com */
var addPrintLink = {
	init:function(sTargetEl,sLinkText) {
		if (!document.getElementById || !document.createTextNode) {return;} // Check for DOM support
		if (!document.getElementById(sTargetEl)) {return;} // Check that the target element actually exists
		if (!window.print) {return;} // Check that the browser supports window.print
		var oTarget = document.getElementById(sTargetEl);
		var oLink = document.createElement('a');
		oLink.id = 'printLink'; // Give the link an id to allow styling
		oLink.href = '#'; // Make the link focusable for keyboard users
		oLink.appendChild(document.createTextNode(sLinkText));
		oLink.onclick = function() {window.print(); return false;} // Return false prevents the browser from following the link and jumping to the top of the page after printing
		oTarget.appendChild(oLink);
	},
/*
addEvent function included here for portability. Replace with your own addEvent function if you use one.
*/
/* addEvent function from http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html */
	addEvent:function(obj, type, fn) {
		if (obj.addEventListener)
			obj.addEventListener(type, fn, false);
		else if (obj.attachEvent) {
			obj["e"+type+fn] = fn;
			obj[type+fn] = function() {obj["e"+type+fn](window.event);}
			obj.attachEvent("on"+type, obj[type+fn]);
		}
	}
};
addPrintLink.addEvent(window, 'load', function(){addPrintLink.init('printThisPage','Print this page');});

