var timeout         = 500;
var closetimer		= 0;
var ddmenuitem      = 0;

function jsddm_open()
{	jsddm_canceltimer();
	jsddm_close();
	ddmenuitem = $(this).find('ul').eq(0).css('visibility', 'visible');}

function jsddm_close()
{	if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');}

function jsddm_timer()
{	closetimer = window.setTimeout(jsddm_close, timeout);}

function jsddm_canceltimer()
{	if(closetimer)
	{	window.clearTimeout(closetimer);
		closetimer = null;}}

$(document).ready(function()
{	$('#jsddm > li').bind('mouseover', jsddm_open);
	$('#jsddm > li').bind('mouseout',  jsddm_timer);});

document.onclick = jsddm_close;

function geografie(url, typ, id, poziom, id_selected, box)
{
	if (!box)
	{
		if (typ == 0)
		{
			box = 'sKraj';
			document.getElementById('sWoj').innerHTML='';
			document.getElementById('sPowiat').innerHTML='';
			document.getElementById('sMiasto').innerHTML='';
		}
		
		if (typ == 1)
		{
			box = 'sWoj';
			document.getElementById('sPowiat').innerHTML='';
			document.getElementById('sMiasto').innerHTML='';
		}

		if (typ == 3)
		{
			box = 'sPowiat';
			document.getElementById('sMiasto').innerHTML='';
		}
		
		if (typ == 2)
		{
			box = 'sMiasto';
		}
	}
	
	$.ajax( {
		type : "GET",
		url : url+"podlega/"+id+"/typ/"+typ+"/id/"+id_selected+"/",
		data : "poziom="+poziom,
		success: function(html){
			$("#"+box).html(html);
		}
	});
}

function mapa(lat, lng, zoom, id_mapy, goc_off)
{
	if (!id_mapy) id_mapy = 'map';

	if (GBrowserIsCompatible()) {
	 mapObiekt = new GMap2(document.getElementById(id_mapy));
	 mapObiekt.setCenter(new GLatLng(lat, lng), zoom);
	 mapObiekt.addControl(new GLargeMapControl());
	 if (!goc_off) mapObiekt.addControl(new GOverviewMapControl());     
	}
	return mapObiekt;
}

function mapaOdczyt(map, lat, lng)
{
    var point = new GLatLng(lat, lng);
	var marker = new GMarker(point);
	map.addOverlay(marker);

	GEvent.addListener(map, 'click', function(overlay, point) {

		if (overlay) {
			map.removeOverlay(overlay);
			document.getElementById('google_x').value = 0;
			document.getElementById('google_y').value = 0;
          
		} else {
			map.clearOverlays();
			map.addOverlay(new GMarker(point));
			document.getElementById('google_x').value = point.y;
			document.getElementById('google_y').value = point.x;
		}
	});
	
	GEvent.addListener(map, 'zoomend', function(zdarzenie) {
		document.getElementById('google_z').value = map.getZoom();
	});
}

function mapaObszar(map, url, ident, link, icon_x, icon_y, icon_img)
{
	if (!icon_img) icon_img = 'info.png';
	if (!icon_x) icon_x = 0;
	if (!icon_y) icon_y = 0;
	
	$.getJSON(url, ab=function(json){
		for(i = 0; i < json.length; i++) {
			

			if (link == 0) html = '<div style="float: left; height: 70px; padding-right: 20px; font-size: 11px; line-height: 17px"><span style="font-size: 12px; font-weight: bold; color: #6fa813"><a href="'+json[i]['url']+'">'+json[i]['nazwa']+'</a></span><br/>ul. '+json[i]['ulica']+'<br/>'+json[i]['miasto']+'<br/><a href="'+json[i]['url']+'">zobacz</a></div>';
			if (link == 1) html = '<div style="float: left; height: 70px; padding-right: 20px; font-size: 11px; line-height: 17px"><span style="font-size: 12px; font-weight: bold; color: #6fa813"><a href="'+json[i]['url']+'">'+json[i]['rodzaj']+' '+json[i]['nazwa']+'</a></span><br/>woj. '+json[i]['region']+'<br/>powiat '+json[i]['powiat']+'<br/><a href="'+json[i]['url']+'">zobacz</a></div>';
			if (link == 2) html = '<div style="float: left; height: 70px; padding-right: 20px; font-size: 11px; line-height: 17px"><span style="font-size: 12px; font-weight: bold; color: #6fa813"><a href="'+json[i]['url']+'">'+json[i]['nazwa']+'</a></span><br/>data: '+json[i]['start_data']+'<br/>lokalizacja: '+json[i]['miejsce_newsa']+'<br/><a href="'+json[i]['url']+'">zobacz</a></div>';
				
			
			var ikona = new GIcon();
			if (json[i]['ico'])
				ikona.image = json[i]['ico'];
			else
			{
				if (ident && ident != 'tolublin')
					ikona.image = 'public/i/'+ident+'/'+icon_img;
				else
					ikona.image = 'public/i/'+icon_img;
			}
				
			ikona.iconAnchor = new GPoint(icon_x, icon_y);
			ikona.infoWindowAnchor = new GPoint(10, 5);
			
		    var point = new GLatLng(json[i]['google_x'], json[i]['google_y']);
		    var marker = dodaj_marker(point, ikona, html, link, json[i]['url']);
					    
		    map.addOverlay(marker);
		}
	});
}

function showAddress(city, ulica, mapka) {

	address = city + document.getElementById(ulica).value;
	var map = new GMap2(document.getElementById(mapka));
	var geocoder = new GClientGeocoder();
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        alert(" ulicy " + document.getElementById(ulica).value +" nie znaleziono");
      } else {
    	map.setCenter(point, 13);
        var marker = new GMarker(point);
        document.getElementById('google_x').value = point.y;
        document.getElementById('google_y').value = point.x;
        map.addOverlay(marker);
        map.addControl(new GLargeMapControl());
		GEvent.addListener(map, 'zoomend', function(zdarzenie) {
			document.getElementById('google_z').value = map.getZoom();
		});
		
		GEvent.addListener(map, 'click', function(overlay, point) {

			if (overlay) {
				map.removeOverlay(overlay);
				document.getElementById('google_x').value = 0;
				document.getElementById('google_y').value = 0;
	          
			} else {
				map.clearOverlays();
				map.addOverlay(new GMarker(point));
				document.getElementById('google_x').value = point.y;
				document.getElementById('google_y').value = point.x;
			}
		});
      }
      
    }
  );

}

