$(document).ready(function () {

    //$.pseudoFocus();
    $(".teaserItem ul").hide();
    initRegionChanger();
    initProvinceChanger();

    initFormCherries();
    processGATracker();
});

function initFormCherries() {
    $('#zipnum').keyup(function () {
        if ($(this).val().length == 4) {
            $('#zipchar').focus();
        }
    });

}

function processGATracker() {
    var gaCode = $('input[name=gasuccesscode]').val();
    if (gaCode != null && gaCode != '') {
        _gaq.push(['_trackPageview', gaCode]);
    }
}

function openTwitter(url) {
    window.open(url, 'Deel_dit_op_Twitter', 'status=1,toolbar=0,location=1,menubar=0,directories=0,resizable=0,scrollbars=0,height=400,width=533');
}

function openFacebook(url) {
    window.open(url, 'Deel_dit_op_Facebook', 'status=1,toolbar=0,location=1,menubar=0,directories=0,resizable=1,scrollbars=0,height=400,width=533');
}

function initRegionChanger() {
    $('form select#region').change(function () {
        var regionVal = $(this).children('option:selected').val();
        var idx = regionVal.indexOf('|');
        var regionId = regionVal.substring(0, idx);
        var offices = $('form select#office');
        var officeId = $(offices).val();
        $(offices).children('option').each(function () {
            var officeVal = $(this).val();
            if (officeVal != '') {
                $(this).remove();
            }
        });
        var allOffices = $(offices).nextAll('select')[0];
        $(allOffices).children('option').each(function () {
            var officeVal = $(this).val();
            var idx = officeVal.indexOf('|');
            var officeRegionId = officeVal.substring(0, idx);
            if (officeRegionId == regionId) {
                var clone = $(this).clone();
                if (clone.val() == officeId) {
                    clone.attr('selected', 'selected');
                }
                $(offices).append(clone);
            }
        });
    });

    $('form select#region').change();
}

function initProvinceChanger() {
    $('form select#province').change(function () {
        var provinceVal = $(this).children('option:selected').val();
        var idx = provinceVal.indexOf('|');
        var provinceId = provinceVal.substring(0, idx);
        var offices = $('form select#office');
        var officeId = $(offices).val();
        $(offices).children('option').each(function () {
            var officeVal = $(this).val();
            if (officeVal != '') {
                $(this).remove();
            }
        });
        var allOffices = $(offices).nextAll('select')[0];
        $(allOffices).children('option').each(function () {
            var officeVal = $(this).val();
            var idx = officeVal.indexOf('|');
            var officeRegionId = officeVal.substring(0, idx);
            if (officeRegionId == provinceId) {
                var clone = $(this).clone();
                if (clone.val() == officeId) {
                    clone.attr('selected', 'selected');
                }
                $(offices).append(clone);
            }
        });
    });

    $('form select#province').change();
}
