

function orphan(str) {
	return str.replace("/\s([aiouwzAIOUWZ])\s/", " $1&nbsp;");
}

function slug(s) {
	if(s == null) return '';
    s = s.toLowerCase();
    s = s.replace(/ż/g,'z');
    s = s.replace(/ó/g,'o');
    s = s.replace(/ł/g,'l');
    s = s.replace(/ć/g,'c');
    s = s.replace(/ę/g,'e');
    s = s.replace(/ś/g,'s');
    s = s.replace(/ą/g,'a');
    s = s.replace(/ź/g,'z');
    s = s.replace(/ń/g,'n');
    s = s.replace(/nbsp/g,'-');
    
    s = s.replace(/[^-A-Z0-9\s]/gi, '');  // remove unneeded chars
    s = s.replace(/^\s+|\s+$/g, ''); // trim leading/trailing spaces
    s = s.replace(/\s+/g, '-');      // convert spaces to hyphens
    return s;// trim to first num_chars chars
}




var tab;
var rel;
	
function zipCodeMask(obj) {
  str = obj.value;
  len = str.length;
 
  if(len > 2)
  {
    if(str.charAt(2) != '-')  obj.value = str.substring(0,2) + '-' + str.substring(2,5);
  }
  
  if (str.substring(len-1) == '-') {
	  obj.value = str.substring(0,len-1);
  }

}

function nipCodeMask(obj) {
	str = obj.value;
	len = str.length;
	digits = '0123456789';
	var new_string = '';	
	for (var i = 0;i < len; i++) {
		
		if (digits.indexOf(str.charAt(i)) != -1) {
			new_string+=str.charAt(i);
		}

	}
	
	len = new_string.length;
	if(len > 8) {
		new_string = new_string.substring(0,8) + '-' + new_string.substring(8,10);
	}
	if(len > 6) {
		new_string = new_string.substring(0,6) + '-' + new_string.substring(6,11);
	}
	if(len > 3) {
		new_string = new_string.substring(0,3) + '-' + new_string.substring(3,12);
	}
	obj.value = new_string;

}

$.fn.clearForm = function() {
  return this.each(function() {
    var type = this.type, tag = this.tagName.toLowerCase();
    if (tag == 'form')
      //$(this).find('.error-message').remove();
      return $(':input',this).clearForm();
    if (type == 'text' || type == 'password' || tag == 'textarea')
      this.value = '';
    else if (type == 'checkbox' || type == 'radio')
      this.checked = false;
    else if (tag == 'select')
      this.selectedIndex = -1;
  });
};

function tab_select( id , rel ) {
	if ($('.'+rel).children().is('div')) {
		el = ' > div';
		ida = id;
	} else {
		el = ' > form > fieldset';
		ida=id+1;
	}
	$('.' + rel + el).hide();
	$('.' + rel + el + ':eq('+ida+')').show();
	$('.tab[rel='+rel+'] li').removeClass('s');
	$('.tab[rel='+rel+'] > li:eq('+id+')').addClass('s');
	
}

function tab_select2( id , rel ) {
	if ($('.'+rel).children().is('div')) {
		el = ' > div';
		ida = id;
	} else {
		el = ' > form > fieldset';
		ida=id+1;
	}
	$('.' + rel + el).hide();
	$('.' + rel + el + ':eq('+ida+')').show();
	$('.tab[id='+rel+'] li').removeClass('s');
	$('.tab[id='+rel+'] > li:eq('+id+')').addClass('s');
}

function empty (mixed_var) {
    // !No description available for empty. @php.js developers: Please update the function summary text file.
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/empty
    // +   original by: Philippe Baumann
    // +      input by: Onno Marsman
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: LH
    // +   improved by: Onno Marsman
    // +   improved by: Francesco
    // +   improved by: Marc Jansen
    // *     example 1: empty(null);
    // *     returns 1: true
    // *     example 2: empty(undefined);
    // *     returns 2: true
    // *     example 3: empty([]);
    // *     returns 3: true
    // *     example 4: empty({});
    // *     returns 4: true
    // *     example 5: empty({'aFunc' : function () { alert('humpty'); } });
    // *     returns 5: false
    
    var key;
    
    if (mixed_var === "" ||
        mixed_var === 0 ||
        mixed_var === "0" ||
        mixed_var === null ||
        mixed_var === false ||
        mixed_var === undefined
    ){
        return true;
    }

    if (typeof mixed_var == 'object') {
        for (key in mixed_var) {
            return false;
        }
        return true;
    }

    return false;
}

