var common_friend_page = 1;
var common_friend_ajax_waiting = false;
var common_friend_href = ""; // Link ostatnio wczytanej strony ajaxowej 
var common_friend_c_member_id = ""; // Link ostatnio wczytanej strony ajaxowej 
var common_friend_regex = new RegExp("common_friend_page=([0-9]+)");
var common_friend_id_regex = new RegExp("(target_c_member_id=|user/)([0-9]+)");

jQuery(document).ready(function(){

	jQuery("#common_friends_border").bind("ajaxSuccess", function () {
		common_friend_ajax_waiting = false;
	});

	jQuery("#common_friends_border").bind("ajaxError", function () {
		common_friend_ajax_waiting = false;
	});

	jQuery("#common_friends_move_right").click(function(){
		if (common_friend_ajax_waiting==false){
			common_friend_move_slider(1);
		}
		return false;
	});

	jQuery("#common_friends_move_left").click(function(){
		if (common_friend_ajax_waiting==false){
			common_friend_move_slider(-1);
		}
		return false;
	});
	
	common_friends_add_hovers();
});

/**
 * Zmienia aktualnie wyświetlaną stronę ze wspólnymi przyjaciółmi przy wykorzystaniu ajax-u
 */
function common_friend_move_slider(direction){
	common_friend_href = null;
	if (direction == 1) common_friend_href = jQuery("#common_friends_move_right").attr("href");
	else common_friend_href = jQuery("#common_friends_move_left").attr("href");
	if (!common_friend_href) return false;
	
	common_friend_page = parseInt(common_friend_href.match(common_friend_regex)[1]);
	common_friend_c_member_id = parseInt(common_friend_href.match(common_friend_id_regex)[2]);
				
	common_friend_ajax_waiting = true;				
	
	url = "index.php?m=pc&a=page_f_home_ajax_common_friend&target_c_member_id="+common_friend_c_member_id+"&common_friend_page="+common_friend_page+"&ajax=1";
	jQuery.get(url, function(content){
		if (content == "session_expired") 
			window.location.reload();
		else {
			var prev = content[0];
			var next = content[1];
			content = content.substring(2);
			jQuery("#common_friends_border").html(content);
			
			// Uaktulnij przycisk do przewijania w prawo
			a_next = jQuery("#common_friends_move_right"); 
			if (next=="1")
			{ 
				a_next.removeClass("common_friends_move_inactive");
				url = turl({a: 'page_f_home', target_c_member_id: common_friend_c_member_id, common_friend_page: common_friend_page + 1 });
				a_next.attr("href", url);
			}
			else
			{
				a_next.addClass("common_friends_move_inactive");
				a_next.attr("href", "");
			}

			// Uaktualnij przycisk do przewijania w lewo
			a_prev = jQuery("#common_friends_move_left"); 
			if (prev=="1")
			{ 
				a_prev.removeClass("common_friends_move_inactive");
				url = turl({a: 'page_f_home', target_c_member_id: common_friend_c_member_id, common_friend_page: common_friend_page - 1 });
				a_prev.attr("href", url);
			}
			else
			{
				a_prev.addClass("common_friends_move_inactive");
				a_prev.attr("href", "");
			}
			
			common_friends_add_hovers();
		}				
	});		
}

function common_friends_add_hovers(){
	jQuery("#common_friends .photos_row dl").hover(function () {
		jQuery("#common_friends_footer dl *").remove();
		jQuery("#common_friends_footer dl").append(jQuery(this).html());
	},function(){});	

	var common_friends_count = jQuery("#common_friends .photos_row li").size();
	if (common_friends_count>0){
		jQuery("#common_friends_footer dl *").remove();
		jQuery("#common_friends_footer dl").append(jQuery("#common_friends .photos_row li dl:first").html());
	}
}
