$(document).ready(
	function() {
		//$('#facebook_field').css('display','none'); display none by css
 
		$('#schoolbook').hover(
			function(){
				$("#schoolbook").animate({"marginLeft": "0"}, "slow");
				return false;
			},
			function(){	
				$("#schoolbook").animate({"marginLeft": "-105"}, "slow");
				return false;
			}
		);
		
	}
);

function number_format(number, decimals, dec_point, thousands_sep) {
    // Strip all characters but numerical ones.
    number = (number + '').replace(/[^0-9+\-Ee.]/g, '');
    var n = !isFinite(+number) ? 0 : +number,
        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
        dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
        s = '',
        toFixedFix = function (n, prec) {
            var k = Math.pow(10, prec);
            return '' + Math.round(n * k) / k;
        };
        
    // Fix for IE parseFloat(0.55).toFixed(0) = 0;
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
    if (s[0].length > 3) {
        s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
    }
    if ((s[1] || '').length < prec) {
        s[1] = s[1] || '';
        s[1] += new Array(prec - s[1].length + 1).join('0');
    }
    
    return s.join(dec);
}


function showReportForm() {
	if($('#report_box').css('display')=='none') {
		$('#report_box').css('display','block');
		$('#report').addClass('reportActive');
	} else {
		closeReportForm();
	}
}

function closeReportForm() {
	$('#report_box').css('display','none');
	$('#report').removeClass('reportActive');
}

function showNewsTab() {
	if($("#news_tab").css('z-index')==2) {
		$("#register_tab").css('z-index','2');
		$("#news_tab").css('z-index','3');
		$("#register_tab_label").css('color','#77c8ee');
		$("#news_tab_label").css('color','#3dec27');
	} else {
		return false;
	}
}

function showRegisterTab() {
	if($("#register_tab").css('z-index')==2) {
		$("#news_tab").css('z-index','2');
		$("#register_tab").css('z-index','3');
		$("#news_tab_label").css('color','#77c8ee');
		$("#register_tab_label").css('color','#3dec27');
	} else {
		return false;
	}
}

function changeRegistrationSex(sex) {
	if(sex=='boy' || sex=='girl') {
		
		var newSex = sex;
		if(sex=='boy') {
			var secondSex='girl';
			var sex_val=1;
		} else {
			var secondSex='boy';
			var sex_val=2;
		}
		
		if($("#"+sex+"_active").css('display')=='none') {
			$("#"+secondSex+"_active").hide();
			$("#"+secondSex+"_gender_active").hide();
			$("#"+secondSex+"_gender").show("slow");
			
			$("#"+sex+"_gender").hide();
			$("#"+sex+"_active").show("slow");
			$("#"+sex+"_gender_active").show("slow");
			
			$("#register_form_sex").attr('value',sex_val);
		} else {
			return false;
		}
	} else {
		return false;
	}
}

function showOrHide(element_id) {
	var header_id=element_id.substring(0,element_id.length-9);
	if($("#"+element_id).length) {
		if($("#"+element_id).css('display')!='none') {
			$("#"+element_id).fadeOut('fast');
			$("#"+header_id+" .row_img").attr('src','http://'+location['host']+'/graphics/windows/show_row.png');
			$("#"+header_id+" .row_img").attr('alt','Rowzwiń');
			$("#"+header_id+" .row_img").attr('title','Rowzwiń');
		} else {
			var parent_id = $("#"+element_id).parent().attr("id");
			var element_class = $("#"+element_id).attr("class");
			$("#"+parent_id+" ."+element_class).fadeOut('fast',function() {
				$("#"+parent_id+" .row_img").attr('src','http://'+location['host']+'/graphics/windows/show_row.png');
				$("#"+parent_id+" .row_img").attr('alt','Rozwiń');
				$("#"+parent_id+" .row_img").attr('title','Rozwiń');
				$("#"+element_id).fadeIn('fast');
				$("#"+header_id+" .row_img").attr('src','http://'+location['host']+'/graphics/windows/hide_row.png');
				$("#"+header_id+" .row_img").attr('alt','Zwiń');
				$("#"+header_id+" .row_img").attr('title','Zwiń');
			});
		}
	}
}

