document.root_folder = '';

function trySendComment( id, title ) {
	var ajax = new myAjax();
	ajax.action = 'trySendComment';
	ajax.post( 'title='+title );
	
}

function togglePhoto( linker ) {
	get( 'ph_main' ).src = linker;
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}




function saveNewPassword() {
	var op = get( 'old_pass' ).value;
	var np = get( 'new_pass' ).value;
	var npr = get( 'new_pass_repeat' ).value;
	var ajax = new myAjax();
	ajax.action = 'saveNewPassword';
	ajax.post( 'op='+op+'&np='+np+'&npr='+npr );
	ajax.onLoad = function() {
		var d = get( 'passwordMessage' );
		switch( this.response ) {
			case 'ok':
				putHtml( d, 'Zapisano nowe hasło' );
				break;
			case 'not_empty':
				putHtml( d, 'Hasło nie może być puste' );
				break;
			case 'bad_compare':
				putHtml( d, 'Hasło nie zgadza się z powtórzeniem hasła' );
				break;
			case 'bad_pass':
				putHtml( d, 'Stare hasło jest nieprawidłowe' );
				break;
			default:
				putHtml( d, this.response );
				break;
		}
	}
}

function searcher( target ) {
	var t = getSearch( target );
	if( t.length < 3 ) {
		alert( 'Wyszukiwany tekst musi mieć conajmniej 3 znaki' );
	} else {
		document.location = '/szukaj/'+t;//+'/kategoria/'+o;
	}
}

function submitentersearch(myfield,e)
{
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;
    if (keycode == 13)
       {
            var t = getValue( 'word' );
            if( t.length < 3 ) {
                    alert( 'Wyszukiwany tekst musi mieÄ conajmniej 3 znaki' );
            } else {
                document.location = '/szukaj/'+t;
            }
            return false;
       }
    else
       return true;
}


function searchCurrent( target, linker ) {
	var t = getSearch( target );
	if( t.length < 3 ) {
		alert( 'Wyszukiwany tekst musi mieć conajmniej 3 znaki' );
	} else {
		var h = linker;
		var len = h.length;
		var ls = h.substr( len - 1, 1 );
		if( ls == '/' ) {
			h = h.substr( 0, len - 1 );
		}
		document.location.href = h + '/szukaj/'+t;
	}
}

function getSearch( target ) {
	get( target ).value = trim( getValue( target ) );
	return getValue( target ).split( ' ' ).join( '+' );
}

function publicLogin() {
	var l 	= getValue( 'public_login' );
	var p	= getValue( 'public_password' );
	var g = getRadioValueByName( 'save_pass' );
//	get( 'loginMessage' ).value = '';
	var ajax = new myAjax();
	var pstr = 'login='+l+'&pass='+p+'&save_pass='+g+'&cn=Client';
	ajax.action = 'publicLogin';
//	ajax.post ( pstr, 'loginMessage' );
	ajax.post ( pstr );
	ajax.onLoad = function() {
		if( this.response == 'ok' ) {
			document.location.reload();
		} else {
			alert( this.response );
		}
	}
}


function trimLeft( s ) {
	while( s.substring( 0, 1 ) == " " ){
		s = s.substr( 1 );
	}
	return s;
}
function trimRight( s ) {
	while( s.charAt( s.length - 1 ) == " " ) {
		s = s.substr( 0, s.length - 1 );
	}
	return s;
}
function trim( s ) {
	s = trimLeft( s );
	s = trimRight( s );
	return s;
}
function inArray( array, id ) {
	for( i in array ) {
			if( array[ i ]  == id ) {
					return true;
			}
	}
	return false;
}
function indexOfArray( array, id ) {
	for( i in array ) {
			if( array[ i ]  == id ) {
					return i;
			}
	}
	return -1;
}
function equalDivs(className){
	var max_height = 0;
	$('.'+className).each(function(index)
	{
		if(max_height < $(this).height()){
			max_height = $(this).height()
		}
	});

	$('.'+className).height(max_height+"px");

}


