// INTERNAL IMPLEMENTATION
var gCoreMetrixSettings = {
		testSentEvents: [],
		offerTagTimeout: null,
		offerTagTimeoutIsOn: false,
		attrTemplate: 'attr01-_-attr02-_-attr03-_-attr04-_-attr05-_-attr06-_-attr07-_-attr08-_-attr09-_-attr10-_-attr11-_-attr12-_-attr13-_-attr14-_-attr15-_-attr16-_-attr17-_-attr18-_-attr19-_-attr20',
		prodCategories: [],
		phoneFiltersGrpMap: [],
		phoneFiltersOfferSkipFirst: true
};
//testing
// gCoreMetrixSettings.testSentEvents.length
// gCoreMetrixSettings.testSentEvents[gCoreMetrixSettings.testSentEvents.length-1]

gCoreMetrixSettings.prodCategories['indyw_abonament_akwizycja'] = '434';
gCoreMetrixSettings.prodCategories['indyw_abonament_mnp'] = '435';
gCoreMetrixSettings.prodCategories['indyw_abonament_upsell'] = '436';
gCoreMetrixSettings.prodCategories['indyw_internet_akwizycja'] = '437';
gCoreMetrixSettings.prodCategories['indyw_internet_upsell'] = '438';
gCoreMetrixSettings.prodCategories['indyw_mix_akwizycja'] = '442';
gCoreMetrixSettings.prodCategories['indyw_mix_mnp'] = '443';
gCoreMetrixSettings.prodCategories['indyw_stacjonarny_akwizycja'] = '444';
gCoreMetrixSettings.prodCategories['biznes_abonament_akwizycja'] = '134';
gCoreMetrixSettings.prodCategories['biznes_abonament_mnp'] = '135';
gCoreMetrixSettings.prodCategories['biznes_abonament_retencja'] = '136';
gCoreMetrixSettings.prodCategories['biznes_abonament_upsell'] = '137';
gCoreMetrixSettings.prodCategories['biznes_internet_akwizycja'] = '138';
gCoreMetrixSettings.prodCategories['biznes_internet_upsell'] = '139';

gCoreMetrixSettings.phoneFiltersGrpMap['Promotions'] = 'Promocje';
gCoreMetrixSettings.phoneFiltersGrpMap['MANUFACTURER'] = 'Producent';
gCoreMetrixSettings.phoneFiltersGrpMap['KEYPAD_S'] = 'Rodzaje_obud';
gCoreMetrixSettings.phoneFiltersGrpMap['OS_S'] = 'Syst_oper';
gCoreMetrixSettings.phoneFiltersGrpMap['MMEDIA_S'] = 'Multimedia';
gCoreMetrixSettings.phoneFiltersGrpMap['DATA_S'] = 'Transmisja';

function getCoreMetrixPredefinedCatKey() {//can be overriden
	try {
		return (gMassUriRequest ? "indyw" : "biznes") + "_" + getTarrifType1() + "_" + getTarrifType2();
	} catch (e) {}
	return '';
}

function getCoreMetrixPageCatId() {
	return gCmPageCatId;//global var
}

function getCoreMetrixProductCatId() {
	var catId = gCoreMetrixSettings.prodCategories[getCoreMetrixPredefinedCatKey()];
	if (!catId) catId = getCoreMetrixPageCatId();
	return catId;
}

function getCoreMetrixDeviceType() {//can be overriden
	return 'inne';
}

function getCoreMetrixPhoneAttrs() {
	var attrs = gCoreMetrixSettings.attrTemplate;
	attrs = attrs.replace(/attr01/, 'eshop');
	attrs = attrs.replace(/attr02/, 'urzadzenie');
	attrs = attrs.replace(/attr03/, getPhoneProperty('manufacturer'));
	attrs = attrs.replace(/attr04/, getCoreMetrixDeviceType());
	return attrs.replace(/attr\d\d/g, '');
}

function getTarrifType1() {
	return 'inne';
}

function getTarrifType2() {
	var offerId = ''+getOfferId();
	var offerIdStr = 'akwizycja';
	if (offerId.indexOf('_mnp') > 0) {
		offerIdStr = 'mnp';
	} else if (offerId.indexOf('_ups') > 0) {
		offerIdStr = 'upsell';
	} else if (offerId.indexOf('_ret') > 0) {
		offerIdStr = 'retencja';
	}
	return offerIdStr;
}

