/**
 * Dom ready
 */
jQuery(document).ready(function()
{
    /**
     * Init
     */
    init();

    jQuery(window).resize(function()
    {
        init();
    });

    if (jQuery('#product-view-tabs').get(0) != null) {
        /**
         * Equalize heights of columns in tab content
         *
         * Selector class: .tab-content
         */
        var height;
        var maxHeight = 0;

        jQuery('#product-view-tabs .tab-content').each(function()
        {
            height = jQuery(this).height();

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

        jQuery('#product-view-tabs .tab-content').css('height', maxHeight + 'px');

        height = maxHeight = null;

        /**
         * Inits tabs on product view page
         *
         * @param id of tab-container
         * @param optional flag wheter to equalize tab-heights (default: false)
         * @param optional flag wheter to jump to anchor on menu click (default: false)
         */
        Akilli.Tabs.load('product-view-tabs', true);

        /**
         * Add click function on anchors to open linked tab
         */
        if (jQuery('#add-to-anchors').get(0) != null) {
            jQuery('#add-to-anchors a').click(function()
            {
                Akilli.Tabs.show('product-view-tabs', jQuery(this).attr('href'));
            });
        }
    }

    /**
     * Create slideshow
     */
    jQuery('.slideshow').each(function()
    {
        jQuery(this).find('.slider-previous').click(function()
        {
            if (jQuery(this).hasClass('disabled')) {
                return;
            }

            var slideshow = jQuery(this).parents('.slideshow:first');
            var list = jQuery(slideshow).find('ul,ol:first');
            var children = jQuery(list).children('li').length;
            var current = jQuery(slideshow).find('.slider-current:first');
            var c = parseInt(jQuery(current).text());

            jQuery(list).children('li').removeClass('first').removeClass('last');
            jQuery(list).prepend(jQuery(list).children('li:last'));
            jQuery(list).children('li:first').addClass('first');
            jQuery(list).children('li:last').addClass('last');

            if (c > 0) {
                c = (c - 1) % children;

                if (c == 0) {
                    c = children;
                }

                jQuery(current).text(c)
            }
        });

        jQuery(this).find('.slider-next').click(function()
        {
            if (jQuery(this).hasClass('disabled')) {
                return;
            }

            var slideshow = jQuery(this).parents('.slideshow:first');
            var list = jQuery(slideshow).find('ul,ol:first');
            var children = jQuery(list).children('li').length;
            var current = jQuery(slideshow).find('.slider-current:first');
            var c = parseInt(jQuery(current).text());

            jQuery(list).children('li').removeClass('first').removeClass('last');
            jQuery(list).append(jQuery(list).children('li:first'));
            jQuery(list).children('li:first').addClass('first');
            jQuery(list).children('li:last').addClass('last');

            if (c > 0) {
                c = (c + 1) % children;

                if (c == 0) {
                    c = children;
                }

                jQuery(current).text(c)
            }
        });

        if (jQuery(this).hasClass('auto-slide')) {
            jQuery(this).slideshow();
        }
    });

    /**
     * Lightbox
     */
    jQuery("a[rel^='prettyPhoto']").prettyPhoto({
        theme: 'light_square'
    });

    /**
     * Trigger to show/hide related element specified in the rel attribute of the trigger
     */
    jQuery('.trigger').each(function()
    {
        if (this.rel != null && jQuery('#' + this.rel) != null) {
            jQuery(this).click(function()
            {
                jQuery(this).toggleClass('open');
                jQuery('#' + this.rel).toggleClass('hide');

                return false;
            });
        }
    });

    /**
     * Left service navigation
     */
    var href = jQuery(document).attr('location').href;
    var hash = jQuery(document).attr('location').hash;
    var search = jQuery(document).attr('location').search;

    if (search.length > 0) {
        href = href.substr(0, href.length - search.length);
    }

    if (hash.length > 0) {
        href = href.substr(0, href.length - hash.length);
    }

    if (href.substr(-1) == '/') {
        href = href.substr(0, href.length - 1);
    }

    jQuery('#left .service-list a[href=' + href + ']').addClass('current');
    href += '/';
    jQuery('#left .service-list a[href=' + href + ']').addClass('current');

    href = hash = search = null;

    /**
     * Agreements
     */
    jQuery('.agreements').each(function()
    {
        jQuery(this).children('a').click(function()
        {
            var e = jQuery(this).parentsUntil('.inbut-box:first').siblings('.agreement-box:first');

            if (e != null) {
                jQuery(e).removeClass('hide');
                jQuery(e).css('top', jQuery(document).scrollTop() + 30);

                jQuery(e).find('.agreement-header a').click(function()
                {
                    jQuery(e).addClass('hide');
                    jQuery(e).removeAttr('style');

                    return false;
                });
            }

            return false;
        });
    });

    /**
     * Newsletter subscription checkbox
     */
    jQuery('.account-create input#is_subscribed, .my-account input#subscription').click(function()
    {
        if (!jQuery(this).attr('checked')) {
            jQuery('.newsletter-groupings input.checkbox').removeAttr('checked');
        }
    });

    jQuery('.account-create .newsletter-groupings input.checkbox').click(function()
    {
        if (jQuery(this).attr('checked')) {
            jQuery('.account-create input#is_subscribed').attr('checked', 'checked');
        }
    });

    jQuery('.my-account .newsletter-groupings input.checkbox').click(function()
    {
        if (jQuery(this).attr('checked')) {
            jQuery('.my-account input#subscription').attr('checked', 'checked');
        }
    });
});

/**
 * Slideshow
 */
jQuery.fn.slideshow = function()
{
    var e       = this;
    var timeout = 5000;
    var fadein  = 2000;
    var fadeout = 2000;

    var init = function()
    {
        if (jQuery(e).children().length <= 1) {
            return;
        }

        jQuery(e).children(':not(:first)').hide();

        if (jQuery(e).height() == 0) {
            jQuery(e).height(jQuery(e).children(':first').height());
        }
        setTimeout(slide, timeout);
    }

    var slide = function()
    {
        jQuery(e).children(':first').fadeOut(fadeout);
        jQuery(e).append(jQuery(e).children(':first'));

        if (jQuery(e).height() == 0) {
            jQuery(e).height(jQuery(e).children(':first').height());
        }

        jQuery(e).children(':first').fadeIn(fadein);
        setTimeout(slide, timeout);
    }

    init();
}

/**
 * Init
 */

function init()
{
    var height, offset;

    /**
     * Init mini cart
     */
    if (jQuery('#cart-sidebar-link').get(0) != null && jQuery('#cart-sidebar').get(0) != null) {
        offset = jQuery('#cart-sidebar-link').offset();
        height = jQuery('#cart-sidebar-link').height();

        jQuery('#cart-sidebar').css(
        {
            'left' : offset.left - 15  + 'px',
            'top' : offset.top + 'px',
            'padding-top' : height + 'px'
        });

        jQuery('#cart-sidebar-link').mouseover(function()
        {
            jQuery('#cart-sidebar').removeClass('hide');

            jQuery('#cart-sidebar').mouseleave(function()
            {
                jQuery('#cart-sidebar').addClass('hide');
            });
        });

        height = offset = null;
    }
}