function dodaj_marker(point, ikona, html, link, url)
{
	var marker = new GMarker(point, ikona);	
	
    if (link==1)
    {
    	GEvent.addListener(marker, "click", function() { window.open(url, '_self');});
		GEvent.addListener(marker,'mouseover',function() {
			marker.openInfoWindowHtml(html);
		});
    }
    else
    {
		GEvent.addListener(marker,'click',function() {
			marker.openInfoWindowHtml(html);
		});
    }
	return marker; 
}

function mapaPunkt(map, lat, lng,nazwa_ikony,icon_x,icon_y)
{
    var point = new GLatLng(lat, lng);
    if (!nazwa_ikony) var marker = new GMarker(point);
    else 
    {
    	if (!icon_x) icon_x = 0;
    	if (!icon_y) icon_y = 0;
    	
    	var icon = new GIcon();
    	icon.image = 'public/i/'+nazwa_ikony;
    	icon.iconAnchor = new GPoint(icon_x, icon_y);
    	
    	var marker = new GMarker(point,icon);
    	
    }
	map.addOverlay(marker);
}

function Switch(id) {
	status = document.getElementById(id).style.display;
	if (status == 'none') document.getElementById(id).style.display = 'block';
	if (status == 'block') document.getElementById(id).style.display = 'none';
}
function Hide(id,id2,id3,id4) {
	document.getElementById(id).style.display = 'none';
	document.getElementById(id2).style.display = 'none';
	document.getElementById(id3).style.display = 'none';
	document.getElementById(id4).style.display = 'none';
}
function loadContent (url, box)
{
	$.ajax({
		type: "POST", url: url, success: function(html) { $(box).html(html); }
	});
}
function get_sub_cat (value, url, source)
{
	$.ajax({
		type: 'POST',
		url: url+'/id/'+value,
		success: function(html) {
			document.getElementById(source).innerHTML = html;
		}
	});
}
function przesun (url1, url2, box)
{
	$.ajax({ url: url1, success: function(){ loadContent(url2, box) } });
}

function zmienOdmiany(base_url, nr, id)
{	
	odmiana_c = document.getElementById('odmiana_c_'+nr).value;
	odmiana_d = document.getElementById('odmiana_d_'+nr).value;
	odmiana_b = document.getElementById('odmiana_b_'+nr).value;
	odmiana_n = document.getElementById('odmiana_n_'+nr).value;
	document.getElementById('loading_'+nr).style.display = 'inline';
	
	$.ajax({
		type: 'POST',
		data : "odmiana_c="+odmiana_c+"&odmiana_d="+odmiana_d+"&odmiana_b="+odmiana_b+"&odmiana_n="+odmiana_n,
		url: base_url+'/geografie/zmien.odmiany/id/'+id+'/nr/'+nr,
		success: function(html) {
			document.getElementById('odmiany_status_'+nr).innerHTML = html;
			document.getElementById('odmiany_'+nr).style.display = 'none';
			document.getElementById('box_poprzedni').value = -1;
			document.getElementById('loading_'+nr).style.display = 'none';
		}
	});
}

function zamknijPoprzedni(nr, nazwa)
{
	// pobierz numer poprzedniego boxa
	poprzedni = document.getElementById('box_poprzedni').value;
	// ukryj box poprzedni
	if (nr != poprzedni) document.getElementById(nazwa+'_'+poprzedni).style.display = 'none';
	// ustaw jako poprzedni
	document.getElementById('box_poprzedni').value = nr;
}

function podpowiedz(url, q, keyword, box, blokada, regiony, typ_geo)
{
	if (regiony == 1) regiony == 1; else regiony = 0; 
	
	document.getElementById('podpowiedzi_kategoria').style.display ='none';
	
	key_bl = document.getElementById(blokada).value;
	if (key_bl == 'off') {
		setTimeout("podpowiedz_fu('"+url+"', "+q+", '"+keyword+"', '"+box+"', '"+blokada+"', "+regiony+", "+typ_geo+")", 100);
		document.getElementById(blokada).value = 'on';
	}
}

function podpowiedz_fu(url, q, keyword, box, blokada, regiony, typ_geo)
{
	value = document.getElementById(keyword).value;
	ident_v = 0;
	
	var length = value.length;

	if (length > q)
	{
		$.ajax({
			data: { key : value, regiony : regiony, typ_geo : typ_geo },
			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_geografii)
{
	document.getElementById(keyword).value = nazwa;
	document.getElementById(box).style.display = 'none';
	if (id_geografii) document.getElementById('id_geografii').value = id_geografii;
}

function okienko (ident)
{
	document.getElementById(ident).style.display='block';
	nr = document.getElementById('okienko_off').value;
	document.getElementById('okienko_off').value = ident;
	if (nr != 'off') document.getElementById(nr).style.display='none';
	if (nr == ident) document.getElementById('okienko_off').value='off';
}
