// location of url with topic
var URL_LOCATION = "geturls.aspx";
var CONTAINER_ELEMENT_ID = "#history_check";
var ANCHOR_NORMAL_COLOR = "rgb(255, 255, 254)";
var ANCHOR_VISITED_COLOR = "rgb(255, 255, 255)";
var ANCHOR_VISITED_COLOR_HEX = "#ffffff";

function getHistoryTopic() {
    // get list of history
    $.ajax(
	{
	    url: URL_LOCATION,
	    dataType: "json",
	    success: function(histories) {
	        var container = $(CONTAINER_ELEMENT_ID);
	        var visited_result = {};
	        var visited_priority = {};

	        for (var i in histories) {
	            var history = histories[i];

	            // inject the element into div
	            var element = $("<a>Link</a>");
	            element.attr("href", history.url);

	            // append, test and remove the element
	            container.append(element);
	            var color = element.css("color");
	            element.remove();
	            	            
	            // check if user visited the link
                if (color == ANCHOR_VISITED_COLOR || color == ANCHOR_VISITED_COLOR_HEX) {

                var topic = history.topic;

                // if topic is not defined
                if (visited_result[topic] == null) {
                    // make a new one
                    visited_result[topic] = 1;
                    visited_priority[topic] = history.priority;
                }
                else {
                    // otherwise add count
                    visited_result[topic]++;
                }
	            }
	        }

	        var result = [];
	        var totalCount = 0;

	        // put back all result into array of object
	        for (var i in visited_result) {
	            var obj =
				{
				    topic: i,
				    count: visited_result[i],
				    priority: visited_priority[i],
				    // small trick to make join works...
				    toString: function() {
				    return "<topic id=\"" + this.topic + "\">" + ((this.count / totalCount) * 100).toFixed(0) + "</topic>";
				    }
				};

	            result.push(obj);
	        }

	        // sort ascending
	        result.sort(function(a, b) {
	            var count = a.count - b.count;

	            if (count == 0) {
	                return a.priority - b.priority;
	            }

	            return count;
	        });


	        if (result.length > 5) {
	            result = result.slice(result.length - 5);
	        }

	        for (var i in result) {
	            totalCount += result[i].count;
	        }
	        // send back to Flash the order of the topic from smallest to largest
	        if (result.length == 0) {
	            sendHistoryTopicResponse("<topic id=\"4\">100</topic>");
	        }
	        else {
	            sendHistoryTopicResponse(result.join(""));
	        }
	    }
	});
}

function sendHistoryTopicResponse(response) {
    response = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><visited>" + response + "</visited>";
    //alert(response);
    golfwagon.historyTopicResponse(response);
}

function getRandomnumber(num) {
    var randomNum = Math.floor(Math.random() * 5);
    var myTopic = ["5", "2", "4", "9", "10", "11"];

    if (myTopic[parseInt(randomNum)] == num) {
        if (myTopic.length == parseInt(randomNum))
            return myTopic[parseInt(randomNum) - 1];
        else
            return myTopic[parseInt(randomNum) + 1];
    }
    else
        return myTopic[parseInt(randomNum)];
}