function showOrHideElement(element_id) {
	/*if($("#"+element_id).length) {
		if($("#"+element_id).css('display')!='none') {
			$("#"+element_id).fadeOut('fast');
		} else {
			$("#"+element_id).fadeIn('fast');
		}
	}*/
	var header_id=element_id.substring(0,element_id.length-9);
	if($("#"+element_id).length) {
		if($("#"+element_id).css('display')!='none') {
			$("#"+element_id).fadeOut('fast');
			$("#"+header_id+" .row_img").attr('src','http://'+location['host']+'/graphics/windows/show_row.png');
			$("#"+header_id+" .row_img").attr('alt','Rowzwiń');
			$("#"+header_id+" .row_img").attr('title','Rowzwiń');
		} else {
			/*var parent_id = $("#"+element_id).parent().attr("id");
			var element_class = $("#"+element_id).attr("class");
			$("#"+parent_id+" ."+element_class).fadeOut('fast',function() {
				$("#"+parent_id+" .row_img").attr('src','http://'+location['host']+'/graphics/windows/show_row.png');
				$("#"+parent_id+" .row_img").attr('alt','Rozwiń');
				$("#"+parent_id+" .row_img").attr('title','Rozwiń');*/
				$("#"+element_id).fadeIn('fast');
				$("#"+header_id+" .row_img").attr('src','http://'+location['host']+'/graphics/windows/hide_row.png');
				$("#"+header_id+" .row_img").attr('alt','Zwiń');
				$("#"+header_id+" .row_img").attr('title','Zwiń');
			//});
		}
	}
}

function cancelUploadNewClothes() {
	var req = mint.Request();
	req.method = "POST";
	req.AddParam('action', 1);	
	req.OnSuccess = function() {
		var response = this.responseText;
		showMessage(response);
	}
	req.Send("http://"+location['host']+"/ajax/clothes.ajax.php");
}

function ajaxClothesFileUpload() {
	//starting setting some animation when the ajax starts and completes
	$("#loading")
	.ajaxStart(function(){
		$(this).show();
	})
	.ajaxComplete(function(){
		$(this).hide();
	});

	$.ajaxFileUpload
	(
		{
			url:'http://'+location['host']+'/ajax/clothesupload.ajax.php', 
			secureuri:false,
			fileElementId:'uploadNewClothes',
			dataType: 'json',
			success: function (data, status)
			{
				if(typeof(data.error) != 'undefined')
				{
					if(data.error != "")
					{
						showMessage('2|'+data.error);
					} else {
						var splited = data.msg.split("|");
						$("#uploadNewClothes").hide();
						$("#newClothes").show();
						$("#clothes_preview").attr('src','http://'+location['host']+'/graphics/clothes/'+splited[1]);
						$("#clothes_file").attr('value',splited[1]);
					}
				}
			},
			error: function (data, status, e)
			{
				alert(e);
			}
		}
	)
	return false;
}

function ajaxAvatarFileUpload() {
	//starting setting some animation when the ajax starts and completes
	$("#loading")
	.ajaxStart(function(){
		$(this).show();
	})
	.ajaxComplete(function(){
		$(this).hide();
	});

	$.ajaxFileUpload
	(
		{
			url:'http://'+location['host']+'/ajax/avatarupload.ajax.php', 
			secureuri:false,
			fileElementId:'uploadAvatar',
			dataType: 'json',
			success: function (data, status)
			{
				if(typeof(data.error) != 'undefined')
				{
					if(data.error != "")
					{
						showMessage('2|'+data.error);
					} else {
						refreshContent('top_avatar','none','slow');
						showMessage('1|'+data.msg);
					}
					refreshContent('top_options_box');
					loadPageContent(1);
				}
			},
			error: function (data, status, e)
			{
				alert(e);
			}
		}
	)
	return false;
}

