var $j=jQuery.noConflict();
var slideSpeed = 500;																									
//Set slide speed in milliseconds
var primaryNavExpanded = false;
var numListElements = 0;
var currentListElement = 1;
var intervalID;

$j(document).ready(function(){
	numListElements = $j('#outerController').children("li").size();

	$j("#featured span a").bigTarget({
		clickZone : 'li:eq(0)' // jQuery parent selector
	});
	$j(".mpaRight span a").bigTarget({
		clickZone : 'div:eq(0)' // jQuery parent selector
	});
	
	intervalID = setInterval( function(){rotate();}, 15000 );

	breadCrumbs();
	handleNav();
	homeMainSwap();
	mpaSwap();
	rewardCalculate();
	speakerThump();
	imageRollovers();
	textReplace();
	featuredProducts();
	locationsPage();
});
 
function breadCrumbs(){
	$j('#breadcrumbsMid div ul li span').click( function(){
		if( !$j(this).children('a').length > 0 ){
			var toggleNav = false;
			if( $j('#primaryNav li.inPath').hasClass('inPath2') ){
				var toggleNav = true;
			}
			
			$j('#primaryNav, #secondPrimaryNav').children('li').removeClass("inPath2");
			$j('#primaryNav, #secondPrimaryNav').children('li').children('ul').slideUp(slideSpeed);
			$j('#topOuter').animate({ height : "96px" }, slideSpeed, function(){
				if( !toggleNav ){
					$j('#topOuter').animate({ height : "280px" }, slideSpeed);
					$j('#primaryNav li.inPath h2').next().slideDown(slideSpeed);
					$j('#primaryNav li.inPath h2').parent().addClass("inPath2");
				}
			});
			primaryNavExpanded = true;
		}
	});
}

function handleNav(){
	$j('#primaryNav .nthTier-2, #secondPrimaryNav .nthTier-2').append("<li class=\"close\"><span>Close</span></<li>");
	
	$j('#primaryNav h2, #secondPrimaryNav h2').click( function(){
		var holdThis = $j(this);
		var theHeight = $j(this).next().height() + 126;
		var toggleNav = false;
		if( holdThis.parent().hasClass('inPath2') ){
			var toggleNav = true;
		}

		if(primaryNavExpanded){
			$j('#primaryNav, #secondPrimaryNav').children('li').removeClass("inPath2");
			$j('#primaryNav, #secondPrimaryNav').children('li').children('ul').slideUp(slideSpeed);
			$j('#topOuter').animate({ height : "96px" }, slideSpeed, function(){
				if( !toggleNav ){
					$j('#topOuter').animate({ height : theHeight + "px" }, slideSpeed);
					holdThis.next().slideDown(slideSpeed);
					holdThis.parent().addClass("inPath2");
				}
			});
		}
		else{
			$j('#topOuter').animate({ height : theHeight + "px" }, slideSpeed);
			holdThis.next().slideDown(slideSpeed);
			holdThis.parent().addClass("inPath2");
			primaryNavExpanded = true;
		}
		return false;
	});
	
	$j('li.close').click( function(){
		$j('#primaryNav, #secondPrimaryNav').children('li').removeClass("inPath2");
		
		$j('#topOuter').animate({ height : "96px" }, slideSpeed);
		
		$j('#primaryNav, #secondPrimaryNav').children('li').children('ul'). slideUp(slideSpeed);
		
		primaryNavExpanded = false;
	});
}

function rotate(){
	var previousListElement = currentListElement - 1;
	if( previousListElement == -1 ){
		previousListElement = numListElements - 1;
	}
	
	$j('div#flash2').children("div").eq(previousListElement).hide();
	if( currentListElement == 0){
		$j('#homePage #realTunesChecking #iPodGiveaway img').hide();
		$j('div#flash2').children("div").eq(currentListElement).fadeIn(1000, function(){
			//$j('#rewardCheckingAmount').focus();
			rewardCalcSliders();
		});
	}
	else if( currentListElement == 2){
		resetCalc();
		$j('div#flash2').children("div").eq(currentListElement).fadeIn(1000, function(){
			tunesAnimate();
		});
	}
	else{
		resetTunes();
		$j('div#flash2').children("div").eq(currentListElement).fadeIn(1000);
	}

	currentListElement++;
	flashSlider('item' + currentListElement);
	if( currentListElement == numListElements ){
		currentListElement = 0;
	}
}

