/**
 *
 */
function epoq(params)
{
    this.go=function(parameter)
    {
        if(typeof(parameter)==="object")
        {
            var cartData = "";
            if(typeof(epoq_productIds)!= "undefined")
            {
                var cartData = "&";
                for ( var i=0, len=epoq_productIds.length; i<len; ++i )
                {
                    cartData += 'productId=' + encodeURIComponent(epoq_productIds[i]) + '&';
                    cartData += 'quantity=' + (typeof(epoq_quantities[i]) != "undefined" ? encodeURIComponent(epoq_quantities[i]) : "") + '&';
                    cartData += 'unitPrice=' + (typeof(epoq_unitPrices[i]) != "undefined" ? encodeURIComponent(epoq_unitPrices[i]) : "")+ '&';
                    cartData += 'variantOf=' + (typeof(epoq_variantOfList[i]) != "undefined" ? encodeURIComponent(epoq_variantOfList[i]) : "")+ '&';
                }

            }

            var getData = "tenantId=" + parameter.tenantId + "&sessionId=" + parameter.sessionId + "&rules=" + parameter.rules + "&widgetTheme=json2" + cartData; //&pretty=true
            if(typeof(parameter.productId)!= "undefined")getData += "&productId=" + parameter.productId;
            if(typeof(parameter.demo)!= "undefined")getData += "&demo=" + parameter.demo;
            if(typeof(parameter.fields)!= "undefined")getData += "&fields=" + parameter.fields;
            if(typeof(parameter.locakey)!= "undefined")getData += "&locakey=" + parameter.locakey;
            var host = parameter.tenantId  + ".arc.epoq.de";
            if(typeof(parameter.host)!= "undefined")host=parameter.host;
            var protocol = (("https:" == document.location.protocol) ? "https://" : "http://");

            jQuery.ajax({
                url: protocol + host + "/inbound-servletapi/getRecommendations",
                data:  getData,
                dataType: "jsonp",
                success: function (data)
                {
                    var needsdefault = true;
                    if(typeof(data) != "undefined" && typeof(data.recommendations)!= "undefined" && typeof(data.recommendations.domain)!= "undefined")
                    {
                        //check one domain || multiple domain [0]
                        if( (typeof(data.recommendations.domain.items)!= "undefined" && typeof(data.recommendations.domain.items.item)!= "undefined") || (typeof(data.recommendations.domain[0].items)!= "undefined" && typeof(data.recommendations.domain[0].items.item)!= "undefined"))
                        {
                            if(typeof(parameter.success)!= "undefined") //if a success function is defined
                            {
                                needsdefault = false;
                                parameter.success(data);
                            }
                            else //for commands
                            {
                                if(typeof(data.recommendations.domain.items.item.length) != "undefined") //check if many items
                                {
                                    for(var i=0; i <data.recommendations.domain.items.item.length; i++)
                                    {
                                        id = data.recommendations.domain.items.item[i]['@id'];
                                        if(typeof(parameter.reccommands[id])!= "undefined")
                                        {
                                            needsdefault = false;
                                            parameter.reccommands[id].ex(data.recommendations.domain.items.item[i]); //in case of a forward the loop appears only once
                                        }
                                    }
                                }
                                else //one item
                                {
                                    id = data.recommendations.domain.items.item['@id']; // no [i]
                                    if(typeof(parameter.reccommands[id])!= "undefined")
                                    {
                                        needsdefault = false;
                                        parameter.reccommands[id].ex(data.recommendations.domain.items.item); // no [i]
                                    }
                                }
                            }
                        }
                    }
                    if(needsdefault)parameter.reccommands['default'].ex();
                },
                error: function () {
                    if(typeof(parameter.error)!= "undefined") //if a success function is defined
                    {
                        parameter.error();
                    }
                    //alert ("Timeout");
                    if(typeof(parameter.reccommands)!= "undefined" && typeof(parameter.reccommands['default'])!= "undefined")parameter.reccommands['default'].ex();
                },
                timeout: 1000,
                cache: false
            });
        }
    }
}

if(typeof(epoq) != "object")epoq = new epoq();
