﻿(function ($) {

    $.fn.dropKick = function (options) {

        var defaults = {
            customNodes: null,
            nodeConvention: 'dd-',
            hoverClass: 'hover',
            onMouseover:null,
            onMouseout: null,
            flipNodes: null
        }

        var options = $.extend(defaults, options),
            $item = $(this).find('> li'),
            $this,
            curNode,
            $ddSlides;

        if (options.customNodes != null) {
            var cNodeLength = options.customNodes.length;
            var i;
            for (i = 0; i < cNodeLength; i++) {
                $item.eq(options.customNodes[i]).addClass('dd-custom');
            }
        }

        if (options.flipNodes != null) {
            var fNodeLength = options.flipNodes.length;
            var i;
            for (i = 0; i < fNodeLength; i++) {
                $item.eq(options.flipNodes[i]).addClass('flip');
            }
        }

        return $item.each(function () {

            $this = $(this);

            if ($this.hasClass('dd-custom')) {
                $this.append($('#' + options.nodeConvention + $this.index()));
                $('#' + options.nodeConvention + $this.index()).addClass('dropDown');
            } else {
                if ($this.find('.dropDown').size() < 1 && $this.find('ul').size() > 0) {
                    $this.find('> ul').addClass('dropDown');
                }
            }

            $this.hoverIntent(function () {
                $this = $(this);
                $this.addClass(options.hoverClass);
                if (Cufon !== 'undefined') {
                    Cufon.refresh('.mainNav > li.hover > a');
                }
                if ($this.find('.dropDown').size() > 0) {
                    $this.find('.dropDown').slideDown(300);
                    if (options.onMouseover != null) {
                        options.onMouseover();
                    }
                }
            }, function () {
                $this = $(this);
                $this.removeClass(options.hoverClass);
                if (Cufon !== 'undefined') {
                    Cufon.refresh('.mainNav > li:not(".on") > a');
                }
                if ($this.find('.dropDown').size() > 0) {
                    $this.find('.dropDown').stop(true, true).slideUp(300);
                }
                if (options.onMouseout != null) {
                    options.onMouseout();
                }

            });
        });
    };
})(jQuery);

