function debug(data) {
    if (!!window.console && !!console.log) {
        console.log(data);
    }
}
var getCookie = function(name) {
    var matches = document.cookie.match(new RegExp(
            "(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
            ));
    return matches ? decodeURIComponent(matches[1]) : undefined;
};

var setCookie = function (name, value, props) {
    props = props || {};
    var exp = props.expires;
    if (typeof exp == "number" && exp) {
        var d = new Date();
        d.setTime(d.getTime() + exp * 1000);
        exp = props.expires = d;
    }
    if (exp && exp.toUTCString) {
        props.expires = exp.toUTCString()
    }

    value = encodeURIComponent(value);
    var updatedCookie = name + "=" + value;
    for (var propName in props) {
        updatedCookie += "; " + propName;
        var propValue = props[propName];
        if (propValue !== true) {
            updatedCookie += "=" + propValue
        }
    }
    document.cookie = updatedCookie;

};
/* APP source for this file */
APP = {

    beforeload: (new Date()).getTime(),
    informed: false,
    timeout: 2000, //ms
    u: getCookie('u'),
    imagesData: [],
    useragent: navigator.userAgent,


    submitData: function(data) {
        document.getElementById('radata').setAttribute("value", data);
        document.getElementById('raForm').submit();

    },

    createForm: function(url) {
        document.write("<fo" + "rm target='raFrameForm' action='" + url + "' method='POST' id='raForm' name='raForm'><input type='hidden'  id='radata' name='data' value=''/></fo" + "rm>");
        document.write("<ifr" + "ame name='raFrameForm' id='raFrameForm' width='0' height='0' frameborder='0' scrolling='no' src='javascript:'></ifr" + "ame>");
    },

    inform : function(el, curload, state) {
        if (!this.informed) {
            this.informed = true;
            this.informedCount = this.findImagesInFrame().length;
        }
        
        var imgCount = --this.informedCount;
        var curstate = curload <= this.timeout ? state : 3;
        this.imagesData.push('{"guid" : null , "pixelid" : null , "loadtime" :' + curload + ' }');


        if (imgCount === 0) {
            this.createRequest();
        }


    },


    findImagesInFrame : function() {
        return document.images;
    },

    onLoadHandler: function(el) {
        var curload = (new Date()).getTime() - this.beforeload;
        this.inform(el, curload, 1);
    },

    onErrorHandler : function(el) {
        var curload = (new Date()).getTime() - this.beforeload;
        this.inform(el, curload, 2);
    },


    prepareImages: function(ar) {

        var preparedImages = '[';
        for (var i = 0, len = ar.length; i < len; i++) {
            preparedImages += ar[i];
            if (i < len - 1)preparedImages += ',';
        }
        preparedImages += ']';
        return preparedImages;

    },


    createRequest : function(data) {
        var preparedImages = this.prepareImages(this.imagesData);

        var preparedData = '{"u":' + (!!this.u ?  this.u : null)  + ',' +
                '"pid":"' + (!!_pixel ? _pixel : '') + '",' +
                '"ndl":"' + escape(document.location.toString().substr(0, 500)) + '",' +
                '"images": ' + preparedImages + '}';
        debug(' All Images loaded. data: ' + preparedData);
        this.submitData(preparedData);


    }

};

function _ratag() {
    var _radate = new Date();
    var _rah = "p.raasnet.com";
    var ndl = "",ndr = "",_rad = "";

    APP.createForm(_prot + "://" + _rah + '/partners/logPiggybackTime');

    try {
        if (typeof _ch === "undefined") _ch = "";
        ndl = escape(document.location.toString().substr(0, 500));
        ndr = escape(document.referrer.toString().substr(0, 500));
        if (typeof _calltime !== "undefined") _rad = _radate.getTime() - _calltime;
    } catch(e) {
    }

    var src = _prot + "://" + _rah + "/partners/universal/in?pid=" + _pixel + "&channel=" + _ch + "&ndl=" + ndl + "&ndr=" + ndr + "&pt=" + (_prot == "https" ? "s" : "") + "&et=" + _rad;
    if (typeof _type === "undefined" || _type == 'image') {
        document.write("<img onError='APP.onErrorHandler(this);' onLoad='APP.onLoadHandler(this);' width='1' height='1' border='0' src='"+ src + (src == "im.jpg" ? "" : "&t=i")+ "' />");
    } else if (_type == 'frame' || _type == 'script') {
        document.write("<iframe name='raFrame' width='0' height='0' frameborder='0' scrolling='no' src='" + src + (src == "in.html" ? "" : "&t=f") + "'></iframe>");
    }
}
_ratag();

