
	/*
	 * jQuery autoResize (textarea auto-resizer)
	 * @copyright James Padolsey http://james.padolsey.com
	 * @version 1.04
	 */

	(function(a){a.fn.autoResize=function(j){var b=a.extend({onResize:function(){},animate:true,animateDuration:150,animateCallback:function(){},extraSpace:20,limit:1000},j);this.filter('textarea').each(function(){var c=a(this).css({resize:'none','overflow-y':'hidden'}),k=c.height(),f=(function(){var l=['height','width','lineHeight','textDecoration','letterSpacing'],h={};a.each(l,function(d,e){h[e]=c.css(e)});return c.clone().removeAttr('id').removeAttr('name').css({position:'absolute',top:0,left:-9999}).css(h).attr('tabIndex','-1').insertBefore(c)})(),i=null,g=function(){f.height(0).val(a(this).val()).scrollTop(10000);var d=Math.max(f.scrollTop(),k)+b.extraSpace,e=a(this).add(f);if(i===d){return}i=d;if(d>=b.limit){a(this).css('overflow-y','');return}b.onResize.call(this);b.animate&&c.css('display')==='block'?e.stop().animate({height:d},b.animateDuration,b.animateCallback):e.height(d)};c.unbind('.dynSiz').bind('keyup.dynSiz',g).bind('keydown.dynSiz',g).bind('change.dynSiz',g)});return this}})(jQuery);
	
	var WALL = {
		start 	: function() {
			$.ajaxSetup({
				type: 		'POST',
				url: 		WALL_CACHE.ACTION_URL,
				dataType:	'json'
			});
			
			//$('.wall_user_posts').show('fast');
		},
		
		like 	: function(item) {
			var cid			= $(item).attr('id').replace('post_like_', '');
			var ids  		= cid.split('_');
			var wall_id 	= ids[0];
			var item_id 	= ids[1];
			
			var act_id 	= $('#post_aid_'+cid).val();
			var own_id 	= $('#post_oid_'+cid).val();
			
			var item_type 	= $('#post_rel_'+cid).val();
			
			$('#post_like_'+cid).html('Iti place!').removeClass('wall_link').unbind('click');
			
			$.ajax({
				data:		{ evt: 'like', uid: WALL_CACHE.USR_ID, aid: act_id, oid: own_id, wid: wall_id, iid: item_id, itp: item_type },
				success:	function(result) {
					if (result != null) {
						$('#post_likes_'+result.d.wid+'_'+result.d.iid).html(result.d.msg).fadeIn();
					}
				}
			});
			
			return false;
		},
		
		comm	 	: function(item) {
			var cid			= $(item).attr('id').replace('post_comm_', '');
			var ids  		= cid.split('_');
			var wall_id 	= ids[0];
			var item_id 	= ids[1];
			
			var item_type 	= $('#post_rel_'+cid).val();
			
			$('#post_comms_'+cid).show();
			$('#post_comm_text_'+cid).trigger('focus');
			
			return false;
		},
		
		comm_focus 	: function(item) {
			var cid			= $(item).attr('id').replace('post_comm_text_', '');
			var ids  		= cid.split('_');
			var wall_id 	= ids[0];
			var item_id 	= ids[1];
			
			var item_type 	= $('#post_rel_'+cid).val();
			
			if ($(item).attr('comm_expanded') == 'off') {
				if ($(item).val() == $(item).attr('comm_default')) {
					$(item).val('');
					$(item).attr('comm_expanded', 'on');
					//$(item).css({height: '60px'});
					$('#post_comm_butt_'+cid).show();
				}
			}
			
			item.focus();
			
			return false;
		},
		
		comm_blur 	: function(item) {
			var cid			= $(item).attr('id').replace('post_comm_text_', '');
			var ids  		= cid.split('_');
			var wall_id 	= ids[0];
			var item_id 	= ids[1];
			
			var item_type 	= $('#post_rel_'+cid).val();
			
			if ($(item).attr('comm_expanded')) {
				if ($(item).val() == '') {
					$(item).val($(item).attr('comm_default'));
					$(item).attr('comm_expanded', 'off');
					$(item).css({height: '16px'});
					$('#post_comm_butt_'+cid).hide();
				}
			}
			
			return false;
		},
		
		comm_send 	: function(item) {
			var cid			= $(item).attr('id').replace('post_comm_butt_', '');
			var ids  		= cid.split('_');
			var wall_id 	= ids[0];
			var item_id 	= ids[1];
			
			var act_id 	= $('#post_aid_'+cid).val();
			var own_id 	= $('#post_oid_'+cid).val();
			
			var item_type 	= $('#post_rel_'+cid).val();
			var txt			= $('#post_comm_text_'+cid).val();
			
			if (txt == '') return false;
			
			$('#post_comm_text_'+cid).val('');
			//$('#post_comm_text_'+cid).attr('disabled', true);
			$('#post_comm_text_'+cid).trigger('blur');
			
			$.ajax({
				data:		{ evt: 'comm', uid: WALL_CACHE.USR_ID, aid: act_id, oid: own_id, wid: wall_id, iid: item_id, itp: item_type, txt: txt },
				success:	function(result) {
					if (result != null) {
						//$('#post_comm_text_'+result.d.wid+'_'+result.d.iid).attr('disabled', false);
						$('#post_comms_items_'+result.d.wid+'_'+result.d.iid).append(result.d.msg);
						//$('#post_comm_text_'+result.d.wid+'_'+result.d.iid).trigger('blur');
					}
				}
			});
			
			return false;
		},
		
		status_focus 	: function(item) {
			if ($(item).attr('status_expanded') == 'off') {
				if ($(item).val() == $(item).attr('status_default')) {
					$(item).val('');
					$(item).attr('status_expanded', 'on');
					//$(item).css({height: '60px'});
					$('#user_status_butt').show();
				}
			}
			
			return false;
		},
		
		status_blur 	: function(item) {
			if ($(item).attr('status_expanded')) {
				if ($(item).val() == '') {
					$(item).val($(item).attr('status_default'));
					$(item).attr('status_expanded', 'off');
					$(item).css({height: '20px'});
					$('#user_status_butt').hide();
				}
			}
			
			return false;
		},
		
		status_send 	: function(item) {
			var txt			= $('#user_status_text').val();
			if (txt == '') return false;
			
			$('#user_status_text').val('');
			$('#user_status_text').trigger('blur');
			
			$.ajax({
				data:		{ evt: 'status', uid: WALL_CACHE.USR_ID, aid: WALL_CACHE.OWN_ID, oid: WALL_CACHE.OWN_ID, txt: txt },
				success:	function(result) {
					if (result != null) {
						$('#wall_user_'+result.d.uid).prepend(result.d.msg);
						$('#user_status_text').trigger('blur');
						WALL.attach_events();
					}
				}
			});
			
			return false;
		},
		
		hide_interact : function (wid) {
			$('#post_interact_'+wid).hide('fast');
			$('#post_status_'+wid).fadeIn('fast');
			return false;
		},
		
		item_interact : function (item) {
			var json = $(item).attr('data-ft');
			var wid	= $(item).attr('id').replace('post_interaction_', '');
			var hid = '<div class="wp_interact_hide"><a href="javascript:void(0)" onclick="WALL.hide_interact('+wid+')">[inchide]</a></div>';
			
			if (json != '') {
				var params = $.parseJSON(json)
				switch(params.type) {
					case 'video':
						switch (params.host) {
							default:
							case 'youtube':
								if (!$('#post_interact_'+wid).attr('interact_deployed')) {
									var html = '';
									
									html += '<div style="width:425px; margin: 10px auto;"><object width="425" height="344">';
									html += '<param name="movie" value="http://www.youtube.com/v/'+params.video_id+'&hl=en_US&fs=1&color1=0xcc2550&color2=0xe87a9f&autoplay=1"></param>';
									html += '<param name="allowFullScreen" value="true"></param>';
									html += '<param name="allowscriptaccess" value="always"></param>';
									html += '<embed src="http://www.youtube.com/v/'+params.video_id+'&hl=en_US&fs=1&color1=0xcc2550&color2=0xe87a9f&autoplay=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed>';
									html += '</object>'+hid+'</div>';
									
									$('#post_interact_'+wid).html(html);
									$('#post_interact_'+wid).attr('interact_deployed', 'on')
								}
								$('#post_status_'+wid).hide('fast');
								$('#post_interact_'+wid).fadeIn('fast');
								
								return false;
							break;
							
							case 'trilulilu':
								if (!$('#post_interact_'+wid).attr('interact_deployed')) {
									var embed_id = params.video_id.replace(/^\/+|\/+$/g,"").split('/');
									var embed_id = embed_id[1];
									
									if (embed_id) {
										$.getScript('http://www.trilulilu.ro/embed-video'+params.video_id, function () {
											var html = '';
											eval('html = show_'+embed_id+'(448, 386, true, true);');
											html = '<div style="width:448px; margin: 10px auto;">'+html.replace(/\.swf\&/g, '.swf?')+''+hid+'</div>'; // bug in embedu de trilulilu
											
											$('#post_interact_'+wid).html(html);
											$('#post_interact_'+wid).attr('interact_deployed', 'on')
										});
										
									}
									else return false;
								}
								
								$('#post_status_'+wid).hide('fast');
								$('#post_interact_'+wid).fadeIn('fast');
								
								return false;
							break;
						}
					break;
				}
			}
			return true;
		},
		
		attach_post_events : function () {
			$('.wall_form_actions').unbind('submit');
			$('.wall_form_actions').bind('submit', function () { return false; });
		
			$('.wall_like').unbind('click');
			$('.wall_like').bind('click', function() { 
				return WALL.like(this); 
			});
			
			$('.wall_comm').unbind('click');
			$('.wall_comm').bind('click', function() { 
				return WALL.comm(this); 
			});
			
			$('.wp_comm_add_text').unbind('focus');
			$('.wp_comm_add_text').bind('focus', function() { 
				return WALL.comm_focus(this); 
			});
			
			$('.wp_comm_add_text').unbind('blur');
			$('.wp_comm_add_text').bind('blur', function() { 
				return WALL.comm_blur(this); 
			});
			
			$('.wp_comm_add_submit').unbind('click');
			$('.wp_comm_add_submit').bind('click', function () { 
				return WALL.comm_send(this);
			});
			
			$('.wp_interaction').unbind('click');
			$('.wp_interaction').bind('click', function () {
				return WALL.item_interact(this); 
			});
			
			$('.wp_comm_add_text').autoResize({
				onResize : function() {
					$(this).css({opacity:0.8});
				},
				animateCallback : function() {
					$(this).css({opacity:1});
				},
				animateDuration : 250,
				extraSpace : 20
			});
			
			/*
			$('.wp_comm_add_submit').bind('click', function() { 
				var cid	= $(this).attr('id').replace('post_form_', '');
				$('#post_comm_butt_'+cid).trigger('click');
			});
			*/
		},
		
		dummy_func 	: function() {
			// ca sunt un bou si uit sa pun virgule
		}
	};
	
	$(function() {
		WALL.attach_post_events();
		
		$('#user_status_text').autoResize({
				onResize : function() {
					$(this).css({opacity:0.8});
				},
				animateCallback : function() {
					$(this).css({opacity:1});
				},
				animateDuration : 250,
				extraSpace : 30
			});
		
		$('#user_status_text').bind('focus', function () {
			return WALL.status_focus(this); 
		});
		
		$('#user_status_text').bind('blur', function () {
			return WALL.status_blur(this); 
		});
		
		$('#user_status_butt').bind('click', function () {
			return WALL.status_send(this); 
		});
	});
	
	$(document).ready(function() {
		WALL.start();
	});
	

