jQuery(document).ready(function() {
    //Populate all clocks
    var offset = serverTime.getTimezoneOffset() / 60;
    
    var sonoTime = new Date(utcTime + ((offset - 4) * 1000 * 60 * 60));
    var nycTime = new Date(utcTime + ((offset - 4) * 1000 * 60 * 60));
    var londonTime = new Date(utcTime + ((offset + 1) * 1000 * 60 * 60));
    var seaTime = new Date(utcTime + ((offset - 7) * 1000 * 60 * 60));
    var hongTime = new Date(utcTime + ((offset - 16) * 1000 * 60 * 60));
    var nagoyaToime = new Date(utcTime + ((offset - 15) * 1000 * 60 * 60));

    setTimeout(function() {
        var clockSono = new Clock("#clockSono", "", sonoTime);
        var clockNyc = new Clock("#clockNyc", "", nycTime);
        var clockLondon = new Clock("#clockLondon", "", londonTime);
        var clockSea = new Clock("#clockSea", "", seaTime);
        var clockHong = new Clock("#clockHong", "", hongTime);
        var clockNagoya = new Clock("#clockNagoya", "", nagoyaToime);
    }, 700);

    //Populate all hover events
    jQuery("#clockSono").hover(function() {
        showOfficeNumber(this, 'South Norwalk', '+1 203 831 8700');
    }, function() { hideOfficeNumber(); });
    jQuery("#clockNyc").hover(function() {
        showOfficeNumber(this, 'New York', '+1 212 481 3452');
    }, function() { hideOfficeNumber(); });
    jQuery("#clockLondon").hover(function() {
        showOfficeNumber(this, 'London', '+44(0)20 7449 1500');
    }, function() { hideOfficeNumber(); });
    jQuery("#clockSea").hover(function() {
        showOfficeNumber(this, 'Seattle', '+1 206 336 3001');
    }, function() { hideOfficeNumber(); });
    jQuery("#clockHong").hover(function() {
        showOfficeNumber(this, 'Hong Kong', '+852 2892 1322');
    }, function() { hideOfficeNumber(); });
    jQuery("#clockNagoya").hover(function() {
        showOfficeNumber(this, 'Nagoya', '+81 (0) 52 508 7781');
    }, function() { hideOfficeNumber(); });

    //Populate closed offices
    showClosedOffices(jQuery("#clockSono"), sonoTime);
    showClosedOffices(jQuery("#clockNyc"), nycTime);
    showClosedOffices(jQuery("#clockLondon"), londonTime);
    showClosedOffices(jQuery("#clockSea"), seaTime);
    showClosedOffices(jQuery("#clockHong"), hongTime);
    showClosedOffices(jQuery("#clockNagoya"), nagoyaToime);

    //Populate closest office
    showClosestOffice();
});

function showClosestOffice() {
    var hourOffset = new Date().getTimezoneOffset() / 60;

    if (hourOffset == 4) { //nyc
        showOfficeNumber(jQuery("#clockNyc"), 'New York', '+1 212 481 3452');
    }
    else if (hourOffset == 7) { //seattle
        showOfficeNumber(jQuery("#clockSea"), 'Seattle', '+1 206 336 3001');
    }
    else if (hourOffset == 0) { //london
        showOfficeNumber(jQuery("#clockLondon"), 'London', '+44(0)20 7449 1500');
    }
    else if (hourOffset == 8) { //Hong Kong
        showOfficeNumber(jQuery("#clockHong"), 'Hong Kong', '+852 2892 1322');
    }
    else if (hourOffset == 9) { //Nagoya
        showOfficeNumber(jQuery("#clockNagoya"), 'Nagoya', '+81 (0) 52 508 7781');
    }
    else {
        showOfficeNumber(jQuery("#clockNyc"), 'New York', '+1 212 481 3452');
    }
}

function showOfficeNumber(div, name, number) {
    jQuery(".clock").each(function(index) {
        jQuery(this).removeClass('clock-active');
    });
    jQuery(div).addClass('clock-active');
    jQuery("#office-call").show();
    jQuery("#office-phone").show();
    jQuery("#office-call").text("Call " + name);
    jQuery("#office-phone").html(number);
}

function hideOfficeNumber() {
    jQuery(".clock").each(function(index) {
        jQuery(this).removeClass('clock-active');
    });
    jQuery("#office-call").hide();
    jQuery("#office-phone").hide();
    showClosestOffice();
}

function showClosedOffices(div, serverTime) {
	if (serverTime.getHours() <= 8 || serverTime.getHours() >= 18) {
		jQuery(div).addClass("clock-closed");
	}
}