﻿// AddEvent
// written by Dean Edwards, 2005
// with input from Tino Zijdel, Matthias Miller, Diego Perini
// http://dean.edwards.name/weblog/2005/10/add-event/
// updated by allen hardin 2011

var Roxy = Roxy || {};
// core class?
Roxy.Core = {
  HasClassName: function(e,c) {
    return e.className.match(new RegExp("(\\s|^)"+c+"(\\s|$)"));
  },
  
  AddClassName: function(e,c) {
	if (!this.HasClassName(e,c)) { e.className += " "+c; }
	//alert( e.className );
  },
  
  RemoveClassName: function(e,c) {
	if (this.HasClassName(e,c)) {
      e.className=e.className.replace(new RegExp("(\\s|^)"+c+"(\\s|$)")," ");
	  //alert( e.className );
	}
  }

}; // end core


Roxy.Util = {
  ViewPortBounds: function() {
    var w=0;
    var h=0;
   
    if (window.innerWidth)
    {
      w = window.innerWidth;
      h = window.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientWidth && document.documentElement.clientWidth != 0)
    {
      w = document.documentElement.clientWidth;
      h = document.documentElement.clientHeight;
    }
    else
    {
      w = document.getElementsByTagName('body')[0].clientWidth;
      h = document.getElementsByTagName('body')[0].clientHeight;
    }
    return {width:w, height:h}
  },
  
  MousePos : function(e) {   
    if(e.pageX || e.pageY){   
      return {x:e.pageX, y:e.pageY};   
    }   
    return {   
      x:e.clientX + document.body.scrollLeft - document.body.clientLeft,   
      y:e.clientY + document.body.scrollTop  - document.body.clientTop   
    };   
  }

}; // end util


// event class?
Roxy.Event = {
  guid: 1,
  
  AddEventS: function(e,t,h) {
	  if (e.addEventListener) 
	    e.addEventListener(t,h,false);
	  else if (e.attachEvent)
		  e.attachEvent( 'on'+t,function() { return h.apply(e, new Array(window.event)); } );
  },

  Add: function(e, t, h) {
    if (e.addEventListener) {
      e.addEventListener(t, h, false);
    } else {
      if (!h.$$guid) { h.$$guid = this.guid++; }
      if (!e.events) e.events = {};
      var handlers = e.events[t];
      if (!handlers) {
        handlers = e.events[t] = {};
        if (e["on" + t]) { handlers[0] = e["on" + t]; }
      }
      handlers[h.$$guid] = h;
      e["on" + t] = this.HandleEvent;
    }
  },
  
  Remove: function(e, t, h) {
    if (e.removeEventListener) {
      e.removeEventListener(t, h, false);
    } else {
      if (e.events && e.events[t]) {
        delete e.events[t][h.$$guid];
      }
    }
  },

  HandleEvent: function(event) {
    var returnValue = true;
    event = event || Roxy.Event.FixEvent(((this.ownerDocument || this.document || this).parentWindow || window).event);
    var handlers = this.events[event.type];
    for (var i in handlers) {
      this.$$handleEvent = handlers[i];
      if (this.$$handleEvent(event) === false) {
        returnValue = false;
      }
    }
    return returnValue;
  },

  FixEvent: function(event) {
   event.preventDefault = this.PreventDefaultEx;
   event.stopPropagation = this.StopPropagation;
   return event;
 },

  PreventDefaultEx: function() {
    this.returnValue = false;
  },

  StopPropagation: function() {
    this.cancelBubble = true;
  },
  
  PreventDefault: function(ev) { 
    if(ev.preventDefault) { 
        ev.preventDefault(); 
    } else { 
      ev.returnValue = false; 
    } 
  } 
}; // end event


// ajax class?
Roxy.AJAX = {
  READY_STATE_UNINITIALIZED: 0,
  READY_STATE_LOADING:       1,
  READY_STATE_LOADED :       2,
  READY_STATE_INTERACTIVE:   3,
  READY_STATE_COMPLETE:      4,
  
  ContentLoader: function( component, url, method, requestParams ) {
     this.component     = component;
     this.url           = url;
     this.method        = method;
     this.requestParams = requestParams;
  }
};