function ajaxGalleryPhotoFileUpload() {
	//starting setting some animation when the ajax starts and completes
	$("#loading")
	.ajaxStart(function(){
		$(this).show();
	})
	.ajaxComplete(function(){
		$(this).hide();
	});

	$.ajaxFileUpload
	(
		{
			url:'http://'+location['host']+'/ajax/photoupload.ajax.php', 
			secureuri:false,
			fileElementId:'uploadPhoto',
			dataType: 'json',
			success: function (data, status)
			{
				if(typeof(data.error) != 'undefined')
				{
					if(data.error != "")
					{
						showMessage('2|'+data.error);
					} else {
						var splited = data.msg.split("|");
						$("#uploadPhotoForm").hide();
						$("#addPhotoForm").show();
						$("#photoPreview").attr('src','http://'+location['host']+'/graphics/photos/'+splited[1]);
						$("#photoName").attr('value',splited[1]);
					}
				}
			},
			error: function (data, status, e)
			{
				alert(e);
			}
		}
	)
	return false;
}
//----------------------------------------
var lastCreatedWindow = null;
var messageContent = null;
var debug = null;

function saveOnline() {
	var req = mint.Request();
	req.method = "POST";	
	req.OnSuccess = function() {
		var response = this.responseText;
		if(response!=1) {
			showMessage('2|Błąd zapisywania aktywności gracza.');
		}
	}
	req.Send("http://"+location['host']+"/ajax/saveonline.ajax.php");
}

function showResult(result) {
	createWindow(24,1);
	loadPageContentText(24,result);
}

function makeJoke(user_id) {
	var req = mint.Request();
	req.method = "POST";
	req.AddParam('make_joke', 1);	
	req.AddParam('defender', user_id);	
	req.OnSuccess = function() {
		var response = this.responseText;
		showResult(response);
	}
	req.Send("http://"+location['host']+"/ajax/attack.ajax.php");
}

function refreshUserProfile(user_id) {
	if(windows[4]['status']>0) {
		setWindowTop(windows[4]['handle'],4,1);
		
		$("#window_content_4").css("opacity","0.5");
		$("#window_content_4").append("<div class='frame_loading'></div>");
		
		var req = mint.Request();
		req.method = "POST";
		req.AddParam('user_id', user_id);	
		req.OnSuccess = function() {
			var content = this.responseText;
			loadPageContentText(4,content);
			$("#window_content_4").css("opacity","1");
			var onclickEvent='refreshUserProfile('+user_id+')';
		addOnclick(4,onclickEvent,'.window_header');
		}
		req.Send("http://"+location['host']+"/ajax/userinfo.ajax.php");
	}
}

function showUserProfile(user_id) {
	if(windows[4]['status']==0) {
		createWindow(4,1);
	}
	
	$("#window_content_4").css("opacity","0.5");
	$("#window_content_4").append("<div class='frame_loading'></div>");
	
	var req = mint.Request();
	req.method = "POST";
	req.AddParam('user_id', user_id);	
	req.OnSuccess = function() {
		var content = this.responseText;
		loadPageContentText(4,content);
		$("#window_content_4").css("opacity","1");
		var onclickEvent='refreshUserProfile('+user_id+')';
		addOnclick(4,onclickEvent,'.window_header');
	}
	req.Send("http://"+location['host']+"/ajax/userinfo.ajax.php");
}

function showGang(gang_id) {
	createWindow(60,1);
	var req = mint.Request();
	req.method = "POST";
	req.AddParam('gang_id', gang_id);	
	req.AddParam('action', 2);	
	req.OnSuccess = function() {
		var content = this.responseText;
		loadPageContentText(60,content);
	}
	req.Send("http://"+location['host']+"/ajax/gangs.ajax.php");
}

function showStudents(page) {
	createWindow(13,1);
	
	//addOnclick(13,'showStudents()','.window_header');
	
	$("#window_content_13").css("opacity","0.5");
	$("#window_content_13").append("<div class='frame_loading'></div>");
	
	var req = mint.Request();
	req.method = "POST";	
	if(page) {
		req.AddParam('page', page);
	}
	req.OnSuccess = function() {
		var content = this.responseText;
		loadPageContentText(13,content);
		$("#window_content_13").css("opacity","1");
	}
	req.Send("http://"+location['host']+"/ajax/students.ajax.php");
}

function searchStudents(page) {
	if(windows[13]['status']>0) {
		
		$("#window_content_13").css("opacity","0.5");
		$("#window_content_13").append("<div class='frame_loading'></div>");
		
		var req = mint.Request();
		var search = $("#search").val();

		req.method = "POST";
		if(page) {
			req.AddParam('page', page);
		}
		req.AddParam('search', search);
		req.OnSuccess = function() {
			var content = this.responseText;
			loadPageContentText(13,content);
			$("#window_content_13").css("opacity","1");
		}
		req.Send("http://"+location['host']+"/ajax/students.ajax.php");
	}
}

