function Doc(id, opts){
  this.id = id;
  this.opts = opts;
  this.fields = new Array();
  this.fieldNames = new Array();
  this.permission = null;
  this.publishDate = null;
  this.name = null;
  this.numComments = null;
  this.typeId = null;
  this.lastModified = null;
  this.title = null;
  this.path = null;
  this.documentId = null;
  this.numRatings = null;
  this.createDate = null;
  this.uri = null;
  this.row = null;
}

Doc.prototype.loadByMap = function(map){

	this.permission = map.permission;
	this.publishDate = map.publishDate;
	this.name = map.name;
	this.numComments = map.numComments;
	this.typeId = map.typeId;
	this.lastModified = map.lastModified;
	this.title = map.title;
	this.path = map.path;
	this.documentId = map.documentId;
	this.numRatings = map.numRatings;
	this.createDate = map.createDate;
	this.uri = map.uri;
	this.row = map.row;

}

Doc.prototype.addField = function(name, value){
  this.fields[name] = value;
  this.fieldNames.push(name);
}

Doc.prototype.getValueOfField = function(name){
  return this.fields[name];
}

Doc.prototype.toString = function(){
  var str = ""
  + "permission = " + this.permission + "\n"
  + "publishDate = " + this.publishDate + "\n"
  + "name = " + this.name + "\n"
  + "numComments = " + this.numComments + "\n"
  + "typeId = " + this.typeId + "\n"
  + "lastModified = " + this.lastModified + "\n"
  + "title = " + this.title + "\n"
  + "path = " + this.path + "\n"
  + "documentId = " + this.documentId + "\n"
  + "numRatings = " + this.numRatings + "\n"
  + "createDate = " + this.createDate + "\n"
  + "uri = " + this.uri + "\n"
  + "row = " + this.row + "\n";
  
  str += "\n--fields--\n";
  for(var i = 0; i < this.fieldNames.length; ++i){
    str += this.fieldNames[i] + " = " + this.fields[this.fieldNames[i]] + "\n";
  }
  
  return str;
}

var docList = new Array();
var docListIndex = 0;
function swapImage(index) {
    // update background image by fading it out and into the next
    var imgPath = docList[index].getValueOfField("backgroundImageFilePath");
    if(imgPath){
        $("#body").css("background-image", "url('"+imgPath+"')");
    }
}
function getCookie(name) {
    var key = name + '=';
    var cookies = document.cookie.split(';');
    for (var i = 0; i < cookies.length; i++) {
        var c = cookies[i];
        c = $.trim(c);
        if (c.indexOf(key) == 0) {
            return c.substring(key.length, c.length);
        }
    }
    return null;
}

function putCookie(name, value) {
    var exp = new Date();
    exp.setDate(exp.getDate() + 4*365);
    document.cookie = name+'='+value+'; expires='+exp.toGMTString()+'; path=/;';
}

$(window).load(function() {
    var c = getCookie('backgroundVisits');
    var index = -1;

    if (c != null) {
        var fileList = c.split(',');
        for (var i = 0; i < docList.length; i++) {
            var found = 0;
            for (var j = 0; j < fileList.length; j++) {
                if (fileList[j] == docList[i].documentId) {
                    found = 1;
                    break;
                }
            }
            if (found == 0) {
                index = i;
                break;
            }
        }

        if (index == -1) {
            putCookie('backgroundVisits', '');
            c = '';
            index = 0;
        }
    }
    else {
        c = '';
        index = 0;
    }

    putCookie('backgroundVisits', c+','+docList[index].documentId);

    swapImage(index);
});

