/* ctl.global.js */

/*
----------------------------------------------------------------
        FUNCTIONS:
----------------------------------------------------------------
*/

/* --------- MAP --------- */
(function($) {
    $.fn.cccmap = function() {
        var map = $(this);
        map.each(function(){
            
            // Only Show One per school: 
            $('.school-2836').eq(0).show();
            $('.school-2841').eq(0).show();
            $('.school-2843').eq(0).show();
            $('.school-2844').eq(0).show();
            $('.school-2845').eq(0).show();
            $('.school-2846').eq(0).show();
            $('.school-2847').eq(0).show();
            $('.school-2848').eq(0).show();
            $('.school-2849').eq(0).show();
            $('.school-2852').eq(0).show();
            $('.school-2853').eq(0).show();
            $('.school-2854').eq(0).show();
            $('.school-2855').eq(0).show();
            $('.school-2856').eq(0).show();
            $('.school-2857').eq(0).show();
            $('.school-2858').eq(0).show();
            $('.school-2859').eq(0).show();
            $('.school-2860').eq(0).show();
            $('.school-2861').eq(0).show();
            $('.school-2863').eq(0).show();
            
            var item_btn = map.find('.map-item').children('.img-crop');
            item_btn.each(function(){
                var _this = $(this);
                var _thisitem = _this.parent();
                var _thiscontent = _this.prev('.item-content');
                var _thisitemh4 = _thisitem.find('h4');
                var zindex;
                
                
                
                _this.hover(function(){
                    zindex = _thisitem.css('z-index');
                    _thisitem.css({ 'z-index' : '9000' });
                }, function(){
                    _thisitem.css({ 'z-index' : zindex });
                });
                $(this).click( function(){
                    item_btn.parent().each(function(){
                        $(this).removeClass('map-item-open');
                        $(this).find('.item-content').hide();
                        $(this).find('h4').hide();
                    });
                    _thisitem.addClass('map-item-open');
                    _thiscontent.show();
                    _thisitemh4.show();
                    _thiscontent.children('a.close-btn').click(function(){
                        _thisitem.removeClass('map-item-open');
                        _thiscontent.hide();
                        _thisitemh4.hide();
                    });
                    return false;
                });
            });
            
            var itemcount = $('.map-item:visible').length;
            var randomnumber=Math.floor(Math.random()*itemcount);
            $('.map-item').eq(randomnumber).addClass('map-item-open').children('.item-content').show().children('a.close-btn').click(function(){
                var item = $(this).parent().parent();
                item.removeClass('map-item-open');
                item.children('.item-content').hide();
                item.find('h4').hide();
            });;
            $('.map-item').eq(randomnumber).find('h4').show();
        });
        
    };
})(jQuery);

/* --------- EQULAIZE --------- */
(function($) {
    $.fn.equalize = function() {
        var eqSel = $(this);
        var aceHeight = 0;
        $(eqSel).each(function(){
            if( $(this).height() >= aceHeight ){
                aceHeight = $(this).height();
            }
        });
        $(eqSel).each(function(){
            $(this).height(aceHeight);
        });    
    };
})(jQuery);

/* --------- ACCORDION MENU --------- */
(function($) {
    $.fn.accordion = function() {
        return this.each(function(){
            _menu = $(this);
            
            _menu.find('li ul li a.active').each(function(){
                $(this).parent('li').parent('ul').parent('li').addClass('active');
            });
            
            _menu.children('li').children('a').click(function(){
                if( $(this).parent('li').children('ul').length >= 1 ){
                    $(this).parent('li').children('ul').slideToggle('fast');
                    return false;
                } else { return true; }
            });
        });
    };
})(jQuery);

/* --------- SMART FOOTER --------- */
(function($) {
    $.fn.smartfooter = function() {
        return this.each(function(){
            var _footer = $(this);
            $(window).bind('resize', function() {
                var footerPos = _footer.position();
                var footerH = _footer.outerHeight();
                var footerBtm = footerPos.top + footerH;
                var windowH = $(window).height();
                var difference = windowH - footerBtm;
                var newH = footerH + difference -1;
                if ( ( footerPos.top + footerH ) < windowH ){
                    _footer.css({ 'height' : newH });
                };
            });
            $(window).trigger('resize');
        });
    };
})(jQuery);

/* --------- NICE ALERT --------- */
(function($){
    $.fn.niceAlert = function( msg ) {
        var _niceAlertModal = $('#nice-alert-modal');
        //Test for existing modal
        if( _niceAlertModal.length == 0 ) {
            //Doesn't exist
            //Do normal alert:
            alert( msg );
        } else {
            //Handle close
            _niceAlertModal.find('a.close-btn').click(function(){_niceAlertModal.slideUp('slow');});
            _niceAlertModal.find('a.close-link').click(function(){_niceAlertModal.slideUp('slow');});
            //Clear prev msg:
            _niceAlertModal.find('p.nice-alert-msg').html( '' );
            //Add msg to alert modal:
            _niceAlertModal.find('p.nice-alert-msg').html( msg );
            //Show nice alert:
            _niceAlertModal.slideDown('slow');
        }
    };
})(jQuery);

