$(document).ready(function(){

// newsletter label disappears when text box is focused
    $('#newsletter_email').focus(function() {
        $(this).css('z-index','300');
    });
    $('#newsletter_email').blur(function() {
        if ($(this).val() == "")
        {
            $(this).css('z-index','100');
        }
    });

// submit the newsletter form
$("#newsletter_submit").click(function() {
    var first = $("input#newsletter_first").val();
    var last = $("input#newsletter_last").val();
    var email = $("input#newsletter_email").val();
    
    var dataString = 'first_name='+ first + '&last_name=' + last + '&email_address=' + email;
    //alert (dataString);return false; //for testing only
    $.ajax({
      type: "POST",
      url: "http://ortravelexperience.com/wp-content/themes/ote/listserve_subscribe_action.php",
      data: dataString,
      success: function() {
        $('#newsletter_form').html("<div id='message'></div>");
        $('#message').html("<h3>Newsletter Form Submitted!</h3>")
        .append("<p>Thank you for subscribing!</p>") //why did I do it this way??? Am I stupid?
        .hide()
        .fadeIn(1500, function() {
          $('#message');
        });
      }
    });
    return false;
});

// home page header slider, baby
$(window).load(function() {
    $('#featured').orbit({
        animation: 'fade',
        //timer: true,
        pauseOnHover: false,
        //directionalNav: true,
        //bullets: false,
        advanceSpeed: 6000,
    });
});

// map page search
$("#markertopic").change(function() {
    $("#topicform").submit();
});

$("#treetopic").change(function() {
    $("#topicform").submit();
});

// This does not work with ajax - the map is just "loading" forever. Boo!!!
//$("#markertopic").change(function() {
//    var mytopic = $(this).val();
//    var mydataString = 'topic='+ mytopic +'&cat=8';
//    
//    $.ajax({
//        type: "POST",
//        url: "http://oregontic.com/public_html/sandbox/makemymap",
//        data: mydataString,
//        success: function(e) {
//            $("#fullmap").html(e);
//        }
//    });
//});
//
//$("#treetopic").change(function() {
//    var mytopic = $(this).val();
//    var mydataString = 'topic='+ mytopic +'&cat=7'
//    
//    $.ajax({
//        type: "POST",
//        url: "makemymap.php",
//        data: mydataString,
//        success: function(e) {
//            $("#fullmap").html(e);
//        }
//    });
//});

//contact form validation
if($('#contactform').length){
    $('#contactform').validate({
        rules: {
            thename: {
                required: true,
                letterswithbasicpunc: true
            },
            theemail: {
                required: true,
                email: true
            },
            thephone: {
                minlength: 8,
                phoneUS: true
            },
            themessage: {
                required: true
            }
        //},
        //success: function(label) {
        //    label.html('OK!').addClass('valid').removeClass('error');
        }
    });
    //additional methods for form
    jQuery.validator.addMethod("letterswithbasicpunc", function(value, element) {
        return this.optional(element) || /^[a-z-.,()'\"\s]+$/i.test(value);
    }, "Letters or basic punctuation only, please");
    jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
        phone_number = phone_number.replace(/\s+/g, ""); 
        return this.optional(element) || phone_number.length > 9 &&
            phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
    }, "Please specify a valid phone number");
}


}); //end document ready