function homeMainSwap(){
	$j('div.item2, div.item3, div.item4').hide();
	$j('#outerController h3:first').parent().addClass("selectedOuterController");
	$j('#outerController').css({backgroundPosition: '14em 0px'});
	rewardCalcSliders();

	$j('#flash2, #flash2 div').click( function(){
		clearInterval( intervalID );
	});
	$j('#outerController h3').click( function(){
		$j('div.item1, div.item2, div.item3, div.item4').hide();
		resetCalc();
		resetTunes();
		//$j('#homePage #realTunesChecking #iPodGiveaway img').hide();
		
		var temp = $j(this).attr("class");
		flashSlider( $j(this).attr("class") );
		
		$j('div.' + temp).fadeIn(1000, function(){
			if( temp = "item2"){
				$j('#rewardCheckingAmount').focus();
				rewardCalcSliders();
			}
			if( temp = "item3"){
				tunesAnimate();
			}
		});
	});
	
	/*$j('.seeDetails').show();
	$j('.seeDetails').click( function(){
		$j(this).hide().prevAll('.mpaRight').hide();
		$j(this).prev('.mpaRight2').slideDown();
	});
	$j('.hideDetails').click( function(){
		$j(this).parent().next().show();
		$j(this).parent().hide().prev().fadeIn();
	});*/
	 
	$j('.mpa1, .mpa2, .mpa3, .mpa4, .mpa5').mouseenter( function(){
		var shakeThis = $j(this).children('.mpaLeft').children('.apy');
		$j(shakeThis).stop()
		.css({'margin-left' : '6em'})
		.animate({ marginLeft: '6.3em' }, "fast", function(){
			$j(this).animate({ marginLeft: '5.7em' }, "fast", function(){
				$j(this).animate({ marginLeft: '6em' }, "fast");
			});
		});
	});
	$j('#mpaController li').hover( function(){
		if( !$j(this).hasClass('selected') ){
			$j(this).children('h4').hide().fadeIn(700);
			$j(this).children('img').stop().animate({ marginLeft: '2.5em' }, 700);
		}
	}, function(){
		if( !$j(this).hasClass('selected') ){
			$j(this).children('h4').hide();
			$j(this).children('img').stop().animate({ marginLeft: '5.5em' }, 700);
		}
	});
}

function flashSlider(classToSlideTo){
	$j('#outerController h3').parent().removeClass("selectedOuterController");
	$j('#outerController h3.' + classToSlideTo).parent().addClass("selectedOuterController");
		
	var slideTo = "14em";
	switch(classToSlideTo){
		case "item1":
			slideTo = "14em";
			break;
		case "item2":
			slideTo = "44em";
			break;
		case "item3":
			slideTo = "73em";
			break;
		case "item4":
			slideTo = "77em";
	}
	$j('#outerController').animate({backgroundPosition: slideTo + ' 0px'}, 1000);
}

function mpaSwap(){
	$j('div.mpa1, div.mpa2, div.mpa3, div.mpa4, div.mpa5').hide();
	$j('div.mpa1').fadeIn(3000);
	$j('#mpaController li:first').addClass("selected");
	$j('#mpaController li:first').addClass("selectedFirst");
	
	$j('#mpaController li').click( function(){
		$j('div.mpa1, div.mpa2, div.mpa3, div.mpa4, div.mpa5').hide();
		$j('#mpaController li').removeClass("selected");
		$j('#mpaController li').removeClass("selectedFirst");
		$j('#mpaController li').removeClass("selectedLast");
		
		$j(this).children('img').css('margin-left', '5.5em');
		
		if($j(this).hasClass("firstChild"))
			$j(this).addClass("selectedFirst");
		else if( $j(this).hasClass("lastChild"))
			$j(this).addClass("selectedLast");
		$j(this).addClass("selected");
		var temp = $j(this).children('h4').attr("class");
		$j('div.' + temp).fadeIn(1000);
	});
}

function rewardCalculate(){
	$j('#rewardCheckingAmount').keypress(function(e){
	    if( e.which!=11 && e.which!=9 && e.which!=8 && e.which!=0 && (e.which<48 || e.which>57) && e.which!=13 && e.which!=36 )
			return false;
	});
	
	$j('#calculate').click( function(){
		clearInterval( intervalID );
		rewardCalcSliders();
		return false;
	});
}

function rewardCalcSliders(){
	if($j('#rewardCheckingAmount').length > 0){
		var enteredAmount = $j('#rewardCheckingAmount').val();
		enteredAmount = enteredAmount.replace("$", "");
		if( enteredAmount != null && enteredAmount != "" ){
			resetCalc();
			
			$j('#rewardPlusAmount').html("$" + roundNumber(parseFloat(enteredAmount) * .0268, 0));
			$j('#cdsAmount').html("$" + roundNumber(parseFloat(enteredAmount) * .0125, 0));
			$j('#moneyMarketAmount').html("$" + roundNumber(parseFloat(enteredAmount) * .0050, 0));
			$j('#savingsAmount').html("$" + roundNumber(parseFloat(enteredAmount) * .0050, 0));
	
			$j('#rewardPlusAmount').animate({width: '21em'}, 1000);
			$j('#cdsAmount').animate({width: '7.5em'}, 1000);
			$j('#moneyMarketAmount').animate({width: '3em'}, 1000);
			$j('#savingsAmount').animate({width: '3em'}, 1000);
		}
	}
}