function setSelectionRange(input, selectionStart, selectionEnd) {
  if (input.setSelectionRange) {
    input.focus();
    input.setSelectionRange(selectionStart, selectionEnd);
  }
  else if (input.createTextRange) {
    var range = input.createTextRange();
    range.collapse(true);
    range.moveEnd('character', selectionEnd);
    range.moveStart('character', selectionStart);
    range.select();
  }
}

function submitForm(form,url,wId,reqTo,resultAddTo) {
	method="post";
	if(reqTo==1) {
		url="http://"+location['host']+"/ajax/"+url;
	} else if(reqTo!=3) {
		url="http://"+location['host']+"/modules/"+url;
	}
	var req = mint.Request();
	req.OnSuccess = function() {
		var response = this.responseText;
		showMessage(response);
		
		if(form=='deleteAvatar' || form=='changeAvatarForm') {
			refreshContent('top_options_box');
		}
		
		if(wId>0) {
			if(wId==5 || wId==46) {
				windows[wId]['handle'].close();
			} else {
				loadPageContent(wId);
			}
		}
	}
	req.SendForm(form,url,method);
	return false;
}

function loadPageContent(wId) {
	if(wId) {
		var obj = $("#window_content_"+wId);
		
		obj.css("opacity","0.5");
		obj.append("<div class='frame_loading'></div>");
		
		var req = mint.Request();
		req.method = "POST";
		req.AddParam('subpage', wId);	
		req.OnSuccess = function() {
			var wContent = this.responseText;
			var splited = wContent.split("|");
			if(splited[0]=='message') {
				wContent=wContent.substring(splited[0].length+1,wContent.length);
				showMessage(wContent);
				windows[wId]['handle'].close();
			} else {
				if(windows[wId]['staticContent']==1) {
					var content = $("#window_content_"+wId).html();

					if(content.search(/frame_loading/i)<0) {
					//if(content!='<div class="frame_loading"></div>') {
						loadPageContentText(wId,content);
					} else {
						obj.html(wContent);	
					}
				} else {
					obj.html(wContent);	
				}
				obj.css("opacity","1");
			}
		}
		req.Send("http://"+location['host']+"/ajax/subpage.ajax.php");
	}
}

function addPageContentText(wId,content) {
	$("#window_content_"+wId).append(content);
}

function removePageContentText(wId,content) {
	$("#window_content_"+wId).remove(content);
}

function loadPageContentText(wId,content) {
	$("#window_content_"+wId).html("<div class='frame_loading'></div>");
	$("#window_content_"+wId).html(content);
}

function showMessage(response) {
	if(response.substring(0,9)!='nomessage') {
		if(response.length==0) {
			messageContent = "<span id='message'>Błąd komunikatu</span>";
		} else {
			var splited = response.split("|");
			if(splited[1].substring(splited[1].length-1,splited[1].length)=='1') {
				splited[1]=splited[1].substring(0,splited[1].length-1);
			}
			messageContent = "<span id='message_text'>"+splited[1]+"</span>" +
			"<div id='message_button'>" +
			"<a href='javascript:void(0)' onclick='closeWindow(0);' id='window_ok'><img src='http://"+location['host']+"/graphics/buttons/ok.png' alt='OK'></a>" +
			"</div>"+
			"<script type='text/javascript'>$('#window_ok').focus();</script>";
		}
		createWindow(0);
		
		var id = lastCreatedWindow.getWindowId();
	
		if(splited[0]==3 || splited[0]==4) {
			logout();
			splited[0]=splited[0]-2;
		}
	
		if(splited[0]==1) {
			$("#"+id+" .window_content").addClass("window_message_positive");
			$("#"+id+" .window_frame").css('background','#EFFFD5');
		} else if(splited[0]==2) {
			$("#"+id+" .window_content").addClass("window_message_negative");
			$("#"+id+" .window_frame").css('background','#FFCCCC');
		}
	}
}

