var DP=Class.create();
DP.prototype={
	initialize:function(aligner, reciever, displayer, options){
		this.callback=null;
		this.editingYear=false;
	},
	show:function(aligner, reciever, displayer, options,e){
		this.settings=options;
		this.aligner=$(aligner);
		this.reciever=$(reciever);
		this.displayer=$(displayer);
		if(this.settings){
		if(this.settings.notime){this.NoTime=this.settings.notime;}else{this.NoTime=false;}}else{this.NoTime=false;}		
		if(this.reciever!=null){
			if(this.reciever.value!='' && this.reciever.value!=0){
				this.dt=new Date(this.reciever.value*1000);
			}else{
				this.dt=new Date();
			}
		}else{
			if(this.settings.time){
				this.dt=new Date(this.settings.time*1000);
			}else{
				this.dt=new Date();
			}
		}
		if(this.settings && this.settings.callback){
			this.callback=this.settings.callback;
		}
		if($('date_picker_div')==null){
			var cont=new Element('div', {'id':'date_picker_div'});
		}else{
			var cont=$('date_picker_div');
		}
		Element.makePositioned(this.aligner);
		Element.makePositioned(this.aligner.parentNode);
		document.body.appendChild(cont);
		var posParms={'offsetTop':17,'setWidth':false,'setHeight':false};
		/*Kolla ända upp till body om man är i en fixed*/
		var infixed=checkIfInFixed(this.aligner);
		if($('destinator_adminbar') != null){
			var pos=$('destinator_topholder').getStyle('position');
			if(pos=='fixed'){
				cont.setStyle({'z-index':'999',position:'fixed'});
				var off=Element.cumulativeScrollOffset(this.aligner);
				posParms.offsetTop=17+off['top'];
			}
		}else{
			if(infixed){
				cont.setStyle({'z-index':'999',position:'fixed'});
				var off=Element.cumulativeScrollOffset(this.aligner);
				posParms.offsetTop=17+off['top'];				
			}else{
				cont.setStyle({'z-index':'999',position:'absolute'});
			}
		}
		/*Kontrollera så inte date-pickern hamnar utanför fönstret...*/
		
		var elmW=Element.getWidth(cont);
		var alignoffset=Element.viewportOffset(this.aligner)['left'];
		var docWidth=document.viewport.getWidth();
		if(elmW+alignoffset > docWidth){
			posParms['offsetLeft']=-Element.getWidth(cont);
		}
		
		Element.clonePosition(cont, this.aligner, posParms);
		hideWindowedObjects();
		this.browserwindow=cont;
		this.getCalendar();
		Event.observe(document, 'click', removeDP);
		Event.observe($('date_picker_div'), 'click', this.stopevent.bindAsEventListener(this));
		Event.observe($('date_picker_year'), 'click', this.editYear.bindAsEventListener(this));
		Event.stop(e);
	},
	editYear:function(e){
		Event.stop(e);
		if(!this.editingYear){
			var y=$('date_picker_year').innerHTML;
			var html='<input type="text" id="dp_year_input" value="'+y+'" /><input type="button" id="dp_year_select" value="'+lang('OK')+'" />';
			$('date_picker_year').update(html);
			Event.observe($('dp_year_select'), 'click', this.changeYear.bindAsEventListener(this));
			this.editingYear=true;
		}
	},
	changeYear:function(e){
		Event.stop(e);
		var y = $('dp_year_input').value;
		this.dt.setFullYear(y);
		this.editingYear=false;
		this.getCalendar();
	},
	zero:function(reciever, displayer,e){
		try{
			$(reciever).value=0;
			$(displayer).update('--');
		}catch(ev){}
	},
	stopevent:function(e){
		Event.stop(e);
	},
	previousMonth:function(utc,e){
		Event.stop(e);
		this.collect(utc, false);
		this.getCalendar();
	},
	nextMonth:function(utc,e){
		Event.stop(e);
		this.collect(utc, false);
		this.getCalendar();
	},
	previousYear:function(utc,e){
		Event.stop(e);
		this.collect(utc, true);
		this.getCalendar();
	},
	today:function(e){
		Event.stop(e);
		this.dt=new Date();
		this.getCalendar();
	},
	nextYear:function(utc,e){
		Event.stop(e);
		this.collect(utc, true);
		this.getCalendar();
	},
	setDay:function(utc,e){
		Event.stop(e);
		$$('.date_selected').each(function(elm, idx){Element.removeClassName(elm,'date_selected');})
		var elm=Event.findElement(e, 'td');
		Element.addClassName(elm, 'date_selected');
		this.dt.setTime(utc);
	},
	collect:function(utc, setmonth){
		var dts=new Date(utc);
		dts.setHours(this.dt.getHours());
		dts.setMinutes(this.dt.getMinutes());
		dts.setDate(this.dt.getDate());
		if(setmonth){
			dts.setMonth(this.dt.getMonth());
		}
		this.dt=dts;
	},
	setHour:function(hour,e){
		Event.stop(e);
		this.dt.setHours(hour);
	},
	setMinute:function(minute,e){
		Event.stop(e);
		this.dt.setMinutes(minute);
	},
	getCalendar:function(){
		var st=(this.dt.getTime()/1000);
		var vars='UTC='+st;
		if(this.NoTime){
			vars = vars +'&noTime=1';
		}
		this.editingYear=false;
		new Ajax.Request('/admin/ajax/ajax_date.php', {method:'post', postBody:vars, asynchronous:false, onSuccess:this.redraw.bind(this)});
	},
	redraw:function(t){
		this.browserwindow.update(t.responseText);
		Event.observe($('date_picker_year'), 'click', this.editYear.bindAsEventListener(this));
	},
	submit:function(e){
		if(!this.NoTime){
			this.setHour($F('datepicker_hour'),e);
			this.setMinute($F('datepicker_minute'),e);
		}else{
			this.dt.setHours(0);
			this.dt.setMinutes(0);
		}
		if(this.reciever && this.reciever!=null){
			this.reciever.value=Math.round((this.dt.getTime()/1000));
		}
		var mn=this.dt.getMonth()+1;
		if(mn<10){mn='0'+mn;}
		var dy=this.dt.getDate();
		if(dy<10){dy='0'+dy;}
		var hr=this.dt.getHours();
		if(hr<10){hr='0'+hr;}
		var min=this.dt.getMinutes();
		if(min<10){min='0'+min;}
		if(this.displayer && this.displayer!=null){
			if(!this.NoTime){
				this.displayer.update(this.dt.getFullYear()+'-'+mn+'-'+dy+' '+hr+':'+min);
			}else{
				this.displayer.update(this.dt.getFullYear()+'-'+mn+'-'+dy);
			}
		}
		if(this.callback && this.callback != null){
			this.callback((this.dt.getTime()/1000));
		}
		this.editingYear=false;
		removeDP(e);
	},
	close:function(e){
		if(e){
			Event.stop(e);
		}
		Element.remove(this.browserwindow);
	}
};
function removeDP(e){
	showWindowedObjects();
	datePicker.close(e);
	Event.stopObserving(document, 'click', removeDP);
}
var datePicker=new DP();
