/**
 * @author FOKA
 */

var FEED_COM_EXT = 5;
var FEED_EXT = 5;

function userName(item) {
	if (item.name == null || item.surname == null) {
		return item.login;
	}
	
	return item.name+' '+item.surname;
}

$.fn.Feeder = function(options){
	return this.each(function(){
		var obj = $(this);
		var tab = options.user.split('-');
		
		if (jQuery.inArray( options.logUser+'', tab ) != -1) {
			obj.FeedForm({
				user: options.user,
				width : 660,
				list : 'feeds',
				dontHide : 1,
				logUser : options.logUser
			});
		}
		
		obj.append('<div id="feeds"></div>');
		
		$('#feeds').FeedList({
			from      : 0,
			count     : FEED_EXT,
			user      : options.user,
			logUser   : options.logUser,
			commented : options.commented
		});
	});
};
		
$.fn.FeedForm = function(options){
	return this.each(function(){
		var obj = $(this);
		
		var html = '<form method="POST" action="ajax.html" class="feedForm" enctype="multipart/form-data">';
		html += '<textarea class="feedTxt" name="descr" style="width: '+options.width+'px; height: 45px;"></textarea>';
		html += '<span class="feedCounter" style="float: right; margin: 0px 10px 0px 0px; font-size: 9px;">pozostało znaków: 250</span>';
		
		html += '<a href="" class="attach-btn">załącz</a><br class="clr"/>';
				
				
		html += '<div class="zalacz" style="display:none;">'
					+ '<label>Link:</label> <input class="feedLink" type="text" name="link" value="" style="width:200px;"/>'
					+ '<br class="clr"/>'
					+ '<label>Zdjęcie:</label>'
					+ '<span class="photoInfo"></span>'
					+ '<input type="hidden" name="album_photo" class="album_photo" value=""/>'
					+ '<span class="spanFile"><input type="file" name="photo" value="" class="photoFile" size="40"/></span>'
					+ '&nbsp;&nbsp;<a href="" class="galbtn">z galerii</a>'
					+ '<br class="clr"/>'
					+ '<div class="photos" style="display:none;margin:10px 0px 10px 0px;height:245px;overflow:auto;"></div>'
				+ '</div>'
				+ '<input type="submit" value="Dodaj wpis" class="btn_reg" style="float: right; margin: 0px 10px 0px 0px;">'
				+ '<input type="hidden" name="ACTION" value="addFeed"/>';		
				
			if (options.parent != null) {	
				html += '<input type="hidden" name="parent" value="'+options.parent+'"/>'		
			}
			 html += '<input type="hidden" name="actionName" value="Feeds"/>'
			+ '</form>'
			
		var form = $(html);
		
		obj.append(form);
			
		form.ajaxForm({
			dataType: 'html',
			success: function(data){
				var status = parseInt(data);
				
				if (status == -2) {
					$('.act_msg_err:first').remove();
					obj.prepend('<div class="act_msg_err">Wpis nie może być pusty</div>');
					
				} else if (status == 1) {
					
						$('.act_msg_err:first').remove();
						obj.find('.zalacz:first').hide();
						obj.find('.feedTxt:first').val('');
						obj.find('.feedLink:first').val('');
						
						//dodanie itema do listy i zwiekszenie zakresu
						if (options.list instanceof jQuery) {
							var count = options.list.children().length;
							
							options.list.html('');
							
							options.list.FeedList({
								from      : 0,
								count     : count+1,
								user      : options.list.data('user'),
								logUser   : options.logUser,
								commented : options.list.data('commented'),
								fid       : options.list.data('fid')
							});
							
							options.list.data('from',1);
						} else {
							var count = $('#'+options.list).children().length;
							
							$('#'+options.list).html('');
							$('#'+options.list).FeedList({
								from      : 0,
								count     : count,
								user      : $('#'+options.list).data('user'),
								logUser   : options.logUser,
								commented : $('#'+options.list).data('commented'),
								fid       : $('#'+options.list).data('fid'),
							});
							
							$('#'+options.list).data('from',1);
						}
						
						if (options.dontHide == null ) {
							form.remove();
						}
					}
			}
		});
			
		obj.find('.attach-btn:first').click(function(){
			if (!obj.find('.zalacz:first').is(':visible')) {
				obj.find('.zalacz:first').show(300);
			}
			else {
				obj.find('.zalacz:first').hide(300);
			}
			return false;
		});
			
		obj.find('.feedTxt:first').keypress(function(event){
			if (250 - $(this).val().length <= 0 && event.keyCode != 8 && event.keyCode != 46) {
				return false;
			}
		}).keyup(function(event){
			obj.find('.feedCounter:first').html('pozostało znaków: ' + (250 - $(this).val().length));
		}).keydown(function(event){
			obj.find('.feedCounter:first').html('pozostało znaków: ' + (250 - $(this).val().length));
		});
			
		obj.find('.galbtn:first').click(function(){
			if (!obj.find('.photos:first').is(':visible')) {
				obj.find('.photos:first').show(300);
			}
			else {
				obj.find('.photos:first').hide(300);
			}
			return false;
		});
			
		$.getJSON('ajax.html?dataSrc=getAllUserPhotos&user=' + options.logUser, function(data){
			obj.find('.photos:first').html('');
				
			$.each(data, function(i, item){
				var tab = item.photo.split('|');
					
				var ph = $('<div class="ph ph2"><div class="ph_img"><img src="' + tab[0] + '" alt="25"></div>' +
				'<div class="ph_desc"><input type="button" class="btn1" value="załącz"></div></div>');
					
				ph.find('.btn1:first').data('photo', tab[0]).data('photoId', item.photoid).click(function(){
					
					obj.find('.photos:first').hide(300);
					obj.find('.photoFile:first').remove();
					obj.find('.album_photo:first').val($(this).data('photoId'));
					obj.find('.photoInfo:first').html('<img style="float:left;" height="30" src="' + $(this).data('photo') + '"/>' +
					'<a class="remPhoto" href=""><img style="float:left;margin:10px 0px 0px 5px;" src="img/del.gif"/></a>');
					obj.find('.gal-btn:first').hide();
						
					obj.find('.remPhoto:first').click(function(){
						obj.find('.gal-btn:first').show();
						obj.find('.photoInfo:first').html('');
						obj.find('.album_photo:first').val('');
						obj.find('.spanFile:first').html('<input type="file" name="photo" value="" class="photoFile" size="40"/>');
						return false;
					});
				});
					
				obj.find('.photos:first').append(ph);
				
					
			});
		});
			
	
		
	});
};