function refreshContent(id,option1,option2) {
	var obj = $("#"+id);
	if(option1) {
		if(option1=='slow') {
			obj.fadeOut("slow");
		}
		if(option1=='fast') {
			obj.fadeOut("fast");
		}
	} else {
		obj.fadeOut("slow");
	}
	
	var req = mint.Request();
	req.method = "POST";
	req.AddParam('refresh', id);	
	req.OnSuccess = function() {
		var content = this.responseText;
		obj.html(content);
		if(option2) {
			if(option2=='slow') {
				obj.fadeIn("slow");
			}
			if(option2=='fast') {
				obj.fadeIn("fast");
			}
		} else {
			obj.fadeIn("slow");
		}
	}
	req.Send("http://"+location['host']+"/ajax/refresh.ajax.php");
}

function logout() {
	refreshContent('top_options_box','fast','slow');
	refreshContent('center_column','fast','slow');

	var windowsCount=windows.length;
	
	for(i=0;i<windowsCount;i++) {
		if(windows[i]['handle']!=null && windows[i]['id']!=0) {
			windows[i]['handle'].close();
		}
	}
}

function autoSetWidth(wId) {
	var parent = $("#"+windows[wId]['handle'].getWindowId());
	var width = parent.css('width');
	width=width.substring(0,width.length-2);
	
	if(wId==0) {
		var setWidth = width - 24;
		$("#window_content_"+wId).css('width',setWidth+'px');
		//autoSetHeight(wId);
	} else {
		var setWidth = width - 40;
		$("#window_content_"+wId).css('width',setWidth+'px');
	}
	
	if(wId==5 || wId==53) {
		parent = $("#"+windows[wId]['handle'].getWindowId()+" .window_frame");
		var height = parent.css('height');
		height=height.substring(0,height.length-2);
		var setHeight = height-40;
		$("#window_content_"+wId+" .content").css('height',setHeight+'px');
	}
}

function autoSetHeight(wId) {
	var contener_height = $("#window_content_"+wId)[0].offsetHeight;
	
	$("#window_content_"+wId).parent().css('height',contener_height+'px');
}

function setWindowTop(wnd,wId,staticContent) {
	if(wnd==null && wId>=0) {
		var wnd = windows[wId]['handle'];
	}

	if(wnd!=null) {
		var id = wnd.getWindowId();
		var self = $("#"+id);
			
		var zindex = parseInt(self.css('z-index'));
		var tmp = 0;
		var windowsCount=windows.length;
		var wId=null;
		for(i=0;i<windowsCount;i++) {
			if(windows[i]['handle']!=null && windows[i]['status']>0) {
				var id = windows[i]['handle'].getWindowId();
				var window = $("#"+id);
				tmp = parseInt(window.css('z-index'));
				if(tmp > zindex) { zindex = tmp; }
				if(wnd==windows[i]['handle']) { 
					wId=i;
				}
			}
		}
		
		zindex=zindex+2;
		
		if(windows[0]['status']>0) {
			var id = windows[0]['handle'].getWindowId();
			var self = $("#"+id);
			$("#message_box_background").css('z-index',zindex);
			zindex=zindex+1;
			self.css('z-index',zindex);
		} else {
			self.css('z-index',zindex);
			if(wId!=null && wId!=40) {
				setAsNotIdle(wId);
				var content=null;
				if(staticContent==1 && wId!=null) {
					var content = $("#window_content_"+wId).html();
					loadPageContentText(wId,content);
				} else {
					loadPageContent(wId);
				}
			}
		}
		return false;
	}
}

function addOnclick(windowId,event,element) {
	if(windows[windowId]['handle']!=null) {
		var id = windows[windowId]['handle'].getWindowId();
		if(element!='') {
			$("#"+id+" "+element).attr("onClick", event);
		} else {
			$("#"+id).attr("onClick", event);
		}
	}
}

function addDblOnclick(windowId,event) {
	if(windows[windowId]['handle']!=null) {
		var id = windows[windowId]['handle'].getWindowId();
		$("#"+id).attr("onDblClick", event);
	}
}

function removeOnclick(windowId) {
	if(windows[windowId]['handle']!=null) {
		var id = windows[windowId]['handle'].getWindowId();
		var window = $("#"+id);
		window.removeAttr("onclick");
	}
}

