var imgWindow = "";

function openPopup(img, width, height) {
   if (img && width && height) {
      width = Math.min(width + 36, 640);
      height = Math.min(height + 30, 480);
      if (imgWindow.location && !imgWindow.closed)
         imgWindow.close();
      imgWindow = window.open(img, "imgWindow" + width + height, "toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,width=" + width + ",height=" + height);
      // imgWindow.focus();
   }
}
Antville = {};
Antville.prefix = "Antville_";

Antville.pixel = new Image();
Antville.pixel.src = "http://static.twoday.net/pixel.gif";

Antville.colors = {"aliceblue": true, "antiquewhite": true, "aqua": true,  "aquamarine": true, "azure": true, "beige": true, "bisque": true,  "black": true, "blanchedalmond": true, "blue": true, "blueviolet": true,  "brown": true, "burlywood": true, "cadetblue": true, "chartreuse": true,  "chocolate": true, "coral": true, "cornflowerblue": true,  "cornsilk": true, "crimson": true, "cyan": true, "darkblue": true,  "darkcyan": true, "darkgoldenrod": true, "darkgray": true,  "darkgreen": true, "darkkhaki": true, "darkmagenta": true,  "darkolivegreen": true, "darkorange": true, "darkorchid": true,  "darkred": true, "darksalmon": true, "darkseagreen": true,  "darkslateblue": true, "darkslategray": true, "darkturquoise": true,  "darkviolet": true, "deeppink": true, "deepskyblue": true, "dimgray": true,  "dodgerblue": true, "firebrick": true, "floralwhite": true, "forestgreen": true, "fuchsia": true, "gainsboro": true, "ghostwhite": true, "gold": true, "goldenrod": true, "gray": true, "green": true, "greenyellow": true, "honeydew": true, "hotpink": true, "indianred ": true, "indigo ": true, "ivory": true, "khaki": true, "lavender": true, "lavenderblush": true, "lawngreen": true, "lemonchiffon": true, "lightblue": true, "lightcoral": true, "lightcyan": true, "lightgoldenrodyellow": true, "lightgrey": true, "lightgreen": true, "lightpink": true, "lightsalmon": true, "lightseagreen": true, "lightskyblue": true, "lightslateblue": true, "lightslategray": true, "lightsteelblue": true, "lightyellow": true, "lime": true, "limegreen": true, "linen": true, "magenta": true, "maroon": true, "mediumaquamarine": true, "mediumblue": true, "mediumorchid": true, "mediumpurple": true, "mediumseagreen": true, "mediumslateblue": true, "mediumspringgreen": true, "mediumturquoise": true, "mediumvioletred": true, "midnightblue": true, "mintcream": true, "mistyrose": true, "moccasin": true, "navajowhite": true, "navy": true, "oldlace": true, "olive": true, "olivedrab": true, "orange": true, "orangered": true, "orchid": true, "palegoldenrod": true, "palegreen": true, "paleturquoise": true, "palevioletred": true, "papayawhip": true, "peachpuff": true, "peru": true, "pink": true, "plum": true, "powderblue": true, "purple": true, "red": true, "rosybrown": true, "royalblue": true, "saddlebrown": true, "salmon": true, "sandybrown": true, "seagreen": true, "seashell": true, "sienna": true, "silver": true, "skyblue": true, "slateblue": true, "slategray": true, "snow": true, "springgreen": true, "steelblue": true, "tan": true, "teal": true, "thistle": true, "tomato": true, "turquoise": true, "violet": true, "violetred": true, "wheat": true, "white": true, "whitesmoke": true, "yellow": true, "yellowgreen": true};

