Wiking.widgets.DatePickerCalendar = (function(){
    function Calendar(basedate,eid,parent){
        this.eid_ = eid;
        this.date_ = basedate;
        this.parent_ = parent;
        parent.disable_();
        Wiking.getMap().addControl(this);
    }
    Calendar.prototype = new GControl(); 
    Calendar.prototype.selectable = function(){return true;}
    Calendar.prototype.printable = function(){return true;}
    Calendar.prototype.initialize = function(map){
        var height = 298;
        var width = 240;
        var html='<div id="calendarx" style="cursor:pointer;position:absolute;left:'+(width-17)+'px;top:-9px;width:27px;height:27px;background: url(\'wimg/x.png\')"></div>'+
                 '<div>'+
                 '<div style="float:left;width:8px;height:39px;background:url(\'wimg/eclefttop.png\')"></div>'+
                 '<div style="float:left;background:url(\'wimg/ectop.png\');height:39px;width:'+(width-16)+'px">'+
                    '<p style="font-size:14px;font-weight:bold;margin-top:12px;margin-left:5px;">Kalendarz</p>'+
                 '</div>'+
                 '<div style="float:left;width:8px;height:39px;background:url(\'wimg/ecrighttop.png\')"></div>'+
                 
                 '<div style="clear:left;float:left;width:8px;background:url(\'wimg/cleft.png\');height:'+(height-47)+'px"></div>'+
                 '<div style="float:left;background:#fff;height:'+(height-47)+'px;width:'+(width-16)+'px">'+
                    //TREŚĆ
                   '<div style="padding:10px;">'+
                      '<div style="text-align:center;color:#666;padding-bottom:6px;border-bottom:1px solid #eee;font-size:10px;font-family:Arial;"><span id="calprevious" style="cursor:pointer;">Poprzedni</span> <span style="font-size:13px;padding:0 4px;font-weight:bold;color:#000;" id="currentMonth"></span> <span style="cursor:pointer;" id="calnext">Następny</span></div>'+
                   '<table id="calendartable" border="0" style="text-align:center;padding-top:29px;margin-top:5px;width:204px; height:204px; background: url(\'/wimg/calendarbg.png\') no-repeat;" cellpadding="0" cellspacing="1">'+
                     '<tr height="28">'+
                        '<td width="28"></td>'+
                        '<td width="28"></td>'+
                        '<td width="28"></td>'+
                        '<td width="28"></td>'+
                        '<td width="28"></td>'+
                        '<td width="28"></td>'+
                        '<td width="28"></td>'+
                     '</tr>'+
                     '<tr height="28">'+
                        '<td></td>'+
                        '<td></td>'+
                        '<td></td>'+
                        '<td></td>'+
                        '<td></td>'+
                        '<td></td>'+
                        '<td></td>'+
                     '</tr>'+
                     '<tr height="28">'+
                        '<td></td>'+
                        '<td></td>'+
                        '<td></td>'+
                        '<td></td>'+
                        '<td></td>'+
                        '<td></td>'+
                        '<td></td>'+
                     '</tr>'+
                     '<tr height="28">'+
                        '<td></td>'+
                        '<td></td>'+
                        '<td></td>'+
                        '<td></td>'+
                        '<td></td>'+
                        '<td></td>'+
                        '<td></td>'+
                     '</tr>'+
                     '<tr height="28">'+
                        '<td></td>'+
                        '<td></td>'+
                        '<td></td>'+
                        '<td></td>'+
                        '<td></td>'+
                        '<td></td>'+
                        '<td></td>'+
                     '</tr>'+
                     '<tr height="28">'+
                        '<td></td>'+
                        '<td></td>'+
                        '<td></td>'+
                        '<td></td>'+
                        '<td></td>'+
                        '<td></td>'+
                        '<td></td>'+
                     '</tr>'+                     
                   '</table>'+
                   '</div>'+
                 '</div>'+
                 '<div style="float:left;width:8px;background:url(\'wimg/cright.png\');height:'+(height-47)+'px"></div>'+
               
                 '<div style="float:left;clear:left;width:8px;height:8px;background:url(\'wimg/cleftbottom.png\')"></div>'+
                 '<div style="float:left;background:url(\'wimg/cbottom.png\');height:8px;width:'+(width-16)+'px"></div>'+
                 '<div style="float:left;width:8px;height:8px;background:url(\'wimg/crightbottom.png\')"></div>'+
               '</div>';
        var div = document.createElement('div');
        div.innerHTML = html;
        div.style.zIndex="300";
        
        map.getContainer().appendChild(div);
        
        var self = this;
        GEvent.addDomListener(document.getElementById('calendarx'),'click',function(){
            map.removeControl(self);
            self.parent_.enable_();
        });
        
        
        this.setCurrentMonth(this.date_);
        this.fill_(this.date_,true);
        this.bind_();
        
        this.prebind = this.previous_.bind(this);
        this.nexbind = this.next_.bind(this);
        $('calprevious').observe('click',this.prebind);
        $('calnext').observe('click',this.nexbind);
        
        this.div_ = div;
        return div;
    };
    Calendar.prototype.setCurrentMonth = function(date)
    {
        var month,m = date.getMonth();
        switch (m)
        {
            case 0: month = "Styczeń"; break;
            case 1: month = "Luty"; break;
            case 2: month = "Marzec"; break;
            case 3: month = "Kwiecień"; break;
            case 4: month = "Maj"; break;
            case 5: month = "Czerwiec"; break;
            case 6: month = "Lipiec"; break;
            case 7: month = "Sierpień"; break;
            case 8: month = "Wrzesień"; break;
            case 9: month = "Październik"; break;
            case 10: month = "Listopad"; break;
            case 11: month = "Grudzień"; break;
        }
        month += " " + String(date.getFullYear()).substring(2);
        document.getElementById('currentMonth').innerHTML=month;
    }
    Calendar.prototype.previous_ = function()
    {
        this.date_.setMonth(this.date_.getMonth()-1);
        this.setCurrentMonth(this.date_);
        this.fill_(this.date_,false); // todo -> gubiony highlight
    }
    Calendar.prototype.next_ = function()
    {
        this.date_.setMonth(this.date_.getMonth()+1);
        this.setCurrentMonth(this.date_);
        this.fill_(this.date_,false); // todo -> gubiony highlight 
    }
    Calendar.prototype.bind_ = function()
    {
        var tds = $$("#calendartable td");
        for (var i=0,l=tds.length; i<l; i++)
        {
            GEvent.addDomListener(tds[i],'click',GEvent.callbackArgs(this,this.pickDate,tds[i].innerHTML));
        }
    }
    Calendar.prototype.fill_ = function(indate,fill)
    {
        var date = new Date(indate.getTime());
        this.rest_ = "-"+this.extend_(date.getMonth()+1)+"-"+date.getFullYear();   
        var tds = $$("#calendartable td");
        var day = date.getDate();
        date.setDate(1);
        var start = date.getDay()-1;
        if (start==-1)
        {
            start=6;
        }       
        date.setDate(32);
        
        if (fill)
        {
            tds[start+day-1].style.backgroundColor="#d69036";
        }
        else
        {
            tds[start+day-1].style.backgroundColor="#fff";
        }
        
        
        for (var j=0, k=start; j<k; j++)
        {
            tds[j].innerHTML="";
        }
        for (var i=start, l=32-date.getDate(), a=1; a<=l; i++, a++)
        {
            tds[i].innerHTML=a;
        }
        for (var h=start+32-date.getDate(), m=tds.length; h<m; h++)
        {
            tds[h].innerHTML="";
        }
    }
    
    Calendar.prototype.extend_ = function(i)
    {
        if (String(i).length==1)
        {
            return "0"+String(i);
        }
        else
        {
            return String(i);
        }
    }
    Calendar.prototype.pickDate = function(td)
    {
        if (td!="")
        {
            document.getElementById(this.eid_).value=this.extend_(td)+this.rest_;
            if (this.eid_=="todate")
            {
                document.getElementById('totime').value="23:59";
            }
            else
            {
                document.getElementById('fromtime').value="00:00";
            }
            Wiking.getMap().removeControl(this);
            this.parent_.enable_();
        }
    }
    
    Calendar.prototype.getDefaultPosition = function(){
        var size = Wiking.getMap().getSize();
        return new GControlPosition(G_ANCHOR_TOP_LEFT,new GSize(size.width/2-459,size.height/2-149));
    };    
    return Calendar;
})();


Wiking.widgets.DatePickerCalendar2 = Class.create(TMapStaticLayer,
{

    clear:function($super)
    {
        $('calprevious').stopObserving('click',this.prebind);
        $('calnext').stopObserving('click',this.nexbind);
        $super();
    }


});