function setAsNotIdle(wId) {
	var content1="<div class='window_loading_bg'></div>";
	var content2="<div class='frame_loading' style='opacity:0.5;'></div>";
	var windowsCount=windows.length;

	for(i=0;i<windowsCount;i++) {
		if(windows[i]['handle']!=null) {
			var id = windows[i]['handle'].getWindowId();
			if(wId==windows[i]['id']) { 
				if($("#window_content_"+windows[i]['id']+" .window_loading_bg").length>0) {
					$("#window_content_"+windows[i]['id']+" .window_loading_bg").remove();
					$("#window_content_"+windows[i]['id']+" .frame_loading").remove();
				}
			} else {
				if($("#window_content_"+windows[i]['id']+" .window_loading_bg").length==0) {
					$("#window_content_"+windows[i]['id']).append(content1);
					$("#window_content_"+windows[i]['id']).append(content2);
					$("#window_content_"+windows[i]['id']+" iframe").hide();
				}
			}
		}
	}
}

function changeLocationRequest(goTo) {
	if(goTo==1 || goTo==2 || goTo==3) {
		if(windows[40]['status']>0) {
			closeWindow(40);
		}

		var windowsCount=windows.length;
		for(i=1;i<windowsCount;i++) {
			if(windows[i]['status']!=0 && windows[i]['location']!=goTo && windows[i]['location']!=0) {
				if(windows[i]['status']==2) {
					var id = windows[i]['handle'].getWindowId();
					if($("#minimized_"+id).length) {
						var el = document.getElementById('minimized_'+id);
 						el.parentNode.removeChild(el);
					}
				}
				windows[i]['status']=0;
				windows[i]['handle'].close(true);
			}
		}

		if(goTo==1) {
			$("#page_contener").attr('class', '')
			$("#page_contener").addClass('page_contener_school');
			
			$("body").attr('class', '')
			$("body").addClass('body_school');
		}
		
		if(goTo==2) {
			$("#page_contener").attr('class', '')
			$("#page_contener").addClass('page_contener_house');
			
			$("body").attr('class', '')
			$("body").addClass('body_house');
		}
		
		if(goTo==3) {
			$("#page_contener").attr('class', '')
			$("#page_contener").addClass('page_contener_retailpark');
			
			$("body").attr('class', '')
			$("body").addClass('body_retailpark');
		}

		var obj = $("#center_column");
		obj.fadeOut("fast");
					
		var req = mint.Request();
		req.method = "POST";
		req.AddParam('subpage', '0');
		req.AddParam('option', goTo);
		req.OnSuccess = function() {
			response = this.responseText;
			obj.html(response);
			obj.fadeIn("slow");
		}
		req.Send("http://"+location['host']+"/ajax/subpage.ajax.php");
	}
}

function changeLocationNow(goTo) {
	closeWindow(0);
	if(goTo==1 || goTo==2 || goTo==3) {
		var req = mint.Request();
		req.method = "POST";
		req.AddParam('goTo', goTo);
		req.AddParam('quickMovement', '1');
		req.OnSuccess = function() {
			var response = this.responseText;
			var splited = response.split("|");
			if(splited[0]=='message') {
				response=response.substring(splited[0].length+1,response.length);
				showMessage(response);
			}
			if(response==1 || response==2 || response==3) {
				refreshContent('top_credits','none','slow');
				createWindow(40);
			}
		}
		req.Send("http://"+location['host']+"/ajax/movement.ajax.php");
	}
}

function changeLocation(goTo) {
	if(goTo==1 || goTo==2 || goTo==3) {
		var req = mint.Request();
		req.method = "POST";
		req.AddParam('goTo', goTo);
		req.OnSuccess = function() {
			var response = this.responseText;

			var splited = response.split("|");
			if(splited[0]=='message') {
				response=response.substring(splited[0].length+1,response.length);
				showMessage(response);
			}
			if(response==1 || response==2 || response==3) {
				createWindow(40);
			}
		}
		req.Send("http://"+location['host']+"/ajax/movement.ajax.php");
	}
}

