// JAVASCRIPT LIBRARIES for curent site //
var IE = document.all?true:false;
var font_size = 1;
var font_size_array = new Array(11, 13, 15);
var mainSite = false;



function closePopup()
{
    if($('popup') && mainSite == true)
    {
	$('popup').setStyle({display:'block'});
        $('popup').observe('click', function(){
    	    $('popup').hide();
    	    $('majowka_player').setStyle({display:'block'});
	});
    }
}
/**
 * Ta funkcja wywołuje funkcje pośrednie po załadowaniu treśći strony
 */
function initializer() {
	//bindMenuTop('div#menuTopBox ul.level1 li.item');
	//bindMenuLeft($$('div#menuLeftBox ul.level1 li.item1'),1);
	
	if($('wydzial')) initListGroup('selectz', document.mark_form.wydzial, document.mark_form.osoba, 'leczna');

	var font = czytaj_ciacho("font");
	if(!isNaN(font) && font >=1 && font <=3) {
		font_size=font;
		document.getElementsByTagName("body").item(0).style.fontSize = font_size_array[font-1] + 'px';
	}
	
	new Ajax.Autocompleter("searchInput", "searchResults", "ajax.php?task=search", { indicator: 'indicator', minChars: 3 });
	
	search_results();
	closePopup();
	//gallery_alphabet();
}

function print_news(no, modul, opt) {

	var win = window.open('print_news.php?opt='+opt+'&modul='+modul+'&id='+no, 'Panoramy', 'menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=no,fullscreen=no,channelmode=no,width=650,height='+screen.height+',left='+((screen.width-650)/2)+',top=0');
}

function addToFavorites(title, url)
{
try{
	if (window.sidebar)
		{ // Mozilla Firefox Bookmark
			window.sidebar.addPanel(url, title, "");
		}
	else if( window.external )
		{ // IE Favorite
			 window.external.addFavorite('http://www.powiatleczynski.pl','Powiat Łęczyński');
		}
	else if(window.opera && window.print)
		{ // Opera Hotlist
			return true;
		}
	}
	catch(e)
	{
	alert(e.description);
	}
}


function galley_alphabet() { 
	var links = $$('div#gallery_links a');
	alert(links.length);
	for(var i=0; i<links.length; i++) {
		links[i].onclick = function() {
			alert(links[i].innerHTML);	
		}
	}
}

function search_results() {
	var divs = $$('div.search_nag');
	
	for(var i=0; i<divs.length; i++) {
		
		var id_ = divs[i].className.split(" "); id_ = id_[1];
			$(id_).style.display = 'none';
		var img_layer_ = divs[i].getElementsByClassName('search_nag_2'); img_layer_ = img_layer_[0];
			img_layer_.style.backgroundImage = "url('../images/search_down.gif')";
		
		divs[i].onclick = 	function() { 
								var id = this.className.split(" "); id = id[1];
								var img_layer = this.getElementsByClassName('search_nag_2'); img_layer = img_layer[0];

								if(img_layer.style.backgroundImage.length == 0 || img_layer.style.backgroundImage.indexOf("up")>0) {
									img_layer.style.backgroundImage = "url('../images/search_down.gif')";
									$(id).style.display = 'none';
								} else {
									img_layer.style.backgroundImage = "url('../images/search_up.gif')";
									$(id).style.display = 'block';
								}
							}
	}
}

/**
 * Funkcja podpina obsługe menu górnego
 */
function bindMenuTop(CSSrule) {
	elements = $$(CSSrule);
	for(i=0;i<elements.length;i++) {
		elements[i].onmouseover = displaySubMenu;
		elements[i].onmouseout = hideSubMenu;
		// Event.observe(elements[i],'mouseover',displaySubMenu);
	}
}

/** 
 * Funkcja wyświetla submenu pod posdem elementu rodzica
 */
function displaySubMenu() {
	subs = this.getElementsBySelector('ul');
	sub = subs[0];
	
	if(sub) {
		// position //
		tmp = Position.positionedOffset(this);
		r = { x:tmp[0] , y:tmp[1] };
		sub.style.top = r.y + this.getHeight() + 'px';
		sub.style.left = r.x + 'px';
		
		sub.style.display = 'block';
		// window.dontHide = 1;
	}
}
/**
 * Funkcja chowa submenu 
 */
