
var time = 0;
//var submenuID = 0;
var prevDisplay = 0;

var ppp = 0;


function showSubmenu(id)
{
	if (time)
	{
		clearTimeout(time);
		time = 0;
	}
	
	var img = new Object();
	img['187'] = 'PL/public/images/menu/home_sel.gif';
	img['194'] = 'PL/public/images/menu/nasza_szkola_sel.gif';
	img['193'] = 'PL/public/images/menu/dokumenty_sel.gif';
	img['192'] = 'PL/public/images/menu/zycie_szkoly_sel.gif';
	img['191'] = 'PL/public/images/menu/kontakt_sel.gif';
	img['190'] = 'PL/public/images/menu/galeria_sel.gif';
	img['189'] = 'PL/public/images/menu/rekrutacja_sel.gif';
	img['188'] = 'PL/public/images/menu/linki_sel.gif';
	
	var height = new Object();
	height['187'] = '0';
	height['194'] = '155';
	height['193'] = '0';
	height['192'] = '105';
	height['191'] = '0';
	height['190'] = '55';
	height['189'] = '55';
	height['188'] = '0';
	
	if (prevDisplay != 0 && prevDisplay != id)	
		hide();
	
	if (prevDisplay != id)
	{
			
		document.getElementById('menu'+id).src = img[id];
		var menu = document.getElementById('submenuDiv'+id);
		if (menu)
		{
			
			menu.style.height = '0px';
			if (height[id] != '0')
				menu.style.display='block';
			var attributes = { height: { from: 0, to: height[id] }  };
			var anim = new YAHOO.util.Anim('submenuDiv'+id, attributes, 0.5);
			anim.animate();			
		}
	}
	if (time != 0 && prevDisplay == id)				
		clearTimeout(time);
	
	
	
	prevDisplay = id;
	
	
	
	
	
}

function hideSubmenu()
{			
	//document.getElementById('ppp').innerHTML = 'opuscilem'+ppp++;
	if (time == 0)
		time = setTimeout("hide()", 30 );	
}

function hide()
{		
	var img = new Object();
	img['187'] = 'PL/public/images/menu/home.gif';
	img['194'] = 'PL/public/images/menu/nasza_szkola.gif';
	img['193'] = 'PL/public/images/menu/dokumenty.gif';	
	img['192'] = 'PL/public/images/menu/zycie_szkoly.gif';
	img['191'] = 'PL/public/images/menu/kontakt.gif';
	img['190'] = 'PL/public/images/menu/galeria.gif';
	img['189'] = 'PL/public/images/menu/rekrutacja.gif';
	img['188'] = 'PL/public/images/menu/linki.gif';
	
	var elem = document.getElementById('menu'+prevDisplay);
	if (elem)
		elem.src = img[prevDisplay];
	
	var elem = document.getElementById('submenuDiv'+prevDisplay);
	if (elem)
	{
		elem.style.display='none';
		prevDisplay = 0;
	}
	time = 0;
	prevDisplay=0;
}

function stopHide()
{	
	if (time != 0)
		clearTimeout(time);
	time = 0;
}



var CMScallMiechy = new Array ( "STYCZEŃ", "LUTY", "MARZEC", "KWIECIEŃ", "MAJ", "CZERWIEC", "LIPIEC", "SIERPIEŃ", "WRZESIEŃ", "PAŹDZIERNIK", "LISTOPAD", "GRUDZIEŃ" );
var CMScallToday = 0, CMScallSelMonth = 0, CMScallSelYear = 0;
var CMScallDni = new Array ('NIEDZIELA', 'PONIEDZIAŁEK', 'WTOREK', 'ŚRODA', 'CZWARTEK', 'PIĄTEK', 'SOBOTA' );

var CMScallSelectDays = 0;

function setSelectDays(days)
{
	CMScallSelectDays = days;
}


function initCallendar(today)
{
	CMScallToday = today;
	CMScallSelMonth = today.month-1;
	CMScallSelYear = parseInt(today.year);
	
	
	displayMonthDays();
	setMonth();
	
}

function nextMonth()
{
	if (CMScallSelMonth == 11)
	{
		CMScallSelMonth = 0;
		CMScallSelYear++;
	}
	else
		CMScallSelMonth++;
	
	displayMonthDays();
	
	setMonth();
}

function prevMonth()
{
	
	if (CMScallSelMonth == 0)
	{
		CMScallSelMonth = 11;
		CMScallSelYear--;
	}
	else
		CMScallSelMonth--;
	
	displayMonthDays();
	setMonth();
}