function createWindow(wId,noContent) {
	saveOnline();
	
	if(windows[wId]['id']!=wId) {
		alert('Błąd konfigutacji okien!');
	}
		
	if(!windows[wId]['width']) {
		alert('Błąd pliku schematów okien!');
	}
	
	if(windows[wId]['status']==0) {
		var loading="<div class='frame_loading'></div>";

		if(windows[wId]['closable']!=false) {
			windows[wId]['closable']=true;
		}
		
		lastCreatedWindow = $.window({
			title: windows[wId]['title'],
	   		content: "<div class='window_content' id='window_content_"+wId+"'>"+loading+"</div>",
	   		containerClass: "contener",
	   		showRoundCorner: windows[wId]['showRoundCorner'],
	   		minimizable: windows[wId]['minimizable'],
	   		maximizable: windows[wId]['maximizable'],
	   		resizable: windows[wId]['resizable'],
	   		closable: windows[wId]['closable'],
	   		width: windows[wId]['width'],
	   		height: windows[wId]['height'],
	   		minHeight: windows[wId]['minHeight'],
	   		minWidth: windows[wId]['minWidth'],
	   		maxHeight: windows[wId]['maxHeight'],
	   		maxWidth: windows[wId]['maxWidth'],
	   		scrollable: windows[wId]['scrollable'],
	   		showFooter: windows[wId]['showFooter'],
	   		x: 200,
	   		y: windows[wId]['top'],
	   		z: 10,
	   		scrollable: true,
	   		onOpen: function(wnd) {
				//none
			},
			onShow: function(wnd) {
				//none
			},
	   		onClose: function(wnd) {
				windows[wId]['status']=0;
				
				if(windows[wId]['id']==0 || windows[wId]['id']==40) {
					$("#message_box_background").fadeOut("slow");
					saveOnline();
				} else {
				
				windowsCount = windows.length;
				for(i=windowsCount-1;i>0;i--) {
					if(windows[i]['status']==1) {
						setWindowTop(null,i,windows[i]['staticContent']);
						break;
					}
				}
				
				}
	   		},
	   		onMinimize: function(wnd) {
				windows[wId]['status']=2;
				windowsCount = windows.length;
				for(i=0;i<windowsCount;i++) {
					if(windows[i]['status']==1) {
						setWindowTop(null,i,windows[i]['staticContent']);
						break;
					}
				}
			},
			afterDrag: function(wnd) {
				var id = windows[wId]['handle'].getWindowId();
				var window = $("#"+id);
				var offset = window.css('top');
				if(offset) {
					offset=offset.substring(0,offset.length-2);
					if(offset<=0) {
						$("#"+id).css('top','3px');
					}
				}
			},
			afterMaximize: function(wnd) {
				autoSetWidth(wId);
			},
			afterCascade: function(wnd) { //po przywroceniu ze stanu zminimalizowanego
				windows[wId]['status']=1;
				autoSetWidth(wId);
				setWindowTop(null,windows[wId]['id'],windows[wId]['staticContent']);
			},
			afterResize: function(wnd) {
				autoSetWidth(wId);
			}
		});
		
		lastCreatedWindow.alignHorizontalCenter();

		windows[wId]['status']=1;
		windows[wId]['handle']=lastCreatedWindow;

		var id = windows[wId]['handle'].getWindowId();
		var window = $("#"+id);

		autoSetWidth(wId);

		setWindowTop(windows[wId]['handle'],wId,windows[wId]['staticContent']);
		
		if(windows[wId]['id']==40) {
			var zindex = parseInt(window.css('z-index'));
			var height=$("body").height();
			var page_height=$("#page_contener").height();
			if(page_height>height) { height=page_height; }
			zindex = zindex+1;
			$("#message_box_background").css('z-index',zindex);
			$("#message_box_background").css('height',height+'px');
			zindex = zindex+2;
			window.css('z-index',zindex);
			$("#message_box_background").fadeIn("slow");
		}
		
		if(windows[wId]['id']==0) {
			var zindex = parseInt(window.css('z-index'));
			var height=$("body").height();
			var page_height=$("#page_contener").height();
			if(page_height>height) { height=page_height; }
			zindex = zindex+1;
			$("#message_box_background").css('z-index',zindex);
			$("#message_box_background").css('height',height+'px');
			zindex = zindex+2;
			window.css('z-index',zindex);
			$("#message_box_background").fadeIn("slow");
			loadPageContentText(wId,messageContent);
		} else {
			if(noContent!=1) {
				loadPageContent(wId);
			}
		}
		
		if(wId!=40 && wId!=37 && wId!=59) {
			var onclickEvent = 'setWindowTop(null,'+windows[wId]['id']+','+windows[wId]['staticContent']+');';
			addDblOnclick(windows[wId]['id'],onclickEvent);
			addOnclick(windows[wId]['id'],onclickEvent,'.window_header');
		}
	} else if(windows[wId]['status']==2) {
		windows[wId]['handle'].restore();
		windows[wId]['status']=1;
		setWindowTop(windows[wId]['handle'],wId,windows[wId]['staticContent']);
	} else if(windows[wId]['status']==1) {
		windows[wId]['status']=1;
		setWindowTop(windows[wId]['handle'],wId,windows[wId]['staticContent']);
	}
}

