﻿window.addEvent('domready', function () {

    if ($('callbackGlobal')) {
        GlobalForm();
    }

    if (location.pathname.toLowerCase().indexOf("/training") == 0) {

        $('phoneNumber').set('text', 'Poole Office : +44 (0)1202 744582');
    }
    
    var SelectedWidgetTab = 0;
    var url = document.location.toString().split("/");

    // Setup Current link on top menu
    // alert(location.pathname);

    var SelectedTopMenuLink = 0;

    if (location.pathname.toLowerCase().indexOf("/security") == 0) {
        SelectedTopMenuLink = 1;
    }

    if (location.pathname.toLowerCase().indexOf("/training") == 0) {
        SelectedTopMenuLink = 2;
    }

    if (location.pathname.toLowerCase().indexOf("/ethos") == 0) {
        SelectedTopMenuLink = 3;
    }

    if (location.pathname.toLowerCase().indexOf("/partners") == 0) {
        SelectedTopMenuLink = 4;
    }


    if (location.pathname.toLowerCase().indexOf("/contactus") == 0) {
        SelectedTopMenuLink = 5;
    }

    if (location.pathname.toLowerCase().indexOf("/blog") == 0) {
        SelectedTopMenuLink = -1;
    }

    if ($$(".TopMenuLink").length < 5) {
        SelectedTopMenuLink = 0;
    }

    $$(".TopMenuLink").each(function (e, i, a) {
        if (i == SelectedTopMenuLink) {
            e.addClass("current");
        }
    });

    if ($('accordion')) {
        $$(".WidgetElementContent").each(function (e, i, a) {
            // Get link url
            var linkURL = $$('.AccordianLink')[i].getProperty("href").split("/");

            // alert(url[4] + " - " + linkURL[2] + " - " + url[5] + " - " + linkURL[3]);

            if (url[4].toLowerCase() == linkURL[2].toLowerCase() && url[5].toLowerCase() == linkURL[3].toLowerCase()) {
                SelectedWidgetTab = i;
            }
        });

        var WidgetElementAccordion = new Fx.Accordion($$(".titleText"), $$(".WidgetElement"), {
            display: -1,
            duration: 300,
            trigger: "mouseenter"
        });

        WidgetElementAccordion.addEvent("active", function (e, t) {
            //change style for non selected iktems 
            $$(".titleText").each(function (e2, i2, a2) {
                e2.setStyle("background", "#666666");
            });

            //change style for selected elemen t 
            e.setStyle("background", "#9C1F31");
        });

        $$(".titleText")[SelectedWidgetTab].fireEvent("mouseenter");

        $$(".titleText").each(function (e, i, a) {
            e.addEvent("active", function () {

            });
        });

        $("accordion").addEvent("mouseleave", function() {
            $$(".titleText")[SelectedWidgetTab].fireEvent("mouseenter");
        });
    }

    //    $$(".WidgetElementContent").each(function (e, i, a) {
    //        // Get link url
    //        var linkURL = $$('.AccordianLink')[i].getProperty("href").split("/");

    //        //alert(url[4] + " - " + linkURL[2] + " - " + url[5] + " - " + linkURL[3]);

    //        if (url[4].toLowerCase() == linkURL[2].toLowerCase() && url[5].toLowerCase() == linkURL[3].toLowerCase()) {
    //            SelectedWidgetTab = i;
    //        }
    //    });
    //    var accordHeight = 0;

    //    $$(".WidgetElementContent").each(function (e, i, a) {
    //        e.store("OriginalHeight", "175px");

    //        if (SelectedWidgetTab != i) {
    //            e.morph({ "height": "40px" });
    //            e.getFirst(".WidgetElementTitle").morph({ "height": "40px" });
    //            e.getFirst(".titleText").morph({ "height": "20px" });
    //            accordHeight += 40;
    //        }
    //        else {
    //            e.morph({ "height": e.retrieve("OriginalHeight") });
    //            e.getFirst(".WidgetElementTitle").morph({ "height": "40px", "background-color": "#9c1f31" });
    //            e.getFirst(".titleText").morph({ "height": "40px" });
    //            accordHeight += 175;
    //        }

    //        e.addEvent("mouseenter", function () {
    //            $$(".WidgetElementContent").each(function (e2, i2, a2) {
    //                if (i != i2) {
    //                    e2.morph({ "height": "40px" });
    //                    e2.getFirst(".WidgetElementTitle").morph({ "height": "40px", "background-color": "#494949" });
    //                    e2.getFirst(".titleText").morph({ "height": "20px" });
    //                }
    //                else {
    //                    e2.morph({ "height": e2.retrieve("OriginalHeight") });
    //                    e2.getFirst(".WidgetElementTitle").morph({ "height": "40px", "background-color": "#9c1f31" });
    //                    e2.getFirst(".titleText").morph({ "height": "40px" });
    //                }
    //            });
    //        });
    //    });

    //    if ($('accordion')) {

    //        $("accordion").setStyle("height", accordHeight);
    //    }

    var array = $$('.blogPost');

    array.each(function (el, i, a) {

        el.addEvents({ 'mouseenter': function () {

            el.morph({ "background-color": "#9c1f31" });
        },

            'mouseleave': function () {

                el.morph({ "background-color": "#b2b2b2" });

            }
        });

    });

    var links = $$('.SecurityConsultancy', '.ExecutiveProtection', '.MaritimeSecurity', '.SurveillanceDueDilligence');
    links.each(function (el, i, a) {
        el.addEvent("mouseenter", function () {

            el.morph({ opacity: 0.1 });

            links.each(function (el2, i2, a2) {
                if (i2 != i) {
                    el2.morph({ opacity: 1 });
                }

            });
        });
    });
});

