/* ---------------------------------------------------------------------
Global JavaScript & jQuery

Target Browsers: All
Authors: Jessica Green, Matt Aho
Revision: $Rev: 5 $
Version: $Id: global.js 5 2011-10-03 20:46:27Z jgreen $
------------------------------------------------------------------------ */

var DLN = DLN || {};

$(function() {

    // Initialize!
    DLN.ButtonFix.init();
    DLN.CustomSelect.init();
    DLN.TableRowColor.init();
    DLN.EmptyDiv.init();
    DLN.ColorAnimate.init();
});

DLN.initializeCfeFormElements = function()
{
    DLN.ButtonFix.init();
    DLN.CustomSelect.init();
    DLN.TableRowColor.init();
    DLN.EmptyDiv.init();
    DLN.ColorAnimate.init();

    var cf = new CustomFormElements({styled: 'styled', uniqueClassName: 'customFormElement', checkboxHeight: 20, radioHeight: 20, selectWidth: 341});
    cf.repaint();
};

/* ---------------------------------------------------------------------
ButtonFix
Author: Matt Aho

Adds a span tag inside button tags to accomplish CSS sliding doors
------------------------------------------------------------------------ */

DLN.ButtonFix = {
    init: function() {
        //$('button').wrapInner('<span />');
        $('button').each(function(){
        	var attrName = 'data-buttonfix';
        	var alreadyInit = $(this).attr(attrName);
        	if (!alreadyInit) {
        		$(this).wrapInner('<span />');
        		$(this).attr(attrName,'true');
        	}
        });
    }
};

/* ---------------------------------------------------------------------
Custom Select
Author: Scott Garrison

Adds a styled class to selects in forms. 
------------------------------------------------------------------------ */

DLN.CustomSelect = {
    init: function() {
        if($('form').length) {
            $('select').not('.no-styling').addClass('styled');
            $('select').not('.no-styling').closest('dd').addClass('select-contain'); //adds contain height so the layout doesn't break
        }
        if($('input:radio').not('.no-styling').length) {
            $('input:radio').not('.no-styling').addClass('styled');
        }
        if($('input:checkbox').not('.no-styling').length) {
            $('input:checkbox').not('.no-styling').addClass('styled');
        }
    }
};

/* ---------------------------------------------------------------------
Table Row Color
Authors: Scott Garrison, Matt Aho

Adds variation of bg color to tables. 
------------------------------------------------------------------------ */

DLN.TableRowColor = {
    init: function() {    
        if($('table').length) {
            $('tbody tr:nth-child(odd)').addClass('odd'); 
        }
    }   
};

/* ---------------------------------------------------------------------
Category Landing span-20 fix
Author: Scott Garrison      


Changes the span-20 on the category landing page to be 100% width
------------------------------------------------------------------------ */

DLN.EmptyDiv = {
    init: function() {
        if($('#globalMessages').children('p').is(':empty')) {
            $('#globalMessages').css({'display' : 'none'});
        }
    }   
};


/* ---------------------------------------------------------------------
Navigation Color Animation
Author: Scott Garrison

On the search pages and product listing navigation, animate the color
bars on hover
------------------------------------------------------------------------ */

DLN.ColorAnimate = {
    init: function() {
        if($('.nav-color').length) {
            var animateDuration = 150;
            
            $('.nav-color').hover(
                function() {
                    $(this).stop().animate({ width: '124px'}, animateDuration);
                }, function() {
                    $(this).stop().animate({ width: '62px'}, animateDuration);
                }
            );
        }
    }   
};