function hideSubMenu(e) {
	subs = this.getElementsBySelector('ul');
	sub = subs[0];
	
	if (IE) { // grab the x-y pos.s if browser is IE
		tempX = event.clientX + document.body.scrollLeft;
		tempY = event.clientY + document.body.scrollTop;
	}
	else {  // grab the x-y pos.s if browser is NS
		tempX = e.pageX;
		tempY = e.pageY;
	}  	
	
	window.dontHide = 0;
	if(sub) {
		if(Position.within(sub,tempX,tempY)) window.dontHide = 1;	
	
		children = Element.getElementsBySelector(sub,'ul');
		for(i=0;i<children.length;i++) {
			if(children[i].style.display == 'block') { window.dontHide = 1; }
		}
		
		if(sub && window.dontHide!=1) {
			sub.hide();
			clsName = this.className;
			clsName = clsName.sub('item', 'level'); // get level
			hrefs = this.getElementsBySelector('a');
			href = hrefs[hrefs.length-1];
			if(href) href.className = clsName;		
		}
	} else {
			clsName = this.className;
			clsName = clsName.sub('item', 'level'); // get level
			hrefs = this.getElementsBySelector('a');
			href = hrefs[hrefs.length-1];
			if(href&&href.rel!="no_touch") href.className = clsName;		
	}
}

/**
 * Funkcja podpina obsługę menu lewego
 */





function bindMenuLeft(elements,level) {
	var l = level;
	l++;
	for(var i=0;i<elements.length;i++) {
		elements[i].onmouseover = displaySubMenuLeft;
		elements[i].onmouseout = hideSubMenu;
		
		el_array = elements[i].getElementsBySelector('ul li.item'+l);
		if(el_array.length>0) bindMenuLeft(el_array,l);
	}
}
/**
 * Funkcja wyświetla lewe podmenu
 */
