/**
 * Search Client
 *
 * Depends on
 *      - jQuery
 *      - common.js
 */

MM.searchClient = (function($J){
    function log( message ) {
        var console = window['console'];
        if (console && console.log) {
            console.log(message);
        }
    }

    function time( message, fn, params ) {
        var start = new Date().getTime();
        if ( $J.isArray( params) ) {
            fn.apply( this, params );
        } else {
            fn.call( this, params );
        }
        var end = new Date().getTime();
        log ( message + ": " + (end - start) + " ms" );
    }

    function requestData( url, ident, profile, params, fn, debugEnabled ) {
        var startTime = new Date().getTime();
        if ( profile !== null && profile !== "" ) {
            $J.ajax({
                type: "GET",
                dataType:       ( MM.cfg.crossDomain ? "jsonp" : "json" ),
                url:            url + "/" + ident + "/" + profile + "/search.json",
                data:           params,
                success: function( json ) {
                    if ( fn && json !== null) {
                        if ( debugEnabled ) {
                            // Log debug messages to console
                            log ( "QT: " + ((json !== null) ? json.searchTimeInMillis + " ms" : " N/A ") );
                            log ( "RTT: " + (new Date().getTime() - startTime) + " ms" );
                            time ( "Result rendering", fn, json );
                        } else {
                            fn ( json );
                        }
                    }
                },
                complete : function( XMLHttpRequest, textStatus ){
                    //MM.util.showLoad( false );
                }
            });
        }
    }

     return {

        simpleSearch: function( term, fn ) {
           requestData( MM.cfg.searchWSUrl, MM.cfg.ident, MM.cfg.main_search_profile, "q=" + encodeURIComponent(term), fn );
        },

        search: function( params, fn ) {
			requestData( MM.cfg.searchWSUrl, MM.cfg.ident, MM.cfg.main_search_profile, MM.searchClient.encodeQuery(params), fn );
        },

        relatedSearch: function( params, fn ) {
            requestData( MM.cfg.searchWSUrl, MM.cfg.ident, MM.cfg.related_search_profile, MM.searchClient.encodeQuery(params), fn );
        },

        backfillSearch: function( params, fn ) {
            requestData( MM.cfg.searchWSUrl, MM.cfg.ident, MM.cfg.backfill_search_profile, MM.searchClient.encodeQuery(params), fn );
        },
		
		// Enkodningsproblem i IE9, ADEPRIMO-3537
		encodeQuery: function(params) {
		
			var arrParams = params.split("&");
			var paramName = "";
			var paramValue = "";
			
			arrParams.each(function(elm, index) {
				var arrNameValue = elm.split("=");
		
				paramName = arrNameValue[0];
				paramValue = arrNameValue[1];
				paramValue = decodeURIComponent(paramValue);
				paramValue = encodeURIComponent(paramValue);
			
				arrParams[index] = paramName + "=" + paramValue;
			});
			
			params = arrParams.join("&");
			
			return params;
		}

    };
})(jQuery.noConflict());
