$(document).ready(function() {
	$(this).scrollTop(0);
	if ($('#loader').length)
	{
		$('#loader').livequery(function() {
			$(this).hide();
		});
	}

	$('input[hint]').inputHint( {
		hintAttr : 'hint'
	});
	

	$('abbr.timeago').livequery(function() {
		$(this).timeago();
	});
	
	$('abbr.timeagofr').livequery(function() {
		$(this).timeagofr();
	});
	
	$('input[@submit].postComment').livequery(function() {
		$(this).hide();
	});
	$('textarea[hint]').livequery(function() {
		$(this).inputHint( {
			hintAttr : 'hint',
			afterFocus : function() {
				$(this).nextAll('input[@button].postComment').show();
				if (!$(this).hasClass('autogrow')) {
					$(this).autogrow();
				}
				// $(document).stopTime('globalStream');
		},
		beforeBlur : function() {
			if ($(this).val() == '') {
				$(this).nextAll('input[@button].postComment').hide();
			}
		}
		});
	});
	
	jQuery.jGrowl.defaults.position = 'center';
	jQuery.jGrowl.defaults.closeTemplate = '';
	$('a.button.download, a.button.smallDownload').unbind('click');
	$('a.button.download, a.button.smallDownload').livequery('click', function(e){
		jQuery.ajax({
			type:'POST',
			dataType:'html',
			url: $(this).attr('ref'),
			beforeSend: function(XMLHttpRequest){
				showNotification($("#msgNotification").val());
			},
			success: function(data, textStatus){
				hideNotification();
			}
		});
		
		return false;
	});
	
	$('.thread').sortElements(function(a, b) {
		if ($(a).attr('abbr') < $(b).attr('abbr'))
		{
			return 1;
		}
		else if ($(a).attr('abbr') == $(b).attr('abbr') && $(a).attr('id') < $(b).attr('id'))
		{
			return 1;
		}
		return -1;
	});
	$('input[hint].invitation').livequery(function() {
		$(this).inputHint( {
			hintAttr : 'hint'
		});
	});
	$('.connection.button').livequery(function() {
		$(this).click(function(){
			var that = this;
	    	$.ajax({
		    	type:'POST',
		    	dataType:'html',
		    	url: $(this).attr('ref'),
		    	context: document.body,
		    	success: function(data){
		    		$(that).parent().html(data);
	    		}
	    	});
		});
	});
	
	$("a.smallShare, a.share").livequery(function() { 
		$(this).fancybox(
		{
		    'autoDimensions'	: false,
			'width'         		: 320,
		    'height'        		: 510,
		    'titleShow' :false,
			ajax:	{
				type:	"GET"
		}});
	});
	
	$("a.payment, a.smallBuy").livequery(function() { 
		$(this).fancybox(
		{
		    'autoDimensions'	: false,
			'width'         		: 360,
		    'height'        		: 'auto',
		    'titleShow' :false,
			ajax:	{
				type:	"GET"
		}});
	});
	
	$('select.language').change(function(){
		this.form.submit();
	});	
});

function showNotification(message)
{
	if (!$('.jGrowl-notification:visible').length){
		$.jGrowl('<div><img src="/images/loading.gif" /><span>'+message+'</span></div>',{ 
			sticky: true,
			life: 2000,
			speed: 100,
			beforeOpen: function(e, m){
				e.height(0);
			},	
			animateOpen: {
				height: '11px'
			},
			animateClose: {
				height: '0'
			}
		});	
	}
}

function showPublicationFacebook()
{
	if (!$('.jGrowl-notification:visible').length){
		$.jGrowl('<div><img src="/images/loading.gif" /><span>Publishing on your Facebook wall  </span></div>',{ 
			sticky: true,
			life: 2000,
			speed: 100,
			beforeOpen: function(e, m){
				e.height(0);
			},	
			animateOpen: {
				height: '11px'
			},
			animateClose: {
				height: '0'
			}
		});	
	}
}

function showPublicationTwitter()
{
	if (!$('.jGrowl-notification:visible').length){
		$.jGrowl('<div><img src="/images/loading.gif" /><span>Publishing a tweet  </span></div>',{ 
			sticky: true,
			life: 2000,
			speed: 100,
			beforeOpen: function(e, m){
				e.height(0);
			},	
			animateOpen: {
				height: '11px'
			},
			animateClose: {
				height: '0'
			}
		});	
	}
}



function hideNotification()
{
	$.jGrowl("close");
	window.location.href=window.location.href;
}

function hideUpload()
{
	$.jGrowlCenter("close");
}
function showRecommendation(message)
{
	if (!$('.jGrowl-notification:visible').length){
		$.jGrowl('<div><img src="/images/loading.gif" /><span>'+message+'</span></div>',{ 
			sticky: true,
			life: 2000,
			speed: 100,
			beforeOpen: function(e, m){
				e.height(0);
			},	
			animateOpen: {
				height: '11px'
			},
			animateClose: {
				height: '0'
			}
		});	
	}
}

