// JavaScript File
// Fix concole problems
if(typeof console === "undefined") console = { log: function() { } };

jQuery(document).ready(function($){
	
	console.log("Hello.");
	sayHello();
	setInterval('sayHello()', 15000);

});

	var hello = {};

	function sayHello(){
		var currentUrl = escape(location.pathname);
		$.getJSON('/app/hello.asp?callback=?', { 'session_id':readCookie('session_id'), 'key_id':readCookie('key_id'), 'visitor_id':readCookie('visitor_id'), 'domain':escape(location.hostname), 'url':currentUrl, 'title':document.title }, function(data){
			console.log(data.session_id + ' ... ' + data.key_id);
			if(!readCookie('session_id')){
				console.log('Saving cookies...');
				writeCookie('session_id',data.session_id);
				writeCookie('key_id',data.key_id);
				writeCookie('visitor_id',data.visitor_id, 2160);
			}
		});	
	}

// writeCookie("myCookie", "my name", 24);
// Stores the string "my name" in the cookie "myCookie" which expires after 24 hours.
function writeCookie(name, value, hours)
{
  var expire = "";
  if(hours != null)
  {
    expire = new Date((new Date()).getTime() + hours * 3600000);
    expire = "; expires=" + expire.toGMTString();
  }
  document.cookie = name + "=" + escape(value) + expire;
}
// Example:
// alert( readCookie("myCookie") );
function readCookie(name)
{
  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0)
  { 
    offset = document.cookie.indexOf(search);
    if (offset != -1)
    { 
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  return cookieValue;
}
