$(document).ready(function() {

	var itemHeight = 150;
	var curFirst = 0;
	var atPage = 10;
	var padding = 5;
	var linksOn = null;
	var working = false;
	
	var commonOnly = false;
	
	var items = $("#friends div.item");
	var commons = $("#friends div.common");
	
	var itemArr = items;
	
	page(0);
	
	$("#commonOnly").removeAttr("checked");
	
	$("#commonOnly").click(function() {
		commonOnly = $(this).attr("checked");
		page(0);
	});
	$("#friends a.commonLink span").click(function() { 
		if($("#commonOnly").attr("checked")) {
			$("#commonOnly").removeAttr("checked");
			commonOnly = false;
		} else {
			$("#commonOnly").attr("checked","true");
			commonOnly = true;
		}
		page(0);
	});
	
	function page(no) {
	
		var first = curFirst * atPage;
		var last = first + atPage;
		if(last > itemArr.length) last = itemArr.length;
		for(i=first; i<last; i++) {
			$(itemArr[i]).hide();
		}
		
		if(commonOnly) {
			itemArr = commons;
		} else {
			itemArr = items;
		}
		curFirst = no;
		
		first = no * atPage;
		last = first + atPage;
		if(last > itemArr.length) last = itemArr.length;
		for(i=first; i<last; i++) {
			$(itemArr[i]).show();
			showItem($(itemArr[i]).attr("id"));
		}
		
		doPaging(no);
		
	}
	
	function showItem(id) {
		var mg = parseInt($("#"+id).css("margin-bottom"));
		if(mg == 0) {
			doHeight(id);
			$("#"+id+" .links").hide();
			doLinks(id);
		}
		$("#"+id).show();
	}
	
	function showLinks(id) {
		$("#"+id+" .links").show();
		$("#"+id).css("padding",(padding-1)+"px");
		$("#"+id).css("border-width","1px");
	}
	
	function hideLinks(id) {
		$("#"+id+" .links").hide();
		$("#"+id).css("padding",(padding)+"px");
		$("#"+id).css("border-width","0");
	}
	
	function errorAlert() {
		$("img.plwiatrak").hide();
		alert("Połączenie się nie powiodło, spróbuj jeszcze raz.");
		working = false;
	}
	
	function doHeight(id) {
		$("#"+id).css("height","auto");
		var h = $("#"+id).outerHeight();
		$("#"+id).css("height",h+"px");
		$("#"+id).css("margin-bottom",(itemHeight-h)+"px");
	}
	
	function doLinks(id) {
	
		$("#"+id).hoverIntent(function() {
			if(working) return false;
			hideLinks(linksOn);
			showLinks(id);
			linksOn = id;
		}, function() {
			if(working) return false;
			hideLinks(id);
		});
		
		$("#"+id+" .showprf").click(function() {
			if(working) return false;
		});
		$("#"+id+" .sendmsg").click(function() {
			if(working) return false;
		});	
		
		$("#"+id+" .rollrecomm").click(function() {
			if(working) return false;
			
			var options = {
				dataType: 'json',
				type: 'post',
				url: $(this).attr("href"),
				success: null,
				beforeSubmit: function(data,form,opt) {
					$(form).parent().children(".plwiatrak").show();
				},
				error: errorAlert
			};
		
			var el = this;
			if($(this).text() == "Dodaj do ulubionych") {
			
				working = true;
				if(confirm("Czy na pewno chcesz dodać do ulubionych?")) {
					options.success = function(re, st) {
						$("img.plwiatrak").hide();
						if(re.result == "ok") {
							if(re.value != null && re.value.length > 0) {
								$(el).attr("href",re.value);
								$(el).text("Usuń z ulubionych");
							} else {
								var it = $(el).parents(".item").get(0);
								it = $(it).attr("id");
								$(el).parent().remove();
								doHeight(it);
							}
						} else {
							errorUI(re.value);
						}
						setTimeout(function() { working = false; },100);
					};
					$(this).ajaxSubmit(options);
				} else {
					working = false;
					return false;
				}
				
			} else if($(this).text() == "Usuń z ulubionych") {
			
				working = true;
				if(confirm("Czy na pewno chcesz usunąć z ulubionych?")) {
					options.success = function(re, st) {
						$("img.plwiatrak").hide();
						if(re.result == "ok") {
							if(re.value != null && re.value.length > 0) {
								$(el).attr("href",re.value);
								$(el).text("Dodaj do ulubionych");
							} else {
								var it = $(el).parents(".item").get(0);
								it = $(it).attr("id");
								$(el).parent().remove();
								doHeight(it);
							}
						} else {
							errorUI(re.value);
						}
						setTimeout(function() { working = false; },100);
					};
					$(this).ajaxSubmit(options);
				} else {
					working = false;
					return false;
				}
				
			}
			return false;
		});
	
		$("#"+id+" .rollinvite").click(function() {
			if(working) return false;
			
			var options = {
				dataType: 'json',
				type: 'post',
				url: $(this).attr("href"),
				success: null,
				beforeSubmit: function(data,form,opt) {
					$(form).parent().children(".plwiatrak").show();
				},
				error: errorAlert
			};
			
			var el = this;
			if($(this).text() == "Usuń ze znajomych") {
			
				working = true;
				if(confirm("Czy na pewno chcesz usunąć ze znajomych?")) {	
					options.success = function(re, st) {
						$("img.plwiatrak").hide();
						if(re.result == "ok") {
							var pr = $(el).parents(".friendsroll");
							pr = $(pr).attr("id");
							$("#"+pr.substring(3)).remove();
							$("#"+pr).remove();
							var qu = parseInt($("#main span.quant_kumple").text());
							$("#main span.quant_kumple").text(""+(qu-1));
						} else {
							errorUI(re.value);
						}
						setTimeout(function() { working = false; },100);
					};
					$(this).ajaxSubmit(options);
				} else {
					working = false;
					return false;
				}
				
			}
			return false;
		});
	
	
		$("#"+id+" .rollnotify").click(function() {
			if(working) return false;
			
			var options = {
				dataType: 'json',
				type: 'post',
				url: $(this).attr("href"),
				success: null,
				beforeSubmit: function(data,form,opt) {
					$(form).parent().children(".plwiatrak").show();
				},
				error: errorAlert
			};
	
			var el = this;
			if($(this).text() == "Powiadamiaj o wpisach") {
	
				working = true;
				if(confirm("Czy na pewno chcesz dodać do powiadomień?")) {
					options.success = function(re, st) {
						$("img.plwiatrak").hide();
						if(re.result == "ok") {
							if(re.value != null && re.value.length > 0) {
								$(el).attr("href",re.value);
								$(el).text("Nie powiadamiaj o wpisach");
							} else {
								var it = $(el).parents(".item").get(0);
								it = $(it).attr("id");
								$(el).parent().remove();
								doHeight(it);
							}
						} else {
							errorUI(re.value);
						}
						setTimeout(function() { working = false; },100);
					};
					$(this).ajaxSubmit(options);
				} else {
					working = false;
					return false;
				}
	
			} else if($(this).text() == "Nie powiadamiaj o wpisach") {
	
				working = true;
				if(confirm("Czy na pewno chcesz usunąć z powiadomień?")) {
					options.success = function(re, st) {
						$("img.plwiatrak").hide();
						if(re.result == "ok") {
							if(re.value != null && re.value.length > 0) {
								$(el).attr("href",re.value);
								$(el).text("Powiadamiaj o wpisach");
							} else {
								var it = $(el).parents(".item").get(0);
								it = $(it).attr("id");
								$(el).parent().remove();
								doHeight(it);
							}
						} else {
							errorUI(re.value);
						}
						setTimeout(function() { working = false; },100);
					};
					$(this).ajaxSubmit(options);
				} else {
					working = false;
					return false;
				}
	
			}
			return false;
		});
	}
	
	function doPaging(no) {
		var count = Math.ceil(itemArr.length / atPage);
		$("#friends div.paging").html(createPaging(count,no));
		$("#friends div.paging a").click(function() {
			var num = $(this).attr("class");
			num = num.substring(5);
			num = parseInt(num);
			page(num);
		});
	}
	
	function createPaging(count,sel) {

		var ret = '';

		if(count < 2) return ret;
	
		ret += '<a class="navto0"><img src="/images/left2.png" alt="&laquo;" /></a>';
		if(sel > 0) {
			ret += '<a class="navto'+(sel-1)+'"><img src="/images/left1.png" alt="&lt;" /></a>';
		}
		if(sel > 2) {
			ret += '<span>...</span>';
		}
		if(sel > 1) {
			ret += '<a class="navto'+(sel-2)+'">';
			if(sel < 11) ret += '0';
			ret += (sel-1)+'</a>';
		}
		if(sel > 0) {
			ret += '<a class="navto'+(sel-1)+'">';
			if(sel < 10) ret += '0';
			ret += (sel)+'</a>';
		}
	
		ret += '<span>';
		if(sel < 9) ret += '0';
		ret += (sel+1)+'</span>';
	
		if(sel < count-1) {
			ret += '<a class="navto'+(sel+1)+'">';
			if(sel < 8) ret += '0';
			ret += (sel+2)+'</a>';
		}
		if(sel < count-2) {
			ret += '<a class="navto'+(sel+2)+'">';
			if(sel < 7) ret += '0';
			ret += (sel+3)+'</a>';
		}
		if(sel < count-3) {
			ret += '<span>z</span>';
			ret += '<a class="navto'+(count-1)+'">';
			if(count < 9) ret += '0';
			ret += (count)+'</a>';
		}
		if(sel < count-1) {
			ret += '<a class="navto'+(sel+1)+'"><img src="/images/right1.png" alt="&gt;" /></a>';
		}
		ret += '<a class="navto'+(count-1)+'"><img src="/images/right2.png" alt="&raquo;" /></a>';
	
		return ret;
	}

});
