var clickedTab = null;

$(document).ready(function(){
	
	var interval = 0;
	loadBackgroundImages();		
	interval = setInterval("rotateImages()",5000);
	$(".heroTab").click(function(){	
			//Stop rotating images
			clearInterval(interval);
			clickedTab = $(this);
			displaySelectedHero(clickedTab);			
			return false;

		});
	
	$(".save-now-item").click(function(){	
		var checkbox = $(this).children('.big-check-box');
		var input = checkbox.children('input');
		
		
		if (checkbox.hasClass('marked')) {
			checkbox.removeClass('marked').addClass('empty');
			input.val('false');
		} else {
			// clear out all the checkboxes that might have been checked before
			$(".big-check-box").each(function() {
				$(this).removeClass('marked').addClass('empty');
				$(this).children('input').val('false');
			});
			
			checkbox.removeClass('empty').addClass('marked');
			input.val('true');
		}
		
	});	
	$(".creditButton").click(function(){
		window.open("http://www.gofreecredit.com/r/4d9a3e609c/?fname=&lname=&zip=&email=&subid=savings+center");
		return false;
	});
});

function displaySelectedHero(selectedTab){
	//Hide all hero images
	$(".heroBg").hide();
	//Remove selected class from tabs
	$(".heroTab").removeClass("selected");
	//Add selected class to current tab
	selectedTab.addClass("selected");
	//Find selected hero image
	var selected = selectedTab.attr('id').split('-tab');
	var id           = selected[0];	
	$("#"+id).show();
	
}
function loadBackgroundImages(){
	$("#heroBoxes > .heroBg").each(function(){
			var backgroundSrc = $(this).css("background-image").replace(/^url|[\(\)]/g, '');	
			backgroundSrc = backgroundSrc.replace(/"/g,'');
			if(backgroundSrc != ''){				
				var img = new Image();
				img.src = backgroundSrc;				
			}
	});	
}
function rotateImages(){	
	var selected = "";
	var nextHero = "";
	var currentHero = $("#heroBoxes > .current");
	
	//Get the next tab in the list of tabs
	//Get the next hero image
	if($("#heroTabs li.selected").next().length == 0){
		selected = $("#heroTabs li").first();
		nextHero = $("#heroBoxes .heroBg").first();
	}else{
		selected = $("#heroTabs li.selected").next();
		nextHero = currentHero.next();
	}
	//Add selected class to current tab
	$(".heroTab").removeClass("selected");
	selected.addClass("selected");		
	
	currentHero.removeClass('current');
	nextHero.addClass('current');
	
	currentHero.fadeOut(500);
	nextHero.fadeIn(1500);	
}

function openSaveNowModal() {	
	$("#saveNowModal").modal({onClose: function (dialog) {
		$.modal.close();
	}});
	
}

function submitSaveNowForm(){
	if($('input[name="credit-report"]').val() == 'true'){ 
		window.open("http://www.gofreecredit.com/debt-relief?lte=all&id=14511&subid");
		$.modal.close();
	} 
	else {
		document.chainForm.submit();
	}
}
