﻿jQuery(document).ready(function ($) {
    if ($('#slideshow img').length <= 1) {
        $('#slideshow img').css('display', 'block'); 
    }
    else {

        $('#slideshow').cycle({
            timeout: 2000
        });
    }
});

jQuery(window).load(function () {

    //For 3 columns  template calculate width of box to fit place left from image
    var parent = $(".three-column-content");
    if (parent.length != 0) {
        if ($('#image-slideshow img').length <= 1) {
            $('#image-slideshow img').css('display', 'block');
        }
        else {
            $('#image-slideshow').cycle({
                delay: 1000,
                timeout: 2000
            });
        }

        var imageBox = $(".three-column-content .image-box").first();
        if (imageBox.length != 0) {
            var image = imageBox.find("img");
            if (image.length != 0) {
                imageBox.css("width", image.width());
                var firstChild = $('.three-column-content  .free-text-area-box:first-child').first();
                var secondChild = $('.three-column-content  .free-text-area-box:nth-child(2)').first();
                var diff1 = firstChild.outerWidth(true) - firstChild.width();
                var diff2 = secondChild.outerWidth(true) - secondChild.width();
                var childWidth = (parent.width() - imageBox.outerWidth(true) - diff1 - diff2) / 2;
                firstChild.css("width", childWidth);
                secondChild.css("width", childWidth);
                childHeight = Math.max(firstChild.height(), secondChild.height());
                firstChild.css("height", childHeight);
                secondChild.css("height", childHeight);
            }
        }

        //For 3 columns  child items
        var imageBoxMaxWidth = 0;
        $.each($(".three-column-content .box-wrapper .image-box"), function (index, value) {
            var image = $(value).find("img")
            if (image.length != 0) {
                imageBoxMaxWidth = Math.max(imageBoxMaxWidth, image.width());
            }
        });

        $.each($(".three-column-content .box-wrapper"), function (index, value) {
            var image = $(value).find("img")
            if (image.length != 0) {
                var imageBox = $(value).find(".image-box");

                imageBox.css("width", imageBoxMaxWidth);
                var firstChild = $(value).find(".left-box");
                var secondChild = $(value).find(".right-box");
                var diff1 = firstChild.outerWidth(true) - firstChild.width();
                var diff2 = secondChild.outerWidth(true) - secondChild.width();
                var childWidth = ($(value).width() - imageBox.outerWidth(true) - diff1 - diff2) / 2;
                firstChild.css("width", childWidth);
                secondChild.css("width", childWidth);
                childHeight = Math.max(firstChild.height(), secondChild.height());
                firstChild.css("height", childHeight);
                secondChild.css("height", childHeight);
            }
        });

    }
});
