function searchRasa(rasa_id) {
// 	$("form[name='rasa_search'] > :input[name='rasa']").val(rasa_id)
// 	$("form[name='rasa_search']").submit();
	document.forms['rasa_search'].elements['rasa'].value = rasa_id;
	document.forms['rasa_search'].submit();
}

function ajaxFileUpload(id) {
	$("#wait")
	.ajaxStart(function(){
		var wh = getPageSize();
		var scroll = getPageScroll();
		$('#load table').css('margin-top', Math.round(wh[3]/2 - 80));
		$('#load').width(wh[2]).css('top', scroll[1]).css('left', 0).show();
		$(this).width(wh[0]).height(wh[1]).css('opacity', 0.1).show();
	})
	.ajaxComplete(function(){
		$('#load').hide();
		$(this).hide();
		$(this).unbind();
	});
	
	$.ajaxFileUpload
	(
		{
			url:'index.php?n=fileupload&file='+id,
			secureuri:false,
			fileElementId:id,
			dataType: 'json',
			success: function (data, status) {
				if(typeof(data.error) != 'undefined') {
					if(data.error != '') {
						alert(data.error);
					} else {
						var msg = "<table id='" + data.name + "' style='margin-left:55px'><tr><td width='160'><img src='images/" + data.file + "' style='width:160px; height:auto; border:1px solid black;'></td><td style='vertical-align:middle;'><input type='hidden' value='" + data.file + "' name='" + data.name + "' />" + data.file + "&nbsp;&nbsp;&nbsp;&nbsp;[ <a href='javascript:void(0)' onclick='ajaxFileRemove(\"" + data.name + "\")'>usuń</a> ]</td></tr></table>";
						$("#"+id).replaceWith(msg);
					}
				}
			},
			error: function (data, status, e) {
				alert(e); //napisac ze blad;
			}
		}
	)
	return false;
}

function ajaxFileRemove(id) {
	$("#wait")
	.ajaxStart(function(){
		var wh = getPageSize();
		var scroll = getPageScroll();
		$('#load table').css('margin-top', Math.round(wh[3]/2 - 80));
		$('#load').width(wh[2]).css('top', scroll[1]).css('left', 0).show();
		$(this).width(wh[0]).height(wh[1]).css('opacity', 0.1).show();
	})
	.ajaxComplete(function(){
		$('#load').hide();
		$(this).hide();
		$(this).unbind();
	});
	
	$.ajax
	(
		{
			url:'index.php',
			type: 'GET',
			data: 'n=fileremove&file='+id,
			dataType: 'json',
			success: function (data) {
				if(typeof(data.error) != 'undefined') {
					if(data.error != '') {
						alert(data.error);
					} else {
						$("#"+id).replaceWith(data.msg);
					}
				}
			},
			error: function (data, status, e) {
				alert(e);
			}
		}
	)
	
	return false;
}

function waitPopup(txt) {
	var wh = getPageSize();
	var scroll = getPageScroll();
	$('#tdwait').prepend(txt);
	$('#load table').css('margin-top', Math.round(wh[3]/2 - 80));
	$('#load').width(wh[2]).css('top', scroll[1]).css('left', 0).show();
	$('#wait').width(wh[0]).height(wh[1]).css('opacity', 0.1).show();
}

function check(name) {
	var i;
	for (i in document.forms['adv_search'].elements) {
		if (document.forms['adv_search'].elements[i].name == name)
			document.forms['adv_search'].elements[i].checked=true;
	}
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function switchImg(thumb) {
	var i;
	
	var arr = thumb.offsetParent.getElementsByTagName('img');
	for (i = 0; i < arr.length; i++)
		arr[i].className = "";
	thumb.className = "active";
	
	var arr = document.getElementById('view').getElementsByTagName('img');
	for (i = 0; i < arr.length; i++)
		arr[i].className = "hidden";
	
	document.getElementById(thumb.getAttribute('rel')).className = "view";
}

function getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
};

function getPageScroll() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
};
