var AsyncLoader = Class.create();
AsyncLoader.prototype = 
{
	initialize:function()
    {
    	this.server = '';
        this.params = '';
        this.element = '';
    },       
    
    success:function(o)
    {
        if(o.responseText !== undefined)
        {
            Element.update(o.argument[0],o.responseText);
        }        
    },
    
    failure:function(o)
    {
        if(o.responseText !== undefined)
        {
            var text = '';
            text+= 'SCRIPT ERROR!\n';
            text+= "Transaction id: "+o.tId+"\n";
            text+= "HTTP status: "+o.status+"\n";
            text+= "Status code message: "+o.statusText;
            alert(text);
        }
    },
    
    load:function()
    {
    	var sUrl = this.server+'?'+this.params; 
        var callback =
        {
          success: this.success,
          failure: this.failure,
          argument: [this.element]
        };        
        var request = YAHOO.util.Connect.asyncRequest('GET',sUrl,callback);
    }
} 

var Tooltip = new Class.create();
Tooltip.prototype = 
{
    
    initialize:function()
    {
        this.style = 'tooltip';        
    },
    
    show:function(element,context)
    {
    	YAHOO.widget.Tooltip.CSS_TOOLTIP = this.style;
        YAHOO.util.Dom.setStyle(context,'opacity', 0.5);
        var tooltip = new YAHOO.widget.Tooltip
        (
            context, 
            {
                effect:
                {
                    effect: YAHOO.widget.ContainerEffect.FADE,
                    duration: 0.25,
                    opacity: { from: 0.1, to: 0.5}
                },
                context:element
            }
        );    
    }	
    
}

var Cal = new Class.create();
Cal.prototype = 
{
	initialize:function(container,input,btn)
	{
	    /***
		Calendar.setup({
	        inputField     :    element,      // id of the input field
	        ifFormat       :    "%d.%m.%Y %I:%M",       // format of the input field
	        showsTime      :    true,            // will display a time selector
	        button         :    btn,   // trigger for the calendar (button ID)
	        singleClick    :    false,           // double-click mode
	        step           :    1                // show all years in drop-down boxes (instead of every other year as default)
	    });	
	    ***/
		var cal = new YAHOO.widget.Calendar("cal-"+input,container,{ title:"Vali kuupäev:", close:true } );
		$(container).style.position = 'absolute';
		$(container).style.display = 'none';
		cal.selectEvent.subscribe(handleSelect,cal,true);
		YAHOO.util.Event.addListener(btn,"click",handleOpen,cal,true);
		// Correct formats for Germany: dd.mm.yyyy, dd.mm, mm.yyyy
		cal.cfg.setProperty("DATE_FIELD_DELIMITER", ".");
		
		cal.cfg.setProperty("MDY_DAY_POSITION", 1);
		cal.cfg.setProperty("MDY_MONTH_POSITION", 2);
		cal.cfg.setProperty("MDY_YEAR_POSITION", 3);
		
		cal.cfg.setProperty("MD_DAY_POSITION", 1);
		cal.cfg.setProperty("MD_MONTH_POSITION", 2);
		
		// Date labels for German locale
		cal.cfg.setProperty("MONTHS_SHORT",   ["Jan", "Vebr", "Mär", "Apr", "Mai", "Jun", "Jul", "Aug", "Sept", "Okt", "Nov", "Dets"]);
		cal.cfg.setProperty("MONTHS_LONG",    ["Jaanuar", "Veebruar", "Märts", "Aprill", "Mai", "Juuni", "Juuli", "August", "September", "Oktoober", "November", "Detsember"]);
		cal.cfg.setProperty("WEEKDAYS_1CHAR", ["P", "E", "T", "K", "N", "R", "L"]);
		cal.cfg.setProperty("WEEKDAYS_SHORT", ["P", "E", "T", "K", "N", "R", "L"]);
		cal.cfg.setProperty("WEEKDAYS_MEDIUM",["P", "E", "T", "K", "N", "R", "L"]);
		cal.cfg.setProperty("WEEKDAYS_LONG",  ["Pühapäev", "Esmaspäev", "Teisipäev", "Kolmapäev", "Neljapäev", "Reede", "Laupäev"]);
		cal.cfg.setProperty("START_WEEKDAY", 1);
		cal.render();	
		function handleSelect(type,args,obj) 
		{
			var dates = args[0]; 
			var date = dates[0];
			var year = date[0], month = date[1], day = date[2];
			// Olemasolev aeg.
			var time = $(input).value.split(' ');
			if (time[1]!=undefined) time = time[1];	
			if (time=='') time = '12:00';	
			if (day<10) day = '0'+day;
			if (month<10) month = '0'+month;	
			$(input).value = day+'.'+month+'.'+year+' '+time;
			cal.hide();
		}	
		function handleOpen(type,args,obj)
		{
			var date = $(input).value.split('.');			
			if (date.length==3) 
			{
				cal.cfg.setProperty("pagedate",date[1]+'.'+date[2],false);
				cal.cfg.setProperty("selected",$(input).value,false);
			} 
			cal.render(); 
			cal.show();
		}
	}	
	
	
}

