/* Vendemore Javascript */

//

function Vendemore() {

    var version = '1.0'

    var BASE_URL = 'ab.vendemore.com/footprint-web';
    var protocol = (("https:" == document.location.protocol) ? "https://" : "http://");
    var COOKIE_NAME = 'vlmref';


    this.init = init;

    // Function called when page loads
    // Collects data and calls method to send ajax request
    function init() {

        var r = encodeURIComponent(document.referrer);
        var l = encodeURIComponent(document.location);
        var t = encodeURIComponent(jQuery.trim(document.title));
        var u = encodeURIComponent(document.url);
        var domain = encodeURIComponent(document.domain);
        var path = encodeURIComponent(location.pathname);
        var hostname = encodeURIComponent(location.hostname);
        var href = encodeURIComponent(location.href);

        // read existing cookie
        var c = getCookie(COOKIE_NAME);

        // Create Hash of parameters
        var params = { 'cookie': c,
            'url': u,
            'path': path,
            'title': t,
            'domain': domain,
            'referrer': r
        };

        sendMessage(params);

    }

    //Function to send ajax request
    // params- prototype Hash of request parameters
    function sendMessage(params) {

        jQuery.getJSON(protocol + BASE_URL + '/alpha?' + jQuery.param(params) + '&callback=?',
				function (response) {
				    // Setting cookie
				    setCookie(COOKIE_NAME, response.cookieId, 365);
				    // Landing page
				    handleLandingPage(response.landingPage);
				});

    }

    function handleLandingPage(landingPage) {
        if (landingPage) {
            if (landingPage.element && landingPage.html) {
                if ($("#" + landingPage.element)) {
                    $("#" + landingPage.element).html(landingPage.html);
                    return;
                }
            }
        }
    }


    //path & domain???
    //Function to set cookie
    // c_name - cookie name
    // c_value - cookie value
    // expiredays - days until cookie expiration
    function setCookie(c_name, value, expiredays) {
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + expiredays);
        document.cookie = c_name
				+ '='
				+ escape(value)
				+ ((expiredays == null) ? '' : ';expires='
						+ exdate.toGMTString());
    }

    //Function to get cookie by name
    function getCookie(c_name) {
        if (document.cookie.length > 0) {
            c_start = document.cookie.indexOf(c_name + '=');
            if (c_start != -1) {
                c_start = c_start + c_name.length + 1;
                c_end = document.cookie.indexOf(';', c_start);
                if (c_end == -1)
                    c_end = document.cookie.length;
                return unescape(document.cookie.substring(c_start, c_end));
            }
        }
        return '';
    }

}

//Run Vendemore.init() onload
$(document).ready(function () {
    new Vendemore().init();
});


