(function($){
	var initLayout = function() {
		var hash = window.location.hash.replace('#', '');
		var currentTab = $('ul.navigationTabs a')
							.bind('click', showTab)
							.filter('a[rel=' + hash + ']');
		if (currentTab.size() == 0) {
			currentTab = $('ul.navigationTabs a:first');
		}
		showTab.apply(currentTab.get(0));
		$('#date').DatePicker({
			flat: true,
			date: '2008-07-31',
			current: '2008-07-31',
			calendars: 1,
			starts: 1,
			view: 'years'
		});
		var now = new Date();
		ngay = now.getDate();
		var ngay = (ngay < 10) ? '0' + ngay : ngay;
		thang = now.getMonth()+1;
		//var thang = (thang < 10) ? '0' + thang : thang;
		now.addDays(-10);
		var now2 = new Date();
		now2.addDays(-5);
		now2.setHours(0,0,0,0);
		
		$('#clearSelection').bind('click', function(){
			$('#date3').DatePickerClear();
			return false;
		});
		
		$('.inputDate').DatePicker({
			format:'d/m/Y',
			date: $('#inputDate').val(ngay),
			date: $('#inputMonth').val(thang),
			current: $('#inputDate').val(),
			starts: 1,
			position: 'right',
			onBeforeShow: function(){
			    
				$('#inputDate').DatePickerSetDate($('#inputDate').val(), true);
			},
			onChange: function(formated, dates){
				
				$('#inputDate').val(formated);
				$('#inputDate').DatePickerHide();
				
				//alert($('#inputDate').val());
				var arr = $('#inputDate').val().split('/');
				
				
				
				
				$.ajax({
					 type: "POST",
					 url: "/FE/ws.asmx/lay_lich_su_kien",
					 data: "{'s_date': '" + $('#inputDate').val() + "'}",
					 contentType: "application/json; charset=utf-8",
					 dataType: "json",
					 success: function(msg) {
						 AjaxSucceeded(msg);
					 },
					 error: AjaxFailed
				 });
				 
				 function AjaxSucceeded(result) {
					set_data_lich_su_kien(result);
				  }
				  
				  function AjaxFailed(result) {
					  alert(result.status + ' ' + result.statusText);
				  } 
				//$('#inputDate').val($('#inputDate').val().substr(0,5));
				
				$('#inputDate').val(arr[0]);
				if (arr[1].substr(0,1) == "0") arr[1] = arr[1].substr(1,1);
				$('#inputMonth').val(parseInt(arr[1]));
			}
		});
		var now3 = new Date();
		now3.addDays(-4);
		var now4 = new Date()
		$('#widgetCalendar').DatePicker({
			flat: true,
			format: 'd B, Y',
			date: [new Date(now3), new Date(now4)],
			calendars: 3,
			mode: 'range',
			starts: 1,
			onChange: function(formated) {
				$('#widgetField span').get(0).innerHTML = formated.join(' &divide; ');
			}
		});
		var state = false;
		$('#widgetField>a').bind('click', function(){
			$('#widgetCalendar').stop().animate({height: state ? 0 : $('#widgetCalendar div.datepicker').get(0).offsetHeight}, 500);
			state = !state;
			return false;
		});
		$('#widgetCalendar div.datepicker').css('position', 'absolute');
	};
	
	var showTab = function(e) {
		var tabIndex = $('ul.navigationTabs a')
							.removeClass('active')
							.index(this);
		$(this)
			.addClass('active')
		$('div.tab')
			.hide()
				.eq(tabIndex)
				.show();
	};
	
	EYE.register(initLayout, 'init');
})(jQuery)