if (typeof Luma == "undefined") {
    Luma = {};
}

Luma.$ = function(id) {
    return document.getElementById(id);
}

Luma.Commons = {};

Luma.Commons.onShowModalElement = function() {
    if (window.navigator.userAgent.toLowerCase().indexOf('msie 6.0') != -1) {
        $('select').css('visibility', 'hidden');
    }
}

Luma.Commons.onHideModalElement = function() {
    if (window.navigator.userAgent.toLowerCase().indexOf('msie 6.0') != -1) {
        $('select').css('visibility', '');
    }
}

Luma.NavigationMenuItem = function(mainElement, subMenu, active) {
    this.mainElement = mainElement;
    this.subMenu = subMenu;
    this.mainElement.self = this;
    this.subMenu.self = this;
    this.active = active;
    $(this.mainElement).mouseover(this.mouseOverListener);
    $(this.mainElement).mouseout(this.mouseOutListener);
    $(this.subMenu).mouseover(this.subMouseOverListener);
    $(this.subMenu).mouseout(this.subMouseOutListener);
}

Luma.NavigationMenuItem.prototype.mouseOverListener = function(event) {
    this.self.showSubmenu();
}
Luma.NavigationMenuItem.prototype.mouseOutListener = function(event) {
    var self = this.self;
    self.timeout = setTimeout(function() {
        self.hideSubmenu();
    }, 300);
}

Luma.NavigationMenuItem.prototype.subMouseOverListener = function(event) {
    this.self.showSubmenu();
    if (this.self.timeout) {
        clearTimeout(this.self.timeout);
    }
}
Luma.NavigationMenuItem.prototype.subMouseOutListener = function(event) {
    this.self.hideSubmenu();
}

Luma.NavigationMenuItem.prototype.showSubmenu = function() {
    $(this.subMenu).show();
    $(this.mainElement).children("a").addClass("selected").addClass('menu_over');

    Luma.Commons.onShowModalElement();

    if (this.subMenuHideTmt) {
        clearTimeout(this.subMenuHideTmt);
        this.subMenuHideTmt = null;
    }
}

Luma.NavigationMenuItem.prototype.hideSubmenu = function() {
    if (!this.active) {
        var self = this;
        this.subMenuHideTmt = setTimeout(function() {
            $(self.subMenu).hide();
            $(self.mainElement).children("a").each(function(index, a) {
                $(a).removeClass('menu_over');
                if (!($(a).is('.navSelected'))) {
                    $(a).removeClass("selected");
                }
            });

            Luma.Commons.onHideModalElement();
        }, 100);

    }
}

Luma.contactUsOptions = {};
Luma.jobsOptions = {};
Luma.profileSelectorOptions = {};

Luma.ProfileSelector = function(container, resultElement) {
    this.container = container;
    this.resultElement = resultElement;

    var self = this;

    this.firstComponent = "";
    this.secondComponent = "";
    this.thirdComponent = "";

    $(this.container).find('.first .profile_job').click(function(e) {
        var tt  = e.target.tagName.toLowerCase();
        if (tt == 'a') {
            e.preventDefault();
        }
        self.onFirstSelectorClick(this);
    });

    $(this.container).find('.profile_first_concern li').click(function(e) {
        var tt  = e.target.tagName.toLowerCase();
        if (tt == 'a') {
            e.preventDefault();
        }
        self.onSecondSelectorClick(this);
    });

    $(this.container).find('.profile_second_concern li').click(function(e) {
        var tt  = e.target.tagName.toLowerCase();
        if (tt == 'a') {
            e.preventDefault();
        }
        self.onThirdSelectorClick(this);
    });
}

Luma.ProfileSelector.prototype.forgetState = function() {
    $.cookie('luma.profileSelector.state', null, {
        path: Luma.profileSelectorOptions.path
    });
}

Luma.ProfileSelector.prototype.saveState = function(state) {
    $.cookie('luma.profileSelector.state', state, {
        path: Luma.profileSelectorOptions.path
    });
}

Luma.ProfileSelector.prototype.restoreState = function() {
    var state = $.cookie('luma.profileSelector.state');
    if (state == null || typeof state == undefined || items == "") {
        return;
    }

    var items = state.split('.');
    if (items.length == 3) {

        $(this.container).find('a').removeClass('selected');

        this.firstComponent = items[0];
        this.secondComponent = items[1];
        this.thirdComponent = items[2];

        this.loadRelatedText();
        var selectors = [
        'input[value=' + this.firstComponent + ']',
        'input[value=' + this.secondComponent + ']',
        'input[value=' + this.thirdComponent + ']'
        ];
        for(var i = 0; i < selectors.length; i++) {
            $(this.container).find(selectors[i]).each(function(index, el) {
                el.checked = true;
                $(el).parent().find('a').addClass('selected');
            });
        }
    }
}

