$(document).ready(function(){

	$('#links').css('opacity','0.5');
	$('#links').hover(function(){$(this).css('opacity','1')}, function(){$(this).css('opacity','0.5')});
	$('#sitename').pngFix();
	$('sup[title]').tooltip({showURL: false, delay: 0, width: 200});
	$('#gal$parent a').lightBox();
	
/* карта */	
	$.fn.maphilight.defaults = {	fill: true, fillColor: '3E92B4', fillOpacity: 0.6, stroke: true, strokeColor: '707070', strokeOpacity: 1, strokeWidth: 1, fade: false, alwaysOn: false}
	$('img[usemap]').maphilight();
	$('.blag_menu a').hover(function(){$.tooltip.blocked = true; $('#s'+$(this).attr('rel')).mouseover()}, function(){$('#s'+$(this).attr('rel')).mouseout(); $.tooltip.blocked = false;});
	
	function out_item(pause) {
		if (pause) {
			tm=setTimeout("$('#inf"+curr_item+"').hide();",200);
		} else {
			$('#inf'+curr_item).hide();
		}
	}
	
	function in_item(item) {
		out_item(0);
		curr_item = item;
		if (window.tm) clearTimeout(tm);
		$('#inf'+curr_item).show();
	}
	curr_item=1;	

	$('area').each(function(){
		h = Math.min(-$('#'+$(this).attr('id')+'i').height()-5,-130);
		$(this).tooltip({
		bodyHandler: function() {
			return $('#'+$(this).attr('id')+'i').html();
		},
		showURL: false,
		track: true,
		delay: 0,
		fixPNG: true,
		left: -60,
		top: h,
		extraClass: 'ballon'
		});
	});

	$('.map_names a').each(function(){
		$(this).tooltip({
		bodyHandler: function() {
			return $('#'+$(this).attr('id')+'i').html();
		},
		showURL: false,
		track: true,
		delay: 0,
		fixPNG: true,
		left: -60,
		top:  -$('#'+$(this).attr('id')+'i').height()-20,
		extraClass: 'ballon'
		});
	});

	$('#switch a').mouseover (function(){
		if (!$(this).hasClass('active')) {
		$('#search').toggle();
		$('#auth').toggle();
		$('#switch a').removeClass('active');
		$(this).addClass('active');
		}
	});
	
	$('#onoff a').click (function(){
		if (curr_item) {
			$('.map_names').hide();
			$('#onoff a').html('показать объекты на карте');
			curr_item = 0;
		} else {
			$('.map_names').show();
			$('#onoff a').html('скрыть объекты на карте');
			curr_item = 1;
		}
	});
	
/* main menu */	

	$('ul.m:not(#menu1 ul li a.active+a)').hide();  
	$('#sub'+$('#menu1 ul li a.active').attr('id')).css({display:'block'});
	$('#menu1 ul li a:not(#menu1 ul.m,#menu1 ul li ul.m li a)').click(function(){
		if ($('#sub'+$(this).attr('id')).val()==''){
		 	is_show = ($('#sub'+$(this).attr('id')).css('display')=='block');
		 
		$('ul.m').slideUp();
		if (!is_show) $('#sub'+$(this).attr('id')).slideToggle(300);
		return false;
}
	});	

/* calendar */
	now = new Date();
	nowday = now.getDate();
	nowmonth = now.getMonth();
	nowyear  = now.getFullYear();
	
	var month_ru = ['Январь','Февраль','Март','Апрель','Май','Июнь','Июль','Август','Сентябрь','Октябрь','Ноябрь','Декабрь'];
	var table = "<table id='cal'><thead><tr><th>Пн</th><th>Вт</th><th>Ср</th><th>Чт</th><th>Пт</th><th>Сб</th><th class='vs'>Вс</th></tr></thead>";
	table +="<tfoot><tr><td colspan='7' class='month'><img src='/images/1/loader.gif' id='cal_loader' width='32' height='32' alt='' /><form action='#'><select name='month' onchange='change_month(this.value)'>";	
	for (i=0; i<12; i++) {
		var im = i > 8 ? i+1 : "0"+(i+1);
		var selected = i==nowmonth ? " selected='selected'":"";
		table +="<option value='"+im+"'"+selected+">"+month_ru[i]+"</option>";
	}
	table +="</select></form></td></tr></tfoot><tbody>";
	table +="</tbody></table>";
	$('#calendar').append(table);
	im = nowmonth > 8 ? nowmonth+1 : "0"+(nowmonth+1);
	change_month(im);
});

function change_month(id) {
	var month = id-1;
	var year = nowyear;
	
	var days = getDaysInMonth(month,year);
	var firstDayDate=new Date(year,month,1);
	var firstDay=firstDayDate.getDay();
	var prev_m = month == 0 ? 11 : month-1;
	var prev_y = prev_m == 11 ? year - 1 : year;
	var prev_days = getDaysInMonth(prev_m, prev_y);
	firstDay = (firstDay == 0 && firstDayDate) ? 6 : firstDay-1;
	var tbody = '<tr>';
	var j = 0;
	
	for (i=0;i<42;i++){
		if (i<firstDay){
			tbody += ('<td><span>'+ (prev_days-firstDay+i+1) +'</span></td>');
		} else if ((i>=firstDay+getDaysInMonth(month,year))) {
			j = j+1;
			tbody += ('<td><span>'+ j +'</span></td>');
		} else {
			day = i-firstDay+1;
			fday = (day<10) ? '0'+day : day;
			tbody += ('<td'+(((day==nowday) && (month==nowmonth))?' class=active':'')+'><a href=\'/calendar/?date='+ year+''+id+''+fday+'\'>'+day+'</a></td>');
		}
		if (i%7==6) tbody += ('</tr>');
            }

 	$('#cal tbody').html(tbody);	
 	
 	$('#cal tbody a').click (function(){
 		$('#cal tbody td.active').removeClass('active');
 		$('#cal_loader').css('visibility','visible');
 		$(this).parent().addClass('active');
		$.get($(this).attr('href')+'&template=82', function(data){
			$('#cal_info').html(data);
 			$('#cal_loader').css('visibility','hidden');
 		});
		return false;
	});

}

function getDaysInMonth(month,year)  {
	var daysInMonth=[31,28,31,30,31,30,31,31,30,31,30,31];
	if ((month==1)&&(year%4==0)&&((year%100!=0)||(year%400==0))) {
		  return 29;
	} else {
	  return daysInMonth[month];
	}
}