function showCreditPayment(message)
{
	if (!$('.jGrowl-notification:visible').length){
		$.jGrowl('<div><img src="/images/loading.gif" /><span>'+message+'</span></div>',{ 
			sticky: true,
			life: 2000,
			speed: 100,
			beforeOpen: function(e, m){
				e.height(0);
			},	
			animateOpen: {
				height: '11px'
			},
			animateClose: {
				height: '0'
			}
		});	
	}
}

function showUpload()
{
	if (!$('.jGrowl-notification:visible').length){
		$.jGrowlCenter('<div id="jGrowlNotif"><img src="/images/loading.gif" /><span>Loading</span></div>',{ 
			sticky: true,
			life: 2000,
			speed: 100,
			beforeOpen: function(e, m){
				e.height(0);
			},	
			animateOpen: {
				height: '11px'
			},
			animateClose: {
				height: '0'
			}
		});	
	}
}

function initPager(el) {
	$('.pager').click(function() {
		var pager = $(this);
		$.ajax( {
			url : nextPageUrl,
			beforeSend : function() {
				pager.find('#loader').show();
			},
			success : function(data) {
				var secureData = eval('(' + data.split('for(;;);')[1] + ')');
				if (secureData.nextPage) {
					nextPageUrl = secureData.nextPage.url;
					pager.find('#loader').hide();
				} else {
					pager.remove();
				}
				for ( var i = 0; i < secureData.content.length; i++) {
					el.append(secureData.content[i].html);
				}

			}
		});
	});
}

function beginLiveStream(type, el) {
	if (el)
	{
		el.everyTime(5000, type, function() {
			var presentFeeds = new Array();
			el.find('.thread').each(function(){
				presentFeeds.push($(this).attr('id').split('_')[1]);
			});
			var liveStreamUrlWithPresence = liveStreamUrl + '&presence='+JSON.stringify(presentFeeds);
			$.get(liveStreamUrlWithPresence, function(data) {
				updateFeed(data, el);
			});
		});
	}
}

function updateFeed(data, el, obj) {
	var secureData = eval('(' + data.split('for(;;);')[1] + ')');
	if (secureData.liveStream) {
		liveStreamUrl = secureData.liveStream.url;
	}
	var content = secureData.content;
	if (content.length) {
		for ( var i = content.length - 1; i >= 0; i--) {
			if ($('#feed_' + content[i].id).length != 0) {
				continue;
			}
			var addedElement = $(content[i].html);
			addedElement.css('opacity', '0');
			addedElement.hide();

			if (content[i].level == 1
					&& $('#feed_' + content[i].id).length == 0) {
				if (!el) {
					var el = $('.stream');
				}
				el.append(addedElement);

			} else if (content[i].level == 2) {
				var container = null;
				if ($('#feed_' + content[i].target_feed_id).length != 0) {
					container = $('#feed_' + content[i].target_feed_id);
				} else {
					if (obj) {
						var id = obj.parent()
								.find('#sf_quick_comment_model_id').val();
						container = obj.parents('#sfQuickComment_' + id);
					}
				}
				if (container) {
					container.find('.bottom .comments').append(addedElement);
					$('.comment', container).sortElements(function(a, b) {
						return $(a).attr('abbr') > $(b).attr('abbr') ? 1 : -1;
					});
				}
			}
			if (!content[i].is_old) {
				addedElement.find('.itemDescription').css( {
					backgroundColor : '#E8F9FF'
				});
				addedElement.find('.itemDescription').mouseover(function() {
					$(this).animate( {
						backgroundColor : 'white'
					});
				});
			}
			addedElement.slideDown('fast');
			addedElement.animate( {
				opacity : 1
			}, 'normal');

			$('.thread').sortElements(function(a, b) {
				if ($(a).attr('abbr') < $(b).attr('abbr'))
				{
					return 1;
				}
				else if ($(a).attr('abbr') == $(b).attr('abbr') && $(a).attr('id') < $(b).attr('id'))
				{
					return 1;
				}
				return -1;
			});
		}
	}
}

function postComment(obj) {
	var textarea = $(obj).prev('.commentBox');
	jQuery.ajax( {
		type : 'POST',
		data : jQuery(obj.form.elements).serialize(),
		beforeSend : function() {
			textarea.attr('disabled', 'disabled');
			$(obj).attr('disabled', 'disabled');
		},
		success : function(data, textStatus) {
			updateFeed(data, null, $(obj));
			textarea.attr('disabled', '');
			$(obj).attr('disabled', '');
			textarea.val(null);
			textarea.blur();
			textarea.height('20px');
		},
		url : '/postComment'
	});
}
