/**
 * Site Wide Javascript Functions
 */
document.observe("dom:loaded", function(){

    // Setting the pop up styles
    $('popup').style.display = 'none';
    $('popup').style.cursor = 'pointer';

    // Setting the effects array
    var menu_effects = new Array();
    /**
     * This function will animate a sub menu up or down
     * @param $sub_id is the ID of the sub menu button
     * @param $direction is set to down to open the sub
     */
    function animate_sub_menu($sub_id, $direction){
        // Removing any old effects
        if(menu_effects[$sub_id]) menu_effects[$($sub_id).id].cancel();
        // Starting the new effect
        menu_effects[$sub_id] = new Effect.Morph($sub_id, {duration: 0.5, style: 'border:#'+(($direction == 'down') ? 'CC0001' : '325A8E')+';background:#'+(($direction == 'down') ? 'fff' : 'efefef')+';height:'+(($direction == 'down') ? $($sub_id).scrollHeight : '0')+'px;'});
    }

    /**
     * Setting up sub menu buttons
     */
    var len = $('menu_list').childNodes.length;
    for(var $i = 0; $i < len; $i++){
        // If the sub menu button has a ID
        if($('menu_list').childNodes[$i].childNodes[0].id){
            // Add the listeners to the button
            Event.observe($('menu_list').childNodes[$i].childNodes[0].id, 'mouseover', function(event) { animate_sub_menu(this.id+'_sub', 'down'); });
            Event.observe($('menu_list').childNodes[$i].childNodes[0].id, 'mouseout',  function(event) { animate_sub_menu(this.id+'_sub', 'up'); });
            // Add the listeners to the sub menu
            Event.observe($('menu_list').childNodes[$i].childNodes[0].id+'_sub', 'mouseover', function(event) { animate_sub_menu(this.id, 'down'); });
            Event.observe($('menu_list').childNodes[$i].childNodes[0].id+'_sub', 'mouseout',  function(event) { animate_sub_menu(this.id, 'up'); });
        }
    }

    // Adding all the sub menu calls
    var len = $('menu_drop_downs').childNodes.length;
    for(var $i = 0; $i < len; $i++){
        $('menu_drop_downs').childNodes[$i].style.height          = '0px';
        $('menu_drop_downs').childNodes[$i].style.overflow        = 'hidden';
        $('menu_drop_downs').childNodes[$i].style.backgroundColor = '#efefef';
    }

});

/**
 * This function checks all forms that are submitted
 * The goal is to handle ones with AJAX that can be handled with AJAX
 * @param object The HTML for Object
 */
function check_form($form){
    // Using validate to validate the fields
    if(validate_forms[$form.id]) {
        if(!validate_forms[$form.id].validate()){
            alert('Please fill in all form data correctly');
            return false;
        }
    }

    // If there is no form_name just return true
    if(!$form.form_name){
        return true;

    // If this form just needs to be set to a loading message
    }else if($form.form_name.value == 'video_demo_form' || 
             $form.d_form){
        // Loading the waiting element
        form_submitted($form.id);
        return true;

    // If this form just needs to be set to a loading message
    }else if($form.form_name.value == 'contact_form'){
        // Submiting the form with AJAX
        var params = new Array();
        params[0] = $form.snort.value;
        params[1] = $form.occurred_dateMonth.value;
        params[2] = $form.occurred_dateDay.value;
        params[3] = $form.occurred_dateYear.value;
        params[4] = $form.address.value;
        params[5] = $form.latitude.value;
        params[6] = $form.longitude.value;
        params[7] = $form.location.value;
        params[8] = $form.category.value;
        params[9] = $form.id;
        var json = new JSON;
        // Loading the waiting element
        form_submitted($form.id);
        json.request('submit_snort', params, contact_us_callback);
        return true;
    }
}
/**
 * This function will confirm if a form has been submitted and will replace it with a message
 * display the error details
 * @param object The HTML for Object
 */
function contact_us_callback($response){
    if(error_check($response['error'][0],$response['error'][1])){
        popup_content_show('Thank you for your inquiry. We will get back to you as soon as possible.', 0.2);
    }
}