// Call me Back Form Starts here
function GlobalForm() {
    $("YourMessage").addEvent("focus", function () {
        if ($("YourMessage").getProperty("value") == "") {
            $("YourMessage").setProperty("value", "");
            $$(".messageValidateGlobal").morph({ left: "-79px", "background-color": "#9C1F31" });

        }
    });
    $$('.messageValidateGlobal').addEvent("click", function () {

        $$(".messageValidateGlobal").morph({ left: "-79px" });
        $("YourMessage").focus();
    });

    $("YourMessage").addEvent("blur", function () {
        if ($("YourMessage").getProperty("value") == "") {
            $("YourMessage").setProperty("value", "");
            $$(".messageValidateGlobal").morph({ left: "8px", "background-color": "#000000" });
        }
    });

    $$('.addressValidate').addEvent("click", function () {

        $$(".addressValidate").morph({ left: "-44px" });
        $("YourMessage").focus();
    });

    $("telephone").addEvent("focus", function () {
        if ($("telephone").getProperty("value") == "") {
            $("telephone").setProperty("value", "");
            $$(".phoneValidateGlobal").morph({ left: "-44px", "background-color": "#9C1F31" });

        }
    });

    $("telephone").addEvent("blur", function () {
        if ($("telephone").getProperty("value") == "") {
            $("telephone").setProperty("value", "");
            $$(".phoneValidateGlobal").morph({ left: "8px", "background-color": "#000000" });
        }
    });

    $$('.phoneValidateGlobal').addEvent("click", function () {

        $$(".phoneValidateGlobal").morph({ left: "-44px" });
        $("telephone").focus();
    });


    $("emailAddress").addEvent("focus", function () {
        if ($("emailAddress").getProperty("value") == "") {
            $("emailAddress").setProperty("value", "");
            $$(".emailValidateGlobal").morph({ left: "-44px", "background-color": "#9C1F31" });

        }
    });

    $("emailAddress").addEvent("blur", function () {
        if ($("emailAddress").getProperty("value") == "") {
            $("emailAddress").setProperty("value", "");
            $$(".emailValidateGlobal").morph({ left: "8px", "background-color": "#000000" });
        }
    });

    $$('.emailValidateGlobal').addEvent("click", function () {

        $$(".emailValidateGlobal").morph({ left: "-44px" });
        $("emailAddress").focus();
    });

    $("fullName").addEvent("focus", function () {
        if ($("fullName").getProperty("value") == "") {
            $("fullName").setProperty("value", "");
            $$(".nameValidateGlobal").morph({ left: "-44px", "background-color": "#9C1F31" });
        }
    });

    $("fullName").addEvent("blur", function () {
        if ($("fullName").getProperty("value") == "") {
            $("fullName").setProperty("value", "");
            $$(".nameValidateGlobal").morph({ left: "8px", "background-color": "#000000" });
        }
    });
    $$('.nameValidateGlobal').addEvent("click", function () {

        $$(".nameValidateGlobal").morph({ left: "-44px" });
        $("fullName").focus();
    });

    $('contactSubmit').addEvent("click", function () {
        return ValidateForm();
    });
}

function ValidateForm() {
    var msg = "";
    //alert(msg);

    if ($("YourMessage").getProperty("value") == "" || $("telephone").getProperty("value") == "") {

        $("YourMessage").focus();
        $("YourMessage").morph({ "background-color": ["#9c1f31", "#ffffff"] });
        msg = msg + "You must enter a phone number\n"
    }

    if ($("telephone").getProperty("value") == "" || $("telephone").getProperty("value") == "") {

        $("telephone").focus();
        $("telephone").morph({ "background-color": ["#9c1f31", "#ffffff"] });
        msg = msg + "You must enter a phone number\n"
    }

    if ($("emailAddress").getProperty("value") == "" || $("emailAddress").getProperty("value") == "") {

        $("emailAddress").focus();
        $("emailAddress").morph({ "background-color": ["#9c1f31", "#ffffff"] });
        msg = msg + "You must enter an email address\n"
    }

    if ($("fullName").getProperty("value") == "" || $("fullName").getProperty("value") == "") {

        $("fullName").focus();
        $("fullName").morph({ "background-color": ["#9c1f31", "#ffffff"] });
        msg = msg + "You must enter your name\n"
    }

    if (msg.length > 0) {
        //alert(msg);
        return false;
    }

    return true;
}
