// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function observe_billing_checkbox() {
    var checkbox = $('#billing-same');    
    var fieldsPrefix = "member_company_";
    var billingPrefix = "billing_";
    var fieldNames = new Array("name", "street", "city", "zip", "country");
    var allFieldsAreIdentical = true;

    // if all the fields are already the same : check the checkbox    
    for (index in fieldNames) {
        var fieldName = fieldsPrefix + fieldNames[index];
        var billingFieldName = fieldsPrefix + billingPrefix + fieldNames[index];
        var fieldValue = $("#" + fieldName).attr('value');
        var billingFieldValue = $("#" + billingFieldName).attr('value');

        if(fieldValue==="" || fieldValue!==billingFieldValue) {
            allFieldsAreIdentical = false;
        }
    }
    
    if(allFieldsAreIdentical) {
        checkbox.attr("checked", true);
    }

    checkbox.click(function() {
        var billingIsSame = $('#billing-same').attr('checked');
        
        if(billingIsSame) {
            for (index in fieldNames) {
                var fieldName = fieldsPrefix + fieldNames[index];
                var billingFieldName = fieldsPrefix + billingPrefix + fieldNames[index];
                
                // copy the value
                $("#" + billingFieldName).attr('value', $("#" + fieldName).attr('value'));
                
                // make field readonly
                $("#" + billingFieldName).attr('readonly', true);
            }
        }
        else {
            for (index in fieldNames) {
                var billingFieldName = fieldsPrefix + billingPrefix + fieldNames[index];
                
                // make field not readonly
                $("#" + billingFieldName).attr('readonly', false);
            }
        }
    });
}

function observe_category_id_radio() {
    var supplierId = "2";
    var radios = $("input:radio[name=member_company\\[category_id\\]]");
    radios.change(function() {
        var selectedValue = $(radios + ":checked").val();
        if(selectedValue===supplierId) {
            $("#notes-block").show();
        }
        else {
            $("#notes-block").hide();
        }
    });
}

function observe_sidebar_login_form_submit() {
    $('#sidebar-login-form-submit').click(function() {
        $('#login-form').submit();
    })
}

function observe_home_login_form_submit() {
    $('#home-login-form-submit').click(function() {
        $('#login-form').submit();
    })
}

function observe_tos_registration_checkbox() {
    var checkbox = $("#tos-checkbox");
    var submitButton = $("#member-submit-button");
    
    checkbox.click(function() {
        if(checkbox.attr("checked")) {
            submitButton.attr("disabled", false);
        }
        else {
            submitButton.attr("disabled", true);
        }
    })
}

function observe_choose_event_link() {
    var link = $("#choose-event-link");
    
    link.click(function() {
        $('#choose-frm').submit()
    });
}

function observe_board_preview_links() {
    var links = $(".board-preview-link");
    
    links.each(function() {
        $(this).hover(
            function () {
                var id = $(this).attr("id").replace("board-preview-link-", "");
                var div = $("#preview-" + id);
                div.show("fast");
            }, 
            function () {
                var id = $(this).attr("id").replace("board-preview-link-", "");
                var div = $("#preview-" + id);
                div.hide("fast");
            }
        );
    })
}

function observe_submit_buttons() {
    $("#contact-submit-image").click(function() {
        $("#contact-form").submit();
    })
}

function rotating_banner() {
    $("#rotating-banner").rotator({ms : 3000});
}

function observe_external_links() {
    // open pdfs and googlemaps in new window
    $("a[rel='external'],a[href$='.pdf'],a[href^='http://maps.google.be'],a[href^='http']").click(function(){
        window.open(this.href);
        return false;
    });
}

function rotating_sponsor() {
  var rotatingDiv = $('#right-column-sponsors');
  rotatingDiv.vTicker({
        speed: 500,
        pause: 3000,
        showItems: 4,
        direction: 'up',
        height: 400
  });
}

$(document).ready(function() {
    observe_billing_checkbox();
    observe_category_id_radio();
    observe_sidebar_login_form_submit();
    observe_home_login_form_submit();
    observe_tos_registration_checkbox();
    observe_choose_event_link();
    observe_board_preview_links();
    observe_submit_buttons();
    rotating_banner();
    rotating_sponsor();
    observe_external_links();
});
