if(Calend==undefined)var Calend={}

Calend.ControlforSub=function(element){
    var _buttonSub=element;var _sub=function(){
        var email=$('#email').attr('value');if(!checkEmail(email)){
            alert('Проверьте e-mail на корректность!');return false;
        }
        else{
            _buttonSub.attr('value','...');$.post("/subscribe/","email="+email,_subCallback,"json");
        }
    }
    var _subCallback=function(js){
        var email=$('#email').attr('value');if(js.success){
            $('#email').attr('value','');
            _buttonSub.attr('value','OK');
            $("#subresult").html("Принят запрос на подписку адреса <b>"+email+"</b> на рассылку \"Календарь событий\". На этот адрес отправлено письмо с просьбой подтвердить подписку. <br />Спасибо за Ваш выбор! ");
        }
        else
        {
            _buttonSub.attr('value','OK');
            $("#subresult").html(js.formErrors.join('\n'));
        }
    }
    _buttonSub.bind('click',_sub);
}

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

$(document).ready(function() {

	try{
		$("object").prepend('<param name="wmode" value="opaque" />'); 
	} catch(e) {
	
	}
	

//left calendarik    
	$('#close-layer').hide();
	
	$('#search-input').bind('focus',function() {
        var thisText=$(this).val();
        if(thisText=='РїСЂР°Р·РґРЅРёРє, РёРјСЏ, РґР°С‚Р°, СЃРѕР±С‹С‚РёРµ, РіРѕСЂРѕРґ, СЃС‚СЂР°РЅР°, СЂРµР»РёРіРёСЏ'){
            $(this).css('color','#000').val('');
        }
        });
    
	$('#search-form a').bind('click',function(){
        var thisText=$(this).text();
        $('#search-input').css('color','#000').focus().val(thisText);
        return false;
    });
	
    
    $('#drop-rss').hide();
    $('#open-drop-rss').bind('click',function() {
        var docHeight=$(document).height();
        $('#close-layer').show().css('height',docHeight+'px');
        $('#drop-rss').slideToggle('fast');
        $('#close-layer').bind('click',function(){
            $('#drop-rss').fadeOut('fast');
            $('#close-layer').unbind('click').hide();
            $('#drop-rss').unbind('mouseenter mouseleave');
        });
        return false;
    });
    
    
    $('#cal-list-months').hide();
    
    $('#cal-current-month').bind('click',function() {
        var currentMonth=$(this).attr('rel');
        var listMonthsTop=(currentMonth-1)*24+5;
        $('#cal-list-months').css('top','-'+listMonthsTop+'px').fadeIn('fast');
        var docHeight=$(document).height();
        $('#close-layer').show().css('height',docHeight+'px');
        
        $('#cal-list-months li[rel="'+currentMonth+'"]').bind('click',function(){
            $('#close-layer').unbind('click').hide();
            $('#cal-list-months').fadeOut('fast');
            $('#cal-list-months li[rel="'+currentMonth+'"]').unbind('click');
        });
        
        $('#cal-list-months a').bind('click',function() {
            var newMonth=$(this).parent('li').attr('rel');
            var newYear = Number($('#cal_year').text());
            var newMonthsTop=(newMonth-1)*24+5;
            var thisText=$(this).text();
            $('#cal-9').load('/ajaxhtmlcalendar/?month='+newMonth+ "&year=" + newYear + "&urlcalend="+$('#urlcalend').attr('value'));
            $('#cal-current-month').attr('rel',newMonth).text(thisText);
            
            $('#cal-list-months li').each(function() {
                if(!$(this).is(':has(a)')){
                    $(this).wrapInner('<a href="#"></a>');
                }
             });
             
         	$(this).parent('li').empty().text(thisText);$('#cal-list-months').animate({
    	      top:'-'+newMonthsTop+'px'
              },500,function() {
        	      $(this).hide();
        	      $('#cal-list-months').unbind('mouseenter mouseleave');
        	      $('#close-layer').unbind('click').hide();
            });
            return false;
        });
        
        $('#close-layer').bind('click',function() {
            $('#cal-list-months').fadeOut('fast');$('#close-layer').unbind('click').hide();
        });
        return false;
    });

    $('#cal-prev, #cal-next').bind('click',function() {
        $('#cal-list-months').hide();$('#close-layer').unbind('click').hide();
        var currentMonth=$('#cal-current-month').attr('rel');

        var newYear = Number($('#cal_year').text());

        if($(this).attr('id')=='cal-prev')
        {
            var newMonth=currentMonth*1-1;
            if(currentMonth==1) {
                newMonth=12;
                var newYear = newYear-1;
            }
        }else{
            var newMonth=currentMonth*1+1;if(currentMonth==12){
                newMonth=1;
                var newYear = newYear+1;
            }
        }
        $('#cal_year').text(newYear);
        if(newMonth>0&&newMonth<13) {
            var newText=$('#cal-list-months li[rel="'+newMonth+'"]').text();
            
            $('#cal-list-months li').each(function(){
                if(!$(this).is(':has(a)')){
                    $(this).wrapInner('<a href="#"></a>');
                }
                });
                $('#cal-list-months li[rel="'+newMonth+'"]').empty().text(newText);
                $('#cal-current-month').attr('rel',newMonth).text(newText);
                $('#cal-9').load('/ajaxhtmlcalendar/?month='+newMonth+'&year='+newYear+"&urlcalend="+$('#urlcalend').attr('value'));
        }
        return false;
    });
    
    $('#cal-list-months').hover(null,function(){
        $(this).hide()
        });$('#auserselm').bind('click',function()

        {
        var currentMonth=$(this).attr('rel');
        var listMonthsTop=(currentMonth-1)*24+5;
        $('#cal-list-months').css('top','-'+listMonthsTop+'px').fadeIn('fast');
        var docHeight=$(document).height();
        $('#close-layer').show().css('height',docHeight+'px');
        
        $('#cal-list-months li[rel="'+currentMonth+'"]').bind('click',function() {
            $('#close-layer').unbind('click').hide();$('#cal-list-months').fadeOut('fast');$('#cal-list-months li[rel="'+currentMonth+'"]').unbind('click');
        });
        
        $('#cal-list-months a').bind('click',function() {
            var newMonth=$(this).parent('li').attr('rel');
            var newMonthsTop=(newMonth-1)*24+5;
            var thisText=$(this).text();
            $('#auserselm').attr('rel',newMonth).text(thisText);
            
            $('#cal-list-months li').each(function() {
                if(!$(this).is(':has(a)')){
                    $(this).wrapInner('<a href="'+$(this).parent('li').attr('url')+'"></a>');
                }
            });
            $(this).parent('li').empty().text(thisText);
            $('#cal-list-months').animate({top:'-'+newMonthsTop+'px'}, 500, function(){$(this).hide();$('#cal-list-months').unbind('mouseenter mouseleave');$('#close-layer').unbind('click').hide();});
            document.location=$(this).attr('href');
            return false;
        });
        
        $('#close-layer').bind('click',function() {
            $('#cal-list-months').fadeOut('fast');
            $('#close-layer').unbind('click').hide();
        });
        return false;
    });

    $('table.calendar-table td:nth-child(6n)').addClass('saturday');
	$('table.calendar-table td:nth-child(7n)').addClass('sunday');
	
//-------------------------------------------------------------    
//top informer    
    $('#events-full').hover(function() {
        $('#events-today-expand, #events-tommorow-expand, #a-tomafter').show();
    },function(){
        $('#events-today-expand, #events-tommorow-expand, #a-tomafter').hide();
    });

    $('.moon-info').hover(function(event){
        var _p = $(this).parent('p:first') ;
        var _e = $('#moon-' + $(this).attr('day'));
        $('#moon-' + $(this).attr('day')).show();
        var offset = $(this).offset(); 
        var offset2 = $(this).attr('day')=='today' ? $("#events-today").offset() : $("#events-tommorow").offset() ;
        _e.css('margin-left', (offset.left - offset2.left));
    }, function(event){
        $('#moon-' + $(this).attr('day')).hide();
    });	

//-------------------------------------------------------------    
//menu     
    $('#top-menu td').hover(function() {
        if($(this).hasClass('selected'))return;
        $(this).addClass('active');
        if($(this).hasClass('first')){
            $(this).addClass('activefirst');
        }else if($(this).hasClass('last')){
            $(this).addClass('activelast');
        }
        },	function() {
        		if($(this).hasClass('selected'))return;
        		$(this).removeClass('active');
        		if($(this).hasClass('first')){
	            	$(this).removeClass('activefirst');
    	    	}else if($(this).hasClass('last')){
        		    $(this).removeClass('activelast');
        		}
	});

	$('#a_add_cal').bind('click', function(event) {
        $('#menu-2').hide();
        $('#open-reg-form-over').show();
		
        if($('#add-calends').is(':visible'))
        {
            $('#add-calends').fadeOut('fast');
        }
        else {
			
            pos=getElementPosition("a_add_cal"); 
            var mouseX=pos.left;
            var mouseY=pos.top + 50;
            fid = 'add-calends';
            if(mouseX>0 && mouseY>0) {
                $('#' + fid).css('top', mouseY-14);
                $('#' + fid).css('left', document.body.scrollLeft+mouseX-145);
            }
            $('#add-calends').fadeIn('fast');
        }
    });
    
    $('.rounded').corners("4px");
    $('#add-calends a').hover(function(event){
        $('#add-calends').css('background','#AABA6B');
    }, function(event){
        $('#add-calends').css('background','#E9F6B3');
    });
   
//-------------------------------------------------------------
//undefinded

    $('div.slink-container').hover(function(){
        $(this).children('div.drop3').show();
    },function(){
        $(this).children('div.drop3').hide();
    });
    
    $('div.drop5-container').children('div.drop5').hide();
    $('div.drop5-container').hover(function(){
        $(this).children('div.drop5').show();
    },function(){
        $(this).children('div.drop5').hide();
    });

    new Calend.ControlforSub($('#buttonSubscribe'));

    informerHeightControl();
    $(window).resize(function() {
        informerHeightControl();
    });

});

var informerHeightControl = function() {
    var h = $('#sinformer').height() ? $('#sinformer').height() : $('#sinformer').css('height').replace('px', '');
    try {
 	  $('#top-menu').css('marginTop', h-100 + 38 + 'px');
	} catch(e) {
//		alert(e)
	}
}

//*****************************************************
