(function () {

    YAHOO.namespace("applegate.marketing");

    var Dom = YAHOO.util.Dom,
    $ = Dom.get,
    Event = YAHOO.util.Event,
    Selector = YAHOO.util.Selector,
    lang = YAHOO.lang,
    applegate = YAHOO.applegate,
    validation = applegate.validation,
    global = YAHOO.applegate.global;

    // Instantiate and configure Loader:
    var loader = new YAHOO.util.YUILoader({

        // Identify the components you want to load.  Loader will automatically identify
        // any additional dependencies required for the specified components.
        require: ["button"],

        // YUI Base directory
        base: '/javascript/yui-2.6.0/',

        // Configure loader to pull in optional dependencies.  For example, animation
        // is an optional dependency for slider.
        loadOptional: true,

        // The function to call when all script/css resources have been loaded
        onSuccess: function() {

            Event.onDOMReady(function () {


                var imageDataSource = {
  image1:{url:"/images/computer_large.jpg", title: 'About Alibaba.com'},
  image2:{url:"/images/aboutAli_large.jpg", title: 'About Alibaba.com'},
  image3:{url:"/images/aliAwards_large.jpg", title: 'Awards'}
};

var lightbox = new YAHOO.com.thecodecentral.Lightbox({
    imageBase:'/javascript/lightbox',
    dataSource: imageDataSource
});



                function renderCountryAutocomplete() {
                    YAHOO.log('Creating Country datasource', 'info', 'marketing.js');
                    var dsCountryAC = new applegate.dwr.DwrDataSource(MarketingResponseDwrModel.suggestCountries, [], {
                        appendRequest: true
                    });

                    YAHOO.log('Creating Country autocompleter', 'info', 'marketing.js');
                    var acCountry = new applegate.autocomplete.AutoComplete("marketingResponseDescriptor.country", "countryContainer", dsCountryAC, {
                        agSelectProperties: ["name"],
                        agSuggestProperties: ["name"],
                        agAllowEmptyQuery: true,
                        minQueryLength: 0,
                        forceSelection: true
                    });
                }




                //Grab localised messages from a div on the associated jsp
                var tb = new YAHOO.applegate.resources.TextBundle('ib_msg_i18n');


                // var marketingResponseModule = new YAHOO.widget.Module("marketingResponseForm");

                var submitMarketingResponseButton = new YAHOO.widget.Button('submitMarketingResponse')

                submitMarketingResponseButton.on("click", handleSubmitMarketingResponse);


                function handleSubmitMarketingResponse(p_oEvent) {
                    document.body.style.cursor = 'wait';
                    submitMarketingResponseButton.set('disabled', true);
                    // short hand
                    var form = Dom.get('marketingResponseForm');
                   // validation.clearMessages(form);
                    var YUC = YAHOO.util.Connect;
                    YUC.setForm(form);
                    YUC.asyncRequest('POST', global.contextPath() + "/marketing.json", {
                        success: handleMarketingResponseSuccess,
                        failure: handleMarketingResponseFailure,
                        scope: this
                    });
                }

                var handleMarketingResponseSuccess = function (o) {
                    var obj = lang.JSON.parse(o.responseText),
                    form = Dom.get('marketingResponseForm');
                    if (obj.hasErrors) {
                        validation.applyBindingResultToForm(obj, form);
                        Dom.setStyle('messageTitle', 'visibility', 'visible');
                        document.body.style.cursor = 'default';
                        submitMarketingResponseButton.set('disabled', false);
                        window.scrollTo(0,0);
                    } else {
                        validation.clearMessages(form);
                        document.body.style.cursor = 'default';
                        submitMarketingResponseButton.set('disabled', false);
                        applegate.dialog.showWarning("Success, thanks for taking the time to respond.","Feedback Received");
                    }
                };

                var handleMarketingResponseFailure = function (o) {
                    applegate.dialog.showWarning("Sorry, there was a problem submitting your response. Please try again or contact us.", "Response Failed");
                    submitMarketingResponseButton.set('disabled', false);
                    document.body.style.cursor = 'default';
                };

                renderCountryAutocomplete();

            });
        },

        //Set your skins member here:
        skin: {
            // The default skin, which is automatically applied if not
            // overriden by a component-specific skin definition.
            // Use the Applegate specific skin.
            defaultSkin: 'applegate'
        },

        // Configure the Get utility to timeout after 10 seconds for any given node insert
        timeout: 10000,

        // Combine YUI files into a single request (per file type) by using the Yahoo! CDN combo service.
        combine: false
    });

    // Load the files using the insert() method. The insert method takes an optional
    // configuration object, and in this case we have configured everything in
    // the constructor, so we don't need to pass anything to insert().
    loader.insert();

})();