function unescHtml(t) {
    var i,e={
        '<':'&lt;',
        '>':'&gt;',
        '&':'&amp;',
        '"':'&quot;'
    };for(i in e)t=t.replace(new RegExp(i,'g'),e[i]);return t;
}

$(document).ready(function() {
	$('input.user_rids').attr('checked',true);
   $('input.user_rids').live('click',function(){
        if(!$(this).attr('checked')){
            $('div.rid'+$(this).val()).hide();
            $('tr.rid'+$(this).val()).hide();
        }else{
            $('div.rid'+$(this).val()).show();
            $('tr.rid'+$(this).val()).show();
        }
   });
   
   $('input.user_cids').attr('checked',true);
   $('input.user_cids').live('click',function(){
        if(!$(this).attr('checked')){
            $('div.cid'+$(this).val()).hide();
            $('tr.cid'+$(this).val()).hide();
        }else{
            $('div.cid'+$(this).val()).show();
            $('tr.cid'+$(this).val()).show();
        }
   });
    
    $('a.calendars-return').bind('click',function() {
        $('a.calendars-edit').text('редактировать');
        $('a.calendars-edit').parent('em').removeClass('save');
        var containerId=$('a.calendars-edit').parent('em').parent('div').parent('div').attr('id');
        
        $('#'+containerId+' ul.calendars-list > li').each(function(){
            if($(this).hasClass('green')){
                liText=$(this).children('span.hide').text();
                var liInput=$(this).children('input[type=checkbox]');
                $(this).empty().text(liText).prepend(liInput);
            }
            $(this).children('input[type=checkbox]').show();
            $(this).children('span.close').remove();
            $(this).children('div.calendars-edit-stage').hide();
            if($(this).attr('fordelete')=='1') {
                $(this).attr('fordelete','');$(this).show();
            }
            rid=$(this).attr('rid');
            mcid=$(this).attr('mcid');
            $("input[name=delete_rids]").val('');
            $("input[name=delete_mcids]").val('');
            if(rid>0) {
                $('div.rid'+rid).show();
                $('tr.rid'+rid).show();
            }
            if(mcid>0){
                $('div.cid'+mcid).show();
                $('tr.cid'+mcid).show();
            }
        });
        $('div.add').show();
        $(this).css('display','none');
    });
    
    $('a.calendars-edit').live('click',function(){
        if($(this).parent('em').hasClass('save')){
            _form=$('#mycalendform');
            var qString=_form.formSerialize();
            $.post("/ajaxeditcalend/",qString, null, "json");
            $('a.calendars-return').css('display','none');
            $(this).text('редактировать');
            $(this).parent('em').removeClass('save');
            var containerId=$(this).parent('em').parent('div').parent('div').attr('id');
            
            $('#'+containerId+' ul.calendars-list > li').each(function(){
                if($(this).hasClass('green')){
                    var liText=$(this).children('input[type=text]').val();
                    if(liText=='')liText=$(this).children('span.hide').text();
                    var liInput=$(this).children('input[type=checkbox]');
                    $(this).empty().text(liText).prepend(liInput);
                }
                $(this).children('input[type=checkbox]').show();
                $(this).children('span.close').remove();
                $(this).children('div.calendars-edit-stage').hide();
            });
            $('div.add').show();
        }
        else {
        	    $('div.add').hide();
        	    $('a.calendars-return').css('display','');
        	    $(this).text('сохранить');
        	    $(this).parent('em').addClass('save');
        	    var containerId=$(this).parent('em').parent('div').parent('div').attr('id');
        	    $('#'+containerId+' ul.calendars-list  > li').each(function() {
                	if($(this).hasClass('green')){
                    	var liText=$.trim($(this).text());
                    	liText=unescHtml(liText);
                    	var liInput=$(this).children('input');
                    	$(this).empty().html('<span class="hide">'+liText+'</span></a><input type="text" class="f06" name="mycalends['+$(this).attr('mcid')+']" value="'+liText+'" />').prepend(liInput);
                	}
                	$(this).children('input[type=checkbox]').hide().after('<span class="close">&nbsp;</span>');
                	$(this).children('div.calendars-edit-stage').show();
            	});
            	$('#'+containerId+' div.calendars-add-more').hide();
        }
        return false;
    });
    
    
    $('span.close').live('click',function(){
        rid=$(this).parent('li').attr('rid');
        mcid=$(this).parent('li').attr('mcid');
        if(rid>0){
            $('div.rid'+rid).hide();
            $('tr.rid'+rid).hide();
            $("input[name=delete_rids]").val($("input[name=delete_rids]").val()+':'+rid);
        }
        if(mcid>0) {
            $('div.cid'+mcid).hide();
            $('tr.cid'+mcid).hide();
            $("input[name=delete_mcids]").val($("input[name=delete_mcids]").val()+':'+mcid);
        }
        $(this).parent('li').attr('fordelete','1');
        $(this).parent('li').hide();
    });

        $('a.calendars-add-more-open').live('click',function(){
        $(this).next('div.calendars-add-more').show();return false;
    });
    
    $('input.calendars-add-more-check-all').live('click',function(){
        var numAll=$(this).parent('span').next('span').next('div').children('ul').children('li').children('input').length;
        if($(this).is(':checked')){
            $(this).parent('span').next('span').next('div').children('ul').children('li').children('input').each(function(){
                $(this).attr('checked','checked');
            });
            var numChecked=numAll;
        }else{
            $(this).parent('span').next('span').next('div').children('ul').children('li').children('input').each(function(){
                $(this).attr('checked','');
            });
            var numChecked=0;
        }
        $(this).parent('span').next('span').text(numChecked+' РёР· '+numAll);
    });
    
    $('input.calendars-add-more-check').live('click',function(){
        var numAll=$(this).parent('li').parent('ul').children('li').children('input').length;var numChecked=$(this).parent('li').parent('ul').children('li').children('input').filter(':checked').length;$(this).parent('li').parent('ul').parent('div').prev('span').text(numChecked+' РёР· '+numAll);
    });
    
    $('a.calendars-add-more-close').live('click',function(){
        $(this).parent('div').parent('div').parent('div').parent('div').hide();return false;
    });
    
    $('input.calendars-add-more-submit').live('click',function(){
        var numAll=$(this).parent('div').prev('div').children('ul').children('li').children('input').length;var numChecked=$(this).parent('div').prev('div').children('ul').children('li').children('input').filter(':checked').length;$(this).parent('div').parent('div').parent('div').parent('div').prev('a').text(numChecked+' РёР· '+numAll+' РґРѕР±Р°РІР»РµРЅРѕ');$(this).parent('div').parent('div').parent('div').parent('div').hide();if($(this).parent('div').parent('div').parent('div').parent('div').parent('div').parent('li').prev('li').children('div.calendars-edit-stage').is(':hidden')){
            $(this).parent('div').parent('div').parent('div').parent('div').parent('div').hide();
        }
        return false;
    });
    
    $('a.calendars-add-open').bind('click',function(){
        var containerId=$(this).attr('rel');if($('#'+containerId+' a.calendars-edit').parent('em').hasClass('save')){
            alert('Добавление личных календарей невозможно в режиме редактирования.');
        }else{
            $(this).next('div.calendars-add').toggle();
        }
        return false;
    });
    
    $('input.calendars-add-input').bind('focus',function(){
        if($(this).val()=='РЅР°С‡РЅРёС‚Рµ РІРІРѕРґРёС‚СЊ РЅР°Р·РІР°РЅРёРµ'){
            $(this).val('').css('color','#000');
        }
        });
    
	$('input.calendars-add-input').bind('keypress',function(){
        if($(this).parent('p').prev('span').children('input.calendars-add-server').is(':checked')){
            $(this).parent('p').next('div').show();
        }
    });
        
    $('div.calendars-add-typing > ul > li > a').live('click',function(){
        var newVal=$(this).text();$(this).parent('li').parent('ul').parent('div').prev('p').children('input.calendars-add-input').val(newVal);$(this).parent('li').parent('ul').parent('div').hide();return false;
    });
    
    $('input.calendars-add-submit').bind('click',function() {
        var calendarName=$('input.calendars-add-input').val();
        var ulObject=$(this).parent('div').parent('div').parent('div').parent('div').prev('ul');
        if(calendarName!='РЅР°С‡РЅРёС‚Рµ РІРІРѕРґРёС‚СЊ РЅР°Р·РІР°РЅРёРµ'){
            if($(this).prev('div').prev('p').prev('span').children('input.calendars-add-server').is(':checked')){
                $.get('ajax/calendars-add.html',{},function(html){
                    $(ulObject).append(html);$(ulObject).children('li').filter(':last').children('div.calendars-edit-stage').show();$(ulObject).children('li').filter(':last').children('div.calendars-edit-stage').children('div.calendars-add-more').show()
                });
            } else {
                	if(calendarName!=''){
                    		$(this).val('Подождите...');
                    		$(this).css('disabled','true');
                    		$.post("/ajaxaddcalend/","name="+calendarName,_callbackAddCalend1,"json");
                	}
                }
            }
        $(this).parent('div').parent('div').parent('div').hide();return false;
    });
    
    var _callbackAddCalend1=function(js) {
        if(js.success==1){
            ulObject=$('ul.calendars-list');ulObject.append('<li class="green" mcid='+js.id+'><input class="user_cids" type="checkbox" value=" '+js.id+'" checked="checked" /> '+js.name+'</li>');$('input.calendars-add-submit').val('Добавить');$('input.calendars-add-submit').css('disabled','false');$('input.calendars-add-input').val('');
        }
    }
    
    $('div.block10, div.block10-b, div.block10-l, div.block10-r, div.block10-t-r, div.block10-t-l, div.block10-b-r, div.block10-b-l').css('background','none');$('div.block10-b-l span.open, div.block10-b-l span.alarm, div.block10-b-l span.delete, div.block10-b-l span.add-comment').hide();
    
    $('div.block10-b-l').hover(function(){
        $(this).removeAttr('style');
        $(this).parent('div').removeAttr('style');
        $(this).parent('div').parent('div').removeAttr('style');
        $(this).parent('div').parent('div').parent('div').removeAttr('style');
        $(this).parent('div').parent('div').parent('div').parent('div').removeAttr('style');
        $(this).parent('div').parent('div').parent('div').parent('div').parent('div').removeAttr('style');
        $(this).parent('div').parent('div').parent('div').parent('div').parent('div').parent('div').removeAttr('style');
        $(this).parent('div').parent('div').parent('div').parent('div').parent('div').parent('div').parent('div').removeAttr('style');
        $(this).parent('div').parent('div').parent('div').parent('div').parent('div').parent('div').parent('div').parent('div').removeAttr('style');
        $(this).children('div').children('span.open').show();
        $(this).children('div').children('span.alarm').show();
        $(this).children('div').children('span.add-comment').show();
        $(this).children('div').children('div').children('span.open').show();
        $(this).children('div').children('div').children('span.alarm').show();
        $(this).children('div').children('div').children('span.delete').show();
        $(this).children('div').children('div').children('span.add-comment').show();
    },function(){
        $('div.block10, div.block10-b, div.block10-l, div.block10-r, div.block10-t-r, div.block10-t-l, div.block10-b-r, div.block10-b-l').css('background','none');$('div.block10-b-l span.open, div.block10-b-l span.alarm, div.block10-b-l span.delete, div.block10-b-l span.add-comment').hide();
    });$('div.block10-b-l span.delete a').bind('click',function(){
        if(confirm('Хотите удалить это событие из личного календаря?')){
            eid=$(this).attr('eid');
            $.post("/ajaxeventdeletefrompersonal/",'usereventid='+eid,null,"json");
            $('tr[eventid='+$(this).attr('eid')+']').remove();
        }
        });$('#a_rem').bind('click',function(){
        if($(this).parent('em').hasClass('save')){
            $(this).parent('em').removeClass('save');
            $(this).text('настроить');
            $('#remdiv').css('display','none');
            if($('#remsel').val()<1){
                $('div.remember span.pr').html('<b>не предлагать</b>');t='';
            }
            else{
                $('div.remember span.pr').html('всегда предлагать');
            }
            if($('#remsel').val()==1)t='за 1 день';
            if($('#remsel').val()==2)t='за 2 дня';
            if($('#remsel').val()==3)t='за 3 дня';
            if($('#remsel').val()==5)t='за 5 дней';
            $('div.remember span.pd').text(t);
            $('div.remember').css('display','block');
            $.post("/ajaxsavereminddefault/","day="+$('#remsel').val(),null,"json");
        }
        else{
            $(this).parent('em').addClass('save');
            $(this).text('сохранить');
            $('div.remember').css('display','none');
            $('#remdiv').css('display','block');
        }
	});
	
	
    $('#show_all').attr('checked',true);
    $('#show_wc').live('click',function(){
        if($(this).attr('checked')) {
            $('tr.noc').hide();$('table.bigcalend td > div.ev').each(function(){
                if(!$(this).children('div.drop5-container').html()){
                    $(this).hide();
                }
            });
        }
    });
    
    $('#show_all').live('click',function() {
        if($(this).attr('checked')){
            $('tr.noc').show();$('table.bigcalend td > div.ev').each(function(){
                $(this).show();
            });
        }
   });

	
	$('a.grey-dashed-list-2').hover(function(event) {
	    $('#aboutnamedates').show();
		}, function(){
    	$('#aboutnamedates').hide();
	});
	
});

function add_new_row() {
    var currrow;
    currow=document.getElementById('tbl').rows.length;
    numb++;
    document.getElementById('tbl').insertRow(currow);
    document.getElementById('tbl').rows[currow].insertCell(0);
    document.getElementById('tbl').rows[currow].insertCell(1);
    str_2='<input type="file" name="image_array['+numb+']" />';
    document.getElementById('tbl').rows[currow].cells[0].innerHTML=str_2;
    document.getElementById('tbl').rows[currow].cells[1].innerHTML='<input type=checkbox value=1 name=image_delete['+numb+']>';
}

	