$.fn.page_userProfile = function(options) {
	var opts = $.extend({}, $.fn.page_userProfile.defaults, options);
	return this.each(function() {  
		
		var $this = $(this);
		
		// Sekošanas poga
		$this.find('.btn_follow a').click(function(e) {
				e.preventDefault();
				var fuser_id = parseInt($(this).attr('href').split('#')[1], 10);
				$.fn.page_userProfile.follow($this, opts, fuser_id);
			});
		
		// Bloķēšanas poga
		$this.find('.btn_block a').click(function(e) {
				e.preventDefault();
				var bl_user_id = parseInt($(this).attr('href').split('#')[1], 10);
				$.fn.page_userProfile.block($this, opts, bl_user_id);
			});
		
		// Bildes pārlukošana - grupēšana
		$this.find('#user_p_photos .group input').change(function() {
				var group = 0;
				if ($(this).is(':checked')) group = 1;
				else group = 0;
				window.location.replace('./?group=' + group);
			});
		
	});
};

$.fn.page_userProfile.defaults = {};

$.fn.page_userProfile.follow = function($this, opts, fuser_id) {
	$follow_line = $this.find('.btn_follow');
	$loaderc = $follow_line.parent().parent().find('h2 span').css('padding-left', '5px');
	$.fn.showLoader($loaderc, {type: 2});
	$.ajax({
		url: HTTP_ROOT + 'profile/user/follow.ajax.php',
		type: "POST",
		data: 'fuser_id=' + fuser_id,
		complete: function(req) {
			$loaderc.html('');
			if (req.status == 200) { //success
				var txt = req.responseText.split('|');
				if (txt[0] == 'ok') {
					if (txt[1] == 'follow') {
				    	$follow_line.html('<img src="' + HTTP_ROOT + 'profile/user/img/icon_unfollow.gif" alt="" /> <a href="#' + fuser_id + '">Pārtraukt sekot profilam</a>');
				    }else {
				    	$follow_line.html('<img src="' + HTTP_ROOT + 'profile/user/img/icon_follow.gif" alt="" /> <a href="#' + fuser_id + '">Sekot profilam</a>');
				    }
				    $follow_line.find('a').click(function(e) {
							e.preventDefault();
							$.fn.page_userProfile.follow($this, opts, fuser_id);
						});
				}else {
					alert(txt[1]);
				}
			}else { //failure
				alert(req.responseText);
			}
		}
	});
}; // END follow

$.fn.page_userProfile.block = function($this, opts, bl_user_id) {
	$block_line = $this.find('.btn_block');
	$loaderc = $block_line.parent().parent().find('h2 span').css('padding-left', '5px');
	$.fn.showLoader($loaderc, {type: 2});
	$.ajax({
		url: HTTP_ROOT + 'profile/user/block.ajax.php',
		type: "POST",
		data: 'bl_user_id=' + bl_user_id,
		complete: function(req) {
			$loaderc.html('');
			if (req.status == 200) { //success
				var txt = req.responseText.split('|');
				if (txt[0] == 'ok') {
					if (txt[1] == 'block') {
				    	$block_line.html('<img src="' + HTTP_ROOT + 'profile/user/img/icon_unblock.gif" alt="" /> <a href="#' + bl_user_id + '">Pārtraukt bloķēt profilu</a>');
				    }else {
				    	$block_line.html('<img src="' + HTTP_ROOT + 'profile/user/img/icon_block.gif" alt="" /> <a href="#' + bl_user_id + '">Bloķēt profilu</a>');
				    }
				    $block_line.find('a').click(function(e) {
							e.preventDefault();
							$.fn.page_userProfile.block($this, opts, bl_user_id);
						});
				}else {
					alert(txt[1]);
				}
			}else { //failure
				alert(req.responseText);
			}
		}
	});
}; // END block

$(document).ready(function() {
	$('#center_content').page_userProfile();
	// Nosūdzēšanas poga
	$('#center_content .btn_report a').plugin_Reports();
	// Profila bilde
	$('.ucol_rgt .profile_img a').fancybox({'titleShow':false});
});