function unMinimize(id) {
	var windowsCount=windows.length;
	var minimized=0;
	
	for(i=0;i<windowsCount;i++) {
		if(windows[i]['handle']!=null && windows[i]['status']==2) {
			minimized+=1;
			if(windows[i]['handle'].getWindowId()==id) {
				windows[i]['handle'].restore();
				windows[i]['status']=1;
			}
		}
	}
	
	var bars = Math.floor(minimized / 5);
	if((bars * 5 + 1)==minimized) {
		scrollLeftBox();
	}
	
	checkScrollBoxes();
}

function closeAll() {
	$.window.closeAll();
	$("#windows_box").html('');
}

function closeWindow(wId) {
	windows[wId]['handle'].close();
}

function minimalizeAll() {
	var windowsCount=windows.length;

	for(i=0;i<windowsCount;i++) {
		if(windows[i]['handle']!=null && windows[i]['status']==1) {
			windows[i]['status']=2;
			windows[i]['handle'].minimize();
		}
	}
}

function showAll() {
	var windowsCount=windows.length;
	
	for(i=0;i<windowsCount;i++) {
		if(windows[i]['handle']!=null && windows[i]['status']==2) {
			windows[i]['handle'].restore();
			windows[i]['status']=1;
		}
	}
}

function scrollLeftBox() {
	var value = $("#windows_box").css('margin-top');
	value = value.substr(0,value.length-2);
	value = parseInt(value);
	var new_value = value + 24;
	if(new_value>0) { new_value=0; }
	new_value=new_value+'px';
	$("#windows_box").animate({"marginTop": new_value}, "slow");
}

function scrollRightBox() {
	var value = $("#windows_box").css('margin-top');
	value = value.substr(0,value.length-2);
	value = parseInt(value);
	var new_value = value - 24;
	
	var windowsCount=windows.length;
	var minimized=0;
	for(i=0;i<windowsCount;i++) {
		if(windows[i]['status']==2) { minimized+=1; }
	}
	
	var rows=Math.floor(minimized/5);
	rows = rows * 24 * -1;
	if(new_value<rows) { new_value=rows; }
	
	new_value=new_value+'px';
	$("#windows_box").animate({"marginTop": new_value}, "slow");
}

function checkScrollBoxes() {
	var windowsCount=windows.length;
	var minimized=0;
	for(i=0;i<windowsCount;i++) {
		if(windows[i]['status']==2) { minimized+=1; }
	}

	if(minimized>5) {
		showScrollBoxes();
	} else {
		hideScrollBoxes();
	}
}

function showScrollBoxes() {
	$("#scrollLeftBox").fadeIn("slow");
	$("#scrollRightBox").fadeIn("slow");
}

function hideScrollBoxes() {
	$("#scrollLeftBox").fadeOut("slow");
	$("#scrollRightBox").fadeOut("slow");
}

function showOrHideRival() {
	var left = $("#user_rival").css("margin-left");
	if(left=='-140px') {
		$("#user_rival").animate({"marginLeft": "0"}, "slow");
		$("#show_rival").fadeOut("fast");
		$("#hide_rival").fadeIn("fast");
	}
	
	if(left=='0px') {
		$("#user_rival").animate({"marginLeft": "-140"}, "slow");
		$("#hide_rival").fadeOut("fast");
		$("#show_rival").fadeIn("fast");
	}
	
	if(left=='-165px') {
		$("#user_rival").animate({"marginLeft": "0"}, "slow");
		$("#show_rival").fadeOut("fast");
		$("#hide_rival").fadeIn("fast");
	}
}