$.fn.FeedList = function(options){
	
	return this.each(function(){
		var obj = $(this);
		
		obj.data('from',options.from);
		obj.data('count',options.count);
		obj.data('fid',options.fid);
		obj.data('user',options.user);
		obj.data('logUser',options.logUser);
		obj.data('commented',options.commented);
		
			
		$.getJSON('ajax.html?dataSrc=getUserFeeds&count='+options.count+'&uid='+options.user+'&from='+options.from+'&fid='+options.fid, function(data) {
			
			$.each(data.data, function(i,item){
				var ph = $('<div class="feed"></div>')
				.FeedItem({
					item      : item,
					logUser   : options.logUser,
					commented : options.commented
				});
				obj.append(ph);
			});
			
			if (data.data.length > 0) {
				obj.show();	
			} else {
				obj.hide();	
			}
			
			var more = $('<a href="" class="btn1 fr" style="margin:10px 0px 0px 0px">więcej</a>');
			
			if (options.from + options.count >= data.info.all) {
				more.hide();
			}
			
			more.click(function() {
				$(this).remove();
				obj.FeedList({
					from  : options.from + options.count, 
					count : FEED_EXT,
					fid   : options.fid,
					user  : options.user,
					admin : options.admin,
					commented : options.commented,
					logUser : options.logUser
				});
				
				return false;
			});
			obj.append(more);
			
			sizerInit();
		});
		
		
		
	});
	
	
};

$.fn.FeedItem = function(options){
	return this.each(function() {
		var obj = $(this);
		
		var item = options.item;
		
		var avatar = item.avatar;
		if (avatar == null || avatar == '') {
			avatar = 'img/ph_null2.gif';
		}
			
		html = '<a href="profil-'+item.login+'.html" class="avatar"><img width="50" height="50" src="'+avatar+'"/></a>';
		
		html += '<div class="descr">';
		html += '	<div class="info">';
		html += '		<a href="profil-'+item.login+'.html" class="name">'+userName(item)+'</a><br/>'
			 +  '		<span class="date">'+item.strdate+'</span><br/>';
							
		if (item.photo != '' && item.photo != null) {
				
			var photos = item.photo.split('|');
				
			html += ' 	<div class="ph">' +
					'		<div class="ph_img">' +
					'			<a rel="photo-sizer" href="'+photos[2]+'" title=""><img src="'+photos[0]+'"></a>' +
					'		</div>' +
					'	</div>';
					
		} else if (item.album_photo != '' && item.album_photo != null) {
			var photos = item.album.split('|');
				
			html += '	<div class="ph">' +
				'			<div class="ph_img">' +
				'				<a rel="photo-sizer" href="'+photos[2]+'" title=""><img src="'+photos[0]+'"></a>' +
				'			</div>' +
				'		</div>';
				
		}						
	
		html += '	</div>';
		
		if (options.logUser == item.uid) {
			html += '<form class="delForm" method="POST" action="ajax.html">'
				+ '<input type="submit" value="usuń" class="delBtn fr"/>'
				+ '<input type="hidden" value="deleteFeed" name="ACTION" />'
				+ '<input type="hidden" value="'+item.fid+'" name="fid" />'
				+ '<input type="hidden" name="actionName" value="Feeds"/>'
				+ '</form>';
		} 
		
		html += '<span class="feedDescr">'+item.descr+'<br/>';
				
		if (item.link != '' && item.link != null ){
			html += '<a target="_blank" href="'+item.link+'">'+item.link+'</a>';
		}
				
		html += '</span><br class="clr"/>';
				
		html += '</div>';
		
		obj.append(html);
		
		obj.find('.delForm').ajaxForm({
			dataType: 'html',
			success: function(data){
				var status = parseInt(data);
				
				if (status == 1) {
					obj.animate({height:'0px'},300,function(){
						obj.remove();
					});	
				}
			}
		});
		
		if(options.commented == 1) {
			var hh = $('<a href="" class="feedCom fr">skomentuj &raquo;</a>');	
			
			obj.find('.descr').append(hh);
			obj.find('.descr').append('<br class="clr"/>');
			
			var formDiv = $('<div></div>').css({
				'margin' : '10px 0px 0px 0px'
			});
			obj.find('.descr').append(formDiv);
			
			var subs = $('<div class="feed"></div>');
			
			hh.click(function(){
				
				if (formDiv.html() == '') {
					formDiv.FeedForm({
						user     : options.user,
						width    : 520,
						parent   : item.fid,
						list     :  subs,
						dontHide : null,
						logUser  : options.logUser
					});
				} else {
					formDiv.html('');
				}
				return false;
			});
			
			obj.find('.descr').append(subs);
			
			subs.FeedList({
				from  : 0, 
				count : FEED_COM_EXT,
				fid   : item.fid,
				logUser : options.logUser
			});
		}
		
	});
};

