$(document).ready(function() {
    $('select#choose_video').change(function() {
        top.location = app['base_url'] + 'community/anglers-legacy/share-the-legacy/bassador/video/' +
           $(this).val();
        return true;
    });
    
    $('a#referred_community_home').click(function() {
        // set a cookie for the next URL since specifying that URL as a request parameter
        // is impossible due to ModX screwing it up
        
        // remove the /referrer/$id parameter at the end so the associated cookie isn't set
        // again if the user is brought back to the URL through the fishington registration process
        var url = window.location.href.replace(/\/referrer\/.*/i, '');
        
        // the cookie points to the current URL and expires in 1 hour;
        // the function ins cookie.funtions.js
        createCookie('nextUrl', url, 1/24); 
    });
    
    var defaultValues = {name: 'Your Name', email: 'Your Email', friend1Name: "Your Friend's Name",
        friend1Email: "Your Friend's Email", friend2Name: "Your Friend's Name", friend2Email: "Your Friend's Email",
        friend3Name: "Your Friend's Name", friend3Email: "Your Friend's Email", message: 'Type a message here'};

    $formFields = $('.hidden_form input[type=text], .hidden_form textarea');
    
    function setDefaultValue() {
        if ($(this).val() == '') {
            $(this).removeClass('font_color_change');
            $(this).val(defaultValues[$(this).attr('name')]);
        // else if we've posted back the form fields
        } else if (OnInit && $(this).val() != defaultValues[$(this).attr('name')]) {
            $(this).addClass('font_color_change');
        }
        return true;
    }
    
    var OnInit = true;
    
    // set the default values
    $formFields.each(setDefaultValue);
    
    OnInit = false;

    $formFields.blur(setDefaultValue);
    
    function clearDefaultValue() {
        if ($(this).val() == defaultValues[$(this).attr('name')]) {
            $(this).val('');
            $(this).addClass('font_color_change');
        }
        return true;
    }
    
    $formFields.focus(clearDefaultValue);
    
    $('#share_form').submit(function() {
        // clear any default values
        $formFields.each(clearDefaultValue);
        return true;
    });
    
    var $sendToFriend = $('div.send_to_friend');
    var $hiddenForm = $('.hidden_form');
    
    // toggle share form visibility
    $sendToFriend.click(function() {
        if ($hiddenForm.length > 0) {
            $hiddenForm.slideToggle(500);
            
            $(this).toggleClass('open_arrow');
            return false;
        }
    });
    
    var $modalInstructions = $('.modal_instructions');
    if ($modalInstructions.length > 0) {
        $('div.send_to_friend, a.twitter, a.facebook').click(function() {
            // show the dialog
            $modalInstructions.show();

            $('a.close_btn').click(function() {
                $modalInstructions.hide();
               return false;
           });

           return false;
        });
    }
    
    // if there are submission errors
    if ($hiddenForm.length > 0 && $hiddenForm.children('.errors').length > 0) {
        // show the form
        $sendToFriend.click();
    }
});
