/**
 * File:    portalHighlights.js
 * Author:  M Chudy
 * Date:    04May2010
 * Description:
 *          This class handles the retrieval and display of "(right) Highlights" for the
 *          USHIK Application.
 * Change Log:
 *          M Chudy - 04May2010 - Initial Creation
 *          M Chudy - 05May2010 - Added documentation
 */
document.addEvent('domready', function(e){
    try{
        var JSONJavaScript = new Asset.javascript('scripts/json2.js',{
            onLoad: function(){
                buildHighlightsBox();
                var pageURI = new URI(window.location);
                var pageSystem = 'mdr';

                // look to the URI to find the system:
                if(pageURI.getData('system') != undefined){
                    pageSystem = pageURI.getData('system');
                } else if(pageURI.getData('System') != undefined){
                    pageSystem = pageURI.getData('System');
                }
                requestData('next');
            }
        });
    } catch(err){
    //alert(err + "\n" + err.description);
    }
    /**
         * Send a remote (AJAX) request to recieve highlights data and delegate
         * the handling of the response to the function populateHighlights()
         */
    function requestData(action){
        ++requestAttemptCount;
        if(action == null || action == undefined){
            action = 'next';
        }
        var highlightsRequest = new Request({
            method: 'post',
            url: 'ajaxHandler',
            data: {
                'request_action':'HighlightsRequestHandler',
                'System':pageSystem,
                'return':action
            },
            onRequest: function() {  },
            onComplete: function(response) {
                var data = JSON.parse(response);
                if(data.highlights == null && requestAttemptCount < 3){
                    //alert('about to re-request data...');
                    requestData(action);
                }
                populateHighlights(JSON.parse(response));
            }
        }
        ).send();
    }

    var requestAttemptCount = 0;

    /**
         *
         */
    function buildHighlightsBox(){
        house = $('portalHighlights');
        if(house == null){
            return;
        }
        var container = new Element('div', {
            'class':'highlightsContainer'
        });
        var headerDiv = new Element('div', {
            'id':'HLights',
            'class':'HLights'
        });
        var topTitle = new Element('div', {
            'class':'HLightsTitle',
            'html':'HIGHLIGHTS'
        });
        var bottomTitle = new Element('div', {
            'class':'HLightsTitle_ds',
            'html':'HIGHLIGHTS'
        });


        var textContainer = new Element('div', {
            'id':'highlightsTextContainer'
        });

        var spaceFiller = new Element('div', {
            'padding':'50px'
        });
        textContainer.appendChild(spaceFiller);

        headerDiv.appendChild(topTitle);
        headerDiv.appendChild(bottomTitle);
        container.appendChild(headerDiv);
        container.appendChild(textContainer);
        house.innerHTML = '';
        house.appendChild(container);
    }

    function populateHighlights(data){
        try{
            if($('highlightsTextContainer') == null){
                buildHighlightsBox();
            }
            $('highlightsTextContainer').innerHTML = '';
            for(var i=0; i< data.highlights.length; i++){
                var highlight = new Element('p', {
                    'class':'individual_highlight',
                    'html':data.highlights[i].value
                });
                $('highlightsTextContainer').appendChild(highlight);
            }
            if(data.displayNext){
                var controlsHouse = new Element('div', {
                    'class':'highlights_controlHouse'
                });

                //var previousControl = new Element('div', {
                //    'class':'highlights_previous',
                //    'html':'&laquo;',
                //    'title':'Display the next item'
                //});
                var nextControl = new Element('div', {
                    'class':'highlights_next',
                    'html':'&raquo;',
                    'title':'Click to learn more about USHIK'
                });
                //controlsHouse.appendChild(previousControl);
                controlsHouse.appendChild(nextControl);
                $('highlightsTextContainer').appendChild(controlsHouse);
                //previousControl.addEvent('click', function(e){
                //    requestData('previous');
                //});
                nextControl.addEvent('click', function(e){
                    requestData('next');
                });
            }
        } catch(err){
        //alert(err + "\n" + err.description);
        }
    }
});