var effects_speed = 540, images = ['img/b-on.png', 'img/b-off.png'];

$(document).ready(function () {
    $('.images a img').each(function () {
        images.push(this.src);
    });

    $('.project > .content').each(function () {
        $(this).find('*').each(function (i) {
            $(this).click(function () {
                showImage(i, $(this).parent('.content'));
            });
        });
    });

    bindToggle('.section');
    bindToggle('.subsection');

    $('.project > .title').click(function () {
        $('.ititle').fadeOut(effects_speed);
        $parent = $(this).parents('.project');
        $content = getContent($parent);
        hideSiblings($parent);

        $parent.find('img').each(function () {
            if (this.src == '') {
                this.src = $(this).attr('alt');
            }
        });

        if (vis(getContent($parent))) {
            contentHide($parent);
        } else {
            $content.children('*').css({display: 'none'}).eq(0).css({display: 'block'});

            $('.buttons').html('').css('width', 0);

            for (i = 0; i < $content.find('img').length; i++) {
                $('.buttons').append('<img src="img/b-off.png" id="'+i+'b"/>').css('width', $('.buttons').width() + 13 + 'px');
            }

            $('.buttons > img').each(function (num) {
                $(this).click(function () {
                    showImage(num, $content, true);
                });
            });

            $('.buttons img:first').attr('src', 'img/b-on.png');

            contentShow($parent, function () {
                b_top = $(this).offset().top + $content.height() - $('.buttons').height() - 17;
                b_left = 495 - Math.round($('.buttons').width() / 2);
                $('.buttons').css({top: b_top + 'px', left: b_left + 'px'}).show();
                window.scrollTo(0, parseInt($parent.offset().top));

                if ($parent.get(0).id != '') {
                    r = new RegExp('(#.*)$');
                    l = window.location.toString();
                    l = l.replace(r, '') + '#' + $parent.attr('id');
                    window.location = l;
                }
            });
        }
    });

    if (location.hash != '' && $(location.hash).length > 0) {
        $p  = $(location.hash);
        $ss = $p.parents('.subsection');
        $s  = $p.parents('.section');

        $s.contents('.title').eq(0).click();
        $ss.contents('.title').eq(0).click();
        $p.contents('.title').eq(0).click();
    }
});

function bindToggle(parent_selector) {
    $(parent_selector + ' > .title').click(function () {
        $('.ititle').fadeOut(effects_speed);
        $parent = $(this).parents(parent_selector);
        hideSiblings($parent);

        if (vis(getContent($parent))) {
            contentHide($parent, function () {
                $(parent_selector + ' .content').hide();
                $(parent_selector + ' .title').removeClass('active');
            });
        } else {
            contentShow($parent);
        }
    });
}

function showImage(key, $content, go_to_num) {
    $list = $content.children('*');
    $list.fadeOut(effects_speed);
    $('.ititle').fadeOut(effects_speed);

    if (typeof(go_to_num) == 'undefined') {
        key = (key == $list.length - 1) ? 0 : (key + 1);
    }

    $list.eq(key).fadeIn(effects_speed, function () {
        $i = $(this).find('img');
        if ($i.length > 0 && $i.get(0).title != '') {
            $('.ititle')
                .html($i.get(0).title)
                .css({left: $i.offset().left + 'px', top: $i.offset().top + 'px'})
                .show();
        }
    });

    $('.buttons > img').attr('src', 'img/b-off.png').eq(key).attr('src', 'img/b-on.png');
}

function getContent(element) {
    return $(element).find('.content').eq(0);
}

function vis(element) {
    return $(element).css('display') != 'none';
}

function contentHide(element, callback) {
    $('.buttons').hide();
    $(element).children('.title').removeClass('active');
    getContent(element).slideUp(effects_speed, callback);
}

function contentShow(element, callback) {
    $(element).children('.title').addClass('active');

    if (typeof(callback) == 'undefined') {
        callback = function () {
            window.scrollTo(0, parseInt($(this).prev().offset().top));
        };
    }

    getContent(element).slideDown(effects_speed, callback);
}

function hideSiblings(element) {
    $(element).siblings().each(function () {
        $(this).children('.title').removeClass('active');
        contentHide(this);
    });
}
