/**
*   Basic object for making XHR requests and using the results as the innerHTML of an element
*   @param button -> Id of the button which will initiate the request
*   @param box -> Id of the element whose innerHTML will be replaced with the xhr results
*   @param loadingEl -> Id of the element to place the loading dialog
**/
(function () {


    // Constructor
    LocationFinder = function (oParams) {
        this.init(oParams);
    }

    // Global
    var me;


    //Private Properties and Methods
    var funcs = {

        onSubmitCallBack: function (e, obj) {
            YUE.preventDefault(e);
            if (me.formDisabled) { return false }

            var box = YUD.get(me.box),
                loadingEl = YUD.get(me.loadingEl),
                regionSelect = YUD.get('locregion'),
                region = regionSelect.options[regionSelect.selectedIndex].value;
            countrySelect = YUD.get('loccountry'),
                country = countrySelect.options[countrySelect.selectedIndex].value;
            businessunitSelect = YUD.get('locbusinessunit'),
                businessunit = businessunitSelect.options[businessunitSelect.selectedIndex].value;

            if (businessunit == 'VO')
                content = 'locationContentVO';
            else
                content = 'locationContent';


            // Errors Temp
            if (regionSelect.selectedIndex == 0 || countrySelect.selectedIndex == 0) {
                this.showError(regionSelect);
                return;
            }
            else {
                this.hideError(regionSelect);
            }

            // Build the request          
            var callback = {
                success: funcs.getLocationsCallBack,
                failure: funcs.getLocationsError
            };
            
            YUC.asyncRequest('GET', '/about_us/contact_us/' + region + '/' + country + '/index.aspx?mode=bodyContent&id=' + content, callback);

            YUD.addClass(loadingEl, 'loading');
            me.formDisabled = true;

            box.innerHTML = '';

            return false;

        },

        // Handles the success case of the XHR return trip 
        getLocationsCallBack: function (json) {
            var box = YUD.get(me.box),
                loadingEl = YUD.get(me.loadingEl);

            YUD.removeClass(loadingEl, 'loading');
            box.innerHTML = json.responseText;
            me.formDisabled = false;

            var parent = YUD.get('locationContent');
            var maps = YUD.getElementsByClassName('mapLocation', 'div', 'locationContent');
            var x, y,
                region = YUD.getRegion(parent),
                mapRegion,
                height,
                width;


            for (var i = 0; i < maps.length; i++) {
                mapRegion = YUD.getRegion(maps[i]);
                height = (mapRegion.bottom - mapRegion.top) + 'px';
                width = (mapRegion.right - mapRegion.left) + 'px';

                // Find the middle point of the region
                x = region.right - ((region.right - region.left) / 2);
                y = region.top + 40;

                // Subtract half the width and height of the element to center from the coords            
                x = x - (parseInt(width) / 2);

                YUD.setXY(maps[i], [x, y]);
            }
        },

        getLocationsError: function (json) {
            var box = YUD.get(me.box),
                loadingEl = YUD.get(me.loadingEl);

            YUD.removeClass(loadingEl, 'loading');
            box.innerHTML = '<h3 class="req">Sorry an error has occured.</h3>';
            me.formDisabled = false;
        }
    };

    LocationFinder.prototype = {

        init: function (oParams) {
            me = this;
            me.box = oParams.box;
            me.form = oParams.form;
            me.formDisabled = false;
            me.loadingEl = oParams.loadingEl;

            YUE.on(me.form, 'submit', funcs.onSubmitCallBack, me, true);
            YUE.on('locationContent', 'click', me.showMap);
        },

        showMap: function (e, obj) {
            var target = YUE.getTarget(e);
            if (!target) { return }
            var map,
                anim;

            if (YUD.hasClass(target, 'initOverlay')) {
                YUE.preventDefault(e);
                map = YUD.get('mapLocation' + target.id.replace('initOverlay', ''));
                YUD.setStyle(map, 'visibility', 'visible');
            }
            else if (YUD.hasClass(target, 'closeWindow')) {
                YUE.preventDefault(e);
                target = target.parentNode.parentNode.parentNode.parentNode;
                YUD.setStyle(target, 'visibility', 'hidden');
            }
        },

        showError: function (obj) {
            if (!YUD.hasClass(obj, 'hasErrors')) {
                var error = document.createElement('span');
                YUD.addClass(error, 'req');
                YUD.addClass(obj, 'hasErrors');
                error.innerHTML = errMsgs.selAll;
                obj.parentNode.insertBefore(error, obj.parentNode.firstChild);
            }
        },

        hideError: function (obj) {
            var error = YUD.getElementsByClassName('req', 'span', obj.parentNode);
            if (error.length > 0) {
                YUD.removeClass(obj, 'hasErrors');
                obj.parentNode.removeChild(error[0]);
            }
        }
    };
})();

locs = new LocationFinder({
    'form': 'locationsSearch',
    'box': 'locationResults',
    'loadingEl': 'locationContent'
});

locFinder = new DropDownList({
    'selectArray': {},
    'dataSource': '/webservices/feservice.aspx?method=locations',
    'baseSelectMenu': 'locregion',
    'targetSelectMenu': 'loccountry'
});
