$(document).ready(function(){
    /**
     * Form submit button mouseover/out styles
     */
    $("fieldset#form_options button").mouseover(function(){
        $(this).css("border-color", "#666")
               .css("cursor", "pointer");
    });
    
    $("fieldset#form_options button").mouseout(function(){
        $(this).css("border-color", "#799493");
    });
    
    /**
     * Focus/Blur events for all fields
     */
    $("input, textarea, select").focus(function(){
        if (!$(this).parents("div.row:first").hasClass("error"))
        {
            $(this).css("border-color", "#999");
        }
    });
    
    $("input, textarea, select").blur(function(){
        if (!$(this).parents("div.row:first").hasClass("error"))
        {
            $(this).css("border-color", "#e2e2dc");
        }
    });
    
    var occurrence = $("div.occurrence1").clone().removeClass("occurrence1");
    var occurrenceCount = 1;
    var addLink = $("<div></div>")
        .addClass("addNewOccurrence")
        .css("bottom", "0")
        .css("color", "#2b8884")
        .css("position", "absolute")
        .css("right", "30px")
        .css("text-decoration", "underline")
        .html("Add a new occurrence")
        .mouseover(function(){
            $(this).css("cursor", "pointer");
        })
        .click(function(){
            occurrenceCount++;
            var temp = occurrence.clone();
            temp.addClass("occurrence" + occurrenceCount);
            $("label", temp).each(function(){
                $(this).attr("for", $(this).attr("for").replace("1", occurrenceCount));
                $(this).html($(this).html().replace("1", occurrenceCount));
            });
            
            $("input", temp).each(function(){
                $(this).attr("name", $(this).attr("name").replace("1", occurrenceCount));
                $(this).attr("id", $(this).attr("id").replace("1", occurrenceCount));
            });
            
            $("select", temp).each(function(){
                $(this).attr("name", $(this).attr("name").replace("1", occurrenceCount));
                $(this).attr("id", $(this).attr("id").replace("1", occurrenceCount));
            });
            
            temp.appendTo($("#occurrences"));
        })
        .insertAfter($("#occurrences legend"));
    
    
    if (navigator.appName != "Microsoft Internet Explorer" || parseFloat(navigator.appVersion) > 4)
    {
        /**
         * General info checkbox initiation
         */
        $("fieldset#general_info input:checkbox:not(:checked)").each(function(){
            var element = $("fieldset#general_info div." + $(this).attr("id"));
            element.hide();
        });
        
        /**
         * General info checkbox change event
         */
        $("fieldset#general_info input:checkbox").change(function(){
            var element = $("fieldset#general_info div." + $(this).attr("id"));
            if ($("input#" + $(this).attr("id") + ":checked").length == 1)
            {
                element.slideDown(200);
            }
            else
            {
                element.slideUp(200);
            }
        });
    }
});
