/*
Copyright (c) 2009, DeLijn Inc. All rights reserved.
version: LAR 1.0
file : DWRYUI.js
*/
/* shortcut anonieme functie die dadelijk uitgevoerd wordt*/
var lang = YAHOO.lang, util = YAHOO.util, Ev = util.Event, DS = util.DataSourceBase, Dom = YAHOO.util.Dom;

(function(){
    Dom.getElementsBy(function(el){
        return el.tagName == "BODY";
    }, "body", null, function(el){
        Dom.addClass(el, "yui-skin-delijn");
    });
    /*
     Dom.getElementsBy(function(el){
     return true;
     }, "input", null, function(el){
     el.setAttribute("autocomplete", "off");
     });
     */
})();
YAHOO.namespace("DELIJN.util");

YAHOO.DELIJN.util.DS_DWR = function(oConfigs){
    // Set any config params passed in to override defaults
    if (oConfigs && (oConfigs.constructor == Object)) {
        for (var sConfig in oConfigs) {
            if (sConfig) {
                this[sConfig] = oConfigs[sConfig];
            }
        }
    }
};

/**
 * To be extended in order to implement UC specific needs
 */
YAHOO.lang.extend(YAHOO.DELIJN.util.DS_DWR, YAHOO.util.XHRDataSource, {

    maxCacheEntries: 50,
    /**
     * Er is geen echt schema (enkel een JSArray) maar XHRDataSource verwacht dit element.
     */
    responseSchema: {
        fields: ["name"]
    },
    /**
     * The last query, to prevent from displaying results from an earlier
     * query
     * @type String
     */
    lastQuery: null,
    
    /**
     * If true, only the last query will be handled on result arrival
     * (because AJAX result may arrive in an unspecified order)
     *
     * @type boolean
     */
    lastQueryOnly: true,
    
    /**
     * Overriding method passes query to Connection Manager. The returned
     * response is then forwarded to the handleResponse function.
     *
     * @method makeConnection
     * @param oRequest {Object} Request object.
     * @param oCallback {Object} Callback object literal.
     * @param oCaller {Object} (deprecated) Use oCallback.scope.
     * @return {Number} Transaction ID.
     */
    makeConnection: function(oRequest, oCallback, oCaller){
    
        var tId = DS._nTransactionId++;
        var callback;
        
        this.lastQuery = oRequest;
        callback = (function(that, oCallback, oRequest){
            return function(result){
                var formattedResult;
                if (!result) {
                    return;
                }
                
                if (that.lastQueryOnly && oRequest !== that.lastQuery) {
                    // This is not the last query, do nothing
                    return;
                }
                
                formattedResult = that.formatDwrResult(result);
                that.handleResponse(oRequest, formattedResult, oCallback);
            };
        })(this, oCallback, oRequest);
        
        this.callDwrService(oRequest, callback);
        
        return tId;
    },
    
    /**
     * Overridable method parses Array data into a response object.
     *
     * @method parseArrayData
     * @param oRequest {Object} Request object.
     * @param oFullResponse {Object} The full Array from the live database.
     * @return {Object} Parsed response object with the following properties:<br>
     * - results (Array) Array of parsed data results<br>
     * - error (Boolean) True if there was an error
     */
    parseArrayData: function(oRequest, oFullResponse){
        oFullResponse.results = oFullResponse;
        return oFullResponse;
    },
     /**
     * Format the DWR result.
     * To be overriden in sublasses
     * @method formatDwrResult
     * @argument result {Object}
     * @return {Object}
     */
    formatDwrResult: function(result){
        return result;
    },
    
    /**
     * To be overriden in sublasses
     *
     * @method callDwService
     * @argument query {String}
     * @argument callback {Function}
     */
    callDwrService: function(query, callback){
        // empty implementation, call the callback directly
        callback.call(this);
    }
    
});

YAHOO.namespace("DELIJN.autocomplete");

/**
 * Gemeente locatie
 */
YAHOO.DELIJN.autocomplete.Gemeente = function(dwrServiceMethod){

    YAHOO.DELIJN.autocomplete.Gemeente.superclass.constructor.call(this);
    
    this.dwrServiceMethod = dwrServiceMethod;
    
};

