(function () {

    "use strict";

    function headerClick() {
        $("a.header_link").live('click', function () {
            var paragraph = $(this).next("div");
            if (!($(paragraph).is(':visible'))) {
                $('div.content').each(function () {
                    if ($(paragraph) !== $(this)) {
                        $(this).slideUp('slow');
                    }
                });
            }
            $(paragraph).slideToggle('slow');
        });
    }

    function fancybox(images) {
        $.fancybox(images, {
            'padding': 0,
            'transitionIn': 'none',
            'transitionOut': 'none',
            'type': 'image',
            'changeFade': 0,
            'opacity': true,
            'overlayColor': '#000',
            'overlayOpacity': 0.8,
            'titleFormat': function (title, currentArray, currentIndex) {
                var titleString = '<span id="fancybox-title-over">Image ' + (currentIndex + 1).toString() + ' / ' + currentArray.length.toString();
                if (title.length) {
                    titleString += ' &nbsp; ' + title;
                }
                titleString += '</span>';

                return titleString;
            }
        });
    }

    function galleryLandscapes() {
        $("#gallery_landscapes").click(function () {
            fancybox(['images/gallery/landscapes/1.jpg', 'images/gallery/landscapes/2.jpg', 'images/gallery/landscapes/3.jpg', 'images/gallery/landscapes/4.jpg', 'images/gallery/landscapes/5.jpg', 'images/gallery/landscapes/6.jpg', 'images/gallery/landscapes/7.jpg', 'images/gallery/landscapes/8.jpg', 'images/gallery/landscapes/9.jpg', 'images/gallery/landscapes/10.jpg']);
        });
    }

    function galleryConcept() {
        $("#gallery_concept").click(function () {
            fancybox(['images/gallery/concept/1.jpg', 'images/gallery/concept/2.jpg', 'images/gallery/concept/3.jpg', 'images/gallery/concept/4.jpg', 'images/gallery/concept/5.jpg', 'images/gallery/concept/6.jpg', 'images/gallery/concept/7.jpg', 'images/gallery/concept/8.jpg', 'images/gallery/concept/9.jpg', 'images/gallery/concept/10.jpg']);
        });
    }

    function galleryPeople() {
        $("#gallery_people").click(function () {
            fancybox(['images/gallery/people/1.jpg', 'images/gallery/people/2.jpg', 'images/gallery/people/3.jpg', 'images/gallery/people/4.jpg', 'images/gallery/people/5.jpg', 'images/gallery/people/6.jpg', 'images/gallery/people/7.jpg', 'images/gallery/people/8.jpg', 'images/gallery/people/9.jpg', 'images/gallery/people/10.jpg']);
        });
    }

    function randOrder() {
        return (Math.round(Math.random()) - 0.5);
    }

    function randColor() {
        var colors = ["#acbd0d", "#009de2", "#d8044d", "#ed9f01"];
        return colors[Math.floor(Math.random() * colors.length)];
    }

    function randFontSize() {
        return Math.floor(Math.random() * (22 - 13 + 1)) + 13;
    }

    function prepareCloud() {

        var $this, $childrens, childCount, i, indices;
        $this = $("#skills_cloud > p");

        if ($this.length) {
            $childrens = $this.children();
            childCount = $childrens.length;

            if (childCount > 1) {
                $childrens.remove();

                indices = [];

                for (i = 0; i < childCount; i++) {
                    indices[indices.length] = i;
                }

                indices = indices.sort(randOrder);
                $.each(indices, function (k) {
                    var $children = $childrens.eq(k);
                    $children.css("color", randColor).css('font-size', randFontSize).css("padding", "3px").append(" ");
                    $this.append($children);

                });
            }
        }
    }

    $(document).ready(function () {
        headerClick();
        galleryPeople();
        galleryLandscapes();
        galleryConcept();
        prepareCloud();
    });

}());

