window.addEvent('domready', function()
{
    // Hide sub menu when empty

    if ($("main-menu-sub").getElements("li").length == 0)
    {
        $("main-menu-sub").setStyle("display", "none");
    }


    // Show h1 text when homepage open in design mode

    if ($$(".homepage").length > 0 && document.isInDesignMode())
    {
        $("logo-txt").setStyle("display", "block");
    }


    // Expend home page box in design mode to set links

    if ($$(".main-center-item").length > 0 && document.isInDesignMode())
    {
        $$(".main-center-item").setStyle("height", "130px");
        $$(".main-center-item").getParent().setStyle("height", "130px");
    }


    // Apply link to main menu boxes

    if (!document.isInDesignMode())
    {
        var currentLink = document.location.href;
        $$(".main-menu-item").each(function(item, index)
        {
            if (item.getElement(".Synergee-Web-Page-Component-Text a"))
            {
                var linkUrl = item.getElement(".Synergee-Web-Page-Component-Text a");
                var link = new Element('a', {
                    'href': linkUrl.getAttribute("href")
                });
                var topLink = linkUrl.getAttribute("href");
                topLink = topLink.slice(7, topLink.lastIndexOf('.html'))

                if (currentLink.indexOf(topLink) != -1)
                {
                    item.addClass('main-menu-item-hover')
                }

                link.injectTop(item);
                item.getElement(".Synergee-Web-Page-Component-Image").injectInside(link);
                item.getElement(".Synergee-Web-Page-Component-Text").injectInside(link);
            }
        });
    }

    // Apply link to logo boxe

    if ($("logo-txt").getElement("a"))
    {
        var linkUrl = $("logo-txt").getElement("a");
        var link = new Element('a', {
            'href': linkUrl.getAttribute("href")
        });
        link.injectTop($("logo"));
        $("logo-img").injectInside(link);
    }


    // Hide / show classes in design mode or not

    if (document.isInDesignMode())
    {
        $$(".inDesignMode").removeClass("inDesignMode");
        $$(".notInDesignMode").setStyle("display", "none");
    }

    // Apply required level's styles in program pages

    if ($$(".program").length > 0)
    {
        if ($("main-left-level"))
        {
            var minLevel = parseInt($("main-left-level-min").getElement(".Synergee-Web-Page-Component-Text-Container").innerHTML);
            var maxLevel = parseInt($("main-left-level-max").getElement(".Synergee-Web-Page-Component-Text-Container").innerHTML);

            if (101 <= minLevel && 112 >= minLevel && 101 <= maxLevel && 112 >= maxLevel && minLevel <= maxLevel)
            {
                $("main-left-level-list").getElements("li").each(function(item, index)
                {
                    var itemLevel = parseInt(item.innerHTML);
                    if (itemLevel >= minLevel && itemLevel <= maxLevel)
                    {
                        item.addClass("leveled");
                        if (itemLevel == minLevel || itemLevel == maxLevel)
                            item.addClass("arrow");
                    }
                });
            }
        }

        // CSS Schedule table from program pages
        if ($$(".program #main-right table").length)
        {
            $$(".program #main-right table")[0].removeProperty("width");
            $$(".program #main-right table")[0].removeProperty("height");
            $$(".program #main-right table")[0].removeProperty("align");

            $$(".program #main-right table")[0].setProperties({
                cellspacing: '0',
                cellpadding: '0',
                border: '0'
            });
        }

        $$(".program #main-right table tr td br").each(function(item, index)
        {
            item.remove();
        })

        $$(".program #main-right table tr").each(function(item, index)
        {
            if (item.getElements("h4").length)
            {
                item.getElements("td").addClass("title");
            }
            else
            {
                item.getElements("td")[0].addClass("left");
                item.getElements("td")[1].addClass("right");
                if (1 == index % 2)
                {
                    item.addClass("colored");
                }
            }

            item.getElements("td").each(function(tItem, tIndex)
            {
                tItem.removeProperty("height");
                tItem.removeProperty("width");
                tItem.removeProperty("valign");
            });
        });

        // CSS Prices table from program pages

        $$(".program #main-right-prices table tr").each(function(item, index)
        {
            if (item.getElements("h4").length)
            {
                item.getElements("td")[0].addClass("titleLeft");
                item.getElements("td")[1].addClass("titleRight");
            }
            else
            {
                item.getElements("td")[0].addClass("left");
                item.getElements("td")[1].addClass("right");
                if (1 == index % 2)
                {
                    item.addClass("colored");
                }
            }
        });
        if (!document.isInDesignMode())
        {
            var contentBottom = $("main-center-bottom").getElement(".Synergee-Web-Page-Component-Text-Container").innerHTML.clean();
            if ('' == contentBottom)
            {
                $("main-center-bottom").setStyle("display", "none");
            }
        }
    }

    // Add description popup program list

    if ($("main-left-courses"))
    {
        $$(".Synergee-Web-Page-Component-CourseTypesList-Courses-List-Item").each(function(item, index)
        {
            item.hoverArea = new Element('div', {
                'class': 'courseHoverArea'
            });
            item.hoverArea.injectInside(item);
            item.hoverArea.addEvent("mouseenter", function(event)
            {
                this.popup = new Element('div', {
                    'class': 'courseTypePopup'
                });
                this.popup.setHTML("<div class='courseTypePopup-content'>" + item.getElement("h4 a").getAttribute("alt") + "</div>");
                this.popup.injectInside(item);
            }.bind(item));
            item.hoverArea.addEvent("mouseleave", function(event)
            {
                this.popup.remove();
            }.bind(item));
        });
    }

    // Hide news from cityPage if enpty

    if ($("latest-news") && !document.isInDesignMode())
    {
        var contentNews = $("latest-news").getElement(".Synergee-Web-Page-Component-Text-Container").innerHTML.clean();
        if ('' == contentNews)
        {
            $("latest-news").setStyle("display", "none");
        }
    }

    // Hide more boxe from cityPage if enpty

    if ($("main-right-more") && !document.isInDesignMode())
    {
        var contentMore = $("main-right-more").getElement(".Synergee-Web-Page-Component-Text-Container").innerHTML.clean();
        if ('' == contentMore)
        {
            $("main-right-more").setStyle("display", "none");
        }
    }
});


window.addEvent('load', function()
{
    if ($$(".program #main-right table tr").length)
    {
        $$(".program #main-right table tr").each(function(item, index)
        {
            var aHeight = 0;
            item.getElements("td").each(function(tItem, tIndex)
            {
                tItem.removeAttribute("align");
            });
            item.getElements("a").each(function(aItem, aIndex)
            {
                aHeight = aItem.offsetHeight > aHeight ? aItem.offsetHeight : aHeight;
                aItem.addEvent("mouseenter", function(event)
                {
                    aItem.delay = (function()
                    {
                        aItem.iframe = new Element('iframe', {
                            'class': 'internalWindow onLoad',
                            'src': aItem.getAttribute("href"),
                            'width': '400',
                            'height': '160'
                        });
                        aItem.iframe.addEvent('load', function()
                        {
                            aItem.iframe.removeClass('onLoad');
                        });
                        aItem.iframe.injectInside(item.getElements("td")[0]);
                    }).delay(1000);
                });
                aItem.addEvent("mouseleave", function(event)
                {
                    $clear(aItem.delay);
                    if(aItem.iframe)
                    {
                        aItem.iframe.remove();
                    }
                });
            });
            item.getElements("a").setStyle("height", aHeight + "px");
            item.getElements("a").addEvent("click", function(event)
            {
                var evt = new Event(event);
                evt.stop();
            });

        });
    }


});
