jQuery(document).ready(function() {
    jQuery("input:checkbox").each(function(){
        if(jQuery(this).attr("checked")){
            jQuery(this).parent().find("a").addClass('current');
        }
    });

    jQuery('.service-link').click(function(event) {
        //var selected = jQuery(this).text().replace(' ','-');
        event.preventDefault();
        if(jQuery(this).parent().find(":checkbox").attr("checked")){
            jQuery(this).parent().find(":checkbox").attr("checked",false);
            jQuery(this).removeClass('current')
        }
        else {
            jQuery(this).parent().find(":checkbox").attr("checked",true);
            jQuery(this).addClass('current')
        }

    });
    
    jQuery("#contact-form").validate({
        rules: {
            email:      {required:true, email:true},
            name:       {required:true, minlength: 3},
            comment:    {required:true, minlength:2}
        },
        messages: {
            email:      {required: "Email is required", email: "Please enter a valid email address"},
            name: 	{required: "Name is required", minlength: "Please enter at least 3 characters"},
            comment:	{required: "Comment is required", minlength: "Please enter at least 2 characters"}
        },
        invalidHandler: function (e, validator) {
            jQuery("p.error").hide();
            jQuery("#validation-summary").show();
        }

    });

    jQuery("#contact-form").submit(function() {
        if (jQuery("#contact-form").valid()) {
            jQuery.ajax({
                type: "POST",
                url: "/wp-content/themes/storyworldwide/mailer.php",
                data: jQuery(this).serializeArray(),
                success: function(response) {
                    if (response == "-1") {
                        jQuery(".email-sent").html("<span style=\"color:red;\">Please recheck the data you entered and submit again.</span>");
                    } else {
                        jQuery("#form-wrapper").slideToggle(500);
                        jQuery(".email-sent").html("<p class=\"thanks-title\">Thanks</p> We'll be in touch shortly. If you simply can't wait, call one of our offices");
                    }
                }
            });

            return false;
        }

        return false;
    });


    //Populate all clocks
    var offset = serverTime.getTimezoneOffset() / 60;

    var sonoTime2 = new Date(utcTime + ((offset - 4) * 1000 * 60 * 60));
    var nycTime2 = new Date(utcTime + ((offset - 4) * 1000 * 60 * 60));
    var londonTime2 = new Date(utcTime + ((offset + 1) * 1000 * 60 * 60));
    var seaTime2 = new Date(utcTime + ((offset - 7) * 1000 * 60 * 60));
    var hongTime2 = new Date(utcTime + ((offset - 16) * 1000 * 60 * 60));
    var nagoyaTime2 = new Date(utcTime + ((offset - 15) * 1000 * 60 * 60));

    setTimeout(function() {
        var clockSono2 = new Clock("#clockSono2", "", sonoTime2);
        var clockNyc2 = new Clock("#clockNyc2", "", nycTime2);
        var clockLondon2 = new Clock("#clockLondon2", "", londonTime2);
        var clockSea2 = new Clock("#clockSea2", "", seaTime2);
        var clockHong2 = new Clock("#clockHong2", "", hongTime2);
        var clockNagoya2 = new Clock("#clockNagoya2", "", nagoyaTime2);
    }, 700);

    //Populate closed offices
    showClosedOffices2(jQuery("#clockSono2"), sonoTime2);
    showClosedOffices2(jQuery("#clockNyc2"), nycTime2);
    showClosedOffices2(jQuery("#clockLondon2"), londonTime2);
    showClosedOffices2(jQuery("#clockSea2"), seaTime2);
    showClosedOffices2(jQuery("#clockHong2"), hongTime2);
    showClosedOffices2(jQuery("#clockNagoya2"), nagoyaTime2);

    //Populate closest office
    showClosestOffice2();
});

function showClosestOffice2() {
    var hourOffset = new Date().getTimezoneOffset() / 60;

    if (hourOffset == 4) { //nyc
        highlightOffice(jQuery("#clockNyc2"));
    }
    else if (hourOffset == 7) { //seattle
        highlightOffice(jQuery("#clockSea2"));
    }
    else if (hourOffset == 0 || hourOffset == -1) { //london
        highlightOffice(jQuery("#clockLondon2"));
    }
    else if (hourOffset == -8) { //Hong Kong
        highlightOffice(jQuery("#clockHong2"));
    }
    else if (hourOffset == -9) { //Hong Kong
        highlightOffice(jQuery("#clockNagoya2"));
    }
    else {
        highlightOffice(jQuery("#clockNyc2"));
    }
}

function highlightOffice(div) {
    jQuery(div).addClass('clock-active');
    jQuery(div).parent().remove().prependTo("#contact-locations ul");
    //$("#myul li:last").remove().prependTo("#myul");
}

function showClosedOffices2(div, serverTime) {
    if (serverTime.getHours() <= 8 || serverTime.getHours() >= 18) {
            jQuery(div).addClass("clock-closed");
    }
}
