if (window.ActiveXObject) window.ie = window[window.XMLHttpRequest ? 'ie7' : 'ie6'] = true;
else if (document.childNodes && !document.all && !navigator.taintEnabled) window.khtml = true;
else if (document.getBoxObjectFor != null) window.gecko = true;
if (window.ie6) try {document.execCommand("BackgroundImageCache", false, true);} catch (e){};

Element.create = function(text) {
  var new_elem = document.createElement('span')
  new_elem.innerHTML = text
  return new_elem.firstChild
};

var Asset = Class.create()
Asset.images = function(images_list,options){
  options = options || {}
  options.prefix = options.prefix || ''
  return $A(images_list).collect(function(img){
    return Element.create('<img src="'+options.prefix+img+'"/>')
  })
}

var Cookie = {
  set: function(name, value, daysToExpire) {
    var expire = '';
    if (daysToExpire != undefined) {
      var d = new Date();
      d.setTime(d.getTime() + (86400000 * parseFloat(daysToExpire)));
      expire = '; expires=' + d.toGMTString();
    }
    path = '; path=/'
    return (document.cookie = escape(name) + '=' + escape(value || '') + path + expire );
  },
  get: function(name) {
    var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]*)'));
    return (cookie ? unescape(cookie[2]) : null);
  },
  erase: function(name) {
    var cookie = Cookie.get(name) || true;
    Cookie.set(name, '', -1);
    return cookie;
  },
  accept: function() {
    if (typeof(navigator.cookieEnabled) == 'boolean') {
      return navigator.cookieEnabled;
    }
    Cookie.set('_test', '1');
    return (Cookie.erase('_test') = '1');
  }
}

Element.addMethods({
  restore_emails:function(elem,id){
    var regex = /([a-zA-Z0-9_\.\-\_\[\]]+)\@([\.a-zA-Z0-9\_\-]+)\.([a-zA-Z0-9]{2,4})/
    var content = String(elem.innerHTML)
    var built = ''
    
    while((email = content.match(regex))){
      from = content.indexOf(email[0])
      built += content.substr(0,from+email[0].length).replace(regex,'<a href="#" class="'+elem.id+'-pigeon">$2@$1.$3</a>')
      content = content.substr(from+email[0].length)
    }
    if(built){
      built += content
      elem.innerHTML = built
    }
  },
  getStyle:function(element, strCssRule){
    var strValue = "";
    if(document.defaultView && document.defaultView.getComputedStyle){
        strValue = document.defaultView.getComputedStyle(element, "").getPropertyValue(strCssRule);
    }
    else if(element.currentStyle){
        strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
            return p1.toUpperCase();
        });
        strValue = element.currentStyle[strCssRule];
    }
    return strValue;
  },
  wrapin: function(element, tagName) {
    element = $(element);
    var wrapper = document.createElement(tagName);
    element.parentNode.replaceChild(wrapper, element);
    wrapper.appendChild(element);
    return Element.extend(wrapper);
  },
  
  glow: function(element){
    element = $(element);
    
    type = element.tagName.toLowerCase();
    var wrapper = element.wrapin('div');
    wrapper.elem = element
    
    if(element.readAttribute('type') == 'checkbox'){
      wrapper.className = element.checked ? 'input_checkbox_checked' : 'input_checkbox'
      element.style.display = 'none'
      wrapper.observe('click', function(e){
        this.elem.checked = !this.elem.checked
        if(this.elem.checked)this.className = 'input_checkbox_checked'
        else this.className = 'input_checkbox'
        if(this.elem.onchange)this.elem.onchange()
      });      
      return true
    }
    wrapper.addClassName(type+' '+type+'_blur'+(element.hasClassName('small') ? '_small' : ''));
    var width = element.getStyle('width')
    if(width != 'auto')wrapper.style.width = (parseInt(width.replace('px','')) + 15) + 'px';
    
    var span = document.createElement('span');
    var pN = $(element.parentNode);
    while(pN.getStyle('background-color') == 'transparent'){
      pN=$(pN.parentNode);        
    }
    span.style.backgroundColor = pN.getStyle('background-color');
    wrapper.appendChild(span);
    
    if(type=='select'){
      new Autocompleter.SelectBox(element);
      element = wrapper.getElementsByTagName('input')[0];
      wrapper.style.width = (parseInt(element.getStyle('width').replace('px','')) + 15) + 'px';
      if(window.ie6){element.focus(); element.blur()}
    }

    Event.observe(element,'focus', function(e){
       type=this.tagName.toLowerCase();
       this.parentNode.removeClassName(type+'_blur'+(this.hasClassName('small') ? '_small' : ''));
       this.parentNode.addClassName(type+'_focus'+(this.hasClassName('small') ? '_small' : ''));
    });
    Event.observe(element,'blur', function(e){
       type=this.tagName.toLowerCase();
       this.parentNode.removeClassName(type+'_focus'+(this.hasClassName('small') ? '_small' : ''));
       this.parentNode.addClassName(type+'_blur'+(this.hasClassName('small') ? '_small' : ''));
    });
    
    return Element.extend(wrapper);
  },
  
  round: function(element, color, size, side){
    element = $(element);
    element.style.position = "relative";
        
    var tl = document.createElement('div');
    var tr = document.createElement('div');
    var bl = document.createElement('div');
    var br = document.createElement('div');
    
    tl.style.overflow = tr.style.overflow = bl.style.overflow = br.style.overflow = "hidden";
    tl.style.position = tr.style.position = bl.style.position = br.style.position = "absolute";
    tl.style.width = tr.style.width = bl.style.width = br.style.width = size + "px";
    tl.style.height = tr.style.height = bl.style.height = br.style.height = size + "px";
    
    
    if(side=='top' || side=='both'){
        tl.style.top = "0px";
        tl.style.left = "0px";
        tl.style.backgroundImage = "url(/images/"+ color +"_tl.gif)";
        element.appendChild(tl);
    }
    
    if(side=='top' || side=='both'){
        tr.style.top = "0px";
        tr.style.right = "0px";
        tr.style.backgroundImage = "url(/images/"+ color +"_tr.gif)";
        element.appendChild(tr);
    }

    if(side=='bottom' || side=='both'){
        bl.style.bottom = "0px";
        bl.style.left = "0px";
        bl.style.backgroundImage = "url(/images/"+ color +"_bl.gif)";
        element.appendChild(bl);
    }

    if(side=='bottom' || side=='both'){
        br.style.bottom = "0px";
        br.style.right = "0px";
        br.style.backgroundImage = "url(/images/"+ color +"_br.gif)";
        element.appendChild(br);
    }
  }
});