function getTarrifType3() {
	return 'voice';
}

function getCoreMetrixOfferAttrs() {
	var attrs = gCoreMetrixSettings.attrTemplate;
	attrs = attrs.replace(/attr01/, 'eshop');
	attrs = attrs.replace(/attr02/, 'taryfa');
	attrs = attrs.replace(/attr11/, getTarrifType1());
	attrs = attrs.replace(/attr12/, getTarrifType2());
	attrs = attrs.replace(/attr13/, getTarrifType3());
	var phoneOffer = ''+isPhoneOffer();
	attrs = attrs.replace(/attr14/, phoneOffer == 'true' || phoneOffer == 'checked' ? 'z urzadzeniem' : 'bez urzadzenia');
	var group = getOfferTypeId();
	if (group) {
		attrs = attrs.replace(/attr15/, group);
	}
	var promoid = getPromoId();
	if (promoid) {
		attrs = attrs.replace(/attr16/, promoid);
	}
	return attrs.replace(/attr\d\d/g, '');
}

function coreMetrixGetProductId() {
	var rateId = prepareRateId();
	var phoneOffer = ''+isPhoneOffer();
	var isPhoneOfferBool = (phoneOffer == 'true' || phoneOffer == 'checked');
	if (!isPhoneOfferBool) {
		var nodes = dict.getNodes("RATE_PLAN", "[OFFER_ID='"+getOfferId()+"'][OFFER_TYPE_ID='"+getOfferTypeId()+"'][RATE_PLAN='"+rateId+"']");
		$(nodes).each(function() {
			var npl = $(this).attr("NO_PHONE_LINK");
			if (npl) rateId = npl;
		});
	}
	return rateId;
}

function coreMetrixGetProductName() {
	return getOfferName();
}


////////////////////
// PUBLIC FUNCTIONS
function coreMetrixCreatePageviewTag() {
	var cm_pageID = document.location.host + document.location.pathname;
	var pageCatId = getCoreMetrixPageCatId();
	gCoreMetrixSettings.testSentEvents.push('cmCreatePageviewTag, '+cm_pageID+', '+pageCatId);
	if (pageCatId) {
		cmCreatePageviewTag(cm_pageID, pageCatId); //funkcja zewn.
	}
}

function coreMetrixCreatePhoneTag() {
	var phoneId = currantPhoneId;//global var
	var phoneName = phoneId;//currentPhoneName;//global var
	var prodCatId = getCoreMetrixProductCatId();
	var attrs = getCoreMetrixPhoneAttrs();
	gCoreMetrixSettings.testSentEvents.push('cmCreateProductviewTag, '+phoneId+', '+phoneName+', '+prodCatId+', '+attrs);
	if (prodCatId) {
		cmCreateProductviewTag(phoneId, phoneName, prodCatId, attrs);
	}
}

function coreMetrixCreateOfferTag(delay) {
	delay = delay ? delay : 3000;
	if (gCoreMetrixSettings.offerTagTimeoutIsOn) {
		clearTimeout(gCoreMetrixSettings.offerTagTimeout);
	}
	gCoreMetrixSettings.offerTagTimeout = setTimeout("doCoreMetrixCreateOfferTag();",delay);
	gCoreMetrixSettings.offerTagTimeoutIsOn = true;
}

function doCoreMetrixCreateOfferTag() {
	gCoreMetrixSettings.offerTagTimeoutIsOn = false;
	var prodCatId = getCoreMetrixProductCatId();
	var attrs = getCoreMetrixOfferAttrs();
	var prodId = coreMetrixGetProductId();
	var prodName = prodId;//coreMetrixGetProductName();
	gCoreMetrixSettings.testSentEvents.push('cmCreateProductviewTag, '+prodId+', '+prodName+', '+prodCatId+', '+attrs);
	if (prodCatId) {
		cmCreateProductviewTag(prodId, prodName, prodCatId, attrs);
	}
}

