﻿window.addEvent('domready', function () {

    signup();
    HighlightCurrentMenuLink();
    if ($('contactForm')) {
        contactForm();
    }
    if ($('gallery-wrapper')) {
        ImageGallery();
        NextPrevGallery();
    }
    $$('#menu li', '#menu2 li').each(function (el, i, a) {
        el.addEvents({

            'mouseenter': function (e) {
                el.set('morph', { duration: '500', transition: 'back:out' });
                el.morph({ 'padding-left': '10px' });
                el.setStyle('font-weight', 'bold');

            },
            'mouseleave': function (e) {
                el.morph({ 'padding-left': '0px', 'font-weight': 'normal' });
            }
        });
    });


    $$('.home-boxes').each(function (el, i, a) {
        el.addEvents({

            'mouseenter': function (e) {
                el.set('morph', { duration: '500', transition: 'back:out' });
                el.morph({ 'border-color': '#569eb7' });
                el.getFirst('h2').morph({ 'background-color': '#569eb7' });
                el.getFirst('h3').morph({ 'background-color': '#569eb7', 'color': '#ffffff' });
                el.getFirst('.brown').morph({ 'opacity': 0 });


            },
            'mouseleave': function (e) {
                el.morph({ 'border-color': '#91792E' });
                el.getFirst('h2').morph({ 'background-color': '#91792E' });
                el.getFirst('h3').morph({ 'background-color': '#ffffff', 'color': '#4D4D4D' });
                el.getFirst('.brown').morph({ 'opacity': 1 });
            }
        });
    });



    $('AdviseList').setStyle('display', 'none');

    $('Advise').addEvent('click', function () {

        $('AdviseList').setStyle('display', 'block');
    });

    $('AdviseList').getFirst("ul").getChildren().each(function (e, i, a) {
        e.addEvent("click", function () {

            $('Advise').setProperty('value', e.get('text'));
            $("AdviseList").fireEvent("click");
            $('AdviseList').setStyle('display', 'none');
            //  $$('#contactDrop').getParent().getFirst().morph({ "opacity": 1, "left": "-119px" });

        })
    });



});


function signup() {

    //    $$('.overtext').each(function (e, i, a) {
    //        new OverText(e);
    //    });
    var val1 = new Form.Validator($("callbackForm"), {
        stopOnFailure: true,
        onElementPass: function (e, arr) {
            if (e.hasClass("overtext") == true) {


                e.morph({ "background-color": "#ffffff", "color": "#625146" });
            }
        },
        onElementFail: function (e, arr) {

            e.morph({ "background-color": "#625146", "color": "#ffffff" });

        },
        onFormValidate: function (passed, form, event) {

            return passed;
        }
    });
}

function contactForm() {

    // $$('#main p','#main h2').setStyle('margin-left', '10px');
    $('callback').setStyle('opacity', 0);
    $$('.errorMsg').each(function (e, i, a) {
        e.setStyle('opacity', 0);
    });
    var val2 = new Form.Validator($("contactForm"), {
        stopOnFailure: true,
        onElementPass: function (e, arr) {
            if (e.hasClass("overtext") == true) {


                e.getParent().getFirst('.errorMsg').morph({ 'opacity': 0 });
            }
        },
        onElementFail: function (e, arr) {

            e.getParent().getFirst('.errorMsg').morph({ 'opacity': 1 });

        },
        onFormValidate: function (passed, form, event) {

            return passed;
        }
    });
}


function ImageGallery() {

    //    $$('#main h2', '#main h3','#main p').setStyles({
    //    margin: '10px 0px 0px 10px',
    //    width: 600
    //});    
    $('transBg').setStyle('opacity', 0.7);

    $$('#viewPhotos ul').set('morph', { duration: 400, transition: 'expo:out' });

    $('viewPhotos').addEvent('click', function () {
        $$('#viewPhotos ul').morph({ 'bottom': '20px' });
    });

    $('viewPhotos').addEvent('mouseleave', function () {
        $$('#viewPhotos ul').morph({ 'bottom': '-129px' });
    });



    var container = $('image-container');
    var thumbs = $$("#viewPhotos ul li");

    container.getElements('img').setStyle('opacity', 0);

    container.getFirst().setStyle('opacity', 1);
    //alert(thumbs);
    thumbs.each(function (e, i, a) {
        e.addEvent("click", function () {
            $('image-container').getElements('img').each(function (e2, i2, a2) {


                if (i == i2) {
                    e2.morph({ 'opacity': '1' });

                    thumbs.each(function (el2, i2, a2) {
                        if (i == i2) {
                            el2.getFirst().morph({ 'opacity': 0.5 });
                        }
                        else {
                            el2.getFirst().morph({ 'opacity': 1 });
                        }
                    });
                }
                else {
                    e2.morph({ 'opacity': '0' });
                }
            });
        });
    });
}

function HighlightCurrentMenuLink() {
    var MenuItems = $$(".menuLinks");
    var urlArray = document.location.toString().split("/");
    //alert(urlArray);
    MenuItems.each(function (e, i, a) {
        // alert(e.getProperty("href"));
        var urlArrayLink = e.getProperty("href").split("/");
        if (urlArray.length > 2) {
            // alert(urlArrayLink[3].toLowerCase());
            //alert(urlArray[5].toLowerCase());
            if (urlArrayLink[1].toLowerCase() == urlArray[3].toLowerCase()) {
                // Highlight e (the li) as link)
                e.addClass('menuCurrent');
            }
        }
    });
}


function NextPrevGallery() {

    var ElementWidth = 666;
    var ElementPosition = 0;
    $("diagram-wrapper-slider").set('morph', { duration: '700', transition: 'expo:out' });

    $('nextImage').addEvent("click", function () {
        ElementPosition++;
        // Hide Button If Needed
        if (ElementPosition == $$(".diagram-wrapper-item").length - 1) {
            $('nextImage').setStyle("opacity", 0);
        }
        // Show Back Button If Needed
        if (ElementPosition > 0) {
            $('prevImage').setStyle("opacity", 1);
        }
        $('diagram-wrapper-slider').morph({ "left": -ElementWidth * ElementPosition });
    });
    $('prevImage').setStyle("opacity", 0);
    $('prevImage').addEvent("click", function () {
        ElementPosition--;
        // Hide Button If Needed
        if (ElementPosition == 0) {
            $('prevImage').setStyle("opacity", 0);
        }
        // Show Back Button If Needed
        if (ElementPosition < $$(".diagram-wrapper-item").length - 1) {
            $('nextImage').setStyle("opacity", 1);
        }
        $('diagram-wrapper-slider').morph({ "left": -ElementWidth * ElementPosition });
    });



    $('beforeAfter').addEvent('click', function () {

        $('diagram-wrapper').morph({ 'opacity': 0 });
    });

    if (ElementPosition == $$(".diagram-wrapper-item").length - 1) {
        $('nextPrev').setStyle("opacity", 0);
    }

}
