/*
 * Econda helper
 */

var econda = {

	emosDebug                   : 0,
	emosKey                     : "",
	emosContent                 : null,
	emosSiteID                  : null,
	emosPageID                  : null,
	emosMarker                  : null,
	emosOrderProcess            : null,
	emosSearch                  : "",
	emosLogin                   : "",
	emosRegister                : "",
	emosTarget                  : "",
	emosProduct                 : new Array(),
	emosProductAjax             : new Array(),
	emosProductAjaxType         : "",
	emosProductAjaxQuantity     : "",
	emosProductAjaxOk           : false,
	emosProductCount            : 0,
	emosBillingCountry          : "",
	emosBillingPostal           : "",
	emosBillingCity             : "",
	emosBilling                 : new Array(),
	emosEventType               : new Array(),
	emosEventName               : new Array(),
	emosEventAction             : new Array(),
	emosEventActionBefore       : new Array(),
	emosEventCount              : 0,
	emosObserverInit            : false,
	emosSendCounter             : 0,
	emospro                     : {},
	emosViewType                : "",
	emosViewTypeInit            : false,

	/*
	 *  public functions to collect values
	 */

	content : function (emosPart) {
		if(this.emosContent == null) {
			this.emosContent = "";
		}
		this.emosContent = this.clean(emosPart);
	},

	getContent : function () {
		if(typeof(this.emosContent) == 'object') {
			return false;
		}
		return this.emosContent;
	},

	siteId : function (emosSite) {
		this.emosSiteID = "" + emosSite;
	},

	pageId : function (emosPage) {
		this.emosPageID = "" + this.clean(emosPage);
	},

	search : function (emosSearchPhrase, emosSearchHits) {
		this.emosSearch = new Array(this.clean(emosSearchPhrase), emosSearchHits);
	},

	marker : function (emosMarkerName) {
		this.emosMarker = "" + emosMarkerName;
	},
	
	viewtype : function (emosViewTypeName) {
		this.emosViewType = emosViewTypeName;
	},
	
	viewtypeinit : function (emosViewTypeInit) {
		this.emosViewTypeInit = emosViewTypeInit;
	},

	target : function (emosTGroup, emosTName, emosTWorth, emosTCalculation) {
		if(typeof(emosTCalculation) == 'undefined' || (emosTCalculation != 'd' && emosTCalculation != 's' && emosTCalculation != 'a')) {
			emosTCalculation = 'd';
		}
		if(typeof(emosTCalculation) == 'undefined') {
			emosTWorth = 1;
		}
		else {
			emosTWorth = parseInt(emosTWorth);
			if(isNaN(emosTWorth)) {
				emosTWorth = 1;
			}
		}
		this.emosTarget = new Array(emosTGroup, emosTName, emosTWorth, emosTCalculation);
	},

	login : function (emosCostumer, emosResult) {
		this.emosLogin = new Array(emosCostumer, emosResult);
	},

	register : function (emosCostumer, emosResult) {
		this.emosRegister = new Array(emosCostumer, emosResult);
	},

	product : function (emosPType, emosPId, emosPName, emosPPrice, emosPCat, emosPQuantity, emosPVar1, emosPVar2, emosPVar3) {
		if(typeof(emosPType) == 'undefined') {
			return false;
		}
		if(emosPType == 'add' || emosPType == 'rmv') {
			emosPType = 'c_' + emosPType;
		}
		if(typeof(emosPVar1) == 'undefined') {
			emosPVar1 = 'NULL';
		}
		if(typeof(emosPVar2) == 'undefined') {
			emosPVar2 = 'NULL';
		}
		if(typeof(emosPVar3) == 'undefined') {
			emosPVar3 = 'NULL';
		}
		this.emosProduct[this.emosProductCount] = new Array();
		this.emosProduct[this.emosProductCount][0] = emosPType;
		this.emosProduct[this.emosProductCount][1] = this.clean(emosPId);
		this.emosProduct[this.emosProductCount][2] = this.clean(emosPName);
		this.emosProduct[this.emosProductCount][3] = this.priceCor(emosPPrice);
		this.emosProduct[this.emosProductCount][4] = this.clean(emosPCat);
		this.emosProduct[this.emosProductCount][5] = this.quantityCor(emosPQuantity);
		this.emosProduct[this.emosProductCount][6] = this.clean(emosPVar1);
		this.emosProduct[this.emosProductCount][7] = this.clean(emosPVar2);
		this.emosProduct[this.emosProductCount][8] = this.clean(emosPVar3);
		this.emosProductCount += 1;
		return true;
	},

	getProductAjaxQuantity : function () {
		return this.emosProductAjaxQuantity;
	},

	customer : function (emosCId, emosCCountry, emosCPostal, emosCCity) {
		if(typeof(this.emosBilling[0]) == 'undefined') {
			this.emosBilling[0] = new Array();
		}
		if(emosCId != null) {
			this.emosBilling[0][1] = emosCId;
		}
		if(emosCCountry != null) {
			this.emosBillingCountry = emosCCountry;
		}
		if(emosCPostal != null) {
			this.emosBillingPostal = emosCPostal;
		}
		if(emosCCity != null) {
			this.emosBillingCity = emosCCity;
		}
	},

	billing : function (emosBId, emosBPrice) {
		if(typeof(this.emosBilling[0]) == 'undefined') {
			this.emosBilling[0] = new Array();
		}
		if(emosBId != null) {
			this.emosBilling[0][0] = emosBId;
		}
		if(emosBPrice != null) {
			this.emosBilling[0][3] = this.priceCor(emosBPrice);
		}
	},

	orderProcess : function (emosStep) {
		if(typeof(emosStep) != 'undefined') {
			this.emosOrderProcess = this.clean(emosStep);
		}
	},

	cKey : function (emosCustomer) {
		this.emosKey = emosCustomer.replace (/^\s+/, '').replace (/\s+$/, '');
		window.emos_dwkdnr = this.emosKey;
	},

	clear : function (emosParam) {
		if(typeof(emosParam) != 'undefined') {
			if(emosParam == 'content') {
				this.emosContent = null;
			}
			else if(emosParam == 'siteId') {
				this.emosSiteID = null;
			}
			else if(emosParam == 'pageId') {
				this.emosPageID = null;
			}
			else if(emosParam == 'search') {
				this.emosSearch = "";
			}
			else if(emosParam == 'marker') {
				this.emosMarker = null;
			}
			else if(emosParam == 'target') {
				this.emosTarget = "";
			}
			else if(emosParam == 'login') {
				this.emosLogin = "";
			}
			else if(emosParam == 'register') {
				this.emosRegister = "";
			}
			else if(emosParam == 'orderProcess') {
				this.emosOrderProcess = "";
			}
			else if(emosParam == 'product') {
				this.emosProduct = new Array();
				this.emosProductCount = 0;
			}
			else if(emosParam == 'productAjax') {
				this.emosProductAjaxType = "";
				this.emosProductAjaxOk == false;
				this.emosProductCount = 0;
			}
			else if(emosParam == 'billing') {
				this.emosBilling = new Array();
			}
			else if(emosParam == 'customer') {
				this.emosBillingCountry = "";
				this.emosBillingPostal = "";
				this.emosBillingCity = "";
			}
		}
		else {
			this.emosContent = null;
			this.emosSiteID = null;
			this.emosPageID = null;
			this.emosSearch = "";
			this.emosLogin = "";
			this.emosRegister = "";
			this.emosOrderProcess = "";
			this.emosMarker = null;
			this.emosTarget = "";
			this.emosProduct = new Array();
			this.emosProductCount = 0;
			this.emospro = {};
		}
	},

	/* internal functions to correct collected values */

	address : function (emosCCountry, emosCPostal, emosCCity) {
		var emosCustAddress = "";
		if(emosCCountry != "") {
			emosCustAddress += emosCCountry + "/";
		}
		if(emosCPostal != "") {
			emosCustAddress += emosCPostal.substring(0, 1) + "/";
			emosCustAddress += emosCPostal.substring(0, 2) + "/";
		}
		if(emosCCity != "") {
			emosCustAddress += emosCCity + "/";
		}
		if(emosCPostal != "") {
			emosCustAddress += emosCPostal;
		}
		return emosCustAddress;
	},

	priceCor : function (emosPrice) {
		var emosPriceSpl = emosPrice.split(" ");
		if(emosPriceSpl.length > 1) {
			emosPrice = emosPriceSpl[1];
		}
		emosPrice = emosPrice.replace(",",".");
		if(emosPrice.substring(emosPrice.length-7, emosPrice.length-6) == ".") {
			emosPrice = emosPrice.substring(0, emosPrice.length-8) + emosPrice.substring(emosPrice.length-6);
		}
		return emosPrice;
	},

	quantityCor : function (emosQuantity) {
		emosPCQuantitySpl = emosQuantity.split("value=");
		if(emosPCQuantitySpl.length > 1) {
			emosPCQuantity = emosPCQuantitySpl[1];
			emosPCQuantitySpl = emosPCQuantity.split(",");
			if(emosPCQuantitySpl.length > 1) {
				emosPCQuantity = emosPCQuantitySpl[0];
			}
		}
		else {
			emosPCQuantity = emosQuantity;
		}
		return emosPCQuantity;
	},

	clean : function (emosPath) {
		if(typeof(emosPath) == 'object' || emosPath.length == 0) {
			return emosPath;
		}
		if(emosPath.substring(0, 1) == "/") {
			emosPath = emosPath.substring(1);
		}
		if(emosPath.substring(emosPath.length-1, emosPath.length) == "/") {
			emosPath = emosPath.substring(0, emosPath.length-1);
		}
		return emosPath;
	},

	stristr : function (haystack, needle, bool) {
	    var pos = 0;
	    haystack += '';
	    pos = haystack.toLowerCase().indexOf((needle + '').toLowerCase());
	    if(pos == -1) {
	        return false;
	    }
	    else {
	        if(bool) {
	            return haystack.substr(0, pos);
	        }
	        else {
	            return haystack.slice(pos);
	        }
	    }
	},

	/* developer helper */

	log : function (emosType, emosValue) {
		try {
			console.log(emosType + " : " + emosValue);
		}
		catch(e) {}
	},

	debug : function (emosDebugMode) {
		if(typeof(emosDebugMode) == 'string') {
			emosDebugMode = parseInt(emosDebugMode);
		}
		this.emosDebug = emosDebugMode;
	},

	getSend : function () {
		return this.emosSendCounter;
	},

	/* send collected values as request*/

	send : function (initObject) {
		if(this.emosKey.length > 3 && this.emosKey != "null") {
			if(this.emosDebug == 1 || this.emosDebug == 2) {
				this.log("Econda is activated", "yes");
				this.log("Econda Activation Key", this.emosKey);
			}
		}
		else if(this.emosDebug == 1 || this.emosDebug == 2) {
			this.log("Econda is activated", "no");
		}
		if(typeof(this.emospro) == 'undefined' || initObject) {
			this.emospro = {};
		}
		if(typeof(this.emosContent) != 'object') {
			this.emospro.content = this.clean(this.emosContent);
		}
		if(typeof(this.emosPageID) != 'object') {
			this.emospro.pageId = this.emosPageID;
		}
		if(typeof(this.emosSiteID) != 'object') {
			this.emospro.siteid = this.emosSiteID;
		}
		if(typeof(this.emosSearch) != 'string' && this.emosSearch[0] != 'null') {
			this.emospro.search = [[this.emosSearch[0], this.emosSearch[1]]];
		}
		if(typeof(this.emosLogin) != 'string') {
			this.emospro.login = [[this.emosLogin[0], this.emosLogin[1]]];
		}
		if(typeof(this.emosRegister) != 'string') {
			this.emospro.register = [[this.emosRegister[0], this.emosRegister[1]]];
		}
		if(typeof(this.emosTarget) != 'string') {
			this.emospro.Target = [[this.emosTarget[0], this.emosTarget[1], this.emosTarget[2], this.emosTarget[3]]];
		}
		if(typeof(this.emosMarker) != 'object') {
			this.emospro.marker = this.emosMarker;
		}
		if(this.emosProduct.length > 0) {
			if(this.emosProductAjaxType != "" && this.emosProductAjaxOk == true) {
				if(this.emosProductAjaxQuantity == "") {
					this.emosProductAjaxQuantity = "1";
				}
				this.emosProductAjax[0] = new Array();
				this.emosProductAjax[0] = this.emosProduct[0];
				this.emosProductAjax[0][0] = this.emosProductAjaxType;
				this.emosProductAjax[0][5] = this.emosProductAjaxQuantity;
				this.emospro.ec_Event = this.emosProductAjax;

			}
			else {
				this.emospro.ec_Event = this.emosProduct;
				if(this.emosProduct.length == 1) {
					this.emosProductAjaxOk = true;
				}
			}
		}

		if(this.emosBilling.length > 0) {
			if(this.emosBilling[0][0] != null) {
				this.emosBilling[0][2] = this.address(this.emosBillingCountry, this.emosBillingPostal, this.emosBillingCity);
				this.emospro.billing = this.emosBilling;
			}
		}

		if(typeof(this.emosOrderProcess) != 'object') {
			this.emospro.orderProcess = this.emosOrderProcess;
		}

		if(this.emosDebug == 1 || this.emosDebug == 2) {
			for(var property in this.emospro) {
				if(typeof(this.emospro[property]) == 'object') {
					for(var i=0; i<this.emospro[property].length; i++) {
						this.log("Econda " + property, this.emospro[property][i]);
					}
				}
				else {
					this.log("Econda " + property, this.emospro[property]);
				}
			}
			if(this.emosDebug == 2 && this.emosKey.length > 3 && this.emosKey != "null") {
				this.log("Econda send request", "yes - debug mode 2");
				window.emosPropertiesEvent(this.emospro);
			}
			else {
				this.log("Econda send request", "no - debug mode 1");
			}
		}
		else {
			if(this.emosKey.length > 3 && this.emosKey != "null") {
				window.emosPropertiesEvent(this.emospro);
			}
		}
		this.emosSendCounter += 1;
	},

	/* set event handler for individual objects */

	observer : function (nodeType, nodeName, actionType, actionBefore) {
		var regOnce = false;
		for (var i=0; i<this.emosEventName.length; i++) {
			if(this.emosEventName[i] == nodeName) {
				regOnce = true;
			}
		}
	    if(!regOnce) {
	    	this.emosEventType[this.emosEventCount] = nodeType;
	    	this.emosEventName[this.emosEventCount] = nodeName;
	    	if(typeof(actionType) != 'undefined') {
	    		this.emosEventAction[this.emosEventCount] = actionType;
	    	}
	    	if(typeof(actionType) != 'undefined') {
	    		this.emosEventActionBefore[this.emosEventCount] = actionBefore;
	    	}
	    }
		if(this.emosObserverInit == false) {
			Event.observe(document, 'mousedown', this.catchEvent);
			this.emosObserverInit = true;
		}
		if(!regOnce) {
			this.emosEventCount += 1;
		}
	},

	/* call a specific operation for each event */

	catchEvent : function (evt) {
		elem = (evt.target.nodeType == 3) ? evt.target.parentNode : evt.target;
		var emosElSrc = "";
		for(var i=0; i<econda.emosEventType.length; i++) {
			if(econda.emosEventType[i] == 'className') {emosElSrc = elem.className;}
			else if(econda.emosEventType[i] == 'name') {emosElSrc = elem.name;}
			else if(econda.emosEventType[i] == 'id') {emosElSrc = elem.id;}

			if(emosElSrc == econda.emosEventName[i]) {
				if(econda.emosEventActionBefore[i] == "econdafunc") {
					try {
						window.econdafunc();
					}catch(e){
						if(econda.emosDebug == 1 || econda.emosDebug == 2) {
							econda.log("Econda Error", "function econdafunc not found");
						}
					}
				}
				if(econda.emosEventAction[i] == "clear") {
					econda.clear();
				}
				if(econda.emosEventAction[i] == "send") {
					econda.send();
				}
				if(econda.emosEventAction[i] == "ajaxProductQuantity") {
					econda.emosProductAjaxQuantity = elem.value;
				}
				if(econda.emosEventAction[i] == "ajaxProductSend") {
					econda.emosProductAjaxType = "c_add";
					if(econda.emosViewTypeInit == true){
						econda.marker(econda.viewtype);
					}
					var content = econda.getContent().replace(/\/\w+\s*\w+\//, "/Add to Cart/");
					econda.content(content);
					econda.pageId(content);
					econda.send();
				}
			}
		}
		return false;
	}
};
