$(document).ready(function() {
	$('input:checkbox').checkbox({cls:'jquery-safari-checkbox'});
});


function podpowiedz(url, l, keyword, box, blokada, nad, ul_kod, nazwa_miasta)
{
	/*
	key_bl = document.getElementById(blokada).value;
	if (key_bl == 'off') {
		setTimeout("podpowiedz_fu('"+url+"', "+l+", '"+keyword+"', '"+box+"', '"+blokada+"', '"+nad+"', '"+ul_kod+"', '"+nazwa_miasta+"')", 300);		
		document.getElementById(blokada).value = 'on';
	}
	*/
}

function podpowiedz_fu(url, l, keyword, box, blokada, nad, ul_kod, nazwa_miasta)
{
	value = document.getElementById(keyword).value;
	
	if (nad != 'off') ident_v = document.getElementById(nad).value;
	else ident_v = 0;
	
	var length = value.length;

	if (length > l)
	{
		$.ajax({
			data: { key : value, ident: ident_v, ul_kod: ul_kod, nazwa_miasta: nazwa_miasta },
			type: "POST",
			url: url,
			success: function(html){
				$("#"+box).html(html);
			}
		});
	} else {
		document.getElementById(box).style.display = 'none';
	}
	document.getElementById(blokada).value = 'off';
}

function podpowiedz_close(nazwa, keyword, box, id_name, id_value)
{
	document.getElementById(keyword).value = nazwa;
	if (id_name != 'off') document.getElementById(id_name).value = id_value;
	document.getElementById(box).style.display = 'none';
}

function ShowSubMenu(kat_lp,kat_back_lp){
	   var lp=1;
	   while (document.getElementById('gm_'+lp)) {
		if (lp==kat_lp) {
			document.getElementById('gm_'+lp).className='on';
		} else {
			document.getElementById('gm_'+lp).className='off';
		}
 	    	lp++;
	   }
	   var lp=1;
	   while (document.getElementById('sm_'+lp)) {
		if (lp==kat_lp) {
			document.getElementById('sm_'+lp).style.display='block';
		} else {
			document.getElementById('sm_'+lp).style.display='none';
		}
 	    	lp++;
	   }
	   if (kat_back_lp)
	   	setTimeout("ShowSubMenu("+kat_back_lp+",0)",1250);			
}


function pokaz_obiekty(base_url, map, x, y, cat, rodzic, id, typ)
{	
	zaznacz = document.getElementById(id).checked;
	
	var status;
	
	if (zaznacz == true) status = 'dodaj';
	if (zaznacz == false) status = 'kasuj';

	obiekty(base_url, map, x, y, cat, rodzic, status, typ);
	
	if (rodzic != 0)
	{
		el = document.getElementsByName('n'+id);
		
		var mi=0;
		for (mi=0; mi<=el.length; mi++)
		{
			el[mi].checked = zaznacz;
		}
	}
}


var marker_tab = new Array();

function obiekty(base_url, map, x, y, cat, rodzic, status, typ, rozszerz)
{	
	$.getJSON(base_url+'pobierz_obiekty_miasta/'+x+'/'+y+'/'+cat+'/'+rodzic+'/'+typ+'/', ab=function(json){
			
		
		for(i = 0; i < json.length; i++) {
			if (status == 'dodaj')
			{
				var ikona = new GIcon();
				ikona.image = json[i]['ico'];
				ikona.iconAnchor = new GPoint(6, 20);
				ikona.infoWindowAnchor = new GPoint(5, 1);
	
				var point = new GLatLng(json[i]['lat'], json[i]['lon']);
				var marker = dodaj_marker(point, ikona, base_url, json[i]['id_lokalizacji']);
				map.addOverlay(marker);
				
				if (json[i]['lat'] != "" && json[i]['lon'] != "" && (json[i]['lat'] != 0 || json[i]['lon'] !=0)) obszar.extend(point);
			}
			
			if (status == 'kasuj')
			{
				map.removeOverlay(marker_tab[json[i]['id_lokalizacji']]);
			}
		}
	
		// wyciągamy zoom
		var nowyZoom = map.getBoundsZoomLevel(obszar);
	
		// wyciągamy środek obszaru
		var nowyPunkt = obszar.getCenter();
			
		// centrujemy do tego punktu na poziomu zoom
		if (rozszerz == 'on') map.setCenter(nowyPunkt,nowyZoom);
		
	})
}

