﻿var currentFile = null;
var formLightbox = null;

var Site = {
    flag: 1,
    init: function() {
        Site._initLightbox();
        Site._loadFormLightbox();
        Site._initRegFormLinks();
        if ($("main-menu")) Site._initMainMenu();
    },
    _initMainMenu: function() {
        var menuItems = $("main-menu").getElements(".main-menu-link");
        var subItems = $("main-menu").getElements(".sub-main-menu");
        menuItems.each(function(item, index) {
            item.addEvent("mouseenter", function() {
                Site._hideAllSubMainMenuItems();
                subItems[index].setStyle("display", "block");
            });
        });
        subItems.each(function(item, index) {
            item.addEvent("mouseleave", function() {
                this.setStyle("display", "none");
            });
        });
    },
    _hideAllSubMainMenuItems: function() {
        var subItems = $("main-menu").getElements(".sub-main-menu");
        subItems.each(function(item, index) {
            item.setStyle("display", "none");
        });
    },
    _initLightbox: function() {
        if (typeof (Lightbox) != "undefined") Lightbox.setOptions({
            opacity: 0.0001,
            containerClass: "lightbox"
        });
    },
    _openRegistrationForm: function(dataFileUrl) {

        var x = Site._get_cookie("gotLead");
        if (!x) {
            var codeName = "ContactDetailsLead";
            currentFile = dataFileUrl;
            Mantis.FormGenerator.FormGeneratorService.GetFormSource(codeName, null, function(source) {
                var list = new Element('div').setHTML(source);
                $("lightbox-lead-form-div").empty();
                $("lightbox-lead-form-div").adopt(list);
                $("downloadButton").hide();
                formLightbox.show();
            });
        }
        else {
            alert("Refresh your page please");
        }
    },
    _loadFormLightbox: function() {
        if ($("lightbox-lead-form") && $("lightbox-close")) {
            formLightbox = new Lightbox($("lightbox-lead-form"), { opacity: 0.5, containerClass: '' });
            $("lightbox-close").removeEvent('click');
            $("lightbox-close").addEvent('click', function(e) {
                $("lightbox-lead-form-div").empty();
                formLightbox.hide();
            });
        }
    },
    _initRegFormLinks: function() {
        //show  lead form only if it's first time
        var x = Site._get_cookie("gotLead");
        if (!x) {
            var protectedDownloadLinks = $$("#page a.protected");
            protectedDownloadLinks.each(function(item) {
                item.target = "_self";
                var tempHtml = item.href;
                item.addEvent('click', function() {
                    Site._openRegistrationForm(tempHtml);
                });
                item.href = "#";

            });
        }

    },

    _get_cookie: function(cookie_name) {
        var results = document.cookie.match('(^|;) ?' + cookie_name + '=([^;]*)(;|$)');
        if (results)
            return (unescape(results[2]));
        else
            return null;
    }

};
$DL(Site.init);