function setMonth()
{
	document.getElementById('CMScallMonth').innerHTML = 
		'<img src="PL/public/images/KalLeftArrow.gif" alt="" style="cursor:pointer;" onclick="prevMonth();">' +
			CMScallMiechy[CMScallSelMonth] + " " + CMScallSelYear +
		'<img src="PL/public/images/KalRightArrow.gif" alt="" style="cursor:pointer;" onclick="nextMonth();">';
}

function displayMonthDays()
{
	//oblicz w jaki dzień tygodnia wypada pierwszy dzień wybranego miesiąca
	var dt = new Date(CMScallSelYear, CMScallSelMonth, 1);
	var pierwszy = dt.getDay();
	var ostatni = LastDayOfMonth(CMScallSelYear, CMScallSelMonth);
	
	//ustal ostatnie widoczne dni poprzedniego miesiaca	
	if (CMScallSelMonth == 0)
	{
		popMonth = 11;
		popYear = CMScallSelYear-1;
	}
	else
	{
		popMonth = CMScallSelMonth-1;
		popYear = CMScallSelYear;
	}
	var popLast = LastDayOfMonth(popYear, popMonth);
	
	//oblicz ile dni poprzedniego miesiąca widać
	var ileStarego = 0;
	pierwszy == 0 ? ileStarego = 6 : ileStarego = pierwszy-1;
	
	
	nastepny = licznik = 1;
	for (var i=1; i<43; i++)
	{
		var elem = document.getElementById('CMScall'+parseInt(i));
		if (elem)
		{
			if (ileStarego != 0)
			{
				CMScallSelMonth == 0 ? prev = 11 : prev = CMScallSelMonth-1;
				prev == 11 ? prevY = CMScallSelYear-1 : prevY = CMScallSelYear;
				var td = ((CMScallToday.month-1) == prev) && ((popLast-ileStarego+1) == CMScallToday.day) && (prevY == CMScallToday.year);
				var select = isSelect(nastepny, prev, prevY);
				var link = getLink(nastepny, prev, prevY);
				
				
				displayDay(elem,popLast-ileStarego+1,false,select,td);
				ileStarego--;
			}
			else if (licznik <= ostatni) //jęsli wyświetlamy dni wybranego miesiąca 
			{				
				var td = ((CMScallToday.month-1) == CMScallSelMonth) && (licznik == CMScallToday.day) && (CMScallSelYear == CMScallToday.year);
				var select = isSelect(licznik, CMScallSelMonth, CMScallSelYear);
				var link = getLink(licznik, CMScallSelMonth, CMScallSelYear);
				displayDay ( elem, licznik++, true, select, td, link );
			}
			else
			{
				CMScallSelMonth == 11 ? next = 0 : next = CMScallSelMonth+1;
				next == 0 ? nextY = CMScallSelYear+1 : nextY = CMScallSelYear;
				var td = ((CMScallToday.month-1) == next) && (nastepny == CMScallToday.day) && (nextY == CMScallToday.year);				
				var select = isSelect(nastepny, next, nextY);
				var link = getLink(nastepny, next, nextY);
				
				displayDay(elem,nastepny++,false,select,td,link);				
			}
		}
		else
		{
			alert('nie znalazlaem CMScall' + i);
			break;
		}
		
			
				
			
	}
	
}

function  isSelect(day, month, year)
{
	day < 10 ? day = '0' + day : '';
	(month+1) < 10 ? month = '0' + (month+1) : '';
	
	var analizowany = '' + year + '-' + month + '-' + day;
	for (var j=0; j<CMScallSelectDays.length; j++ )
		if (analizowany == CMScallSelectDays[j].date)
			return true ;
	
	return false;
}

function getLink(day, month, year)
{
	day < 10 ? day = '0' + day : '';
	(month+1) < 10 ? month = '0' + (month+1) : '';
	
	var analizowany = '' + year + '-' + month + '-' + day;
	for (var j=0; j<CMScallSelectDays.length; j++ )
		if (analizowany == CMScallSelectDays[j].date)
			return CMScallSelectDays[j].link ;
	
	return '';
}

function displayDay(elem, value, inside, sel, today, link)
{
	var clas = '';
	today ? clas +=' today ' : '';
	inside ? clas += ' inside ' : clas += ' outside ';
	sel ? clas += 'sel' : '';
	link != '' ? beginlink='<a class="sel" href="'+link+'">' : beginlink='';
	link != '' ? endlink='</a>' : endlink='';
	
	
	if (elem)
	{
		elem.className = clas;
		elem.innerHTML = beginlink + value + endlink;
	}
	else
		alert('Niezdefiniowana komórka kalendarza');
}

function LastDayOfMonth(Year, Month)
{
   return(new Date((new Date(Year, Month + 1, 1))-1)).getDate();
}

