/**
 * Akilli_Tabs
 *
 * @category   Akilli
 * @package    Akilli_Tabs
 * @author     Ayhan Akilli <info@ayhan.biz>
 */

if (!window.Akilli) var Akilli = {};

Akilli.Tabs =
{
    /**
     * Flag wheter to equalize tab heights
     *
     * @var bool
     */
    adjustHeight : false,

    /**
     * Flag wheter to jump to anchor on menu click
     *
     * @var bool
     */
    jumpToAnchor : false,

    /**
     * Loads tabs container and creates tabs menu
     *
     * @param string parentId
     * @param bool adjustHeight
     */
    load : function(parentId, adjustHeight, jumpToAnchor)
    {
        if (parentId == null || jQuery('#' + parentId).get(0) == null) {
            return false;
        }

        this.adjustHeight = (adjustHeight) ? true : false;
        this.jumpToAnchor = (jumpToAnchor) ? true : false;

        var parent = jQuery('#' + parentId);
        var menu, a, height;
        var menuId = parentId + '-menu';
        var i = 0;
        var maxHeight = 0;

        menu = document.createElement('div');
        menu.id = menuId;
        menu.className = 'tabs-menu';

        if (this.jumpToAnchor) {
            menu.className += ' tab-menu-jump';
        }

        parent.children(':not(\'.tabs-menu\')').each(function()
        {
            if (!this.id) {
                this.id = parentId + '-tab-' + (i + 1);
            }

            jQuery(this).addClass('tab');

            a = document.createElement('a');
            a.id = this.id + '-menu';
            a.href = '#' + this.id;

            if (jQuery(this).hasClass('default-tab')) {
                a.className = 'default-tab';
            }

            if (jQuery(this).children('.tab-title:first').text()) {
                a.innerHTML = jQuery(this).children('.tab-title:first').text();
                jQuery(this).children('.tab-title:first').addClass('hide');
            } else if (this.title != '') {
                a.innerHTML = this.title;
            } else {
                a.innerHTML = 'Tab ' + (i + 1);
            }

            menu.appendChild(a);

            height = jQuery(this).height();

            if (height > maxHeight) {
                maxHeight = height;
            }

            i++;
        });

        parent.prepend(menu);

        if (this.adjustHeight && maxHeight > 0) {
            parent.children('.tab').css('height', maxHeight + 'px');
        }

        jQuery('#' + menuId).children('a').click(function()
        {
            Akilli.Tabs.show(parentId, jQuery(this).attr('href'));

            return jQuery('#' + menuId).hasClass('tab-menu-jump');
        });

        jQuery('#' + menuId).children('a:first').addClass('first');
        jQuery('#' + menuId).children('a:last').addClass('last');

        this.show(parentId);
    },

    /**
     * Shows current tab in given container
     *
     * Child is specified with the anchor link (p.e. "#child-id").
     *
     * If no child is specified:
     *
     * 1. First window.location.hash value will be checked against the id's of all tabs.
     * 2. If no appropriate child for window.location.hash value exists, it will look for a tab with class "default-tab".
     * 3. If no tab with class "default-tab" exists, the first tab will be chosen.
     *
     * @param string parentId
     * @param string child
     */
    show : function(parentId, child)
    {
        if (parentId == null || jQuery('#' + parentId).get(0) == null) {
            return false;
        }

        var parent = jQuery('#' + parentId);
        var menuId = '#' + parentId + '-menu';

        parent.children('.tab').addClass('hide');
        jQuery(menuId + ' a').removeClass('current');

        if (!child && jQuery(document).attr('location').hash) {
            child = jQuery(document).attr('location').hash;
        }

        if (child != null && parent.children(child).get(0) != null) {
            parent.children(child).removeClass('hide');
            jQuery(menuId).children(child + '-menu').addClass('current');
        } else if (parent.children('.default-tab:first').get(0) != null) {
            parent.children('.default-tab:first').removeClass('hide');
            jQuery(menuId).children('a.default-tab:first').addClass('current');
        } else {
            parent.children('.tab:first').removeClass('hide');
            jQuery(menuId).children('a:first').addClass('current');
        }
    }
}