Luma.ProfileSelector.prototype.onFirstSelectorClick = function(container) {
    $(container).parent().find('a').removeClass('selected');
    $(container).find('a').addClass('selected');
    this.firstComponent = $(container).find('a').attr("rel");
    $(container).find('input[type=radio]').each(function(index, el) {
        el.checked = true;
    });

    this.checkLoad();
}
    
Luma.ProfileSelector.prototype.onSecondSelectorClick = function(container) {
    $(container).parent().find('a').removeClass('selected');
    $(container).find('a').addClass('selected');
    this.secondComponent = $(container).find('a').attr("rel");
    $(container).find('input[type=radio]').each(function(index, el) {
        el.checked = true;
    });

    this.checkLoad();
}

Luma.ProfileSelector.prototype.onThirdSelectorClick = function(container) {
    $(container).parent().find('a').removeClass('selected');
    $(container).find('a').addClass('selected');
    this.thirdComponent = $(container).find('a').attr("rel");
    $(container).find('input[type=radio]').each(function(index, el) {
        el.checked = true;
    });

    this.checkLoad();
}

Luma.ProfileSelector.prototype.checkLoad = function() {
    if (this.firstComponent != "" && this.secondComponent != ""
        && this.thirdComponent != "") {
        this.loadRelatedText();
    }
}

Luma.ProfileSelector.prototype.loadRelatedText = function() {
    var self = this,
    result = this.firstComponent + "." + this.secondComponent + "." + this.thirdComponent;
        
    $.ajax({
        url: Luma.profileSelectorOptions.url + "&profileId=" + encodeURIComponent(result),
        success: function(data) {
            $(self.resultElement).html(data);
            self.saveState(result);
        },
        error: function(xhr) {
            Luma.ajaxErrorFunction(xhr);
        }
    });
}

// default message
Luma.ajaxErrorMessage = 'Oops. An error occured while processing your request. Request status is #status#.';
Luma.ajaxErrorFunction = function(xhr) {
    alert(Luma.ajaxErrorMessage.replace("#status#", xhr.status));
}

Luma.showContactUs = function(event) {

    $.ajax({
        url: Luma.contactUsOptions.loadFormUrl,

        success: function(data) {
            Luma.showContactUsBgPanel(Luma.$('contact_us_panel'));
            var div = $('#contact_us_form');
            if (!div.length) {
                div = document.createElement('div');
                div.id = "contact_us_form";
                div.className = 'contact_us_form_panel';
                document.body.appendChild(div);
                div = $(div);
            }
            Luma.Commons.onShowModalElement();
            $(div).show();
            $(div).html(data);
        },

        error: Luma.ajaxErrorFunction
    });

}

Luma.showContactUsBgPanel = function(element) {
  if (window.navigator.userAgent.toLowerCase().indexOf('msie 6.0') != -1) {
      element.parentNode.removeChild(element);
      document.body.appendChild(element);
      $(element).css('position', 'absolute').css('zIndex', '999');
      $(element).css('left', '0').css('top', '0');
      $(element).css('width', ($(document).width() - 21) + 'px').css('height', $(document).height());
      $(element).css('filter', 'alpha(opacity=70)');
  }
  $(element).show();
}

Luma.hideContactUs = function(event) {
    $('#contact_us_form').hide();
    $('#contact_us_panel').hide();
    Luma.Commons.onHideModalElement();
}

Luma.sendContactUs = function(event) {
    $.ajax({
        url: Luma.contactUsOptions.sendFormUrl,
        type: "POST",
        data: $('#contact_us_form form').serialize(),

        success: function(data) {
            $('#contact_us_form').html(data);
        },

        error: Luma.ajaxErrorFunction
    });
}


Luma.loadJobInfo = function(id, containerId, link) {
    $.ajax({
        url: Luma.jobsOptions.url + "&jobId=" + encodeURIComponent(id),
        success: function(data) {
            $('#' + containerId).show().html(data);
            $(link).hide();
            $(link).next('span:first').hide();
        },
        error: function(xhr) {
            $(link).show();
            $(link).next('span:first').show();
            $('#' + containerId).hide();
            Luma.ajaxErrorFunction(xhr);
        }
    });
}

Luma.loadFlashText = function(id, containerId) {
    $.ajax({
        url: Luma.flastTextOptions.url + "&textId=" + encodeURIComponent(id),
        success: function(data) {
            $('#' + containerId).html(data);
        },
        error: Luma.ajaxErrorFunction
    });
}