function coreMetrixCreateConversionEventTag(step) {
	var title = gMassUriRequest ? "Formularz kontaktowy B2C" : "Formularz kontaktowy B2B";
	gCoreMetrixSettings.testSentEvents.push('cmCreateConversionEventTag, '+title+', '+step);
	if (step == "1") 
		cmCreateConversionEventTag(title, step, "Formularze kontaktowe");
	else
		cmCreateConversionEventTag(title, step, "Formularze kontaktowe", "100");
}

function coreMetrixCreateSpecialOfferTag(id) {
	if (id == 'sme') {
		var pid = 'sme';
		var title = 'Oferta dla srednich i duzych firm';
		var catId = getCoreMetrixProductCatId();
		var attrs = gCoreMetrixSettings.attrTemplate.replace(/attr01/, 'eshop').replace(/attr02/, 'taryfa').replace(/attr\d\d/g, '');
		gCoreMetrixSettings.testSentEvents.push('cmCreateProductviewTag, '+pid+', '+title+', '+catId+', '+attrs);
		if (catId) {
			cmCreateProductviewTag(pid, title, catId, attrs);
		}
	} else if (id == 'telemetryczna') {
		var pid = 'telemetryczna';
		var title = 'Taryfa Telemetryczna';
		var catId = getCoreMetrixProductCatId();
		var attrs = gCoreMetrixSettings.attrTemplate.replace(/attr01/, 'eshop').replace(/attr02/, 'taryfa').replace(/attr\d\d/g, '');
		gCoreMetrixSettings.testSentEvents.push('cmCreateProductviewTag, '+pid+', '+title+', '+catId+', '+attrs);
		if (catId) {
			cmCreateProductviewTag(pid, title, catId, attrs);
		}
	} else {
		var catId = getCoreMetrixProductCatId();
		if (catId == '439' || catId == '440') {
			var offer = $('ul.choosenSet li #opcja').text();
			if (offer) offer = $.trim(offer);
			if (offer) {
				var option = $('ul.choosenSet li .wartosc').text();
				if (option) option = $.trim(option);
				var pid = option ? offer + ' ' + option : offer;
				var title = pid;
				tt2 = 'akwizycja';
				var tt3 = 'voice';
				if (catId == '439') {
					tt3 = 'data';
				}
				var attrs = gCoreMetrixSettings.attrTemplate.replace(/attr01/, 'eshop').replace(/attr02/, 'taryfa')
					.replace(/attr11/, 'karta').replace(/attr12/, tt2).replace(/attr13/, tt3).replace(/attr\d\d/g, '');
				gCoreMetrixSettings.testSentEvents.push('cmCreateProductviewTag, '+pid+', '+title+', '+catId+', '+attrs);
				cmCreateProductviewTag(pid, title, catId, attrs);
			}
		} else if (catId == '441') {
			var pid = 'Karta_MNP';
			var title = pid;
			var attrs = gCoreMetrixSettings.attrTemplate.replace(/attr01/, 'eshop').replace(/attr02/, 'taryfa')
				.replace(/attr11/, 'karta').replace(/attr12/, 'mnp').replace(/attr13/, 'voice').replace(/attr\d\d/g, '');
			gCoreMetrixSettings.testSentEvents.push('cmCreateProductviewTag, '+pid+', '+title+', '+catId+', '+attrs);
			cmCreateProductviewTag(pid, title, catId, attrs);
		}
	}
}

function coreMetrixCreatePhoneFiltersElementTag(itemName, groupId) {
	if (gCoreMetrixSettings.phoneFiltersOfferSkipFirst && groupId == 'Oferta') {
		// na poczatku jest zawsze jakies radio oferty zaznaczone, monituj od 2-go zdarzenia
		gCoreMetrixSettings.phoneFiltersOfferSkipFirst = false;
		return;
	}
	groupId = groupId && gCoreMetrixSettings.phoneFiltersGrpMap[groupId] ? gCoreMetrixSettings.phoneFiltersGrpMap[groupId] : groupId;
	var elemName = (groupId ? groupId + '_' : '') + itemName;
	var elemCat = gCurrentUri.indexOf('/telefony/Telefony') >= 0 ? 'Portal_filtry_telefony' : 'Portal_filtry_urzadzenia';
	gCoreMetrixSettings.testSentEvents.push('cmCreateElementTag, '+elemName+', '+elemCat);
	if (itemName) {
		cmCreateElementTag(elemName, elemCat);
	}
}

