Prototype.Browser.IEpng=false;
if (Prototype.Browser.IE && window.attachEvent)
{
  try{
      document.execCommand('BackgroundImageCache', false, true);
  }catch(e){}

  var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
  Prototype.Browser.IEpng = (rslt != null && Number(rslt[1]) >= 5.5 && Number(rslt[1]) < 7);
}

Prototype.Browser.Nice=(typeof encodeURIComponent != 'undefined') && (typeof document.getElementById != 'undefined');
var vars = {};
if (!JSRoot)
{
  var JSRoot="";
  if (Prototype.Browser.Nice)
  {
    var scriptTags = document.getElementsByTagName("script");
    for(var i=0;i<scriptTags.length;i++)
    {
      if(scriptTags[i].src && scriptTags[i].src.match(/js\/just\.(.*)?$/))
      {
        JSRoot = scriptTags[i].src.replace(/js\/just\.(.*)?$/,'');
        break;
      }
    }
  }

}

var Just={
  debug: 1,
  windows: {},

  init: function()
  {
    if (Prototype.Browser.Gecko || Prototype.Browser.WebKit)
    HTMLElement.prototype.click = function()
    {
        var evt = this.ownerDocument.createEvent('MouseEvents');
        evt.initMouseEvent('click', true, true, this.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
        this.dispatchEvent(evt);
    };

  },

  toggle: function(on, off)
  {
    if (on) for (var i=0;i<on.length;i++) { $(on[i]).removeClassName('hidden') };
    if (off) for (var i=0;i<off.length;i++) { $(off[i]).addClassName('hidden') };
  },

  visible: function(el, visible)
  {
    $(el).setStyle({'visibility':visible?'visible':'hidden'})
  },

  addCss: function(cssCode)
  {
    var styleElement = document.createElement("style");
    styleElement.type = "text/css";

    if (styleElement.styleSheet) {
      styleElement.styleSheet.cssText = cssCode;
    } else {
      styleElement.appendChild(document.createTextNode(cssCode));
    }
    document.getElementsByTagName("head")[0].appendChild(styleElement);
  },

  addHref : function(href, add, return_href) {
        return (return_href ? href : '') + (href.indexOf("?") != -1 ? "&" : "?") + add;
  },

  intoView: function(el)
  {
    el=$(el);

    var a=el.cumulativeOffset();

    var top = (document.documentElement.scrollTop ?
        document.documentElement.scrollTop :
        document.body.scrollTop);

    var vpH = document.viewport.getHeight();
    var coverage = parseInt(vpH + top);

    if ( a.top<top || a.top>coverage ) {
        el.scrollIntoView(true);
    }
  },

  wopen : function(url, name, w, h, antiblock, resizable, scrollbars)
  {
    Just.windows[name] = window.open(url, name, "width=" + w + ",height=" + h + ",resizable=" + ((typeof resizable != "undefined") ? resizable : "1") + ",toolbar=0,location=0,status=0,menubar=0,directories=0,scrollbars=" + ((typeof scrollbars != "undefined") ? scrollbars : "yes"));
    Just.windows[name].focus();
    return Just.windows[name];
  },

  // clear responseText to be a json (i.e. remove PHP errors or other output)
  req : function(r)
  {
    if (!Just.debug) return r.substr(r.indexOf('{'));;

    if (r && r.substr(0,1)!='{')
    {
      if (Just.windows['error_console'] && !Just.windows['error_console'].closed)
      {
        var vc = Just.windows['error_console'];
        vc.document.getElementsByTagName('body')[0].innerHTML = r;
      }
      else
      {
        var vc = Just.wopen('', 'error_console', 640, 480, 1, 1, 1);
        vc.document.write(' '+r);
        vc.document.close();
      }

      return false;
    }
    else
    {
      return r;
    }
  },

  escape : function(s) {
    return encodeURIComponent(s);
  }
}

function cookie(name,value,days) {

  // if no "value" and "days" passed - return cookie by "name"
  if (typeof value=='undefined')
  {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
      var c = ca[i];
      while (c.charAt(0)==' ') c = c.substring(1,c.length);
      if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
  }

  var expires = days?"; expires="+((d=new Date()).setTime(new Date().getTime()+(days*24*60*60*1000)) && d.toGMTString()):"";
  document.cookie = name+"="+value+expires+"; path=/";
  return value;
}


function uncookie(name) {
  cookie(name,"",-1);
}

function SelfLabeledInput(){
  var labelSelector = arguments[0] || 'label.l';
  $$(labelSelector).findAll(function(l) {return (l.readAttribute('for'))}).each(function(l){
      //l.absolutize();
      var f=$(l.readAttribute('for'));
      l.clonePosition(f);
      if (f.value == '') Just.visible(l,1);

      $(f).observe('blur',function(e){if(Event.element(e).value == '') Just.visible(l,1);}.bind(l));
      $(f).observe('focus',function(e){Just.visible(l,0);}.bind(l));
      l.observe('click',function(e){Just.visible(l,0);$(l.readAttribute('for')).focus()}.bind(l));
  });
};


document.observe("dom:loaded", Just.init);

