module("Fotobox.UI.Tracking", function() {
var paramsGlobal;
var paramsRequest1;
var paramsRequest2;
var paramsPage;
var tifPrefix;
var tifOptions;
var tifSect5;
var tifSect6;
var isSampleInSlices = false;
function setIsSampleInSlices() {
var slices = [50, 51, 52, 53, 54, 55, 56, 57, 58, 59];
var sample;
if (!/(^|;)\s*ns_sample=(\d+)/.exec(document.cookie)) {
sample = (Math.random() * 100) | 0;
/([^.]+\.[^.]+)$/.exec(location.hostname);
document.cookie =
"ns_sample=" + sample +
"; expires=" + new Date((new Date() * 1 + 63072000000)).toGMTString() +
"; path=/; domain=." + RegExp.$1 + ";";
} else {
sample = RegExp.$2 * 1;
}
var s = slices.length;
while (s--) {
if (slices[s] === sample) {
isSampleInSlices = true;
return;
}
}
isSampleInSlices = false;
}
function getSfaScreenres() {
return screen.width + "x" + screen.height;
}
function getSfaDate() {
var now = new Date();
var year = (now.getYear() + 1900).toString();
var month = (now.getMonth() + 1).toString();
var day = now.getDate().toString();
sfaDate = year +
(month.length == 2 ? month : 0 + month) +
(day.length == 2 ? day : 0 + month);
return sfaDate;
}
function sendRequest(pGlobal, pRequest, section, nsType) {
if (pGlobal && pRequest) {
(new Image()).src =
"//" + pGlobal.domain + "/" + pRequest.corp + "/" + pRequest.site + "/" +
"s?" + pRequest.prefix + "." + section +
"&wa_id=" + pGlobal.id +
"&wa_uas=" + pGlobal.uas +
"&wa_brand=" + pGlobal.brand +
"&wa_mts=" + (new Date() * 1) +
"&sfa_screenres=" + getSfaScreenres() +
"&sfa_date=" + getSfaDate() +
"&ns_type=" + nsType +
"&ns_c=" + (document.characterSet || document.defaultCharset) +
"&ns_ti=" + escape(document.title) +
"&ns_jspageurl=" + escape(location.href) +
"&ns_referrer=" + escape(document.referrer);
}
}
function track(section, nsType) {
sendRequest(paramsGlobal, paramsRequest1, section, nsType);
if (isSampleInSlices) {
sendRequest(paramsGlobal, paramsRequest2, section, nsType);
}
}
function setGlobal(domain, id, uas, brand) {
if (!paramsGlobal) {
paramsGlobal = {};
paramsGlobal.domain = domain;
paramsGlobal.id = id;
paramsGlobal.uas = uas;
paramsGlobal.brand = brand;
}
}
function setRequest1(corp, site, prefix) {
if (!paramsRequest1) {
paramsRequest1 = {};
paramsRequest1.corp = corp;
paramsRequest1.site = site;
paramsRequest1.prefix = prefix;
}
}
function setRequest2(corp, site, prefix) {
if (!paramsRequest2) {
paramsRequest2 = {};
paramsRequest2.corp = corp;
paramsRequest2.site = site;
paramsRequest2.prefix = prefix;
}
}
function trackPage(page) {
if (!paramsPage && page) {
paramsPage = page;
setIsSampleInSlices();
track("pi." + paramsPage + ".index", "view");
}
}
function trackEvent(event) {
if (paramsPage && event) {
track("event." + paramsPage + "." + event, "hidden");
}
}
function tifTrack(sect5, sect6) {
if (window.NSfTIF) {
NSfTIF.tifInit($.extend({}, tifOptions, {pageidentifier: tifPrefix + "/" + sect5 + "/" + sect6}));
}
}
function initTifTrack(brand, region, sect4, sect5, sect6) {
var custno = brand === "gmxde" && "2644" || "7529";
var url = "//uim.tifbs.net/js/" + custno + ".js";
tifPrefix = brand === "gmxde" && "freemail/mediacenter" || "freemail/smartdrive";
tifPrefix += "/photoalbum/" + sect4;
tifOptions = {region: region};
tifSect5 = sect5;
tifSect6 = sect6;
if (window.NSfTIF) {
tifTrackPage();
} else {
getScriptAndCache(url, tifTrackPage);
}
}
function tifTrackOpenDia() {
tifTrack("photo", "diashow");
}
function tifTrackPage() {
tifTrack(tifSect5, tifSect6);
}
function getScriptAndCache(url, callback){
$.ajax({
type: "GET",
url: url,
success: callback,
dataType: "script",
cache: true
});
};
return {
setGlobal : setGlobal,
setRequest1 : setRequest1,
setRequest2 : setRequest2,
trackPage : trackPage,
trackEvent : trackEvent,
initTifTrack : initTifTrack,
tifTrack : tifTrack,
tifTrackOpenDia : tifTrackOpenDia,
tifTrackCloseDia : tifTrackPage
};
}());

