/*
*
* Copyright (c) 2007 Andrew Tetlaw
* 
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* * 
*
*
* FastInit
* http://tetlaw.id.au/view/javascript/fastinit
* Andrew Tetlaw
* Version 1.4.1 (2007-03-15)
* Based on:
* http://dean.edwards.name/weblog/2006/03/faster
* http://dean.edwards.name/weblog/2006/06/again/
* Help from:
* http://www.cherny.com/webdev/26/domloaded-object-literal-updated
* 
*/
var FastInit = {
  onload : function() {
    if (FastInit.done) { return; }
    FastInit.done = true;
    for(var x = 0, al = FastInit.f.length; x < al; x++) {
      FastInit.f[x]();
    }
  },
  addOnLoad : function() {
    var a = arguments;
    for(var x = 0, al = a.length; x < al; x++) {
      if(typeof a[x] === 'function') {
        if (FastInit.done ) {
          a[x]();
        } else {
          FastInit.f.push(a[x]);
        }
      }
    }
  },
  listen : function() {
    if (/WebKit|khtml/i.test(navigator.userAgent)) {
      FastInit.timer = setInterval(function() {
        if (/loaded|complete/.test(document.readyState)) {
          clearInterval(FastInit.timer);
          delete FastInit.timer;
          FastInit.onload();
        }}, 10);
    } else if (document.addEventListener) {
      document.addEventListener('DOMContentLoaded', FastInit.onload, false);
    } else if(!FastInit.iew32) {
      if(window.addEventListener) {
        window.addEventListener('load', FastInit.onload, false);
      } else if (window.attachEvent) {
        return window.attachEvent('onload', FastInit.onload);
      }
    }
  },
  f:[],done:false,timer:null,iew32:false
};
/*@cc_on @*/
/*@if (@_win32)
FastInit.iew32 = true;
document.write('<script id="__ie_onload" defer src="' + ((location.protocol == 'https:') ? '//0' : 'javascript:void(0)') + '"><\/script>');
document.getElementById('__ie_onload').onreadystatechange = function(){if (this.readyState == 'complete') { FastInit.onload(); }};
/*@end @*/
FastInit.listen();

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();
   }
}

function setImageVarinat() {
  var maxwidth=100;
  var maxheight=100;
  var boxwidth=116;
  var imageContainer = document.getElementById("imageContainer");
  if(imageContainer ) {
    var imageContainerImg = imageContainer.getElementsByTagName("img");
    for (var i=0;i<imageContainerImg.length;i++) {
/*
      var urllength=imageContainerImg[i].src.length;
      var url0=imageContainerImg[i].src.substring(0,urllength-4);
      var url1=imageContainerImg[i].src.substring(urllength-4,urllength);
      imageContainerImg[i].src=url0+"_small"+url1;
*/
      if(imageContainerImg[i].offsetWidth>=imageContainerImg[i].offsetHeight) {
        scale=imageContainerImg[i].offsetWidth/imageContainerImg[i].offsetHeight;
        imageContainerImg[i].style.width=maxwidth+'px';
        imageContainerImg[i].style.height=Math.round(maxwidth/scale)+'px';
        imageContainerImg[i].style.marginLeft='7px';
        imageContainerImg[i].style.marginRight='19px';
      }
      else {
        scale=imageContainerImg[i].offsetHeight/imageContainerImg[i].offsetWidth;
        newwidth=Math.round(maxheight/scale);
        imageContainerImg[i].style.width=newwidth+'px';
        imageContainerImg[i].style.height=maxheight+'px';
        imageContainerImg[i].style.marginLeft=Math.round((boxwidth-newwidth-2)/2)+'px';

imageContainerImg[i].style.marginRight=Math.round((boxwidth-newwidth-2)/2+12)+'px';
      }
      if(((i+1)%4)==0) imageContainerImg[i].style.marginRight='0px';
    }
  document.getElementById("imageContainer").style.visibility="visible";
  }
}

FastInit.addOnLoad(setImageVarinat);

/*
if (document.all) {
  attachEvent("onload", setImageVarinat);
} else {
  window.addEventListener("load", setImageVarinat, true);
}
*/Antville = {};
Antville.prefix = "Antville_";

Antville.pixel = new Image();
Antville.pixel.src = "http://www.coke-side-of-winter.at/static/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://www.coke-side-of-winter.at/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://www.coke-side-of-winter.at/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;
}
