
$(document).ready(function()
{
    var a=0; b=0; c=0;
    var c1=0; c2=0; c3=0; c4=0; c5=0; c6=0; c7=0;
    // enable multi-select functionality for select fields
    if (typeof jQuery.fn.multiSelect == 'function') {
        $('select.multi-select').multiSelect();
    }

    // enable customized look for select fields
    if (typeof jQuery.fn.SelectCustomizer == 'function') {
        $('select.form-select').SelectCustomizer();
    }

    // enable customized look for checkboxes and radio buttons
    if (typeof jQuery.fn.checkBox == 'function') {
        $('input.form-checkbox, input.form-radio').checkBox({
            replaceInput: true,
            addLabel: false
        });

        // Select "All" functionality
        $('#box-volunteer-availability, #box-volunteer-search-availability').each(function()
        {
            var rows = $('#box-volunteer-availability tbody tr, #box-volunteer-search-availability tbody tr');
            var rowsCnt = $(rows).size();

            var i = 0; var j = 0;
            // enumerate rows
            $(rows).each(function() {
                $(this).attr('title', ++i);
                var k = 0;
                // enumerate cols
                $(this).find('td').each(function () {
                    $(this).attr('title', ++k);
                });
            });
            // enumerate the links
            $(rows).eq(rowsCnt-1).find('a').each(function() {
                $(this).attr('title', ++j);
            });

            // links functionality
            $('#box-volunteer-availability td a, #box-volunteer-search-availability td a').bind('click', function()
            {
                var thisrow = $(this).parents('tr').find('td');

                // toggle the rows
                if ($(thisrow).size()-1 == 7)
                {
                    
                   if(a == 0 && ($(this).parents('tr').attr('title') == 1))
			        {
                       $(thisrow).find('input[type=checkbox]').checkBox('changeCheckStatus', true);
                       a=1;
                    }
			        else if (a == 1 && ($(this).parents('tr').attr('title') == 1))
                   {
                        $(thisrow).find('input[type=checkbox]').checkBox('changeCheckStatus', false);
                        a=0;
                   }
                    if(b == 0 && ($(this).parents('tr').attr('title') == 2))
			        {
                       $(thisrow).find('input[type=checkbox]').checkBox('changeCheckStatus', true);
                       b=1;
                    }
			        else if(b == 1 && ($(this).parents('tr').attr('title') == 2))
                   {
                        $(thisrow).find('input[type=checkbox]').checkBox('changeCheckStatus', false);
                        b=0;
                   }

                    if(c == 0 && ($(this).parents('tr').attr('title') == 3))
			        {
                       $(thisrow).find('input[type=checkbox]').checkBox('changeCheckStatus', true);
                       c=1;
                    }
			        else if(c == 1 && ($(this).parents('tr').attr('title') == 3))
                   {
                        $(thisrow).find('input[type=checkbox]').checkBox('changeCheckStatus', false);
                        c=0;
                   }

                }
                // toggle the columns
                if (parseInt($(this).parents('tr').attr('title')) == rowsCnt)
                {
                   var thiscol = parseInt($(this).attr('title'));
                    if(c1 == 0 && thiscol == 1)
			        {
                       
                      $(rows).find('td[title='+thiscol+'] input[type=checkbox]').checkBox('changeCheckStatus', true);
                      c1 = 1;
                    }
			        else if(c1 == 1 && thiscol == 1)
                    {
                      $(rows).find('td[title='+thiscol+'] input[type=checkbox]').checkBox('changeCheckStatus', false);
                      c1 = 0;
                    }
                    if(c2 == 0 && thiscol == 2)
			        {
                       
                      $(rows).find('td[title='+thiscol+'] input[type=checkbox]').checkBox('changeCheckStatus', true);
                      c2 = 1;
                    }
			        else if(c2 == 1 && thiscol == 2)
                    {
                      $(rows).find('td[title='+thiscol+'] input[type=checkbox]').checkBox('changeCheckStatus', false);
                      c2 = 0;
                    }
                    if(c3 == 0 && thiscol == 3)
			        {
                       
                      $(rows).find('td[title='+thiscol+'] input[type=checkbox]').checkBox('changeCheckStatus', true);
                      c3 = 1;
                    }
			        else if(c3 == 1 && thiscol == 3)
                    {
                      $(rows).find('td[title='+thiscol+'] input[type=checkbox]').checkBox('changeCheckStatus', false);
                      c3 = 0;
                    }
                    if(c4 == 0 && thiscol == 4)
			        {
                       
                      $(rows).find('td[title='+thiscol+'] input[type=checkbox]').checkBox('changeCheckStatus', true);
                      c4 = 1;
                    }
			        else if(c4 == 1 && thiscol == 4)
                    {
                      $(rows).find('td[title='+thiscol+'] input[type=checkbox]').checkBox('changeCheckStatus', false);
                      c4 = 0;
                    }
                    if(c5 == 0 && thiscol == 5)
			        {
                       
                      $(rows).find('td[title='+thiscol+'] input[type=checkbox]').checkBox('changeCheckStatus', true);
                      c5 = 1;
                    }
			        else if(c5 == 1 && thiscol == 5)
                    {
                      $(rows).find('td[title='+thiscol+'] input[type=checkbox]').checkBox('changeCheckStatus', false);
                      c5 = 0;
                    }
                    if(c6 == 0 && thiscol == 6)
			        {
                       
                      $(rows).find('td[title='+thiscol+'] input[type=checkbox]').checkBox('changeCheckStatus', true);
                      c6 = 1;
                    }
			        else if(c6 == 1 && thiscol == 6)
                    {
                      $(rows).find('td[title='+thiscol+'] input[type=checkbox]').checkBox('changeCheckStatus', false);
                      c6 = 0;
                    }
                    if(c7 == 0 && thiscol == 7)
			        {
                       
                      $(rows).find('td[title='+thiscol+'] input[type=checkbox]').checkBox('changeCheckStatus', true);
                      c7 = 1;
                    }
			        else if(c7 == 1 && thiscol == 7)
                    {
                      $(rows).find('td[title='+thiscol+'] input[type=checkbox]').checkBox('changeCheckStatus', false);
                      c7 = 0;
                    }


                    
                }
            });
        });
    }

    // autoclear input field
    if (typeof jQuery.fn.toggleVal == 'function') {
        $("form .form-item input[type='text']").toggleVal();
    }

    // enable custom look for input textfields
    $("form input[type='text'], form input[type='password']").each(function() {
        // retrieve width info
        var w = $(this).attr('class').replace('form-text','');
        if (w.search('multiSelect') == -1) { // ignore multiselect customized inputs
            $(this).removeClass(w);
            // create wrapper
            $(this).wrap('<span class="text-wrapper"></span>');
            $(this).parent('span').addClass(w);
        }
    });

    // enable custom look for textareas
    $("form textarea").each(function() {
        if ($(this).attr('class')=='') {
            $(this).wrap('<span class="textarea-wrapper"></span>');
        }
    });

    // controls affiliate member login
    $('#affiliate-member .link_login').click(function() {
        $(this).parents('#affiliate-member').toggleClass('expand')
    });

    // controls expandable/collapsable boxes
    $('.box.box-expanded').not('.box-nocollapse').find('h3.title').after('<a class="rover link_boxclose"></a>');
    $('.box.box-collapsed').not('.box-nocollapse').find('h3.title').after('<a class="rover link_boxopen"></a>');
    $('.box-collapsed .box-content,.box-collapsed .box-btm').css('display','none'); //hide only after DOM loads
	$('.box .box-top .rover').keydown( function(e) {
		if( e.keyCode == 13 || e.keyCode == 32 ) {	
			if ($(this).hasClass('link_boxclose')) {
				closeBox($(this));
			}
			else if ($(this).hasClass('link_boxopen')) {
				// close all others
				closeBox($(this).parents('.box').siblings('.box-expanded').find('.box-top .rover.link_boxopen, .box-top .rover.link_boxclose'));
				// open up box
				openBox($(this));
			}
		
		}
	});
    $('.box .box-top .rover').click(function()
    {
        if ($(this).hasClass('link_boxclose')) {
            closeBox($(this));
        }
        else if ($(this).hasClass('link_boxopen')) {
            // close all others
            closeBox($(this).parents('.box').siblings('.box-expanded').find('.box-top .rover.link_boxopen, .box-top .rover.link_boxclose'));
            // open up box
            openBox($(this));
        }
    });

    // appends "20 more" input fields to add new members  - Removed
   
   
    
   
});

closeBox = function (a)
{
    a.parents('.box-top').siblings().slideUp();
    a.parents('.box').addClass('box-collapsed').removeClass('box-expanded');
    a.addClass('link_boxopen').removeClass('link_boxclose');
    // hide box-top content
    if (a.parents('.box').hasClass('box-team-detail')) {
        a.siblings().hide();
        a.parents('.team-details').find('p').hide();
    }
}

openBox = function (a)
{
    a.parents('.box-top').siblings().slideDown();
    a.parents('.box').addClass('box-expanded').removeClass('box-collapsed');
    a.addClass('link_boxclose').removeClass('link_boxopen');
    // show box-top content
    if (a.parents('.box').hasClass('box-team-detail')) {
        a.siblings().show();
        a.parents('.team-details').find('p').show();
    }
}
