/**
*	Author: Jarrett M. Barnett
*	E-mail: jarrett@mc2design.com
*	Company: MC2 Design Group, Inc.
*	Copyright (c) 2010
*	Last Modified: 2010-01-13
*	Notes: Built and tested using jQuery library v1.3.2
*/


$(document).ready(function() {
    initNavigation();
    initBusinessForm();
    initEventFilter();
}); // end jquery ready event

function initNavigation()
{
    var _menulink = $("#menu li:has(ul) a");

    _menulink.click(function(){
        var _parentitem = $(this).parent();

        if(_parentitem.hasClass('parent-active')) {
            _parentitem.toggleClass('parent-active');
            return false;
        } else if(_parentitem.hasClass('sub-level-0')) {
            _parentitem.toggleClass('active');
            return false;
        }


    })
}

function initBusinessForm()
{
    function validateWordCount(_o,_m) // _object and _maximum word count
    {
        if(_o.val().split(" ").length > _m || _o.val().length == 0)
        {
            _o.addClass("error");
            return false;
        } else {
            _o.removeClass("error")
            return true;
        }
    }


    $(".textarea").each(function(){
        $(this).bind("keypress", function(){
            validateWordCount($(this), 150);
        })
        $(this).change(function(){
            validateWordCount($(this), 150);
        })
    })


    $("#freeform").submit(function()
    {
        $(".textarea").each(function(){
            if ( validateWordCount($(this),150) == false) $errorflag = true;
        })

        if($errorflag == true)
        {
            $("#errormessage").slideDown(300);
            return false;
        }
        else
        {
            $("#errormessage").slideUp(300);
        }

    });


}

function initEventFilter()
{
    $("#countyfilter").change(function(){

        var _value = $(this).val();

        if(_value != "")
        {
            $.ajax({
                dataType: 'html',
                url: '/index.php/helpers/_eventsbycounty/'+_value,
                type: 'GET',
                success: function(data) {
                    $("#schedule").html(data);
                }
            }); // end ajax
        }

    });
}