YAHOO.lang.extend(YAHOO.DELIJN.autocomplete.Gemeente, YAHOO.DELIJN.util.DS_DWR, {

    cachedGemeente: "",
    
    dwrServiceMethod: null,
    /**
     * Here your code to adapt the query parameter to your Java method signature (according to DWR converters)
     * @param {Object} query
     * @param {Object} callback
     */
    callDwrService: function(query, callback){
        var arg;
        this.dwrServiceMethod(query, {
            callback: callback
        });
    }
    
});
/**
 * Locatie met één niveau van afhankelijkheid nl. de gemeente
 */
YAHOO.DELIJN.autocomplete.locOnderGemeente = function(dwrServiceMethod, oConfigs){

    YAHOO.DELIJN.autocomplete.locOnderGemeente.superclass.constructor.call(this, oConfigs);
    
    this.dwrServiceMethod = dwrServiceMethod;
    
};

YAHOO.lang.extend(YAHOO.DELIJN.autocomplete.locOnderGemeente, YAHOO.DELIJN.util.DS_DWR, {

    dwrServiceMethod: null,
    /**
     * Here your code to adapt the query parameter to your Java method signature (according to DWR converters)
     * @param {Object} query
     * @param {Object} callback
     */
    callDwrService: function(query, callback){
        this.dwrServiceMethod(Dom.get(this.gemeenteInput).value, query, {
            callback: callback
        });
    },
     /**
     * We moeten deze methode 'overschrijden' want enkel de ORequest param wordt gecached
     * en niet de eventuele combinatie gemeente/straat met ORequest.
     * Bij een nieuwe gemeente wordt de cache geflushed.
     * @method getCachedResponse
     * @param oRequest {Object} Request object.
     * @param oCallback {Object} Callback object.
     * @param oCaller {Object} (deprecated) Use callback object.
     * @return {Object} Cached response object or null.
     */
    getCachedResponse: function(oRequest, oCallback, oCaller){
        var currentGemeente = Dom.get(this.gemeenteInput).value;
        if (this.cachedGemeente != currentGemeente) {
            this.flushCache();
        }
        YAHOO.DELIJN.autocomplete.locOnderGemeente.superclass.getCachedResponse.call(oRequest, oCallback, oCaller);
    }
});
/**
 * Locatie met twee niveaus van afhankelijkheid nl. de gemeente en straat
 */
YAHOO.DELIJN.autocomplete.locOnderGemeenteStraat = function(dwrServiceMethod, oConfigs){

    YAHOO.DELIJN.autocomplete.locOnderGemeenteStraat.superclass.constructor.call(this, oConfigs);
    
    this.dwrServiceMethod = dwrServiceMethod;
    
};