Roxy.AJAX.ContentLoader.prototype = {
   GetTransport: function() {
      var t;
      if ( window.XMLHttpRequest )
         t = new XMLHttpRequest();
      else if ( window.ActiveXObject ) {
         try {
            t = new ActiveXObject('Msxml2.XMLHTTP');
         }
         catch(err) {
            t = new ActiveXObject('Microsoft.XMLHTTP');
         }
      }
      return t;
   },

   SendRequest: function() {
      var p = []
      for ( var i = 0 ; i < arguments.length ;  i++ )
         p.push(arguments[i]);

      var THIS = this;
      var r = this.GetTransport();
      r.open( this.method, this.url, true );
      r.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded');
      r.onreadystatechange = function() { THIS.HandleAjaxResponse(r) };
      r.send( this.QueryString(r) );
  },

  QueryString: function(a) {
     var p = [];
     for ( var i = 0 ; i < this.requestParams.length ; i++ )
        p.push(this.requestParams[i]);
     for ( var j = 0 ; j < a.length ; j++ )
        p.push(a[j]);

     var q = "";
     if ( p && p.length > 0 ) {
        for ( var i = 0 ; i < p.length ; i++ )
           q += p[i] + '&';
        q = q.substring(0, q.length-1);
     }
     return q;
  },

  HandleAjaxResponse: function(r) {
     if ( r.readyState == Roxy.AJAX.READY_STATE_COMPLETE ) {
        if ( this.Success(r) )
           this.component.AjaxUpdate(r);
        else
           this.component.HandleError(r);
     }
  },

  Success: function(r) {
    return  r.status == 0 || (r.status >= 200 && r.status < 300);
  }
}; // end ajax


// ui
Roxy.UI = {
  ElementEx: function(type, id, className, name, value) {
    if(type) {
      var o = document.createElement(type);
      if(id) { o.id=id };
      if(className) { o.className=className };
      if(name) { o.name=name };
      if(value) { o.value=value };
      return o;
    }
    return null;
  },
  
  InputEx : function(type, id, className, name, value, disabled, checked) {
    var o = document.createElement("input");
    o.type=type?type:'text';

    if(id) { o.id=id };
    if(className) { o.className=className };
    if(name) { o.name=name };
    if(value) { o.value=value };

    return o;
  },
  
  Input : function(options) {
    var o={
      type: options.type || "text",
      id: options.id || null,
      className: options.className || null,
      name: options.name || null,
      value: options.value || null,
      disabled: options.disabled || false,
      checked: options.checked || false
    };
    
    var e = document.createElement("input");
    if(o.type) { e.type=o.type };
    if(o.id) { e.id=o.id };
    if(o.className) { e.className=o.className };
    if(o.name) { e.name=o.name };
    if(o.value) { e.value=o.value };
    if(o.disabled) { e.disabled=o.disabled };
    if(o.checked) { e.checked="checked" };

    return e;
  },
  
  Element: function(options) {
    var o={
      type: options.type || "",
      id: options.id || "",
      className: options.className || "",
      name: options.name || "",
      value: options.value || ""
    };
    if(o.type) {
      var e = document.createElement(o.type);
      if(o.id) { e.id=o.id };
      if(o.className) { e.className=o.className };
      if(o.name) { e.name=o.name };
      if(o.value) { e.value=o.value };
      return e;
    }
    return null;
  },
  
  Radio : function(name, value, checked) {    
    var r = '<input type="radio" name="' + name + '"'; 
    if (value) { r += ' value="' + value + '"'; };       
    if (checked) { r += ' checked="checked"'; };
    r += '/>';    
    var o = document.createElement('div');    
    o.innerHTML = r;    
    return o.firstChild;
  },
   
  //watermark class
  Watermark : function(id, watermark) {
    var e = $(id);
    var w = watermark;
    e.value=w;
    Roxy.Core.AddClassName(e,"watermark"); 
    Roxy.Event.AddEventS(e,"focus",Watermark_OnFocus);
    Roxy.Event.AddEventS(e,"blur",Watermark_OnBlur);
    
    function Watermark_OnFocus(ev) { 
      if(this.value === w) {  
       this.value="";
       Roxy.Core.RemoveClassName(e,"watermark");  
      } 
    }
    
    function Watermark_OnBlur(ev) {
      if(this.value === "") {   
         this.value=w;
         Roxy.Core.AddClassName(e,"watermark");  
      }       
    }
  }, // end watermark class
  
  Dialog: function(options) {
    this.id=options.Id || "dialog1",  
    this.Modal=false;
    this.Draggable=options.Draggable || true;
    this.CancelButton=false;
    this.OkButton=false;
    this.HeaderText=options.HeaderText || "Dialog Header";
    this.Content=options.Content || null;
    this.StaticContent= options.StaticContent || null;
    this.Width=600;
    this.Height=0;
    
  }// end dialog class
}; //end ui