function dodaj_marker(point, ikona, base_url, id)
{
	var marker = new GMarker(point, ikona);
	marker_tab[id] = marker;
	
	GEvent.addListener(marker,'click',function()
	{
		$.ajax({
			url: base_url+'pobierz_jeden_obiekt/'+id,
			success: function(html){
				marker.openInfoWindowHtml(html);
			}
		});
	});
 
	return marker; 
}

function fotki(map, x, y, obrazek, link, title, autor, autor_url)
{
	icon = new GIcon();
	icon.image = obrazek;
	icon.iconSize = new GSize(20, 20);
	icon.iconAnchor = new GPoint(6, 20);
	icon.infoWindowAnchor = new GPoint(5, 1);

	var point = new GLatLng(x, y); 
	var marker = new GMarker(point, icon);
	map.addOverlay(marker);
	
	GEvent.addListener(marker,'click',function()
	{
		var html = '<div style="float: left; margin-right: 10px"><a href="'+link+'"><img src="'+obrazek+'" /></a></div><div style="float: left; width: 150px; margin-right: 10px"><h3 style="font-size: 12px; font-family: tahoma">'+title+'</h3>autor: <a href="'+autor_url+'">'+autor+'</a><br/><br/><a href="'+link+'">pokaż zdjęcie</a></div>';
		marker.openInfoWindowHtml(html);
	});
}

function drukuj(map, typ, base)
{
	lat = map.getCenter().lat();
	lng = map.getCenter().lng();
	zoom = map.getZoom();
	window.open(base+'/drukuj/'+lat+'/'+lng+'/'+zoom+'/'+typ+'/',"drukuj","width=620,height=680");
}

function drukuj_url(url)
{
	window.open(url,"drukuj","width=620,height=680");
}

function przewoznik_szczegoly(url, id, i)
{	
	stat = document.getElementById('detale_przewoznika_'+i).style.display;
	
	$.ajax({
		data: { id : id },
		type: "POST",
		url: url,
		success: function(html){
			$("#detale_przewoznika_"+i).html(html);
		}
	});
	
	if (stat == 'none')
	{
		document.getElementById('detale_przewoznika_'+i).style.display = 'block';
		document.getElementById('szczegoly_przewoznika_'+i).innerHTML = 'zwiń szczegóły';
	}
	
	if (stat == 'block')
	{		
		document.getElementById('detale_przewoznika_'+i).style.display = 'none';
		document.getElementById('szczegoly_przewoznika_'+i).innerHTML = 'zobacz szczegóły';
	}
}

function show_hide(id, s)
{
	if (s==1) document.getElementById(id).style.display = 'block';
	if (s==2) document.getElementById(id).style.display = 'none';
}

function switch_wysz(id, txt1, txt2)
{
	pole = document.getElementById(id).style.display;
	if (pole == 'none')
	{
		document.getElementById(id).style.display = 'block';
		document.getElementById('tryb_wysz').innerHTML = txt2;
		document.getElementById('tryb_stat').value = 'zaawansowany';
	}
	else
	{
		document.getElementById(id).style.display = 'none';
		document.getElementById('tryb_wysz').innerHTML = txt1;
		document.getElementById('tryb_stat').value = 'prosty';
	}
}

function switch_data()
{
	stat = document.getElementById('data_odjazdu').disabled;

	if (stat == false) document.getElementById('data_odjazdu').disabled = true;
	if (stat == true) document.getElementById('data_odjazdu').disabled = false;
}

function getCookie(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 setCookie( name, value, expires, path, domain, secure ) 
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

expires = (( expires ) ? expires : 1) * 1000 * 60 * 60 * 2;

var expires_date = new Date( today.getTime() + (expires) );

var cook = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
( ( path ) ? ";path=" + path : "" ) + 
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
document.cookie = cook;

}