function tunesAnimate(){
	$j('#realTunesChecking h4').stop()
	.css('width', '58em')
	.animate({ width: '19.6em' }, "2500", function(){
		$j('p#mainStuff').stop()
		.css('width', '170em')
		.animate({ width: '30em' }, "16000");
	});

	$j('#homePage #realTunesChecking #iPodGiveaway img').stop()
	.css({'height' : '0px', 'left' : '215px', 'top' : '303px', 'width' : '0px'})
	.animate({ top: '227px', left: '140px', height: '152px', width: '149px' }, 800 );
}

function resetCalc(){
	$j('#rewardPlusAmount').html("$0");
	$j('#cdsAmount').html("$0");
	$j('#moneyMarketAmount').html("$0");
	$j('#savingsAmount').html("$0");
	
	$j('#rewardCheckingRight span').css("width", "1.5em");
	$j('#rewardCheckingRight').css("background", "url(/custom/citybankonline2/image/rewardCheckingArrow.png) 330px 50px no-repeat");
}

function resetTunes(){
	$j('#realTunesChecking h4').css('width', '58em');
	$j('p#mainStuff').css('width', '170em');
	$j('#homePage #realTunesChecking #iPodGiveaway img')
	.css({'height' : '0px', 'left' : '215px', 'top' : '303px', 'width' : '0px'});
}

function imageRollovers(){
	$j('.learnMore, .mpaRight .mpaLearnMore img').mouseover(function(){
    	$j(this).attr("src","/custom/citybankonline2/image/learnMoreHover.png");
    }).mouseout(function(){
    	$j(this).attr("src","/custom/citybankonline2/image/learnMore.png");
    });
	
	$j('.mpaRight').mouseover(function(){
    	$j(this).find('.mpaLearnMore img').attr("src","/custom/citybankonline2/image/learnMoreHover.png");
    }).mouseout(function(){
    	$j(this).find('.mpaLearnMore img').attr("src","/custom/citybankonline2/image/learnMore.png");
    });

	
	$j('#rewardCheckingRight img').mouseover(function(){
    	$j(this).attr("src","/custom/citybankonline2/image/learnMoreDarkerHover.png");
    }).mouseout(function(){
    	$j(this).attr("src","/custom/citybankonline2/image/learnMoreDarker.png");
    });
}

function textReplace(){
	$j('#search input').click(function (){
		if( $j('#search input').val() == "Search" ){
			$j('#search input').val("");
		}
	});
	$j('#search input').blur(function (){
		if( $j('#search input').val() == "" ){
			$j('#search input').val("Search");
		}
	});
}

function speakerThump(){
	$j('#homePage #realTunesChecking').mouseenter( function(){
		$j(this).children('#speakers').children('img').stop()
		.css({'height' : '95px', 'left' : '403px', 'top' : '277px', 'width' : '245px'})
		.animate({ top: '269px', left: '395px', height: '103px', width: '261px' }, "fast", function(){
			$j(this).animate({ top: '277px', left: '403px', height: '95px', width: '245px' }, "slow");
		});
	});
}

function featuredProducts(){
	$j('#featuredProduct #tabs li:first').addClass("selected");
	$j('#featuredProduct #tabs li').click( function(){
		var tabClass = $j(this).attr('class');
		
		$j('#featuredProduct #tabs li').removeClass("selected");
		$j(this).addClass("selected");
		
		$j('#featuredContent .item1, #featuredContent .item2, #featuredContent .item3, #featuredContent .item4').hide();
		$j('#featuredContent .' + tabClass).fadeIn(1000);
		
		var slideTo = "10em";
		switch(tabClass){
			case "item1":
				slideTo = "10em";
				break;
			case "item2":
				slideTo = "32.5em";
				break;
			case "item3":
				slideTo = "54.5em";
				break;
			case "item4":
				slideTo = "77em";
		}
		$j('#tabs ul').animate({backgroundPosition: slideTo + ' bottom'});
	});
}

function locationsPage(){
	$j('.hours-and-details-content p').each( function(){
		if( !($j(this).next().children().length > 0) )
			$j(this).hide();
	});
	
	$j('.hours-and-details-content').hide();
	
	$j('.hours-and-details-toggle').click( function(){
		if( !$j(this).hasClass('open') ){
			$j(this).addClass('open').next().slideDown(500);
		}
		else{
			$j(this).removeClass('open').next().slideUp();
		}
	});
}

//Helper functions
function roundNumber(num, dec) {
	return Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
}