function displaySubMenuLeft() {
	subs = this.getElementsBySelector('ul');
	sub = subs[0];
	
	// position //
	tmp = Position.positionedOffset(this);
	r = { x:tmp[0] , y:tmp[1] };	
	
	if(sub) {
		width = 0;
		/* IE FIXES */
		if(IE) {
			// level
			level = this.className.charAt(this.className.length - 1);
			level = parseInt(level);
			
			if(document.documentElement) scrollTop = document.documentElement.scrollTop;
			else scrollTop = document.body.scrollTop			
			if(scrollTop > 0) {
				if(level>1) {
					width = 214 * (level-1);
				}
				// r.y += scrollTop;
			}
		}
		/* END IE FIXES */	
		
		// sub.style.position = 'absolute';
		sub.style.top = r.y  + 'px';
		sub.style.left = r.x + this.getWidth() + width + 'px';
		sub.style.display = 'block';
		// $('searchInput').value = scrollTop;
		// alert(sub.className + "|" + sub.style.left);
	}
	
	
	
	// HOVER //
	clsName = this.className;
	clsName = clsName.sub('item', 'level'); // get level 

	hrefs = this.getElementsBySelector('a');
	href = hrefs[hrefs.length-1];
	if(href) {
		if(sub) href.className = clsName+'_hover';
		else href.className = clsName+'_hover2';
	}
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

/**
 * Funkcja wyświetla podanego flasha
 */
function swf_insert(filename,w,h) {
			var oeTags = '<object type="application/x-shockwave-flash" data="'+filename+'" width="'+w+'" height="'+h+'" id="VideoPlayback" >'
			+ '<param name="movie" value="'+filename+'" />'
			+ '<param name="allowScriptAcess" value="sameDomain" />'
			+ '<param name="quality" value="good" />'
			+ '<param name="bgcolor" value="#FFFFFF" />'
			+ '<param name="scale" value="noScale" />'
			+ '<param name="salign" value="TL" />'
			+ '<param name="wmode" value="transparent" />'
			+ '<param name="FlashVars" value="playerMode=embedded" />'
			+ '</object>';
			var oeTagsIE = '<object type="application/x-shockwave-flash" data="'+filename+'" width="'+w+'" height="'+h+'" id="VideoPlayback"  codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0">'
			+ '<param name="movie" value="'+filename+'" />'
			+ '<param name="allowScriptAcess" value="sameDomain" />'
			+ '<param name="quality" value="good" />'
			+ '<param name="bgcolor" value="#FFFFFF" />'
			+ '<param name="scale" value="noScale" />'
			+ '<param name="salign" value="TL" />'
			+ '<param name="wmode" value="transparent" />'
			+ '<param name="FlashVars" value="playerMode=embedded" />'
			+ '</object>';
		majorVersion=8;
		minorVersion=8;
		installinfo='<div class="get_fla"><a href="https://www.macromedia.com/go/getflashplayer" class="black" target="_blank" ><img src="/images/get_flash.jpg" alt="pobierz Flash Playera" border="0" /><br/></a></div>'; // URL zawierajacy informacje o potrzebie instalacji
		
		if(navigator.product=='Gecko' || navigator.userAgent.indexOf('Opera')!=-1) {
		 if(plugin=navigator.plugins["Shockwave Flash"]) {
		  if(majorVersion > parseInt(plugin.description.substr(plugin.description.indexOf(".")-1, 1))
		  || minorVersion > parseInt(plugin.description.substr(plugin.description.length-2)) )
			document.write(installinfo);  // pobieranie flasha
		  else document.write(oeTags);
		 } else {
			document.write(installinfo);  // pobieranie flasha 
		 }
		}
		else document.write(oeTagsIE);
		//-->
}


function change_size(type) {
	
	switch(type) {
		case 'lower': if(font_size>1) font_size--; break;
		case 'upper': if(font_size<3) font_size++; break;
	}
	document.getElementsByTagName("body").item(0).style.fontSize = font_size_array[font_size-1] + 'px';
	
	var waznosc=new Date();
	var tresc=font_size;
	tresc=escape(tresc);
	waznosc.setTime(waznosc.getTime()+1000*60*60*24*365);
	document.cookie="font=" + tresc + ";expires=" + waznosc.toGMTString();
	//tworzy cookie, parametr


}

function czytaj_ciacho(nazwa) {

	nazwa+="=";
	//uzupełnia nazwę
	
	startCookie=document.cookie.indexOf(nazwa);
	//sprawdza czy istnieje cookie o podanej nazwie
	
	if (startCookie==-1) {return ""}
	//jeżeli nie istnieje
	
	startCookie+=nazwa.length;
	//początek treści cookie
	
	if (document.cookie.indexOf(";",startCookie)==-1)
	//jeżeli jest więcej cookie
	{
		koniecCookie=document.cookie.length;
		//jeżeli nie ma podstawia jako koniec cookie, koniec całego tekstu
	} else {
		koniecCookie=document.cookie.indexOf(";",startCookie);
		/*
		podstawia pod koniec miejsce występowania ";"
		(czyli początek następnego cookie)
		*/
	}
	
	textCookie=document.cookie.substring(startCookie,koniecCookie);
	//kopiuje treść cookie
	
	textCookie=unescape(textCookie);
	//rozkodowuje treść
	
	return textCookie;
	//zwraca wartość
}

function show_mail(user,domena,kraj,attr) {
	var start = "<a href=\"mailto:"+user+"@"+domena+"."+kraj+"\" title=\"\""
	+ attr + ">";
	var end = "</a>";
	document.write(start+user+"@"+domena+"."+kraj+end);
	return 0;
}

function getDocRoot() {
	if (navigator.appName.indexOf("Microsoft") != -1)	
		return window;
	else 
		return document;
}

function setMovie(id) {
	var root = getDocRoot();	
	root.movie_player.videoPath('_filebase/moviebase/'+id+'.flv');
}

function submit_form(formid,objid) {
	advAJAX.submit($(formid), {
		onSuccess : function(obj) {
			if(objid) { 
				HTMLcode = obj.responseText;
				$(objid).innerHTML = HTMLcode;
				if(HTMLcode.indexOf("mark_not_send")>0) {
					after_send(1); 
				} 
				if(HTMLcode.indexOf("mark_send")>0) {
					after_send(2);
				} 
				if(HTMLcode.indexOf("!--comment-->")>0) {
					after_send(3); 
				} 
				if(HTMLcode.indexOf("!--OK-->")>0) {
					after_send(4); 
				}
				if(HTMLcode.indexOf("!-- newsletter -->")>0) {
					after_send(5); 
				}
				if(HTMLcode.indexOf("!-- tellafriend -->")>0) {
					after_send(6); 
				}
				if(HTMLcode.indexOf("!-- tellafriend OK -->")>0) {
					after_send(7); 
				}
				if(HTMLcode.indexOf("!-- contact -->")>0) {
					after_send(8); 
				}
				if(HTMLcode.indexOf("!-- contact OK -->")>0) {
					after_send(9); 
				}
				
			}
		},
		onError : function(obj) { $(objid).innerHTML = 'ERROR while loading <br/> ' + obj.status; }
	});
}

function vote(form,txt) {
	var ok = false;
	for(var i=0; i<form.odp.length; i++) {
		if(form.odp[i].checked) ok = true;
	}
	if(ok) {
		$('submit_div').innerHTML = txt+'...';
		$('submit_div').style.backgroundImage = 'url(../images/my_loading.gif)';
		$('submit_div').style.backgroundPosition = 'right 50%';
		$('submit_div').style.backgroundRepeat = 'no-repeat';
		$('submit_div').style.padding = '3px 19px 0 0';
		$('submit_div').style.height = '17px';
		//$('submit_div').style.width = '68px';
		$(form).action = "ajax.php?task=ankieta";
		//new Ajax.Updater('results','ajax.php?task=ankieta');
		submit_form(form.id,'results');
	} else {
		alert('Prosimy wybrać odpowiedź na pytanie, a nastepnie nacisnąć „głosuj”.');	
	}
	return false;
	
}

function add_mail(form,txt) {

$('newsletter_button').innerHTML = txt+'...';
	$('newsletter_button').style.backgroundImage = 'url(../images/my_loading.gif)';
	$('newsletter_button').style.backgroundPosition = 'right 50%';
	$('newsletter_button').style.backgroundRepeat = 'no-repeat';
	$('newsletter_button').style.padding = '3px 19px 0 0';
	$('newsletter_button').style.height = '19px';
	//$('newsletter_button').style.width = '90px';
	$(form).action = "ajax.php?task=newsletter";
	submit_form(form.id,'newsletter_result');
	return false;
	
}

function send_mark(form,txt) {

	$('out_button').innerHTML = txt+'...';
	$('out_button').style.backgroundImage = 'url(../images/my_loading.gif)';
	$('out_button').style.backgroundPosition = 'right 50%';
	$('out_button').style.backgroundRepeat = 'no-repeat';
	$('out_button').style.padding = '3px 19px 0 0';
	$('out_button').style.height = '18px';
	//$('out_button').style.width = '82px';
	$(form).action = "ajax.php?task=mark";
	submit_form(form.id,'mark_result');
	return false;
	
}

function after_send(action) {
	if(action == 1) {
		$('mark_result').style.padding = '7px 0';
		$('out_button').style.backgroundImage = 'none';
		$('out_button').style.padding = '0';
		$('out_button').style.height = '';
		$('out_button').style.width = '';		
		$('out_button').innerHTML = '<input type=\"submit\" value=\"wyślij\" class=\"button\" />';
	} else if(action == 3) { 
		$('comm_submit_div').style.backgroundImage = 'none';
		$('comm_submit_div').style.padding = '0';
		$('comm_submit_div').style.height = '';
		$('comm_submit_div').style.width = '';		
		$('comm_submit_div').innerHTML = '<input type=\"submit\" value=\"wyślij\" class=\"button\" />';	
		var losowa = Math.round(Math.random()*10000);
		$('token').style.backgroundImage = 'url(../scripts/token.php?id='+losowa+')';
		var losowa = Math.round(Math.random()*10000);
		$('token').style.backgroundImage = 'url(../scripts/token.php?id='+losowa+')';
		$('tokeen123').value = '';
	} else if(action == 4) { 
		$('comm').value = '';
	} else if(action == 5) { 
		$('newsletter_button').style.backgroundImage = 'none';
		$('newsletter_button').style.padding = '0';
		$('newsletter_button').style.height = '';
		$('newsletter_button').style.width = '';		
		$('newsletter_button').innerHTML = '<input type=\"submit\" value=\"zapisz\" class=\"button\" />';		
	} else if(action == 6) {
		$('tellafriend_submit_div').style.backgroundImage = 'none';
		$('tellafriend_submit_div').style.padding = '0';
		$('tellafriend_submit_div').style.height = '';
		$('tellafriend_submit_div').style.width = '';		
		$('tellafriend_submit_div').innerHTML = '<input type=\"submit\" value=\"wyślij\" class=\"button\" />';
	} else if(action == 7) {
		$('taf_1').value = '';
		$('taf_2').value = '';
	} else if(action == 8) {
		$('contact_submit_div').style.backgroundImage = 'none';
		$('contact_submit_div').style.padding = '0';
		$('contact_submit_div').style.height = '';
		$('contact_submit_div').style.width = '';		
		$('contact_submit_div').innerHTML = '<input type=\"submit\" value=\"wyślij\" class=\"button\" />';
	} else if(action == 9) {
		$('imie').value = '';
		$('mail').value = '';
		$('firma').value = '';
		$('telefon').value = '';
		$('pytanie').value = '';
	}
}

function tellafriend(form,txt) {
		$('tellafriend_submit_div').innerHTML = txt+'...';
		$('tellafriend_submit_div').style.backgroundImage = 'url(../images/my_loading.gif)';
		$('tellafriend_submit_div').style.backgroundPosition = 'right 50%';
		$('tellafriend_submit_div').style.backgroundRepeat = 'no-repeat';
		$('tellafriend_submit_div').style.padding = '3px 0 0 0';
		$('tellafriend_submit_div').style.height = '19px';
		$('tellafriend_submit_div').style.width = '82px';
		$(form).action = "ajax.php?task=tellafriend";
		submit_form(form.id,'tellafriend_result');
	return false;	
}

function contact(form,txt) {
		$('contact_submit_div').innerHTML = txt+'...';
		$('contact_submit_div').style.backgroundImage = 'url(../images/my_loading.gif)';
		$('contact_submit_div').style.backgroundPosition = 'right 50%';
		$('contact_submit_div').style.backgroundRepeat = 'no-repeat';
		$('contact_submit_div').style.padding = '3px 0 0 0';
		$('contact_submit_div').style.height = '19px';
		$('contact_submit_div').style.width = '82px';
		$(form).action = "ajax.php?task=contact";
		submit_form(form.id,'contact_result');
	return false;	
}

function comment(form) {
		$('comm_submit_div').innerHTML = 'Dodawanie...';
		$('comm_submit_div').style.backgroundImage = 'url(../images/my_loading.gif)';
		$('comm_submit_div').style.backgroundPosition = 'right 50%';
		$('comm_submit_div').style.backgroundRepeat = 'no-repeat';
		$('comm_submit_div').style.padding = '3px 0 0 0';
		$('comm_submit_div').style.height = '19px';
		$('comm_submit_div').style.width = '82px';
		//$('token').style.backgroundImage = 'none';
		$(form).action = "ajax.php?task=comment";
		submit_form(form.id,'comm_result');
	return false;	
}

function check_length(field, int) {
	var st = field.scrollTop;
	if(field.value.length > int) field.value = field.value.substr(0,int);	
	field.scrollTop = st;
}

function maps(sel) {
						var version = deconcept.SWFObjectUtil.getPlayerVersion();
						if (document.getElementById && version["major"] < 7) {
							
						} else {
							var so = new SWFObject("mapa_all.swf", "mapka", "450", "360", "7", "#FFFFFF");
							so.addVariable("cat", sel.value);
							so.addParam("scale", "noscale");
							so.addParam("wmode", "transparent");
							so.write("baza_mapa");
						}
	
	return false;
}

function showPanorama() {
	//alert(screen.height);
	window.open('/panoramy/?955', 'Panoramy', 'menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=no,resizable=no,fullscreen=no,channelmode=no,width='+screen.width+',height='+screen.height+',left=0,top=0');
}


/** INITIALIZE ONLOAD FUNCTIONS **/
Event.observe(window, 'load', initializer, false);

	
