/*Author Stewart Leach
*		stewart.leach@infomentum.co.uk
*
* Javascript for tracking 'most popular' content.
* Requires cookies to be enabled.
*
*/

//to allow for degrading of firebug in non-firefox browsers	
try { console.assert(1); } catch(e) { console = { log: function() {}, assert: function() {} } }

//onload handled on template page so IDOC vars can be passed to the logFAQ function.
jQuery().unload(pageClosed);

var Cookie = { 
    create: function(name, value, days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            var expires = "; expires=" + date.toGMTString();
        }
        else var expires = "";
        document.cookie = name + "=" + value + expires + "; path=/";
    },

    read: function(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;
    },

    erase: function(name) {
        this.create(name, "", -1);
    }
}

function logFAQ(webroot,url,contentId,title){	
var timestampString = Cookie.read('CRUKDate');
    if (timestampString) {
        var timestamp = new Date(timestampString);
        var temp = new Date();
        temp.setMinutes(temp.getMinutes(), temp.getSeconds() - 30);
				//console.log("timestamp:%s, temp:%s",timestamp,temp);
				var cookieId = Cookie.read('CRUKId');
        // If this is true, the load is after a set time period from the last reload
        if (timestamp < temp || contentId != cookieId) {
	        //console.log("time difference sufficient, in main.");
					Cookie.create('CRUKDate', setNowDate());
					Cookie.create('CRUKId',contentId);
					logFAQAjax(webroot,url,contentId,title);
        }else{
        	//console.log("time difference not sufficient, no action taken");
        	//console.log("cookie timestamp:%s, temp current:%s",timestamp,temp);
        }
    }
    else{
	    Cookie.create('CRUKDate', setNowDate());
	    var checkCookiesEnabled = Cookie.read('CRUKDate');
	    if (checkCookiesEnabled)
	    	logFAQAjax(webroot,url,contentId,title);
  	}
}

function logFAQAjax(webroot,url,contentId,title){
	jQuery.ajax({
    url: webroot + 'idcplg?IdcService=CRUK_FAQ_LOG&url='+ url +'&contentId='+ contentId +'&title='+ title + '&IsJava=1',
    type: 'GET',
    dataType: 'html',
    timeout: 1000,
    error: function(){
			//console.log("unable to add faq");
    },
    success: function(xml){
			//console.log("faq successfully logged : [%s], [%s], [%s]", url, contentId, title);
    }
	});
}

function setNowDate(){
 var temp = new Date();
 temp.setMinutes(temp.getMinutes(), temp.getSeconds());
 return temp;
}

function pageClosed() {
		//console.log("page closed");
    Cookie.create('CRUKDate', new Date());
}

function preventLogGeneration(classname){
 $('ul.' + classname + ' li a')
 .each(function()
   { 
      var temp = this.href;
      temp += '&log=0';
      this.href = temp;
   });	
}

