var YA_COOKIE_PLATFORM = "ya_web_platform";
function ya_createCookie(name,value, hours) {
var date = new Date();
date.setTime(date.getTime()+(hours*60*60*1000));
var expires = "; expires="+date.toGMTString();
document.cookie = name+"="+value+expires+"; path=/";
}
function ya_readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0)
return c.substring(nameEQ.length,c.length);
}
return null;
}
var YOUAPPI_PREFIX = "youappi";
var YA_COOKIE_USER_TOKEN = "ya_user_token";
var yaWidgets = [];
var yaTimerID = 0;
var yaUserStatus = 0;
var youAPPiWidget = {
baseURL: "//track.56txs4.com",
widgetToken: "3779956d-e8c2-4521-8360-96f55e5b9f3f",
publisherToken: "",
userToken: "",
templateToken: "",
styleToken: "",
placementsCount: 0,
width: "300",
height: "300",
creativeSize: "",
containerID: YOUAPPI_PREFIX + "_widget_container",
overridePlatform: "",
extraParams: "",
subid: "",
subid2: "",
subid3: "",
subid4: "",
subid5: "",
create: create
}
if (typeof jQuery == 'undefined') {
document.write("");
if (typeof $ != 'undefined') {
document.write("");
}
}
function create() {
createWidget();
if (yaTimerID == 0) {
yaTimerID = setInterval("checkVisible()", 1000 * 1);
}
}
function createWidget() {
var url = youAPPiWidget.baseURL + "/widget/generate/?widgettoken=" + youAPPiWidget.widgetToken;
figureUserToken();
url = addIfNotEmpty(url, "publishertoken", youAPPiWidget.publisherToken);
url = addIfNotEmpty(url, "usertoken", youAPPiWidget.userToken);
url = addIfNotEmpty(url, "templatetoken", youAPPiWidget.templateToken);
url = addIfNotEmpty(url, "styletoken", youAPPiWidget.styleToken);
// Check if user has a platform cookie
if (youAPPiWidget.overridePlatform.length == 0) {
var cookiePlatform = ya_readCookie(YA_COOKIE_PLATFORM);
if (cookiePlatform != null && cookiePlatform.length > 0) {
youAPPiWidget.overridePlatform = cookiePlatform;
}
}
url = addIfNotEmpty(url, "overrideplatform", youAPPiWidget.overridePlatform);
url += "&userStatus=" + yaUserStatus;
if (youAPPiWidget.extraParams.length > 0)
url += "&" + youAPPiWidget.extraParams;
if (youAPPiWidget.placementsCount > 0)
url += "&placementscount=" + youAPPiWidget.placementsCount;
url = addIfNotEmpty(url, "creativeSize", youAPPiWidget.creativeSize);
// Sub IDs
url = addIfNotEmpty(url, "subid", youAPPiWidget.subid);
url = addIfNotEmpty(url, "subid2", youAPPiWidget.subid2);
url = addIfNotEmpty(url, "subid3", youAPPiWidget.subid3);
url = addIfNotEmpty(url, "subid4", youAPPiWidget.subid4);
url = addIfNotEmpty(url, "subid5", youAPPiWidget.subid5);
// Timestamp
url += "&ts=" + new Date().getTime();
// Extra params
url += "&" + "widgetToken=3779956d-e8c2-4521-8360-96f55e5b9f3f&webSiteURL=http%3A%2F%2Fsmartstore.youappi.com%2Fresources%2Flinks%2Fshow_widget.jsp%3FwidgetToken%3Dnut_aw_a33a00L2Vd8v7Iu4WucIIbhJ2H11r5643&webSiteName=More apps >&showButton=yes&showRating=yes";
// Width stuff
if (youAPPiWidget.width == 'auto') {
youAPPiWidget.width = jQuery(window).width();
}
var html = "";
jQuery(getWidgetContainerID()).html(html);
yaWidgets.push(getWidgetContainerID());
}
function figureUserToken() {
// Publisher passed
yaUserStatus = 0;
var cookieUT = ya_readCookie(YA_COOKIE_USER_TOKEN);
if (cookieUT != null && cookieUT.length >0) {
// Found in cookie
yaUserStatus = 1;
if (youAPPiWidget.userToken.length == 0) {
youAPPiWidget.userToken = cookieUT;
} else {
// Conflict?
if (youAPPiWidget.userToken != cookieUT)
yaUserStatus = 2;
}
}
if (youAPPiWidget.userToken.length == 0) {
youAPPiWidget.userToken = new Date().getTime() + "_" + Math.floor(Math.random()* 100000000);
// Generated
yaUserStatus = 3;
}
ya_createCookie(YA_COOKIE_USER_TOKEN, youAPPiWidget.userToken, 24 * 365 * 40);
}
function addIfNotEmpty(url, paramName, paramValue) {
if (paramValue.length > 0)
url += "&" + paramName + "=" + paramValue;
return url;
}
function getWidgetContainerID() {
return "#" + youAPPiWidget.containerID;
}
function checkVisible() {
for (i = 0; i < yaWidgets.length; i++) {
var currentID = yaWidgets[i];
if (isElementVisible(jQuery(currentID))) {
signalVisible(currentID);
}
}
}
function signalVisible(currentID) {
var win = jQuery(currentID + " iframe")[0].contentWindow;
win.postMessage(YOUAPPI_PREFIX + "_impression", "*");
}
function isElementVisible($element) {
var winTop = jQuery(window).scrollTop();
var winBottom = winTop + window.innerHeight;
var elemTop = $element.offset().top;
var elemBottom = elemTop + $element.height();
if ((elemTop >= winTop) && (elemTop <= winBottom))
return true;
if ((elemBottom >= winTop) && (elemBottom <= winBottom))
return true;
// Check middle
if ((elemTop < winTop) && (elemBottom > winBottom))
return true;
return false;
}