window.addEvent('domready',function(){
    $$('.defvalue').each(function(item,index){
        item.addEvents({
            'focus':function(){
                if(this.get('defvalue')==null||this.get('defvalue')==''||this.value==this.get('defvalue')){
                    this.set('defvalue', this.value);
                    this.value='';
                }
            },
            'blur':function(){
                if(this.value.trim()=='') this.value=this.get('defvalue');
            }
        });
    });
    var catSlide = new Array();
    var el = new Array();
    $$('.opensubcat').each(function(item,index){
        el[index] = item.get('id')+'_child';
        catSlide[index] = new Fx.Slide(el[index]);
        catSlide[index].hide();
        item.addEvent('click',function(e){
            //e.stop();
            catSlide[index].toggle();
            var parent = $(el[index]).getParent('div');
            item.set('src','/images/miniarr_'+(parent.getStyle('height').toInt()>0?'down':'up')+'.gif');
            item.set('alt',(parent.getStyle('height').toInt()>0?gettext('Развернуть'):gettext('Свернуть')));
        });
    });
    $try(function(){
        document.id('subcat_'+slidein_cat).fireEvent('click');
        success = true;
    }, function(){
        failure = true;
    });
    $$('.selector').each(function(item,index){
        handlSelect(item.get('id'));
		
    });
    if(Browser.Engine.trident){
        document.addEvent('click',function(e){
            hideSelect(e);
        });
    }
    else{
        window.addEvent('click',function(e){
            hideSelect(e);
        });
    }
});

function clicked_on(el,e){
    var click_x = e.page.x;
    var click_y = e.page.y;
    var coords = el.getCoordinates();
    var x_beg = coords.left;
    var x_end = x_beg+coords.width;
    var y_beg = coords.top;
    var y_end = y_beg+coords.height;
    if((click_x>=x_beg && click_x<=x_end) && (click_y>=y_beg && click_y<=y_end) && el.getStyle('display')!='none' && el.getStyle('visibility')!='hidden' && el.getStyle('visibility')!='collapse'){
        return true;
    }
    else{
        return false;
    }
}
function gettext(text){
    return text;
}
function hideSelect(e){
    $try(function(){
        $$('.selector').each(function(item,index){
            if(clicked_on(document.id('other'+item.get('id')+'s'),e)==false && clicked_on(item,e)==false){
                document.id('other'+item.get('id')+'s').setStyle('display','none');
            }
        });
        success = true;
    }, function(){
        failure = true;
    });
}
function handlSelect(el){
    $try(function(){
        document.id(el).addEvent('click', function(e){
            $try(function(){
                $$('.selector').each(function(item,index){
                    if(item.get('id')!=el.trim()){
                        if(clicked_on(document.id('other'+item.get('id')+'s'),e)==false && clicked_on(item,e)==false){
                            document.id('other'+item.get('id')+'s').setStyle('display','none');
                        }
                    }
                });
                success = true;
            }, function(){
                failure = true;
            });
            if(document.id('other'+el+'s').getStyle('display')=='block'){
                document.id('other'+el+'s').setStyle('display','none');
            }
            else{
                var coords = this.getCoordinates();
                document.id('other'+el+'s').setStyles({
                    'display':'block',
                    'border':'none',
                    'top':coords.bottom,
                    'left':coords.left+(Browser.Engine.trident?0:1),
                    'width':coords.width
                });
            }
            return false;
        });
        document.id('other'+el+'s').getElements('div').each(function(item,index){
            item.addEvents({
                'mousemove':function(){
                    this.setStyles({
                        'background-color':'#007DC2'
                    });
                    this.getChildren().each(function(item2,index2){
                        item2.setStyles({
                            'color':'#fff',
                            'border':'none'
                        });
                    });
                },
                'mouseout':function(){
                    this.setStyles({
                        'background-color':'#EBEFE9'
                    });
                    this.getChildren().each(function(item2,index2){
                        item2.setStyles({
                            'color':'#0096C7',
                            'border-bottom':(item2.get('tag')=='a'?'dotted 1px #0096c7':'')
                        });
                    });
                },
                'click':function(){
					
                    document.id(el).set('html',this.get('html')+"<img class='open' src='/images/arrow_down.gif' alt='Раскрыть' />");
                    document.id(el).getElements('a').each(function(item,index){
                        item.setStyles({
                            'color':''
                        });
                    });
                    if(this.getElement('a').get('href').test('^#')==false && this.getElement('a').get('href').trim()!='') window.location.href = this.getElement('a').get('href').trim();
                    else if(this.getElement('a').get('href').test('^#') && this.getElement('a').get('href').trim()!='#') {
                        newCalRender(this.getParent().get('id'));
                    // AjaxRequest(this.getParent().get('id'), this.getElement('a').get('href').trim());
                    }
                    document.id('other'+el+'s').setStyle('display','none');
                    return false;
                }
            });
        });
    });
}
function AjaxRequest(type, value){
    if(type == 'otheryearsels' || type == 'othermonsels'){
        var expr = new RegExp('#[0-9]{1,}$', 'ig');
        var mon =  $('monsel').getElement('a').get('href').match(expr);
        var year = $('yearsel').getElement('a').get('href').match(expr);
        alert();
        new Request({
            method:'post',
            url:'/ajax.php',
            data:{
                'type':'cal/'+year+'/'+mon+'/'+lang+'/'+url,
                'time':Math.random()
            },
            onSuccess:function(answer){
                $('archive_days').set('html',answer);
            }
        }).send();
    }
}

function newCalRender(type){
    try{
        if(type != 'otheryearsels' && type != 'othermonsels') return false;
        var mon =  $('monsel').getElement('a').get('href').substr(1);
        var year = $('yearsel').getElement('a').get('href').substr(1);
        if (mon.indexOf('ttp:') > -1){
            mon = mon.split('#').pop();
        }
        if (year.indexOf('ttp:') > -1){
            year = year.split('#').pop();
        }
        var tmp_str = '';
        var total = cal_array[year][mon].length;
        for (var i = 0; i < total; i++) {
            var tmp_day = cal_array[year][mon][i];
            var now = new Date();
            if (now.getFullYear() == year && (now.getMonth()+1) == mon && now.getDate() == (i+1)) var tmp_class = 'selected'; else var tmp_class = '';
            tmp_str += (tmp_day==0?'<span class="'+tmp_class+'">'+(i+1)+'</span>':'<a href="/'+lang+'/deyatelnost/cal/'+year+'/'+mon+'/'+(i+1)+'" class="'+tmp_class+'">'+(i+1)+'</a>');
        }
        $('archive_days').set('html',tmp_str);
    } catch (e){
        //alert('year - '+year+' mon - '+mon+' | error - '+ e.description);
    }
    return false;
}
