function Theme() {
  this.pathPrefix = "";
  var itemMap = new Array();
  var properties = new Array();
  
  this.addItem = function(item) {
    itemMap[item.key] = item;
  }
  
  this.getItem = function(key, setKey) {
    if (setKey == null) {
      return itemMap[key];
    } else {
      return itemMap[setKey + "_" + key];
    }
  }
  
  this.getImagePath = function(key, setKey) {
    var item = this.getItem(key, setKey); 
    if(item == null) item = this.getItem(key, 0);
    if(item == undefined) return null;
    return this.pathPrefix + item.path + item.filename + item.extension;
  }
  
  this.setProperty = function(key, value) {
    properties[key] = value;
  }
  
  this.getProperty = function(key, defaultValue) {
    if(properties[key] == null) {
      return defaultValue;
    }
    return properties[key];
  }
  
  this.getPropertyInt = function(key, defaultValue) {
    if(properties[key] == null) {
      return defaultValue;
    }
    return parseInt(properties[key]);
  }
  
  this.getPropertyColor = function(key, defaultValue) {
    if(properties[key] == null) {
      return defaultValue;
    }
    return intToHexRGB(parseInt(properties[key]));
  }
}

function ThemeItem() {
  this.key;
  this.title;
  this.desc;
  this.width;
  this.height;
  this.path;
  this.filename;
}

function ThemeTypeEditVisu() {
    this.defaultExists = true;
    this.defaultKey = "edit_visu.default";
    this.defaultPath = "/users/bt1/themes/edit_visu/default/";

    this.userExists = false;
    this.userKey = "edit_visu.user";
    this.userPath = "/users/bt1/themes/edit_visu/";
}

function ThemeTypeHIC() {
    this.defaultKey = "hic.default";
    this.defaultPath = "/users/bt1/themes/hic/default/";

    this.userExists = true;
    this.userKey = "hic.user";
    this.userPath = "/users/bt1/themes/hic/";
}

function ThemeTypeInfo(defaultKey, defaultPath, userKey, userPath) {
    this.defaultKey = defaultKey;
    this.defaultPath = defaultPath;

    this.userKey = userKey;
    this.userPath = userPath;
}

var ThemeTypeEditVisu = new ThemeTypeInfo(
        "edit_visu.default", 
        "/users/bt1/themes/edit_visu/default/", 
        "edit_visu.user", 
        "/users/bt1/themes/edit_visu/");
var ThemeTypeHIC = new ThemeTypeInfo(
        "hic.default", 
        "/users/bt1/themes/hic/default/", 
        "hic.user", 
        "/users/bt1/themes/hic/");

function loadThemeIndex(themeIndexPath, theme, themeType, callback) {
  $.ajax({
    dataType:   'xml',
    url:        themeIndexPath,
    cache:      false,
    success:    function(data){
                  log.info("Data loaded");
  
                  if($($(data).find("themes").get(0)).attr("version") === "1.1") {
                        themeType.userExists = false;

                    var http_prefix = $($(data).find("themes").get(0)).attr("http_prefix");
                    $(data).find("theme").each(function () {
                        var key = $(this).attr("key");
                          if(key === themeType.userKey) {
                                themeType.userExists = true;
                                themeType.userPath = http_prefix + $(this).attr("path");
                            }
                          if(key === themeType.defaultKey) themeType.defaultPath = http_prefix + $(this).attr("path");
                      });
          }
                  
                    if(themeType.userExists) {
                      loadTheme(theme, themeType.userPath, themeType.defaultPath, callback);
                    } else {
                      loadTheme(theme, themeType.defaultPath, null, callback);
                    }
                },
  error:      function() {
              loadTheme(theme, themeType.userPath, themeType.defaultPath, callback);
            }
  });
}

function loadTheme(theme, path, fallbackPath, callback) {
  if(path == null) {
    if(fallbackPath == null) {
      alert("Unable to load an icon theme!");
      return;
    } else {
      loadTheme(theme, fallbackPath, null, callback);
    }
  }

  log.info("Requesting Theme: " + path);

  $.ajax({
    dataType: 'xml',
    url: path + "ThemeSettings.xml",
    cache: false,
    success: function(data){
        log.info("Data loaded");
        theme.pathPrefix = path;
        
        $(data).find("properties").children().each(function() {
          theme.setProperty($(this).get(0).tagName, $(this).text());
        });
          
        $(data).find("icon").each(function () {
          var element = this;
          if ($(this).parent().get(0).tagName == "iconset") {
            theme.addItem(loadThemeItem(element, 0));
            var sets = $(element).siblings("set");
            for(var i = 0; i < sets.length; i++) {
              theme.addItem(loadThemeItem(element, $(sets[i]).attr("key")));
            }
          } else {
            theme.addItem(loadThemeItem(element, null));
          }
        });
        
        if(callback != null) {
          callback();
        }
      },
    error: function() {
        loadTheme(theme, fallbackPath, null, callback);
      }
  });
}

function loadThemeItem(element, setKey) {
  var item = new ThemeItem();
  if (setKey == null) {
    item.key = $(element).attr("key");
    item.filename = $(element).find("filename").text();
    item.extension = $(element).find("extension").text();
  } else {
    item.key = setKey + "_" + $(element).attr("key");
    item.filename = setKey + "_" + $(element).find("filename").text();
    if(setKey == "0") {
      item.extension = $(element).find("extension").text();
    } else {
      item.extension = $(element).find("extension_"+setKey).text();
    }
  }
  item.title = $(element).find("title").text();
  item.desc = $(element).find("desc").text();
  item.width = $(element).find("width").text();
  item.height = $(element).find("height").text();
  item.path = $(element).find("path").text();
  return item;
}
