
$(function(){
	starUpardeStatistics.init();
});

var starUpardeStatistics = (function(window, $, undefined){
	var _uparde = {};
	
	//初始化方法
	_uparde.init = function init(){
		
		_uparde.initDatePicker();
		
		_uparde.initButton();
	}
	
	_uparde.initButton = function initButton(){
		//数据展示按钮
		var upardeButton = _uparde.createInput('upardeButton', 'button', 'upardeButton');
		upardeButton.value = 'query';
		document.getElementById('queryButton').innerHTML = upardeButton.outerHTML;
		//数据展示按钮--注册事件
		var upardeButton = document.getElementById("upardeButton");
		upardeButton.onclick = _uparde.loadDataGridData('data');
		
		//clear 按钮
		var clearButton = _uparde.createInput('clearButton', 'button', 'clearButton');
		clearButton.value = 'clear';
		document.getElementById('clearButton').innerHTML = clearButton.outerHTML;
		//clear 按钮--注册事件
		var clearButton = document.getElementById("clearButton");
		clearButton.onclick = _uparde.clear;
	}
	
	/**清空按钮*/
	_uparde.clear = function clear(){
		$('#beginDate').datebox('setValue','');
		$('#endDate').datebox('setValue','');
		$('#vhcid').val("");
	}
	
	/**加载表格数据*/
	_uparde.loadDataGridData = function loadDataGridData(displayType){
		return function(){
			var beginDate = $('#beginDate').datebox('getValue');
			var endDate = $('#endDate').datebox('getValue');
			$("#start").val(beginDate);
			$("#end").val(endDate);
			$("#querylist").submit();
		}
	}

	_uparde.initDatePicker = function initDatePicker(){
		debugger;
		//1.创建input
		var beginDateInput = _uparde.createInput('beginDate', 'text', 'upardeDatebox');
		document.getElementById('beginDateContainer').innerHTML = beginDateInput.outerHTML;
		
		var endDateInput = _uparde.createInput('endDate', 'text', 'upardeDatebox');
		document.getElementById('endDateContainer').innerHTML = endDateInput.outerHTML;
		
		//2.设置时间控件
		$('.'+beginDateInput.className).datebox({
			formatter : function(date){
				return date.formatDate("yyyy-MM-dd");
			},
			parser: _uparde.parseDate
		});
	}
	
	/**
	*创建input标签
	*param id	input标签的id
	*param type input标签的type
	*param className input标签的class
	*return inputObj input对象
	*/
	_uparde.createInput = function createInput(id, type, className){
		var inputObj = document.createElement('input');
		inputObj.id = id;
		inputObj.type = type;
		inputObj.className = className;
		return inputObj;
	}
	
	//把时间格式字符串转化为时间
	//如下格式   
	//2006   
	//2006-01   
	//2006-01-01   
	//2006-01-01 12   
	//2006-01-01 12:12   
	//2006-01-01 12:12:12
	_uparde.parseDate = function parseDate(dateStr) {
		if($.trim(dateStr)==""){
			return new Date();
		}
	    var regexDT = /(\d{4})-?(\d{2})?-?(\d{2})?\s?(\d{2})?:?(\d{2})?:?(\d{2})?/g;  
	    var matchs = regexDT.exec(dateStr);
	    var date = new Array();
	    for (var i = 1; i < matchs.length; i++) {
	        if (matchs[i]!=undefined) {
	            date[i] = matchs[i];
	        } else {
	            if (i<=3) {
	                date[i] = '01';  
	            } else {
	                date[i] = '00';
	            }
	        }
	    }
	    return new Date(date[1], date[2]-1, date[3], date[4], date[5],date[6]);
	}
	
	//为date类添加一个format方法
	//yyyy 年
	//MM 月
	//dd 日
	//hh 小时
	//mm 分
	//ss 秒
	//qq 季度
	//S  毫秒
	//author: meizz
	Date.prototype.formatDate = function (format){
	    var o = {  
	        "M+": this.getMonth() + 1, //month   
	        "d+": this.getDate(),    //day   
	        "h+": this.getHours(),   //hour   
	        "m+": this.getMinutes(), //minute   
	        "s+": this.getSeconds(), //second   
	        "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter   
	        "S": this.getMilliseconds() //millisecond   
	    }  
	    if (/(y+)/.test(format)){
	    	format = format.replace(RegExp.$1,(this.getFullYear() + "").substr(4 - RegExp.$1.length));
	    }  
	    for (var k in o){
	    	if(new RegExp("(" + k + ")").test(format)){
	    		format = format.replace(RegExp.$1,RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
	    	}
	    }
	    return format;
	}
	
	return _uparde;
})(window,jQuery);