/*
 * Author: Vertex Logic, Inc
 * Copyright 2005, 2006, 2007 - All rights reserved
 *
 * Vertex Logic, Inc., California, USA 
 *
 * Vertex Logic grants you ("Licensee") a non-exclusive license to use
 * and modify this source and recompile it in
 * accordance with the terms of the Agreement under which this software is bought
 * and provided that (i) this copyright notice appear on all copies of the Software; 
 * (ii) Licensee does not sale the software as is or with modification without
 * the prior consent of Vertex Logic. (iii) Licensee agrees that it does not have any 
 * title or ownership of the Software.
 *
 * The program is provided "as is" without any warranty express or
 * implied, including the warranty of non-infringement and the implied
 * warranties of merchantibility and fitness for a particular purpose.
 * Vertex Logic will not be liable for any damages suffered 
 * by you as a result of using the Program. 
 * In no event will Vertex Logic be liable for any
 * special, indirect or consequential damages or lost profits even if
 * Vertex Logic have been advised of the possibility of their occurrence. 
 * Vertex Logic will not be liable for any third party claims against you.
 */


Af.TimeDropDown = Class.create();

Af.TimeDropDown.prototype = {
   initialize: function(textBox, trigger, handler, container, dialogContainer) {
      this._initializeDialog(textBox, trigger, handler, container, dialogContainer);
      this.value = 0;
   },
   
   _initializeDialog: function(textBox, trigger, handler, container, dialogContainer) {
      this.textBox = textBox;
      this.trigger = trigger;
      this.handler = handler;
      this.container = container;
      this.dialogContainer = dialogContainer;
      this.trigger.onclick = this.timeSelectorClicked.bindAsEventListener(this);
      this.textBox.onchange = this.parseTime.bind(this);
   },
   
   timeSelectorClicked: function(ev) {
      var eventTarget = ev.target ? ev.target : ev.srcElement;
      
      var p = RicoUtil.toDocumentPosition(eventTarget);
      var x = p.x;
      var y = p.y  + eventTarget.offsetHeight;
      
      if (!is_firefox) {
         x += document.documentElement.scrollLeft;
         y += document.documentElement.scrollTop;
      }
     
      this.show(x, y);
      
      return true;
   },
   
   parseTime: function() {
    try {
     var s = this.textBox.value;
     if (s == null) {
        showMessageDialog( "The time you have entered for this event is incomplete. Please enter or select a valid time.", "Excuse us!", 350, 130, null, true);
        this.textBox.value = "8:00 AM";
        return -1;
     }
     s = s.toLowerCase();
     var i1 = s.indexOf(":");
     var i2 = s.indexOf("am");
     var am = true;
     if (i2 < 0) {
       am = false;
       i2 = s.indexOf("pm");
     }
     
     if (i2<0 || i2 <= i1) {
        //this.textBox.value = this.getDisplayTimeFromMins(this.value);		
		 this.textBox.value = "8:00 AM";
         showMessageDialog( "The time you have entered for this event is incomplete. Please enter or select a valid time.", "Excuse us!", 350, 130, null, true);
        return -1;
     }
     
     var temp;
     if (i1 >= 0) {
       temp = s.substring(0, i1);
     } else {
       temp = s.substring(0, i2);
     }
     var h = parseInt(temp, 10);	 
     
     if (!am && h != 12) {
       h += 12;
     }
     if(h > 24){
		 h = 1;
	 }
     var m=0;
     
     if (i1 >= 0) {
       temp = s.substring(i1+1, i2);
       m = parseInt(temp, 10);
     }
     
     //debugA("h: " + h + ", m: " + m);
     
     h += parseInt(m/60, 10);
     m = m % 60;
     
     var ov = this.value;

     if(h == 12 && am){
		h = 0;
	 }

     this.setValue(h * 60 + m);
     if (this.handler != null) {
		this.handler.timeChanged(this, this.value, ov);
     }
     
     return this.value;
     
    } catch(exc) {
       //this.textBox.value = this.getDisplayTimeFromMins(this.value);
	    this.textBox.value = "8:00 AM";
       showMessageDialog( "The time you have entered for this event is incomplete. Please enter or select a valid time.", "Excuse us!", 350, 130, null, true);
       return -1;
    }
   },
   
   setValue: function(v) {
      this.value = v;
      this.textBox.value = this.getDisplayTimeFromMins(v);
   },
   
   getValue: function(v) {
      return this.value;
   },
   
   close: function(ev) {
      this.hide();
   },
   
   hide: function() {	
      if (this.element != null) {         
		 try{
			 document.getElementsByTagName('body')[0].removeChild(this.element);
			 this.element.style.display = "none";
		 }catch(err){
			 this.element.style.display = "none";
		 }
      }
	   document.onclick = null;
       this.visible = false;
   },
   
   addDcoumentClickListener: function() {
      document.onmousedown = this.documentClicked.bindAsEventListener(this);
   },
   
   documentClicked: function(evt) {
      var eventTarget = evt.target ? evt.target : evt.srcElement;
      var t = eventTarget;
      while (t != null) {
         if (t == this.element) {
            return true;
         }
         t = t.parentNode;
      }
      this.hide();
   },
   
   show: function(x, y) {
      document.onclick = null;
      this.visible = true;
      if (this.element == null) {
         this.createElement();
      }
      
      document.getElementsByTagName('body')[0].appendChild(this.element);
      this.element.style.left = x + "px";
      this.element.style.top = (y + 2)+ "px";
      this.element.style.zIndex = "5000010";
      this.element.style.display = "";
      setTimeout(this.addDcoumentClickListener.bind(this), 10);
      
      var scrollTop = 0;
      if (this.value != 0) {
        scrollTop = parseInt((this.value / 30) * 15);
      }
      this.scrollTop = scrollTop;
      setTimeout(this.doScroll.bind(this), 10);
   },
   
   doScroll: function() {
      this.element.scrollTop = this.scrollTop;
   },
   
   createElement: function() {
     
      var td;
      var tr;
      
      var scroller = document.createElement('div');
      scroller.className = 'TimeDropDown';
 
      var table2 = document.createElement('table');
      table2.cellSpacing = '0px';
      table2.cellPadding = '0px';
      table2.className = "TimeTable";
      scroller.appendChild(table2);
      
      tbody = document.createElement('tbody');
      table2.appendChild(tbody);
      
      var l = this.getTimeOptions();
      
      for (var i=0; i<l.length; i++) {
         tr = document.createElement('tr');
		 tbody.appendChild(tr);
		 td = document.createElement('td');
		 td.onmouseover = this.cellMouseOver.bindAsEventListener(this);
		 td.onmouseout = this.cellMouseOut.bindAsEventListener(this);
		 td.className = "TimeCell";
		 td.innerHTML = this.getDisplayTimeFromMins(l[i]);
		 td.id = l[i];
		 tr.appendChild(td);
      }
      
      
      table2.onclick = this.cellSelected.bindAsEventListener(this);
      /* table2.onmousedown= this.mouseDownOnElement.bindAsEventListener(this);
      if (is_ie || is_firefox) {
         scroller.onmousedown= this.mouseDownOnElement2.bindAsEventListener(this);
      }
      */
      this.table = table2;
      this.element = scroller;

      
   },
   
   cellMouseOver: function(evt) {
      var eventTarget = evt.target ? evt.target : evt.srcElement;
      eventTarget.className = "TimeCellHighlighted";
   },
   
   cellMouseOut: function(evt) {
      var eventTarget = evt.target ? evt.target : evt.srcElement;
      eventTarget.className = "TimeCell";
   },
   
   getTimeOptions: function() {
      m = 0;
      var l = new Array(48);
      for (var i=0; i<l.length; i++) {
         l[i] = m;
         m += 30;
      }
      return l;
   },
   
   cellSelected: function(evt) {
      var eventTarget = evt.target ? evt.target : evt.srcElement;
      var t = eventTarget;
      while (t != null) {
         if (t == this.element) {
            return true;
         } else if (t == this.table) {
            break;
         }
         t = t.parentNode;
      }
      if (eventTarget.tagName == "TD") {
         var ov = this.value;
         this.setValue(eventTarget.id);
		 if (this.handler != null) {
			this.handler.timeChanged(this, this.value, ov);
		 }
      }
      this.hide();
      return this.consume(evt);
   },
   
   mouseDownOnElement: function(e) {
      this.consume(e);
   },
   
   mouseDownOnElement2: function(e) {
       this.consume(e);
   },
   
   getDisplayTimeFromMins: function(h) {
	  h = parseInt(h);
	  var hr = parseInt(h / 60);
	  var m = h % 60;
	  
	  var s = "AM";
	  if (hr >= 12) {
		s = "PM";
		if (hr > 12) {
			hr -= 12;
		}
	  }
	  
	  if (m == 0) {
		m = "00";
	  } else if (m < 10) {
	    m = "0" + m;
	  }
	  
	  if (hr == 0) {
		hr = 12;
	  }
	  
	  return hr + ":" + m + " " + s;
	},
	
	consume: function(e) {
	  if (e.preventDefault) {
		  e.preventDefault();
	  } 
	  if (e.stopPropagation) {
		  e.stopPropagation();
	  } 
	  e.cancelBubble = true;
	  return false;
	}
}

