if (!window.ccs.voting) { ccs.voting = {}; // package declaration } ccs.Voting = function() { var self; // constructor function function constructorFn() { self = this; } // init function called once document is loaded constructorFn.prototype.init = function() { } //makes an ajax request to load the html for setting up the UGC constructorFn.prototype.doVote = function (direction, objectId, callback) { if (isCookiesEnabled()) { new Ajax.Request(ccs.contextPath + '/json/' + ccs.appName + '/en/content/ugc_content/vote.json?objectid=' + objectId + '&direction=' + direction, { method:'get', onSuccess: function(transport) { var response = transport.responseText || "no response text"; var result = eval('(' + response + ')'); if (result[0].result == "true") { self.setVoteCookie(objectId); callback(); } }, onFailure: function() { //alert('Something went wrong...') } }); } } /** * We can have a set number cookies at a time. This stops them voting the same object more than once, we are using one cookie * to store all the values, so every time it is set, it resets the expiry to 24 hours. Therefor this LIFO queue will * free the oldest item. In theory then, the user could come back and vote multiple times, after voting for other stuff. */ constructorFn.prototype.setVoteCookie = function(objectId) { var listLength = 60; var voteCookie = getCookie("vids"); if (voteCookie == null) { voteCookie = objectId; } else { var voteList = voteCookie.split("X"); if (listLength <= voteList.length) { //We need to knock one off voteList[listLength] = objectId; voteCookie = ""; for (var i=1; i < voteList.length; i++) { voteCookie += voteList[i]; if (i!=voteList.length) { voteCookie += "X"; } } } else { voteCookie+="X" + objectId; } } var expire = new Date(); var fiveD = 1 * 24 * 60 * 60 * 1000; var dateBuffy = expire.getTime() + fiveD; expire.setTime(dateBuffy); setCookie("vids", voteCookie, expire, ccs.contextPath); } return new constructorFn(); } ccs.voting = new ccs.Voting()