var idelement_focused='';

//datepicker settings override
var year_field_height=13;
var offset_x=-1;
var offset_y=-18;



jQuery.fn.delay = function(time,func){
	this.each(function(){ setTimeout(func,time); });
	return this;
};

jQuery.fn.aForm = function(options) {
	options = options || {};

	$(this).bind("submit", function() {
		if(options.in_progress==1) return false;
		forma=this;

		options.in_progress=1;
		$(forma).parent().css('opacity',0.4);

		$(this).delay(200, function() {
			post_form(forma);
			return false;
		});
		return false;
	});

	function post_form(forma) {
		$.ajax({
			url: 'includes/forms.php?SID='+$('#SID').val()+'&form_name='+$(forma).attr('id'),
			data: $(forma).serialize(),
			type: 'post',
			dataType: (options.dataType!="" ? options.dataType:'script'),
			cache: false,
			error: function(request) { alert('AJAX: Server connection error!'); },
			success: function(response) {
				options.in_progress=0;
				$(forma).parent().css('opacity',1);
				if(options.dataType=='html' && options.dataTarget!="") {
					$('#'+options.dataTarget).html(response);
				}
			}
		});
	}
}

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};






function potwierdzenie(link,pytanie,komunikat) {
	if(window.confirm(pytanie)==true) {
		window.location=link;
		if(komunikat!="" && komunikat!=" ") alert(komunikat);
		return false;
	}
}

function link_potwierdzenie(link,pytanie) {
	if(window.confirm(pytanie)==true) window.location=link;
}

function input_validator(e, regexp) {
	if(navigator.userAgent.indexOf('Gecko')!=-1) charCode = e.which; else charCode = e.keyCode;
	if(charCode>31) return regexp.test(String.fromCharCode(charCode));
}

function go(link) {
	window.location=link;
	return false;
}





/* PROFIL */

function napisz_file_open(elem) {
	$(elem).blur();
	$('#napisz_add_link').hide();

	line=$('#files_root').html();
	line=line.replace(new RegExp('_X', 'g'), '_0');
	$('#files_cont').html(line);

	$('#napisz_files').show();
}

function napisz_file_close(elem) {
	$(elem).blur();
	$('#napisz_add_link').show();
	$('#napisz_files').hide();
	$('#files_cont').html("");
	$('#file_plus').show();
}

function add_file_row(elem) {
	current_file_row=$("#files_cont input[type=file]").length;
	$(elem).blur();
	if(current_file_row>=10) return false;

	line=$('#files_root').html();
	line=line.replace(new RegExp('_X', 'g'), '_'+current_file_row);
	$('#files_cont').append(line);

	if(current_file_row>=9) $(elem).hide();
}

function post_show_more(id) {
	$('#post_'+id+' span.post_show_more').hide();
	$('#post_'+id+' span.post_more').show();
	$('#post_'+id+' span.pierwszy_zalacznik').remove();
}

function kom_show_all(elem, id) {
	$(elem).remove();
	$('#komentarze_'+id).css('opacity',0.3);

	$.ajax({
		url: 'includes/forms.php?SID='+$('#SID').val()+'&form_name=post_kom_load',
		data: 'post_id='+id,
		type: 'post',
		dataType: 'html',
		cache: false,
		error: function(request) { alert('AJAX: Server connection error!'); },
		success: function(response) { $('#komentarze_'+id).html(response).css('opacity',1); }
	});
}

function post_del(post_id, kom_id) {
	if(window.confirm('Na pewno usunąć?')) {
		$('#komentarze_'+post_id).css('opacity',0.3);
		$.ajax({
			url: 'includes/forms.php?SID='+$('#SID').val()+'&form_name=post_del',
			data: 'post_id='+post_id+'&kom_id='+kom_id+'&sh_all_l='+$('#komentarze_'+post_id).find('a.tablica_post_komentarz_wszystkie').length,
			type: 'post',
			dataType: 'html',
			cache: false,
			error: function(request) { alert('AJAX: Server connection error!'); },
			success: function(response) {
				if(response=="-1") {
					$('#post_'+post_id).fadeOut(150,function(){ $('#post_'+post_id).remove(); })
				} else {
					$('#komentarze_'+post_id).html(response).css('opacity',1);
				}
			}
		});
	}
}




jQuery.fn.komForm = function(options) {
	options = options || {};

	$(this).bind("submit", function() {
		if(options.in_progress==1) return false;

		options.in_progress=1;
		$(this).parent().css('opacity',0.4);

		post_form(this);
		return false;
	});

	function post_form(forma) {
		$.ajax({
			url: 'includes/forms.php?SID='+$('#SID').val()+'&form_name='+$(forma).attr('id'),
			data: $(forma).serialize()+'&sh_all_l='+$(forma).parent().parent().find('a.tablica_post_komentarz_wszystkie').length,
			type: 'post',
			dataType: 'html',
			cache: false,
			error: function(request) { alert('AJAX: Server connection error!'); },
			success: function(response) {
				options.in_progress=0;
				$(forma).parent().css('opacity',1);
				$(forma).parent().find('textarea').val("").css('height','16px');
				if(response!="" && response!=" ") {
					$(forma).parent().parent().find('div.tablica_post_komentarz_container').html(response);
				} else {
					$(forma).parent().find('textarea').focus();
				}
			}
		});
	}
}


