$(document).ready(function() {
	// menu
	$('#mainNav li').hover(
		function () {
			//show its submenu
			if($(window).width() >= 480) {
				$('ul', this).slideDown(100);
			}
		},
		function () {
			//hide its submenu
			if($(window).width() >= 480) {
				$('ul', this).slideUp(100);
			}
		}
	);


	// setup error checking for request info form
	$(".freeInvestorsKit form").submit(function(e){
		var errorMessage = "";
		if($("#input_name").val() == "") {
			errorMessage += "<li>Enter your full name.</li>";
		}

		var emailFilter=/^.+@.+\..{2,6}$/;
		var illegalChars= /[\s\!\*\(\)\<\>\,\;\:\\\/\"\[\]]/
	    if (!(emailFilter.test($("#input_email").val()))) {
			errorMessage += "<li>Enter your valid email address.</li>";
    	}
    	else if ($("#input_email").val().match(illegalChars)) {
			errorMessage += "<li>Enter your valid email address.</li>";
    	}

		if($("#input_phone").val() == "") {
			errorMessage += "<li>Enter your phone number.</li>";
		}

		if(errorMessage.length == 0) {
			return true;
		} else {
			$(".freeInvestorsKit .errors ul").html(errorMessage);
			$(".freeInvestorsKit .errors").fadeIn(400).delay(3000).fadeOut(400);
			return false;
		}
	});


	// Setup Homepage Promo Slideshow
	$(".homePromos .slides").cycle({
		fx: "fade", pager: ".slide_controls", speed: 1000, timeout: 5500, pause: 1, pauseOnPagerHover: 0
	});
	var homePromosControlWidth = $(".homePromos .slide_controls").width() * $(".homePromos .slides li").length;
	$(".slide_controls").css("width", homePromosControlWidth + "px");

	// Setup Homepage Popular Coin Slideshow
	$(".popularCoins").append('<a href="#" class="coinSliderControls" id="prevCoinPage">previous</a><a href="#" class="coinSliderControls" id="nextCoinPage">next</a>')
	$(".popularCoins .coins").cycle({
		fx: "scrollHorz", prev: "#prevCoinPage", next: "#nextCoinPage", speed: 1000, timeout: 0
	});

	// Setup Homepage Quotes Slideshow
	$(".quotes").append('<a href="#" class="quoteSliderControls" id="prevQuoteControl">previous</a><a href="#" class="quoteSliderControls" id="nextQuoteControl">next</a>')
	$(".quotes ul").cycle({
		fx: "scrollHorz", prev: "#prevQuoteControl", next: "#nextQuoteControl", speed: 1000, timeout: 0
	});


	// Chart Switching
	$('#gold_chart').show();
    $('#silver_chart').hide();
    $('#plat_chart').hide();

    $('#show_gold').click(function(e) {
		$(".moreCharts").attr("href", "http://www.learcapital.com/gspp-charts-and-news/au.html");
        $('#silver_chart').hide();
        $('#plat_chart').hide();
        $('#gold_chart').show();
        $("#show_gold").attr("class", "selected");
        $("#show_silver").attr("class", "");
        $("#show_plat").attr("class", "");
		e.preventDefault();
    });

    $('#show_silver').click(function(e) {
		$(".moreCharts").attr("href", "http://www.learcapital.com/gspp-charts-and-news/ag.html");
        $('#gold_chart').hide();
        $('#plat_chart').hide();
        $('#silver_chart').show();
        $("#show_silver").attr("class", "selected");
        $("#show_gold").attr("class", "");
        $("#show_plat").attr("class", "");
		e.preventDefault();
    });

    $('#show_plat').click(function(e) {
		$(".moreCharts").attr("href", "http://www.learcapital.com/gspp-charts-and-news/pt.html");
        $('#gold_chart').hide();
        $('#silver_chart').hide();
        $('#plat_chart').show();
        $("#show_plat").attr("class", "selected");
        $("#show_gold").attr("class", "");
        $("#show_silver").attr("class", "");
		e.preventDefault();
    });

	// coin flipping
	$(".popularCoins .flipCoin a").click(function(e){
		//get img tag
		var $coinImg = $(this).parent().parent().find(".coinCover img");
		if($(this).attr("data-coinBackURL") != $coinImg.attr("src")) {
			$coinImg.attr("src", $(this).attr("data-coinBackURL"));
		} else {
			$coinImg.attr("src", $(this).attr("data-coinFrontURL"));
		}
		e.preventDefault();
	})

	// hide forms label on click
	$(".overinput").each(function(){
		if($(this).val() != "") {
			if($(window).width() >= 780) {
				$(this).parent().find(".overlabel").hide();
			}
		}
	});
	$(".overlabel").click(function(){
		if($(window).width() >= 780) {
			$(this).hide();
			$(this).parent().find(".overinput").focus();
		}
	});
	$(".overinput").focus(function(){
		if($(window).width() >= 780) {
			$(this).parent().find(".overlabel").hide();
		}
	});
	$(".overinput").blur(function(){
		if($(window).width() >= 780) {
			if($(this).val() == "") {
				$(this).parent().find(".overlabel").show();
			}
		}
	});
});


function isInteger(s)    {
	// check whether an input value is a number
	var i;
	for (i = 0; i < s.length; i++) {
		// Check that current character is number.
		var c = s.charAt(i);
		if ((c < "0") || (c > "9")) return false;
	}
	// All characters are numbers.
	return true;
}
