var shop_id, newsletter_flag, catalogorder_flag, catalogorder_name, product_recommendation, user_searchkeywords, order_article, customer_zip, product_onsale, product_id, wishlist_article, travel_start_date, travel_end_date, travel_kids, travel_adults, travel_hotel_category, eos_event, order_id, order_total, order_payment_method, customer_status, XPExtCustomerId;

function EOSDateFormat(date){
	dateArray = date.split("-");
	if(dateArray.length<3) return date;
	return dateArray[2]+"-"+dateArray[1]+"-"+dateArray[0];
}

function EOSParam(value){
	return value || "";
}

function EOSProfiling(r) {
	// read & define parameters
	shop_id = EOSParam(r.shop_id);
	newsletter_flag = EOSParam(r.newsletter_flag);
	catalogorder_flag = EOSParam(r.catalogorder_flag);
	catalogorder_name = EOSParam(r.catalogorder_name);
	product_recommendation = EOSParam(r.product_recommendation);
	user_searchkeywords = EOSParam(r.user_searchkeywords);
	order_article = EOSParam(r.order_article);
	customer_zip = EOSParam(r.customer_zip);
	product_onsale = EOSParam(r.product_onsale);
	product_id = EOSParam(r.product_id);
	wishlist_article = EOSParam(r.wishlist_article);
	travel_start_date = EOSDateFormat(EOSParam(r.travel_start_date));
	travel_end_date = EOSDateFormat(EOSParam(r.travel_end_date));
	travel_kids = EOSParam(r.travel_kids);
	travel_adults = EOSParam(r.travel_adults);
	travel_hotel_category = EOSParam(r.travel_hotel_category);
	eos_event = EOSParam(r.eos_event);
	// call xplosion profiling script
	document.write('<script id="xplscript" type="text/javascript" src="http://cdn.xplosion.de/xpl_profiling.js?easyTag"></script>')
}

function EOSCheckOut(h) {
	// read & define parameters
	order_article = EOSParam(h.order_article);
	order_id = EOSParam(h.order_id);
	order_total = EOSParam(h.order_total);
	order_payment_method = EOSParam(h.order_payment_method);
	customer_status = EOSParam(h.customer_status);
	customer_zip = EOSParam(h.customer_zip);
	travel_start_date = EOSDateFormat(EOSParam(h.travel_start_date));
	travel_end_date = EOSDateFormat(EOSParam(h.travel_end_date));
	travel_kids = EOSParam(h.travel_kids);
	travel_adults = EOSParam(h.travel_adults);
	travel_hotel_category = EOSParam(h.travel_hotel_category);
	eos_event = EOSParam(h.eos_event);
	XPExtCustomerId = EOSParam(h.XPExtCustomerId);
	// call secure profiling script
	document.write('<script id="xplscript" type="text/javascript" src="https://ssl.xplosion.de/xpl_profiling.js?easyTag"></script>')
}

function EOSsearchParameters() {
	// pic up the search parameters in map form
	var args = Novasol.Utils.getParametersAsMap();
	//the delimeter characther used in the return string
	var delimeterChar = ',';
	var rsStr = '';

	// the array of parameters where we just take the value of the parameter. All other characters are converted
	// to have the value 0 or 1.

	var parameterArrayDates = ['SD','ED'];
	var parameterArrayValue = ['C','R','SD','ED','PE','K','QE1','Q50','Q54','P','Q51','Q52','Q67'];

	// all the parameters that we test for having a value
	var parameterArray = ['C','R','SD','ED','PE','K','QE1','Q50','Q54','P','Q51','Q52','Q67','Q00','Q01','Q45','Q02','Q03','Q06','Q05','Q30','Q10','Q04','Q38','Q09','Q29','Q08','Q31','Q13','Q15','Q12','Q26','Q32'];

	// run through the array of all parameters
	for(var i=0, searchParameter;searchParameter = parameterArray[i]; i++) {
		// search to see if the parameter is of type "Value" or type "0 or 1"
		if (jQuery.inArray(searchParameter,parameterArrayValue) != -1) {
			// here we the value of the parameter (can be empty)
			if (args[parameterArray[i]]) {
				if (jQuery.inArray(searchParameter,parameterArrayDates) != -1) {
					var dateArr = args[parameterArray[i]].split('-');
					if(dateArr.length==3) {
						rsStr += dateArr[2]+ '-' + dateArr[1] + '-' + dateArr[0];
					}
					else {
						rsStr += 'false';
					}
				}
				else {
					rsStr += args[parameterArray[i]];
				}
			}
			rsStr += delimeterChar;
		}
		else { // here we know that the return value is 0 or 1
			if (args[parameterArray[i]]) { // If we have the parameter value and the value is not 'N' we return 1
				if (args[parameterArray[i]] == 'N') {
					rsStr += '0';
				} else {
					rsStr += '1';
				}

			} else {
				// here we don't have the parameter
				rsStr += '0';
			}
			rsStr += delimeterChar;
		} // end if

	} // end for
	return rsStr.substring(0,rsStr.lastIndexOf(delimeterChar));
} // end function

//do the tracking calls
switch(Novasol.context) {
	case "showhouse_":
		EOSProfiling({
			product_onsale: Novasol.house.isLastminute,
			product_id: location.pathname.split("/").pop(),
			eos_event: 'product_detail'
		});
		break


	case "searchresults_":
		var event = "";
		var housedata = "";
		var searchparameters = "";
		var args = Novasol.Utils.getParametersAsMap();

		if (args["HQ"]) {
			var delim1 = ':', delim2 = '|';
			if(args["HQ"].indexOf('^') != -1) {
				delim1 = '^';
				delim2 = '_ ';
			}
			var houselist = args["HQ"].split(delim1);

			for (var i = 0; i < houselist.length; i++) {
				var valueset = houselist[i].split(delim2);
				housedata = housedata +","+valueset[0];
			}
			housedata = housedata.substring(1,housedata.length);
			event = 'wishlist';
		}
		else {
			searchparameters = EOSsearchParameters();
			event = 'search_result';
		}

		EOSProfiling({
			user_searchkeywords: searchparameters,
			wishlist_article: housedata,
			eos_event: event
		});

		break;
	default:
		if(location.pathname.indexOf('.html') != -1) {
			EOSProfiling({
				shop_id: location.pathname.substring(1).replace('.html','')
			});
		}
}