YAHOO.lang.extend(YAHOO.DELIJN.autocomplete.locOnderGemeenteStraat, YAHOO.DELIJN.util.DS_DWR, {
    cachedStraat: "",
    dwrServiceMethod: null,
    /**
     * Here your code to adapt the query parameter to your Java method signature (according to DWR converters)
     * @param {Object} query
     * @param {Object} callback
     */
    callDwrService: function(query, callback){
        var arg;
        this.dwrServiceMethod(Dom.get(this.gemeenteInput).value, Dom.get(this.straatInput).value, query, {
            callback: callback
        });
    },
     /**
     * @method getCachedResponse
     * @param oRequest {Object} Request object.
     * @param oCallback {Object} Callback object.
     * @param oCaller {Object} (deprecated) Use callback object.
     * @return {Object} Cached response object or null.
     */
    getCachedResponse: function(oRequest, oCallback, oCaller){
        var currentGemeente = Dom.get(this.gemeenteInput).value;
        var currentStraat = Dom.get(this.straatInput).value;
        if (this.cachedGemeente != currentGemeente || this.cachedStraat != currentStraat) {
            this.flushCache();
        }
        YAHOO.DELIJN.autocomplete.locOnderGemeenteStraat.superclass.getCachedResponse.call(oRequest, oCallback, oCaller);
    }
});
YAHOO.namespace("DELIJN.widget");
YAHOO.DELIJN.widget.AutoComplete = new function(){
    this.oConfigs = {
        maxResultsDisplayed: 10,
        prehighlightClassName: "yui-ac-prehighlight",
        queryDelay: 0.4,
        minQueryLength: 2
    }
    this.oConfigs2 = {
    	maxResultsDisplayed: 10,
    	prehighlightClassName: "yui-ac-prehighlight",
    	queryDelay: 0.4,
    	minQueryLength: 3
    }
    var scope = this.scope || scope || window;
    var locatieConfigs = scope.locatieConfigs;
    if (locatieConfigs && (locatieConfigs.constructor == Array)) {
        for (var ctr in locatieConfigs) {
            if (locatieConfigs[ctr].gemeenteInput) {
                var gemeentesDS = new YAHOO.DELIJN.autocomplete.Gemeente(AutocompleterDelijn.completeGemeente);
                new YAHOO.widget.AutoComplete(locatieConfigs[ctr].gemeenteInput, locatieConfigs[ctr].gemeenteContainer, gemeentesDS, this.oConfigs);
            }
            if (locatieConfigs[ctr].straatInput) {
                var stratenDS = new YAHOO.DELIJN.autocomplete.locOnderGemeente(AutocompleterDelijn.completeStraat, {
                    gemeenteInput: locatieConfigs[ctr].gemeenteInput
                });
                new YAHOO.widget.AutoComplete(locatieConfigs[ctr].straatInput, locatieConfigs[ctr].straatContainer, stratenDS, this.oConfigs);
            }
            if (locatieConfigs[ctr].halteInput) {
                var haltesDS = new YAHOO.DELIJN.autocomplete.locOnderGemeente(AutocompleterDelijn.completeHalte, {
                    gemeenteInput: locatieConfigs[ctr].gemeenteInput
                });
                new YAHOO.widget.AutoComplete(locatieConfigs[ctr].halteInput, locatieConfigs[ctr].halteContainer, haltesDS, this.oConfigs);
            }
            if (locatieConfigs[ctr].halteOptGemeenteInput) {
            	var haltesOptGemeenteDS = new YAHOO.DELIJN.autocomplete.locOnderGemeente(AutocompleterDelijn.completeHalteOptGemeente, {
            		gemeenteInput: locatieConfigs[ctr].gemeenteInput
            	});
            	new YAHOO.widget.AutoComplete(locatieConfigs[ctr].halteOptGemeenteInput, locatieConfigs[ctr].halteOptGemeenteContainer, haltesOptGemeenteDS, this.oConfigs2);
            }
            if (locatieConfigs[ctr].stationInput) {
                var stationsDS = new YAHOO.DELIJN.autocomplete.locOnderGemeente(AutocompleterDelijn.completeStation, {
                    gemeenteInput: locatieConfigs[ctr].gemeenteInput
                });
                new YAHOO.widget.AutoComplete(locatieConfigs[ctr].stationInput, locatieConfigs[ctr].stationContainer, stationsDS, this.oConfigs);
            }
            if (locatieConfigs[ctr].herkenningspuntInput) {
                var herkenningspuntenDS = new YAHOO.DELIJN.autocomplete.locOnderGemeente(AutocompleterDelijn.completeHerkenningspunt, {
                    gemeenteInput: locatieConfigs[ctr].gemeenteInput
                });
                new YAHOO.widget.AutoComplete(locatieConfigs[ctr].herkenningspuntInput, locatieConfigs[ctr].herkenningspuntContainer, herkenningspuntenDS, this.oConfigs);
            }
            if (locatieConfigs[ctr].kruispuntInput) {
                var kruispuntenDS = new YAHOO.DELIJN.autocomplete.locOnderGemeenteStraat(AutocompleterDelijn.completeKruispunt, {
                    gemeenteInput: locatieConfigs[ctr].gemeenteInput,
                    straatInput: locatieConfigs[ctr].straatInput
                });
                new YAHOO.widget.AutoComplete(locatieConfigs[ctr].kruispuntInput, locatieConfigs[ctr].kruispuntContainer, kruispuntenDS, this.oConfigs);
            }
        }
    }
};


