$(document).ready(function() {

	/* wyszukiwarka miast */
	$(function(){
		$('#woj').selectCombo('/index/getcity/', '#city');
	});
	$(function(){
		$('#wojd').selectCombo('/index/getcity/', '#cityd');
	});

	/* likwidacja obramowania wokół linku */
	$('a').click(function() {
      this.blur();
	});
	/* pokaż mapę lokalizacyjną */
	$('.sMp').click(function() {
		var gps = $(this).attr('name');
		var gpsdata = gps.split(",");
		var address = gpsdata[2];
		if(address.toString().length > 1){
			getAddress2(address);
		}else{
		$('#mapBox').slideToggle('slow');
          if ($(this).is('.show')) {
                $(this).removeClass('show').addClass('hide');
                $(this).html('Ukryj lokalizację');
				mapL(gpsdata[0], gpsdata[1]);
            } else {
                $(this).removeClass('hide').addClass('show');
                $(this).html('Pokaż lokalizację');
            }         
		}
	});


	/* dialog */
	$(function() {
			$('.dialog_link').click(function(){
			var idk = $(this).attr('id');
			$('#'+idk+'d').dialog('open');
		return false;
	});

	/* zakładki */
		$('#dialog_link, ul#icons li').hover(
			function() { $(this).addClass('ui-state-hover'); }, 
			function() { $(this).removeClass('ui-state-hover'); }
		);
	});
	
	/* zakładki: miasta usługi */
	$('.rty').mouseover(function () {
		$(this).addClass('relative');
		var $sp = $(this).find('.rtyu');
		$sp.show();
	});
	$('.rty').mouseout(function () {
		$(this).removeClass('relative');
		var $sp = $(this).find('.rtyu');
		$sp.hide();
	});
	$('#zp').click(function() {
		$('#mainAdvS').slideToggle('slow');

	});

});
		function showNotify(page){ //pokaż powiadomienia na zakładce
			jQuery.ajax({
				type: "POST",
				url: '/profile/ajaxnotify/page/'+page+'',
				beforeSend: function(l){
								$('#nServ').html('<img src="/gfx/loading.gif" alt="" />');
                            },
				success: function(r){
					$('#nServ').html(r);

				},
				error: function(){
                           $('#nServ').html('<strong>Wystąpił błąd.</strong>');
                }
			});

		}
		function showNotifyPoint(page){ //pokaż powiadomienia na zakładce
			jQuery.ajax({
				type: "POST",
				url: '/profile/ajaxnotifypoint/page/'+page+'',
				beforeSend: function(l){
								$('#nPoint').html('<img src="/gfx/loading.gif" alt="" />');
                            },
				success: function(r){
					$('#nPoint').html(r);

				},
				error: function(){
                           $('#nPoint').html('<strong>Wystąpił błąd.</strong>');
                }
			});

		}

		function delNotify(mid,sid,pid) {
			jQuery.ajax({
				type: "POST",
				url: '/service/powiadomienieusun/cityid/'+mid+'/servid/'+sid+'/pointid/'+pid+'',
				beforeSend: function(l){
								$('#nServ').html('<img src="/gfx/loading.gif" alt="" />');
                            },
				success: function(r){
					$('.infoSuccess').show();
					$('.infoSuccess').html(r);
					showNotify(1);
					showNotifyPoint(1);
					addEvent(window,'load',tooltip); 
				},
				error: function(){
					$('.infoError').show();
                    $('.infoError').html('<strong>Wystąpił błąd.</strong>');
                }
			});

		}
		function showTip() { //dla powiadomień w profilu
			$('.rty').mouseover(function () {
				$(this).addClass('relative');
				var $sp = $(this).find('.rtyu');
				$sp.show();
			});
			$('.rty').mouseout(function () {
				$(this).removeClass('relative');
				var $sp = $(this).find('.rtyu');
				$sp.hide();
			});
		}
		
		function delPost(pid,sid,id) {
			jQuery.ajax({
				type: "POST",
				url: '/service/deletepost/pointid/'+pid+'/servid/'+sid+'/id/'+id+'',
				beforeSend: function(l){
								$('#dp').html('<img src="/gfx/loading.gif" alt="" />');
                            },
				success: function(r){
					$('#dp').html('<strong>Zgłoszenie zostało przyjęte.</strong>');

				},
				error: function(){
                           $('#dp').html('<strong>Wystąpił błąd.</strong>');
                }
			});

		}

	/* mapa lokalizacja punktu */
	function mapL(latitude, longitude) {
		if (GBrowserIsCompatible()) {
			map = new GMap2(document.getElementById("mapLocal"));
			map.addControl(new GSmallZoomControl());
			map.setCenter(new GLatLng(latitude, longitude), 15);
			var marker = new GMarker(new GLatLng(latitude, longitude));
			map.addOverlay(marker);
		}
	}
	 var ddd;
	 var foo=function(){
		 var gps2 = ddd.toString();
 		 var gpsdata = gps2.split(",");
		// alert(gpsdata);

		$('#mapBox').slideToggle('slow');
          if ($('.sMp').is('.show')) {
                $('.sMp').removeClass('show').addClass('hide');
                $('.sMp').html('Ukryj lokalizację');
				//alert(gpsdata[1]+','+gpsdata[0]);
				mapL(gpsdata[1], gpsdata[0]);
            } else {
                $('.sMp').removeClass('hide').addClass('show');
                $('.sMp').html('Pokaż lokalizację');
            } 
	}
	function getAddress2(search) {
     var geo2 = new GClientGeocoder();
       var p;
       geo2.getLocations(search, function (result)
         {
           if (result.Status.code == G_GEO_SUCCESS) {
             p = result.Placemark[0].Point.coordinates;
             var lat=p[0];
             var lng=p[1];
             ddd = p;
			 foo();
           }
         }
       );
	 }
    