function szukaj_adv_switch(typ) {
	if(typ==1) {
		$("#cont_typp").show();
	} else {
		$("#cont_typp").hide();
	}
}

function szukaj_adv_komp() {
	select_k=$("#select_k");
	off=$(select_k).offset();

	$(select_k).blur().css("visibility","hidden");
	$("#szukaj_adv_k_cont")
		.css("top", off.top + $(select_k).height() - 333 + "px")
		.show();

	if($("#szukaj_adv_k_cont_opts").html()=="") {
		$("#szukaj_adv_k_cont_opts").html('<div style="padding-top:100px; text-align:center; line-height:16px;"><b>Trwa ładowanie<br />kompetencji...</b><br /><br /><img src="images/loader_220.gif" /></div>');

		$.ajax({
			url: 'includes/forms.php?SID='+$('#SID').val()+'&form_name=szukaj_kompetencje&k_opts='+$("#k_opts").val(),
			dataType: 'html',
			cache: false,
			error: function(request) { alert('AJAX: Server connection error!'); },
			success: function(ajax_data) {
				$("#szukaj_adv_k_cont_opts div").fadeOut(350, function(){
					$("#szukaj_adv_k_cont_opts")
						.hide()
						.html(ajax_data)
						.jScrollPane({'scrollbarWidth':17})
						.fadeIn(150)
						.focus();
				});
			}
		});
	}

	//close events
	$(document).bind('click',function(e){
		var DIV=$("#szukaj_adv_k_cont");
		var offset=DIV.offset();
		L = offset.left;
		R = L+DIV.width();
		T = offset.top;
		B = T+DIV.height();
		X = e.pageX;
		Y = e.pageY;
		if(X>0 && Y>0) {
			if(X>L && X<R && Y>T && Y<B) { //inner-click

			} else { //outer-click
				$(document).unbind('click');
				szukaj_adv_komp_close();
			}
		}
	});
	$(document).keydown( function(e) {
		if(e.which==27) szukaj_adv_komp_close(); //ESC
	}); 
}

function szukaj_adv_komp_count() {
	select_k=$("#select_k");
	choosen=$("#k_opts_length").val();
	v=$("#k_opts").val();

	$(select_k)
		.find("option")
			.text((choosen==0 ? "-- wybierz --":"Wybrane: "+choosen))
			.val(v)
}

function szukaj_adv_komp_close() {
	select_k=$("#select_k");
	var v="", choosen=0;
	$.each($("#szukaj_adv_k_cont_opts").find("input:checked"), function(index, elem) {
		v+=$(elem).val()+",";
		choosen++;
	});
	v=v.substring(0, v.length-1);

	$("#szukaj_adv_k_cont").hide();
	$(select_k)
		.blur()
		.css("visibility","visible")
		.find("option")
			.text((choosen==0 ? "-- wybierz --":"Wybrane: "+choosen))
			.val(v)
}

function szukaj_adc_comp_changes() {
	select_k=$("#select_k");
	var v="", choosen=0;
	$.each($("#szukaj_adv_k_cont_opts").find("input:checked"), function(index, elem) {
		v+=$(elem).val()+",";
		choosen++;
	});
	v=v.substring(0, v.length-1);

	$(select_k)
		.find("option")
			.text((choosen==0 ? "-- wybierz --":"Wybrane: "+choosen))
			.val(v)
}

function zglos_naduzycie(elem,post_id,kom_id) {
	$(elem).blur().html('<img src="images/loader_12.gif" border="0" />');

	$.ajax({
		url: 'includes/forms.php?SID='+$('#SID').val()+'&form_name=zglos_naduzycie&post_id='+post_id+'&kom_id='+kom_id,
		dataType: 'html',
		cache: false,
		error: function(request) { alert('AJAX: Server connection error!'); },
		success: function(ajax_data) {
			$(elem).html("");
			showModalPage(ajax_data);
		}
	});
}

function zglos_kompetencje(zaint) {
	$.ajax({
		url: 'includes/forms.php?SID='+$('#SID').val()+'&form_name=zglos_kompetencje&zaint='+zaint,
		dataType: 'html',
		cache: false,
		error: function(request) { alert('AJAX: Server connection error!'); },
		success: function(ajax_data) { showModalPage(ajax_data); }
	});
}



function showModalPage(ajax_data) {
	$('#modalPage').modal({
		close: false,
		opacity: 60,
		overlayId: 'modalPageOverlay',
		containerId: 'modalPageCont', 
		onOpen: function(dialog) {
			dialog.overlay.slideDown(200, function () {
				dialog.container.show();
				dialog.data.hide().fadeIn(250);	 
			});
			$(document).bind('keydown', function(e) { if(e.which == 27) $.modal.close(); }); 
		},
		onShow: function(dialog) {
			dialog.data.find('.modalPageMessage').append(ajax_data);
		},
		onClose: function(dialog) {
			dialog.data.fadeOut(250, function () {
				dialog.container.hide();
				dialog.overlay.slideUp(200, function () {
					$.modal.close();
				});
			});
			$(document).unbind('keydown');
		}
	});
}