﻿var actionInProgress = false;

$(function() {
    $(".coach .buttons a.more").click(function() {
        if (!actionInProgress) {
            actionInProgress = true;
            var hasSelection = $(this).parents('.coach').children('.buttons').hasClass('hasSelection');
            var input = $(this).parents('.readMoreTab').children('input[type="hidden"]');
            var _this = this;
            var nodeToOpen = input.val();
            if (hasSelection) {
                // 1. colaps details panel
                $(this).parents('.coach').find('.details.opened').slideUp('slow', function() {
                    $(this).removeClass('opened');
                    // 2. Deselect prev tab
                    var prevTab = $(this).parents('.coach').find('.readMoreTab.selected');
                    prevTab.find('a.more').show();
                    prevTab.find('a.less').hide();
                    prevTab.removeClass('selected');
                    // 3. Select new tab
                    $(_this).hide();
                    $(_this).next('a.less').show();
                    $(_this).parents('.readMoreTab').addClass('selected');
                    // 4. uncolaps selected details
                    $(this).parents(".coach").find('.' + input.val()).slideDown("slow", function() {
                        actionInProgress = false;
                        $(this).addClass('opened');
                    });
                });
            } else {
                $(this).parents('.coach').find('.buttons').addClass('hasSelection');
                $(this).hide();
                $(this).next("a.less").show();
                $(this).parents('.readMoreTab').addClass('selected');
                $(this).parents('.coach').find('.' + nodeToOpen).slideDown("slow", function() {
                    actionInProgress = false;
                    $(this).addClass('opened');
                });
            }
        }
        return false;
    });

    $(".coach .buttons a.less").click(function() {
        if (!actionInProgress) {
            actionInProgress = true;
            var _this = this;
            $(this).parents('.coach').find('.details.opened').slideUp('slow', function() {
                $(_this).hide();
                $(_this).prev('a.more').show();
                $(_this).parents('.readMoreTab').removeClass('selected');
                $(this).parents('.coach').children('.buttons').removeClass('hasSelection');
                actionInProgress = false;
            });
        }
        return false;
    });

    $(".coach .details .gallery img").click(function() {
        var dialog = $("<div class='galleryPopup'><img id='mainImage' /><div id='thumbImageContainer'></div></div>");
        $(this).parent(".gallery").children("img").clone().prependTo(dialog.children('#thumbImageContainer')).click(function() {
            dialog.children("#mainImage").attr("src", $(this).attr("main"));
        });

        dialog.children("#mainImage").attr("src", $(this).attr("main"));
        //var closeWindowMsg = 'Luk vindue'; *XSLT
        dialog.dialog({
            bgiframe: true,
            height: 490,
            width: 610,
            modal: true,
            title: closeWindowMsg,
            dialogClass: 'modalDialog',
            resizable: false
        });
    }
    );
});
