$(document).ready(function() {
	$(".vote-like").voteLike();
	$(".vote-info").voteInfo();
});


(function($){
	jQuery.fn.extend({
		voteLike: function(){
			return this.each(function(){
				var container = $(this);
				
				var profile_type = container.find("input[name='profile_type']").val();
				var profile_id = container.find("input[name='profile_id']").val();
                                var author_type = container.find("input[name='author_type']").val();
				var author_id = container.find("input[name='author_id']").val();
				var type = container.find("input[name='type']").val();
				var subject_id = container.find("input[name='subject_id']").val();
				var type_like = container.find("input[name='type_like']").val();
				
				//lubie
				container.find(".like").click(function(){$(this).vote({profile_type: profile_type,profile_id: profile_id,author_type: author_type,author_id: author_id,type: type,subject_id: subject_id,type_like:type_like,is_liked: 1});return false;});
				//nie lubie
				container.find(".dislike").click(function(){$(this).vote({profile_type: profile_type,profile_id: profile_id,author_type: author_type,author_id: author_id,type: type,subject_id: subject_id,type_like:type_like,is_liked: 0});return false;});
                                //container.find('span').hide();
			})
		},
		voteInfo: function(){
			return this.each(function(){
				var container = $(this);
				
				var profile_type = container.find("input[name='profile_type']").val();
				var profile_id = container.find("input[name='profile_id']").val();
				var type = container.find("input[name='type']").val();
				var subject_id = container.find("input[name='subject_id']").val();
				//wyswietlenie listy uzytkownikow
				container.find("a.green").click(function(){$(this).showListLike({profile_type: profile_type,profile_id: profile_id,type: type,subject_id: subject_id});return false;});			
			})
		},
		vote : function(params){
			
			var container = $(this);
			var container_vote_info = container.parent(".vote-like").parent().parent().find(".vote-info");
		
			container.fadeOut(function(){
				$.post("/vote/like/save/",params, function(data){
					container_vote_info.replaceWith(data);
					container.parent(".vote-like").parent().parent().find(".vote-info").find("a.green").click(function(){$(this).showListLike(params);return false;});
					if(params['is_liked'])
						container.parent(".vote-like").find('.dislike').show();
					else
						container.parent(".vote-like").find('.like').show();			
				});
                                container.parent(".vote-like").find('span:last').hide();
			});
		},
		showListLike: function(params){
			var is_liked = $(this).prev("input[name='is_liked']").val();
			params['is_liked'] = is_liked;
			$(this).lightBoxList({
				lightBoxID		: '#imageAddPanel',
				urlAjax	   		: '/profile/like/box-list',
				urlSearchAjax	: '/profile/like/list-ajax',
				params			: params,
				onLoadLightBoxList : function(){
						
					//usuniecie znajomego - link na liscie rozwijanej 
					$("#imageAddPanel ul.queue li .operations .del-friend").click(function(){
						var friend_id = $(this).parent(".operations").find("input[name='friend_id[]']").val();
						$(this).parent().parent("li").myFriendDelete(friend_id);
					})
			
					//dodanie znajomego - link na licie rozwijanej
					$("#imageAddPanel ul.queue li .operations .add-friend").click(function(){
						var user_id = $(this).parent(".operations").find("input[name='friend_id[]']").val();
						$(this).myFriendAdd(user_id);
					})
				}
			});
		}
	})
})(jQuery)	
