$(document).ready(function()
{
	var mode_start=0, mode_max_end=contacts_setting[block_mode.MAX]['peoplePerColumn']*contacts_setting[block_mode.MAX]['visibleColumns']-1;
	var photo_mode_start=0, photo_mode_max_end = photos_setting[block_mode.MAX]['peoplePerColumn']*photos_setting[block_mode.MAX]['visibleColumns']-1;
	var mini_gallery_start=0, mini_gallery_end=2;
	
	var cp_setting = {"1":{"peoplePerColumn":0,"visibleColumns":0},"2":{"peoplePerColumn":5,"visibleColumns":2},"3":{"peoplePerColumn":2,"visibleColumns":7},"4":{"peoplePerColumn":1,"visibleColumns":1}};
	var cp_mode_start=0, cp_mode_max_end=0;
	
	$.cookie("ifly_cp_mode", block_mode.MAX);
	
	function contactsRemoveSel()
	{
		$("#contacts_controls > a").each(function()
		{
			$(this).removeClass("sel");
		});
	}
	
	function photosRemoveSel()
	{
		$("#photos_controls > a").each(function()
		{
			$(this).removeClass("sel");
		});
	}
	
	function removeSelCP()
	{
		$("#cp_controls > a").each(function()
		{
			$(this).removeClass("sel");
		});
	}
	
	$("#contacts_im_up").click(function()
	{
		$.cookie("ifly_contacts_mode", block_mode.HIDE);
		
		contactsRemoveSel();
		$(this).addClass("sel")
			.addClass("im_down");
			
		$("#contacts_text").hide();
		$("#contacts_mini").hide();
		$("#contacts_max").hide();
        
        $("#contacts_more").hide();
		
		$("#contacts_hide").show();
		return false;
	});
	
	$("#photo_im_up").click(function()
	{
		$.cookie("ifly_photo_mode", block_mode.HIDE);
		
		photosRemoveSel();
		$(this).addClass("sel")
			.addClass("im_down");
			
		//$("#contacts_text").hide();
		$("#photos_mini").hide();
		$("#photos_max").hide();
        
        $("#photos_more").hide();
		
		$("#photos_hide").show();
		return false;
	});
	
	$("#contacts_im_minimize").click(function()
	{
		$.cookie("ifly_contacts_mode", block_mode.MINI);
		
		contactsRemoveSel();
		$(this).addClass("sel");
		$("#contacts_im_up")
			.removeClass("im_down")
			.addClass("im_up");
		
		$("#contacts_text").hide();
		$("#contacts_hide").hide();
		$("#contacts_max").hide();
        
        $("#contacts_more").show();
		
		$("#contacts_mini").show();
		
		if (total_friends > contacts_setting[block_mode.MINI]['peoplePerColumn']*contacts_setting[block_mode.MINI]['visibleColumns'])
			$("#contacts_arrows").show();
		else
			$("#contacts_arrows").hide();
		
		return false;
	});
	
	$("#photo_im_minimize").click(function()
	{
		$.cookie("ifly_photo_mode", block_mode.MINI);
		
		photosRemoveSel();
		$(this).addClass("sel");
		$("#photo_im_up")
			.removeClass("im_down")
			.addClass("im_up");
		
		//$("#photos_text").hide();
		$("#photos_hide").hide();
		$("#photos_max").hide();
        
        $("#photos_more").show();
		
		$("#photos_mini").show();
		
		if (total_albums > photos_setting[block_mode.MINI]['peoplePerColumn']*photos_setting[block_mode.MINI]['visibleColumns'])
			$("#photos_arrows").show();
		else
			$("#photos_arrows").hide();
		
		return false;
	});
	
	$("#photo_im_maximize").click(function()
	{
		$.cookie("ifly_photo_mode", block_mode.MAX);
		
		photosRemoveSel();
		$(this).addClass("sel");
		$("#photo_im_up")
			.removeClass("im_down")
			.addClass("im_up");
		
		//$("#photos_text").hide();
		$("#photos_hide").hide();
		$("#photos_mini").hide();
        
        $("#photos_more").show();
		
		$("#photos_max").show();
		
		if (total_albums > photos_setting[block_mode.MAX]['peoplePerColumn']*photos_setting[block_mode.MAX]['visibleColumns'])
			$("#photos_arrows").show();
		else
			$("#photos_arrows").hide();
		
		return false;
	});
	
	$("#contacts_im_maximize").click(function()
	{
		$.cookie("ifly_contacts_mode", block_mode.MAX);
		
		contactsRemoveSel();
		$(this).addClass("sel");
		$("#contacts_im_up")
			.removeClass("im_down")
			.addClass("im_up");
		
		$("#contacts_text").hide();
		$("#contacts_hide").hide();
		$("#contacts_mini").hide();
        
        $("#contacts_more").show();
		
		$("#contacts_max").show();
		
		if (total_friends > contacts_setting[block_mode.MAX]['peoplePerColumn']*contacts_setting[block_mode.MAX]['visibleColumns'])
			$("#contacts_arrows").show();
		else
			$("#contacts_arrows").hide();
		
		return false;
	});
	
	$("#contacts_im_text").click(function()
	{
		$.cookie("ifly_contacts_mode", block_mode.TEXT);
		
		contactsRemoveSel();
		$(this).addClass("sel");
		$("#contacts_im_up")
			.removeClass("im_down")
			.addClass("im_up");
		
		$("#contacts_hide").hide();
		$("#contacts_mini").hide();
		$("#contacts_max").hide();
        
        $("#contacts_more").show();
		
		$("#contacts_text").show();
		
		if (total_friends > contacts_setting[block_mode.TEXT]['peoplePerColumn']*contacts_setting[block_mode.TEXT]['visibleColumns'])
			$("#contacts_arrows").show();
		else
			$("#contacts_arrows").hide();
		
		return false;
	});                   
	
	$("#contacts_prev").click(function()
	{
		var mode = $.cookie("ifly_contacts_mode");
		
		if (mode == block_mode.MAX)
		{
			if (mode_start == 0)
				return false;
			mode_max_end--;
			$("#contacts_max>div:gt("+mode_max_end+")").hide();
			$("#contacts_max>div:lt("+mode_start+"):last").show();
			mode_start--;
			
		}
		//else if ()
		return false;
	});
	
	$("#contacts_next").click(function()
	{
		var mode = $.cookie("ifly_contacts_mode");
		
		if (mode == block_mode.MAX)
		{
			if (mode_max_end == total_friends-1)
				return false
			mode_start++;
			$("#contacts_max>div:lt("+mode_start+")").hide();
			$("#contacts_max>div:gt("+mode_max_end+"):first").show();
			/*
			$("#contacts_max>div:lt("+mode_start+")")
			.css("position", "relative")
			.animate({left: "-100px", opacity: "hide"}, 500, function()
			{
				$("#contacts_max>div:gt("+mode_max_end+"):first").show();
			});
			*/
			mode_max_end++;
			
		}
		return false;
	});
	
	//Перемотка блока фотоальбомов назад
	$("#photos_prev").click(function()
	{
		var mode = $.cookie("ifly_photo_mode");
		
		if (mode == block_mode.MAX)
		{
			if (photo_mode_start == 0)
				return false;
			photo_mode_max_end--;
			$("#photos_max>div:gt("+photo_mode_max_end+")").hide();
			$("#photos_max>div:lt("+photo_mode_start+"):last").show();
			photo_mode_start--;
			
		}
		//else if ()
		return false;
	});
	
	//Перемотка блока фотоальбомов вперед
	$("#photos_next").click(function()
	{
		var mode = $.cookie("ifly_photo_mode");
		
		if (mode == block_mode.MAX)
		{
			if (photo_mode_max_end == total_albums-1)
				return false
			photo_mode_start++;
			$("#photos_max>div:lt("+photo_mode_start+")").hide();
			$("#photos_max>div:gt("+photo_mode_max_end+"):first").show();
			photo_mode_max_end++;
			
		}
		return false;
	});
	
	//перемотка мини галерии назад
	$("#mini_gallery_prev").click(function()
	{
		if (mini_gallery_start == 0)
			return false;
		mini_gallery_end--;
		$("#mini_gallery_items>a:gt("+mini_gallery_end+")").hide();
		$("#mini_gallery_items>a:lt("+mini_gallery_start+"):last").show().css("display", "inline-block");
		mini_gallery_start--;
		
		return false;
	});
	
	//перемотка мини галерии вперед
	$("#mini_gallery_next").click(function()
	{
		if (mini_gallery_end == total_mini_gallery-1)
			return false
		mini_gallery_start++;
		$("#mini_gallery_items>a:lt("+mini_gallery_start+")").hide();
		$("#mini_gallery_items>a:gt("+mini_gallery_end+"):first").show().css("display", "inline-block");

		mini_gallery_end++;

		return false;
	});
	
	//ДЛЯ ЛИЧНОГО КАБИНЕТА
	$("#cp_im_up").click(function()
	{
		$.cookie("ifly_cp_mode", block_mode.HIDE);
		
		removeSelCP();
		$(this).addClass("sel")
			.addClass("im_down");
			
		$("#cp_text").hide();
		$("#cp_mini").hide();
		$("#cp_max").hide();
        
        $("#cp_more").hide();
		
		$("#cp_hide").show();
		return false;
	});
	
	$("#cp_im_maximize").click(function()
	{
		$.cookie("ifly_cp_mode", block_mode.MAX);
		
		removeSelCP();
		$(this).addClass("sel");
		$("#cp_im_up")
			.removeClass("im_down")
			.addClass("im_up");
		
		$("#cp_text").hide();
		$("#cp_hide").hide();
		$("#cp_mini").hide();
        
        $("#cp_more").show();
		
		$("#cp_max").show();
		
		if (cp_count > cp_setting[block_mode.MAX]['peoplePerColumn']*cp_setting[block_mode.MAX]['visibleColumns'])
			$("#cp_arrows").show();
		else
			$("#cp_arrows").hide();
		
		return false;
	});
	
	$("#cp_im_text").click(function()
	{
		$.cookie("ifly_cp_mode", block_mode.TEXT);
		
		removeSelCP();
		$(this).addClass("sel");
		$("#cp_im_up")
			.removeClass("im_down")
			.addClass("im_up");
		
		$("#cp_hide").hide();
		$("#cp_mini").hide();
		$("#cp_max").hide();
        
        $("#cp_more").show();
		
		$("#cp_text").show();
		
		if (cp_count > cp_setting[block_mode.TEXT]['peoplePerColumn']*cp_setting[block_mode.TEXT]['visibleColumns'])
			$("#cp_arrows").show();
		else
			$("#cp_arrows").hide();
		
		return false;
	});                   
	
	$("#cp_prev").click(function()
	{
		var mode = $.cookie("ifly_cp_mode");
		if(mode==null)
			mode=block_mode.MAX;
		
		if (mode == block_mode.MAX)
		{
			if (cp_mode_start == 0)
				return false;
			cp_mode_max_end--;
			$("#cp_max>div:gt("+cp_mode_max_end+")").hide();
			$("#cp_max>div:lt("+cp_mode_start+"):last").show();
			cp_mode_start--;
			
		}
		//else if ()
		return false;
	});
	
	$("#cp_next").click(function()
	{
		var mode = $.cookie("ifly_cp_mode");
		if(mode==null)
			mode=block_mode.MAX;
		if (mode == block_mode.MAX)
		{
			if (cp_mode_max_end == cp_count-1)
				return false;
			cp_mode_start++;
			$("#cp_max>div:lt("+cp_mode_start+")").hide();
			$("#cp_max>div:gt("+cp_mode_max_end+"):first").show();
			cp_mode_max_end++;
			
		}
		return false;
	});
});
