var newsitems; 
var curritem=0; 
var iPause=0;
$(document).ready(function() {
    $("body").prepend(modal_html());


    $("#slider").easySlider({
        auto: true,
        continuous: true,
        speed: 2000
    });

    $('#sponsors_causes').jcarousel({
        auto: 3,
        wrap: 'last',
        buttonNextHTML: null,
        buttonPrevHTML: null,
        initCallback: mycarousel_initCallback
    });


    $("#sign_in").jqm({
        trigger: '.m_sign_in',
        closeClass: 'close',
        overlay: 50
    });


    $(".AddCartContainer").click(function() {
        $('#contact_message').jqmShow();
        return false;
    });
    $("#contact_message").jqm({
        closeClass: 'close_action',
        overlay: 50
    });


    $("#equipment").click(function() {
        $('#equipment_message').jqmShow();
        return false;
    });
    $("#equipment_message").jqm({
        closeClass: 'close_action',
        overlay: 50
    });

    $("#thank_you").jqm({
        closeClass: 'close_action',
        overlay: 50
    });
    $("#thank_you_friend").jqm({
        closeClass: 'close_action',
        overlay: 50
    });

    $("#show360").click(function() {
        $('#view360').jqmShow();
        return false;
    });
    $("#view360").jqm({
        closeClass: 'close_action',
        overlay: 50
    });

    $("#error").jqm({
        closeClass: 'close_action',
        overlay: 50
    });

    $(".get_notified").hover(
		function() {
		    this.src = this.src.replace("get_notified.png", "get_notified_hover.png");
		},
		function() {
		    this.src = this.src.replace("get_notified_hover.png", "get_notified.png");
		}
	);
    $(".visit_our_blog").hover(
		function() {
		    this.src = this.src.replace("visit_our_blog.png", "visit_our_blog_hover.png");
		},
		function() {
		    this.src = this.src.replace("visit_our_blog_hover.png", "visit_our_blog.png");
		}
	);
    $(".m_submit_survey").hover(
		function() {
		    this.src = this.src.replace("submit_survey.png", "submit_survey_hover.png");
		},
		function() {
		    this.src = this.src.replace("submit_survey_hover.png", "submit_survey.png");
		}
	);
    $(".cancel").hover(
		function() {
		    this.src = this.src.replace("cancel.png", "cancel_hover.png");
		},
		function() {
		    this.src = this.src.replace("cancel_hover.png", "cancel.png");
		}
	);
    $("#sign_in .sign_in").hover(
		function() {
		    this.src = this.src.replace("sign_in_form.png", "sign_in_form_hover.png");
		},
		function() {
		    this.src = this.src.replace("sign_in_form_hover.png", "sign_in_form.png");
		}
	);
    $(".return_home").hover(
		function() {
		    this.src = this.src.replace("return_home.png", "return_home_hover.png");
		},
		function() {
		    this.src = this.src.replace("return_home_hover.png", "return_home.png");
		}
	);


    /* Newsletter signup */
    $("#get_notified").click(function() {
        var fullname = $(".newsletter_form").find("input[name='fullname']");
        var semail = $(".newsletter_form").find("input[name='email']");
        var fnamevalue = fullname.val();
        var emailvalue = semail.val();
        if (emailvalue && fnamevalue && emailvalue != "Your Email Address" && fnamevalue != "Your Name") {
            $.getJSON("http://mailchimp.covalent.us/Default.aspx?email=" + emailvalue + "&fullname=" + fnamevalue + "&callback=?", function(myJson) {
            
                switch (myJson.mystatus) {
                    case "SUCCESS":
                        $('#thank_you').jqmShow();
                        break;
                    case "INVALID_EMAIL":
                        modal_error("Invalid Email", "The email you entered is invalid, please try again.");
                        break;
                    case "INVALID_NAME":
                        modal_error("Invalid Name", "Please enter your First and Last Name with a space between them.");
                        break;
                    case "ERROR_MESSAGEAPI":
                        modal_error("Error", "An unknown error occurred while hitting the Messageing API.");
                        break;
                    case "ERROR_API":
                        modal_error("Error", "An unknown error occurred while hitting the API.");
                        break;
                    default:
                        modal_error("Error", "An unknown default error occurred.");
                        break;
                }
            });
            return false;

        } else {

            $("input#email").removeClass("newsletter_error")
            $("input#fullname").removeClass("newsletter_error")
            if (!semail.val() || semail.val() == "Your Email Address") {
                semail.val("Email Required");
                semail.addClass("newsletter_error");
            }
            if (!fullname.val() || fullname.val() == "Your Name") {
                fullname.val("Name Required");
                fullname.addClass("newsletter_error");
            }

            return false;
        }

    });






});

function modal_error (heading, paragraph) {
	$("#error").find("h2").text(heading);
	$("#error").find("p").text(paragraph);
	$('#error').jqmShow();
}

function modal_success (heading, paragraph, h2_class) {
	$("#thank_you").find("h2").text(heading);
	$("#thank_you").find("p").text(paragraph);
	if(h2_class) {
		$("#thank_you").find("h2").addClass(h2_class);
	}
	$('#thank_you').jqmShow();
}


function mycarousel_initCallback(carousel) {
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};