$(document).ready(function(){
    $('#content a').each(function() {
        //$(this).click(function(){
            //parent.location.href = parent.location.href+"#top";
            //$(this).click(function(){window.parent.location = window.parent.location+'#'});
        //});
    });
    $("#tabs").tabs();

    //$(".lightbox").prettyPhoto();
    //$("a[rel^='prettyPopin']").prettyPopin({width:300});

    //$("a[rel^='prettyPhoto']").prettyPhoto({

     $("a.pp").each(function(i){
         $(this).prettyPhoto({
            custom_markup: '<div id="map_canvas" style="width:400px; height:300px"></div>',
            changepicturecallback: function(){
                //initialize();
                showMapNew( $("a.pp:eq("+i+")").attr('id') );
            }
    });



     });

  /*  $("a.pp").prettyPhoto({
            custom_markup: '<div id="map_canvas" style="width:260px; height:265px"></div>',
            changepicturecallback: function(){
                //initialize();
             //   alert($(this).html());
                //showMapNew($(this).attr('id'));
            }
    });*/


 /*   $('.xxx').click(function(){
        $(this).prettyPhoto({
            custom_markup: '<div id="map_canvas" style="width:260px; height:265px"></div>',
            changepicturecallback: function(){
                initialize();
            }
        });
    });
*/


});

function showMapNew(id) {
    var ajax = new myAjax();
    var pstr = 'id='+id;
    ajax.action = 'showOnMap';
    ajax.post ( pstr );
    ajax.onLoad = function() {
        if( this.response != 'error' ) {
                //if (geocoder) {
                    var coords = this.response;
                    //alert( this.response );
                    showAddress3(coords);
                //}
        } else {
                alert( this.response );
        }
    }
}


/*
var map = null;
var geocoder = null;

function initialize() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map_canvas"));
    var center = new GLatLng(37.4419, -122.1419);
    map.setCenter(center, 13);
    var marker = new GMarker(center, {draggable: true});
    map.addOverlay(marker);
    geocoder = new GClientGeocoder();
  }
}


function showMap(id) {
    var ajax = new myAjax();
    var pstr = 'id='+id;
    ajax.action = 'showOnMap';
    ajax.post ( pstr );
    ajax.onLoad = function() {
        if( this.response != 'error' ) {
                if (geocoder) {
                    var coords = this.response;
                    showAddress(coords);
                }
        } else {
                alert( this.response );
        }
    }
}



function showAddressNew(address,id) {
  if (geocoder) {
    geocoder.getLatLng(
      address,
      function(point) {
        if (!point) {
          alert(address + " not found");
        } else {
            $('.unit-results').find(".map").html("");
            map = new GMap2(document.getElementById("mapa_"+id));
            map.addControl(new GSmallMapControl());
            var center = new GLatLng(point);
            var marker = new GMarker(point, {draggable: false});
            //marker.openInfoWindowHtml('XXX');
            map.setCenter(point, 15);
            map.addOverlay(marker);
            geocoder = new GClientGeocoder();

        }
      }
    );
  }
}

function showAddress(address) {
  if (geocoder) {
    geocoder.getLatLng(
      address,
      function(point) {
        if (!point) {
          alert(address + " not found");
        } else {

            map = new GMap2(document.getElementById("map_canvas"));
            map.addControl(new GSmallMapControl());
            var center = new GLatLng(point);
            var marker = new GMarker(point, {draggable: false});
            //marker.openInfoWindowHtml('XXX');
            map.setCenter(point, 15);
            map.addOverlay(marker);
            geocoder = new GClientGeocoder();

        }
      }
    );
  }
}
*/
/* -------------------------------------------------------------------------- */

//$("a[rel^='prettyPhoto']").prettyPhoto({
//	custom_markup: '<div id="map_canvas" style="width:260px; height:265px"></div>',
//	changepicturecallback: function(){ initialize(); }
//});

  function initialize() {
				    var latlng = new google.maps.LatLng();
				    var myOptions = {
				      zoom: 8,
				      center: latlng,
				      mapTypeId: google.maps.MapTypeId.ROADMAP
				    };
				    var map = new google.maps.Map(document.getElementById("map_canvas"),
				        myOptions);
				  }


  function showAddress3(address) {
      var arr = address.split(',');
      var latlng = new google.maps.LatLng(parseFloat(arr[0]), parseFloat(arr[1]));
      var myOptions = {
          zoom: 15,
          center: latlng,
          mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
      var marker = new google.maps.Marker({
          position: latlng,
          title:""
      });

      // To add the marker to the map, call setMap();
      marker.setMap(map);
  }