Roxy.UI.Dialog.prototype = {
  Create: function() {
    var o={
      type: "div",
      id: this.id,
      className: "dialog"
    }
    var e=Roxy.UI.Element(o);
    
    var h=this.Header();
    e.appendChild(h);
    
    var b=this.Body();
    e.appendChild(b);
    
    var bb=this.Button();
    e.appendChild(bb);

    document.body.appendChild(e);
    
    if(this.Draggable)
      var dd = Roxy.Dragable.Init("dialogHeader");
  },
  
  Header: function() {
    var o={
      type: "div",
      id: "dialogHeader",
      className: "dialogHeader"
    };
    var ho=Roxy.UI.Element(o);
    
    if(close) {
    o.id="dialogHeaderClose",
    o.className="dialogHeaderClose"
    var b=Roxy.UI.Element(o);
    var e=["click","mouseover","mouseout","mousedown"];
    for(var i=0; i<e.length; i++) { $EV.Add(b, e[i], bind(this, this.OnMouseEvent)); }
    ho.appendChild(b);
    }
    
    o.id="",
    o.className="dialogHeaderContent"
    var hi=Roxy.UI.Element(o)
    hi.innerHTML = this.HeaderText;
    ho.appendChild(hi);
    
    return ho;
  },
  
  Body: function() {
    var o={
      type: "div",
      id: "",
      className: "dialogBody"
    };
    var e=Roxy.UI.Element(o);
    
    // add static here
    
    if(this.StaticContent)
      e.appendChild(this.StaticContent);
    
    
    // add dynamaic here
    o.id="dialogBodyContent";
    o.className="dialogBodyContent";
    var c=Roxy.UI.Element(o);
    e.appendChild(c);
    
    return e;
  },
  
  Button: function() {
    var o={
      type: "div",
      className: "dialogButtonBar"
    };
    var e=Roxy.UI.Element(o);

    o.type="button";
    o.className="button";
    o.value="Cancel";
    var ok=Roxy.UI.Input(o);
    e.appendChild(ok);
    
    o.type="button";
    o.className="button";
    o.value="Ok";
    var c=Roxy.UI.Input(o);
    e.appendChild(c);
    
    return e;
  },
  
  Footer: function() {
  
  },
  
  Dispose: function() {
    this.RemoveHandlers();
    document.body.removeChild($(this.id));
  },
  
  RemoveHandlers: function() {  
    // remove events for close button  
    var o=$("dialogHeaderClose");
    var e=["click","mouseover","mouseout","mousedown"];
    if(o)
      for(var i=0; i<e.length; i++){ $EV.Remove(o, e[i], this.OnMouseEvent); } 
      //for(var i=0; i<e.length; i++){ $EV.Remove(o, e[i], bind(this, this.OnMouseEvent)); } 
  },
  
  
  OnMouseEvent: function(ev) {
    var sender = ev.target || ev.srcElement;
    var p=0;
    switch(ev.type)
    {
      case "click":
        this.Dispose();
      break;
      case "mouseover":
        p= -18;
        break;
      case "mousedown":
        p= -36;
      break;
      case "mouseout":
        p=0; 
      break;
    }
    sender.style.backgroundPosition='0px ' + p + 'px';
  }
};


function bind(scope, fn) {     
  return function () {         
    fn.apply(scope, arguments);     
  }; 
} 



// globals
function $(element) {
  return document.getElementById(element);
};

function $$(etype, id, css) {
  var e = document.createElement(etype);   
  if(e && (typeof id == "string")) {e.id=id;} 
  if(e && (typeof css == "string")) {e.className=css;} 
  return e;
};

function $T(element, tag) {
  var e = element || document;
  return e.getElementsByTagName(tag)
};

$EV = Roxy.Event;


window.onload = function() {
  try{
  // call init on other page
  if(Roxy.Page){ if(typeof Roxy.Page.Init == "function"){ Roxy.Page.Init(); } }
  }catch(ex){
    alert("No Page.Init");
  } 
};