Antville.ColorPickerFactory = function() {
   this.prefix = Antville.prefix + "ColorPicker_";
   this.valuePrefix = Antville.prefix + "ColorValue_";

   this.open = function(name, text, skin) {
      if (skin == "colorpickerExt")
         var cpWindow = window.open("http://uvmann.twoday.net/colorpicker?name=" + name + "&text=" + text + "&skin=" + skin, Antville.ColorPicker.prefix, "toolbar=no,location=no,directories=no,status=no,scrollbars=no,resizable=yes,width=480,height=360");
      else
         var cpWindow = window.open("http://uvmann.twoday.net/colorpicker?name=" + name + "&text=" + text + "&skin=" + skin, Antville.ColorPicker.prefix, "toolbar=no,location=no,directories=no,status=no,scrollbars=no,resizable=yes,width=350,height=320");
   }

   this.set = function(name, color) {
      var prefix = Antville.ColorPicker.prefix;
      var valuePrefix = Antville.ColorPicker.valuePrefix;
      var color = Antville.parseColor(color);
      if (color)
         document.getElementById(prefix + name).style.backgroundColor = color;
      else
         color = Antville.parseColor(document.getElementById(prefix + name).style.backgroundColor);
      if (color.indexOf("#") == 0)
         color = color.substr(1,color.length-1);
      document.getElementById(valuePrefix + name).value = color;
      return;
   }

   return this;
}

Antville.ColorPicker = new Antville.ColorPickerFactory();

Antville.encode = function(str) {
   var chars = ["&", "<", ">", '"'];
   for (var i=0; i < chars.length; i++) {
      var c = chars[i];
      var re = new RegExp(c, "g");
      str = str.replace(re, "&#" + c.charCodeAt() + ";");
   }
   return str;
}

Antville.decode = function(str) {
   return str.replace(/&amp;/g, "&");
}

Antville.parseColor = function(color) {
   var c = color.toLowerCase();
   if (Antville.colors[c])
      return c;
   var rgb = new RegExp("rgb ?\\( ?([0-9^,]*), ?([0-9^,]*), ?([0-9^ \\)]*) ?\\)");
   var result = color.match(rgb);
   if (result) {
      var R = parseInt(result[1], 10).toString(16);
      var G = parseInt(result[2], 10).toString(16);
      var B = parseInt(result[3], 10).toString(16);
      if (R.length == 1) R="0"+R;
      if (G.length == 1) G="0"+G;
      if (B.length == 1) B="0"+B;
      return "#"+R+G+B;
   }
   if (c.indexOf("#") == 0)
      c = c.substr(1,c.length-1);
   if (c.length == 6) {
      var nonhex = new RegExp("[^0-9,a-f]");
      nonhex.ignoreCase = true;
      var found = c.match(nonhex);
      if (!found)
         return "#" + c;
   }
   return;
}

Antville.Referrer = function(url, text, count) {
   this.url = url;
   this.text = text;
   this.count = count;
   return this;
}

Antville.Referrer.prototype.compose = function(key, prefix) {
   var query = new Antville.Query(this.url);
   if (query[key]) {
      if (prefix == null)
         prefix = "";
      return prefix + Antville.encode(query[key]);
   }
   return this.text;
}

Antville.Query = function(str) {
   if (str == undefined)
      var str = location.search.substring(1);
   else if (str.indexOf("?") > -1)
      var str = str.split("?")[1];
   if (str == "")
      return this;
   try {
      var parts = Antville.decode(decodeURIComponent(str)).split("&");
   } catch (e) {
      var parts = Antville.decode(unescape(str)).split("&");
   }
   for (var i = 0; i < parts.length; i++) {
      var pair = parts[i].split("=");
      var key = pair[0];
      if (key) {
         key = key.replace(/\+/g, " ");
         var value = pair[1];
         if (value)
            value = value.replace(/\+/g, " ");
         this[key] = value;
      }
   }
   return this;
}

Antville.Filter = function(def, key) {
   this.key = key;
   if (def == null)
      this.items = [];
   else if (def instanceof Array)
      this.items = def;
   else
      this.items = def.replace(/\r/g, "\n").split("\n");
   return this;
}

Antville.Filter.prototype.test = function(str) {
   if (!str)
      return false;
   for (var n = 0; n < this.items.length; n++) {
      try {
         var re = new RegExp(this.items[n], "i");
         if (re.test(str)) return true;
      } catch (err) {
         ;
      }
   }
   return false;
}