/**
 * Converting coordinates from DD to DMS
 * 
 * @param coor
 * @return
 */
function dd2dms(coor) {
	coor.lat = parseFloat(coor.lat);
	coor.long = parseFloat(coor.long);
	
	var lat1 = parseInt(coor.lat);
	var lat2 = Math.round(60*Math.abs(coor.lat - parseInt(coor.lat)));
	var lat3 = Math.round(60*((60*Math.abs(coor.lat - parseInt(coor.lat)))-parseInt(60*Math.abs(coor.lat - parseInt(coor.lat)))));
	
	if (coor.lat < 0) {
		var lat4 = "S";
	} else {
		var lat4 = "N";
	}
	coor.lat = lat1 + "° " + lat2 + "' " + lat3 + '" ' + lat4; 
	
	var long1 = parseInt(coor.long);
	var long2 = Math.round(60*Math.abs(coor.long - parseInt(coor.long)));
	var long3 = Math.round(60*((60*Math.abs(coor.long - parseInt(coor.long)))-parseInt(60*Math.abs(coor.long - parseInt(coor.long)))));
	if (coor.long < 0) {
		var long4 = "E";
	} else {
		var long4 = "W";
	}
	coor.long = long1 + "° " + long2 + "' " + long3 + '" ' + long4; 
	
	return coor;

}

function report(model, id) {
	$.nyroModalManual({
		url: baseUrl+'/report/form/'+model+'/'+id,
		contentError: 'Żądanie nie może zostać wykonane<br />Prosimy spróbowac ponownie<br /><a href="#" class="nyroModalClose">Zamknij</a>'
	});
}

function new_comment() {
	$("#new_comment").trigger('click');
}
function new_topic() {
	$("#new_topic").trigger('click');
}

function drawCircle(center, radius, nodes, liColor, liWidth, liOpa, fillColor, fillOpa){
 
	// source: http://www.bindichen.co.uk/files/map/drawCircle.html
	var latConv = center.distanceFrom(new GLatLng(center.lat()+0.1, center.lng()))/100;
	var lngConv = center.distanceFrom(new GLatLng(center.lat(), center.lng()+0.1))/100;
	var points = [];
	var step = parseInt(360/nodes)||10;
	for(var i=0; i<=360; i+=step)
	{
		var pint = new GLatLng(center.lat() + (radius/latConv * Math.cos(i * Math.PI/180)), 
		center.lng() + 
	    (radius/lngConv * Math.sin(i * Math.PI/180)));
		points.push(pint);
		bounds.extend(pint); 
	}
	points.push(points[0]); 
	fillColor = fillColor||liColor||"#0055ff";
	liWidth = liWidth||2;
	var poly = new GPolygon(points,liColor,liWidth,liOpa,fillColor,fillOpa);
	map.addOverlay(poly);
}


function searcher(obj) {
	if ($(obj).children("fieldset").children("div").children("input[name=search]").val() == "Podaj nazwę miasta lub kod pocztowy") {
		$(obj).children("fieldset").children("div").children("input[name=search]").val('');
	}
}


$(document).ready(function() {
	$("#chng_h2kul").mouseenter(function(){
		$("#h2kul").show();
	});
	$("#h2kul").mouseleave(function(){
		$(this).hide();
	});	
	
	$('li','#mnu').mouseenter(function(){
		$('ul',$(this)).show();
		$('a:eq(0)',$(this)).addClass('o');
	}).mouseleave(function(){
		$('ul',$(this)).hide();
		$('a:eq(0)',$(this)).removeClass('o');
	});
	
	$("#InstitutionSearch").autocomplete(baseUrl+"/users/placesandzips", { scroll: true } ); 
	
});