Luma.updateContactFieldLabel = function(select, labelId) {
    if (select.value == null || select.value == "") {
        $('#' + labelId).text(Luma.contactUsOptions.defaultText + ":");
        return;
    }
    $('#' + labelId).text($(select).find('option[value=' + select.value + ']').text() + ":");
}

Luma.ButtonsObserver = {};

Luma.ButtonsObserver.initialize = function() {
    $('.grey_button').each(function(index, element) {
        Luma.ButtonsObserver.processElement(element);
    });
    setTimeout(Luma.ButtonsObserver.initialize, 1000);
}

Luma.ButtonsObserver.processElement = function(element) {
    if ($(element).attr('_grey_button_processed') == "yes") {
        //console.info("Already processed:" + $(element).find('a, input').html());
        return;
    }
    $(element).attr('_grey_button_processed', "yes");
    var tagName = element.tagName.toLowerCase(), selector;
    if (tagName == "div") {
        selector = $(element).find('a');
    } else if (tagName == "input") {
        selector = $(element);
    }
    selector.mouseover(function() {
        $(this).addClass('grey_button_hover');
        $(this).nextAll('span').addClass('grey_button_span_hover');
    });
    selector.mouseout(function() {
        $(this).removeClass('grey_button_hover');
        $(this).nextAll('span').removeClass('grey_button_span_hover');
    });
}


/***********************************************
* Show Hint script- © Dynamic Drive (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for this script and 100s more.
***********************************************/

var horizontal_offset = "-100px"; //horizontal offset of hint box from anchor link

/////No further editting needed

var vertical_offset = "0"; //horizontal offset of hint box from anchor link. No need to change.
var ie = document.all;
var ns6 = document.getElementById && !document.all;

function getposOffset(what, offsettype) {
    var totaloffset = (offsettype == "left") ? what.offsetLeft : what.offsetTop;
    var parentEl = what.offsetParent;
    while (parentEl != null) {
        totaloffset = (offsettype == "left") ? totaloffset + parentEl.offsetLeft : totaloffset + parentEl.offsetTop;
        parentEl = parentEl.offsetParent;
    }
    return totaloffset;
}

function iecompattest() {
    return (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body;
}

function clearbrowseredge(obj, whichedge) {
    var edgeoffset = (whichedge == "rightedge") ? parseInt(horizontal_offset) * -1 : parseInt(vertical_offset) * -1;
    if (whichedge == "rightedge") {
        var windowedge = ie && !window.opera ? iecompattest().scrollLeft + iecompattest().clientWidth - 30 : window.pageXOffset + window.innerWidth - 40;
        dropmenuobj.contentmeasure = dropmenuobj.offsetWidth;
        if (windowedge - dropmenuobj.x < dropmenuobj.contentmeasure)
            edgeoffset = dropmenuobj.contentmeasure + obj.offsetWidth + parseInt(horizontal_offset);
    } else {
        var windowedge = ie && !window.opera ? iecompattest().scrollTop + iecompattest().clientHeight - 15 : window.pageYOffset + window.innerHeight - 18;
        dropmenuobj.contentmeasure = dropmenuobj.offsetHeight;
        if (windowedge - dropmenuobj.y < dropmenuobj.contentmeasure)
            edgeoffset = dropmenuobj.contentmeasure - obj.offsetHeight;
    }
    return edgeoffset;
}

function showhint(menucontents, obj, e, tipwidth) {
    if ((ie || ns6) && document.getElementById("hintbox")) {
        dropmenuobj = document.getElementById("hintbox");
        dropmenuobj.innerHTML = menucontents;
        dropmenuobj.style.left = dropmenuobj.style.top = -500;
        if (tipwidth != "") {
            dropmenuobj.widthobj = dropmenuobj.style;
            dropmenuobj.widthobj.width = tipwidth;
        }
        dropmenuobj.x = getposOffset(obj, "left");
        dropmenuobj.y = getposOffset(obj, "top");
        dropmenuobj.style.left = dropmenuobj.x - clearbrowseredge(obj, "rightedge") + obj.offsetWidth + "px";
        dropmenuobj.style.top = dropmenuobj.y - clearbrowseredge(obj, "bottomedge") + "px";
        dropmenuobj.style.visibility = "visible";
        obj.onmouseout = hidetip;
    }
}

function hidetip(e) {
    dropmenuobj.style.visibility = "hidden";
    dropmenuobj.style.left = "-500px";
}

function createhintbox() {
    var divblock = document.createElement("div");
    divblock.setAttribute("id", "hintbox");
    document.body.appendChild(divblock);
}


