dojo.declare("MfDateTextBox", [dijit.form.DateTextBox], {

	_setValueAttr: function(value, priorityChange, formattedValue){
		
		if(!value) return;
		
		if( value.split ) {
			value = value.split("-");		
			value = new Date(value[0], value[1]-1, value[2]);
		}
		
		this.inherited(arguments);
		if(this._picker){
			if(!value){value=new Date();}
			this._picker.attr('value', value);
		}
	},
	
	getValue: function() {
		var value = new Date(this.attr('value'));
		
		var year = value.getFullYear();
		var month = (value.getMonth()+1)+"";
		var day = value.getDate()+"";
		
		if(month.length==1)
			month = "0"+month;
		if(day.length==1)
			day = "0"+day;
		
		value = year+'-'+month+'-'+day;
		
		return value;
	}
	

});


dojo.declare("MfTimeTextBox", [dijit.form.TimeTextBox], {

	_setValueAttr: function(value, priorityChange, formattedValue){
		if(!value) return;
		
		if( value.split ) {
			value = value.split(":");		
			value = new Date(1970, 1, 1, value[0], value[1], value[2]);
		}
		
		this.inherited(arguments);
		if(this._picker){
			if(!value){value=new Date();}
			this._picker.attr('value', value);
		}
	},
	
	
	getValue: function() {
		var value = new Date(this.attr('value'));
		
		if(!value)
			return "00:00:00";
		
		var h = value.getHours()+"";
		var m = value.getMinutes()+"";
		var s = value.getSeconds()+"";
		
		if(h.length==1)
			h = "0"+h;
		if(m.length==1)
			m = "0"+m;
		if(s.length==1)
			s = "0"+s;
		
		value = h+':'+m+':'+s;
		
		return value;
	}
	

});


dojo.extend(dojo.NodeList, {
	/**
	 * shows selected nodes
	 */
    show: function(){
		 this.forEach(function(item){
	         item.style.display='block';
	     });
        return this;
    },
    /**
     * hides selected nodes
     */
   hide: function(){
        this.forEach(function(item){
        	item.style.display='none';
        });
       return this;
    },
    /**
     * shows or hides selected nodes according to their actual status
     */
    toggle: function(){
        this.forEach(function(item){
            if(item.style.display=='none'){
            	item.style.display='block';
           } else {
        	   item.style.display='none';
            }
        });
        return this;
    },
    
    /**
     * Selects following tag at the same level, if nodeType is specified, first following tag at same level of selected type is selected
     */
    next: function(nodeType){
			if(typeof(nodeType) == "undefined") nodeType=false;
	        var result = dojo.NodeList();
	        this.forEach(function(node){
	                var next = node.nextSibling;
	                var last = node;
	                if(nodeType){
	                   do{ 
	                	   while(next && next.nodeType != 1 ){
	   	                    	next = next.nextSibling;
	   	                   }
	                	   if(typeof(next.isSameNode) != "undefined"){
		   	                   if(!next || next.isSameNode(last)){
			   	                   next=null;
			   	                   break;
		   	                   }
	                	   } else {
	                		   if(!next || next == last){
	                			   next=null;
	                			   break;
	                		   }
	                	   }
	   	                   last = next;
	   	                   if(next.tagName.toLowerCase() == nodeType.toLowerCase()){
		   	                   break;
	   	                   }
	                   } while(next);
	                } else {
	                    while(next && next.nodeType != 1 ){
	                    	next = next.nextSibling;
	                    }
	                }
	                if(next){
	                	result.push(next);
	                } 
	        });
	        return result;
	},
	
	/**
     * Selects previous tag at the same level, if nodeType is specified, first previous tag at same level of selected type is selected
     */
	prev: function(nodeType){
		if(typeof(nodeType) == "undefined") nodeType=false;
	    var result = dojo.NodeList();
	    this.forEach(function(node){
	            var prev = node.previousSibling;
	            var last = node;
	            if(nodeType){
	               do{ 
	            	   while(prev && prev.nodeType != 1 ){
		                    	prev = prev.previousSibling;
	                   }
	            	   if(typeof(prev.isSameNode) != "undefined"){
	            		   if(!prev || prev.isSameNode(last)){
	       	                   prev=null;
	       	                   break;
    	                   }
	            	   } else {
	            		   if(!prev || prev == last){
	            			   prev=null;
	       	                   break;
	            		   }
	            	   }
	                   
	                   last = prev;
	                   
	                   if(prev.tagName.toLowerCase() == nodeType.toLowerCase()){
	                	   break;
	                   }
	               } while(prev);
	            } else {
	                while(prev && prev.nodeType != 1 ){
	                	prev = prev.previousSibling;
	                }
	            }
	            if(prev){
	            	result.push(prev);
	            }
	    });
	    return result;   
	},
	
    /**
     * sets content to all selected nodes
     */
    setContent: function(html){
    	this.forEach(function(item){
	         dojo.html.set(item, html);
	     });
    	return this;
    },
    /**
     * gets innerHTML content from first selected node
     */
    getContent: function(){
    	if(this.length == 0) return null;
    	return this.at(0).attr("innerHTML");
    }
});