/* --------- Easy Validate --------- */
(function($) {
    $.fn.easyvalidate = function(options) {
        // build main options before element iteration
        var opts = $.extend({}, $.fn.easyvalidate.defaults, options);
        // iterate and reformat each matched element
        return this.each(function() {
            _this = $(this);
            // build element specific options
            var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
            //Set the valid status:
            var validStatus = Array();
            validStatus[0] = false;
            var theForm = $(o.formID);
            var theRequired = $(theForm).find(o.requiredClass);
            //add an on-the-fly validation test to each required field in the form
            theRequired.each(function( index ){
                
                var fieldIndex = index;
                
                var _thisField = $(this);
                _thisField.focus(function(){
                    _thisField.removeClass('field-invalid');
                });
                _thisField.blur(function(){
                    if( $(this).val() == '' ){
                        _thisField.addClass('field-invalid').parents('.form-item').find('.form-required').text('Required *');
                        validStatus[fieldIndex] = false;
                    } else {
                        _thisField.removeClass('field-invalid').parents('.form-item').find('.form-required').text('*');
                        validStatus[fieldIndex] = true;
                    }
                });
            });
            //valid?
            theForm.submit(function(){
                //Test each required field again:
                theRequired.each(function( index ){
                    var fieldIndex = index;
                    var _thisField = $(this);
                    if( $(this).val() == '' ){
                        _thisField.addClass('field-invalid');
                        validStatus[fieldIndex] = false;
                    } else {
                        _thisField.removeClass('field-invalid');
                        validStatus[fieldIndex] = true;
                    }
                });
                //Search the array for invalid values:
                var validForm = true;
                $.each(validStatus, function( index, value ){
                    if( value == false ){
                        validForm = false;
                        return false;
                    } else { return true; }
                });
                // If invalid, stop submission and alert invalid Fields:
                if( validForm == false ){
                    var invalidMsg = '<ul>';
                    theRequired.each(function(){
                        if ( $(this).hasClass('field-invalid') ){
                            invalidMsg += '<li><span class="capitalize">' + $(this).attr('name') + '</span> field is required. </li>';
                        }
                    });
                    invalidMsg += '</ul>';
                    $().niceAlert(invalidMsg);
                    //Stop submission:
                    return false;
                } else { return true; }
            });
        });
    };
    // plugin defaults
    $.fn.easyvalidate.defaults = {
        formID: '#formID',
        requiredClass: '.required'
    };
})(jQuery);

/*
----------------------------------------------------------------
        DOM READY:
----------------------------------------------------------------
*/

$(document).ready(function() {
    if (jQuery().equalize ){
        $('.equal-z').equalize();
        $('#bottom-blocks .pane').equalize();
    };
    if (jQuery().accordion ){
        $('#sidebar-left ul').accordion();
    };
    if (jQuery().smartfooter ){
        $('.window-bottom').smartfooter();
    };
    if (jQuery().easyvalidate ) {
        $('#node-form #content input#edit-submit').easyvalidate({
            'formID' : '#node-form',
            'requiredClass' : '.required'
        });
    };
    if( jQuery().cccmap ){
        $('#ccc-map').cccmap();
    };
    /* Calendar */
    if ( $.cookie && $('body.page-calendar').length > 0 ) {
        // Cache the list & checkboxes:
        var catList = $('ul.event-categories-list');
        var boxes = catList.find('input[type=checkbox]');
        //Add category class to parent li:
        boxes.each(function(){
            _item = $(this);
            var itemcatname = _item.attr('name');
            _item.parent('li').addClass('item-' + itemcatname);
        });
        //Prep a new selection list:
        var newSelection = Array();
        //If cookie data exists:
        if( $.cookie('calendar topic selections') ){
            //Get the cookie data:
            var selectedList = $.cookie('calendar topic selections');
            //Make the cookie data an array:
            selectedList = selectedList.split(',');
            //Clear all checkboxes:
            boxes.each(function(){
                $(this).attr('checked', false);
            });
            //Hide all events:
            $('div.calendar-calendar').find('div.cal-entry').hide();
            //Check all and show events based on selections list:
            $.each(selectedList, function( index, value ){
                catList.find('input[name=' + value +']').attr('checked', true);
                $('.calendar-calendar').find('div.' + value).show();
            });
        } else {
            //Check all checkboxes:
            boxes.each(function(){
                $(this).attr('checked', true);
            });
            //Show all events:
            $('.calendar-calendar').find('div').show();
        }
        boxes.click(function(){
            //Reset new selection list:
            newSelection.length = 0;
            //Set new selection from checked boxes:
            boxes.each(function(){
                var catName = $(this).attr('name');
                if( $(this).attr('checked') ){
                    newSelection.push( catName );
                    $('.calendar-calendar').find('div.' + catName).fadeIn();
                } else {
                    $('.calendar-calendar').find('div.' + catName).fadeOut();
                }
            });
            $.cookie('calendar topic selections', newSelection );
        });
    };
    
    /* Login fields */
    $('input#edit-pass').after('<span class="login-password-label">password</span>');
    $('span.login-password-label').hover(function(){
        $('input#edit-pass').toggleClass('hover');
    }, function(){
        $('input#edit-pass').toggleClass('hover');
    });
    $('span.login-password-label').click(function(){
        $(this).remove();
        $('input#edit-pass').focus();
    });
    $('input#edit-pass').focus(function(){
        $('span.login-password-label').remove();
        $(this).removeClass('hover');
    });
    $('#toolbar-pane input#edit-name').keypress(function(){
        setTimeout(function(){
            if( $('#edit-pass').val() != "" ) {
                $('span.login-password-label').remove();
                $(this).removeClass('hover');
            }
        }, 100);
    });
    $(window).bind( 'mousemove', function(){
        setTimeout(function(){
            if( $('#edit-pass').val() != "" ) {
                $('span.login-password-label').remove();
                $(this).removeClass('hover');
                $(window).unbind('click')
            }
        }, 100);
        
    });
    
});