var GlobalEffects = Class.create()
GlobalEffects.prototype = {
  initialize:function(){
    this.applyGlow()
    this.applyButtons()
  },
  applyGlow:function(){
    elements = $A()
    elements = elements.concat($A(document.getElementsByTagName('div')))
    elements = elements.concat($A(document.getElementsByTagName('input')))
    elements = elements.concat($A(document.getElementsByTagName('select')))
    elements = elements.concat($A(document.getElementsByTagName('textarea')))
    elements = elements.concat($A(document.getElementsByTagName('li')))
    
    elements.each(function (el){
      if(el.className.indexOf('glow')!=-1)Element.Methods.glow(el)
      if(el.className.indexOf('category_title')!=-1)$(el).round("blue", 2, "both")
      if(el.className.indexOf('comment-li')!=-1)$(el).round("grey", 2, "both")
    })

  },
  applyButtons:function(){
    $A(document.getElementsByTagName('a')).concat($A(document.getElementsByTagName('button'))).each(function(btn){
      if(btn.className.indexOf('white_btn')!=-1)this.setButton(btn,'white')
      if(btn.className.indexOf('small_white_btn')!=-1)this.setButton(btn,'small_white')
    }.bind(this))    
  },
  setButton:function(btn, name){
    Event.observe($(btn),'mouseover',function(b){$(this).addClassName(name+'_btn_focus')})
    Event.observe($(btn),'focus',function(b){$(this).addClassName(name+'_btn_focus')})
    Event.observe($(btn),'mouseout',function(b){$(this).removeClassName(name+'_btn_focus')})
    Event.observe($(btn),'blur',function(b){$(this).removeClassName(name+'_btn_focus')})
  }
}
