var group = {
	join: function(id) {
		dialog.confirm('/groups/join/?id='+id);
    },
	leave: function(id) {
    	dialog.confirm('/groups/leave/?id='+id);
    },
    request: function(id) {
    	dialog.confirm('/groups/request/?id='+id);
    },
    kick: function(id, uid) {
    	dialog.confirm('/groups/kick/?id='+id + '&uid='+uid);
    },
    removeDoc: function(id, did) {
		dialog.confirm('/groups/removedocument/?id='+id + '&did='+did);
	}
}

var friend = {
	request: function(id) {
		dialog.confirm('/friends/request/?id='+id);
	},
	remove: function(id) {
		dialog.confirm('/friends/remove/?id='+id);
	}
}

var image = {
	remove: function(id, albumview) {
		var callback = null;
		if (albumview) {
			callback = imagealbum.removeImageFromList;
		}
		dialog.confirm('/images/remove/?id='+id, callback);
	},
	albumremove: function(id) {
		dialog.confirm('/images/albumremove/?id='+id);
	}
}

var doc = {
	remove: function(id, enableAjax) {
		var callback = null;
		if (enableAjax && documents) {
			callback = documents.removeDocument;
		}
		dialog.confirm('/documents/remove/?id='+id, callback);
	},
	mark: function(id) {
		dialog.confirm('/documents/mark/?id='+id);
	},
	unmark: function(id, enableAjax) {
		var callback = null;
		if (enableAjax && documents) {
			callback = documents.removeDocument;
		}
		dialog.confirm('/documents/unmark/?id='+id, callback);
	}
}

var message = {
	removesent: function(id) {
		dialog.confirm('/messages/removesent/?id='+id);
	},
	removereceived: function(id) {
		dialog.confirm('/messages/removereceived/?id='+id);
	}
}

var auth = {
	logout: function() {
		window.location.href = '/auth/logout/?actionchallenge='+actionchallenge;
	}
}

var systemnews = {
	close: function() {
		$('#systemnews').hide("blind", { direction: "vertical" });
		$.get("/index/closesystemnews/");
	}
}

var tabs = {
	load: function(tabLink, url) {
		var ul = $(tabLink).parent().parent();
		var content = ul.siblings('.tabs_content');
		content.loaderContent();
		content.trigger('tabs.click');
		$.getJSON(url, {},
			function(answer){
				if (answer['html']) {
					content.html( answer['html'] );
					content.trigger('tabs.load');
				}
			}
		);
	}
}

var indication = {
	submit: function(contextType, contextId, contextAdditional) {
		$.post('/indication/',
	  		{ contextType: contextType, contextId: contextId, contextAdditional: contextAdditional, actionchallenge: actionchallenge },
			function(answer) {
				var link = $('.indication_'+contextType+'_'+contextId+'_'+contextAdditional);
				if (answer['done']) {
					link.text(answer['done']);
					link.animate({opacity:0.2}, 500);
				}
				if (answer['actionchallenge']) {
					actionchallenge = answer['actionchallenge'];
				}
			},
			'json'
		);
	}
}

var emptytext = {
	init: function() {
		$('input.emptytext, textarea.emptytext').focus(function() {
			if ($(this).data('emptytext_empty')) {
				$(this).val('').removeClass('emptytext_empty');
			}
		});
		$('input.emptytext, textarea.emptytext').blur(function() {
			if ($(this).val()) {
				$(this).removeData('emptytext_empty');
			} else {
				$(this).data('emptytext_empty', 1).val($(this).data('emptytext_revert')).addClass('emptytext_empty');
			}
		});
		$('input.emptytext, textarea.emptytext').change(function() {
			if ($(this).val()) { $(this).removeData('emptytext_empty'); }
		});
		$('form').submit(function() {
			$(this).find('input.emptytext, textarea.emptytext').each(function(i) {
				if ($(this).data('emptytext_empty')) { $(this).val(''); }
			});
		});
	}
}

var email = {
	send: function(prefix, suffix) {
		window.location.href='mailto:' + prefix + '@' + suffix;
	}
}

var topdocs = {
	showMore: false,
	toggleMore: function() {
		this.showMore = !this.showMore;
		$('.topdocslist .tabs_content .topdocs_more').slideToggle(300, function() { topdocs.applyLastClass(); });
		this.applyMoreGeneral();
		if (this.showMore) { this.applyLastClass(); }
	},
	applyMore: function() {
		if (this.showMore) {
			$('.topdocslist .tabs_content .topdocs_more').show();
		} else {
			$('.topdocslist .tabs_content .topdocs_more').hide();
		}
		this.applyMoreGeneral();
		this.applyLastClass();
	},
	applyMoreGeneral: function() {
		$('.topdocslist .tabs_content ~ .button').hide();
		if ($('.topdocslist .tabs_content .topdocs_more').length > 0) {
			if (this.showMore) {
				$('.topdocslist .tabs_content ~ .button.less').show().css('opacity', 1);
			} else {
				$('.topdocslist .tabs_content ~ .button.more').show().css('opacity', 1);
				
			}
		}
	},
	applyLastClass: function() {
		$('.topdocslist .tabs_content ul li').removeClass('last medium_pluslast');
		if (this.showMore || $('.topdocslist .tabs_content ul li').length <= 4) {
			$('.topdocslist .tabs_content ul li:last').addClass('last medium_pluslast');
		} else {
			$('.topdocslist .tabs_content ul li:eq(4)').addClass('last medium_pluslast');
		}
	},
	initMore: function() {
		$('.topdocslist .tabs_content ul li:gt(4)').wrapAll('<ul class="topdocs_more"></ul>');
		this.applyMore();
	}
}
$(document).ready(function() {
	topdocs.initMore();
	$('.topdocslist .tabs_content').bind('tabs.click', function() {
		$('.topdocslist .tabs_content ~ .button').css('opacity', 0);
	});
	$('.topdocslist .tabs_content').bind('tabs.load', function() {
		topdocs.initMore();
	});
});



$(document).ajaxError(function(event, request, options, error){
	if (typeof(dialog) != 'undefined') {
		dialog.close();
	}
	alert('An unexpected error occured.\nPlease reload the page and try again!');
});

$(document).ready(function(){
	$('.openerbox.initopen .opener_content').show();
	$('.openerbox h2.opener').prepend('<img src="' + staticUrl + '/img/icons/opener/close.png" />');
	$('.openerbox:not(.initopen) h2.opener img').attr('src', staticUrl + '/img/icons/opener/open.png');
	$('.openerbox h2.opener').click(
		function() {
			$(this).parent().find('.opener_content').toggle();
			if ($(this).parent().find('.opener_content').is(':visible')) {
				$(this).find('img').attr('src', staticUrl + '/img/icons/opener/close.png');		
			} else {
				$(this).find('img').attr('src', staticUrl + '/img/icons/opener/open.png');
			}
		});
});

$(document).ready(function(){
	$('input.clearable').each(function(i){
		var icon = $('<div class="clearbutton"></div>');
		$(this).wrap('<div style="position:relative;"></div>').after(icon);
		icon.click(function(){
			$(this).siblings('input.clearable').val('').focus().trigger('change');
		});
	});
	$('input.searchfield').each(function(i){
		var icon = $('<div class="searchbutton"></div>');
		$(this).wrap('<div style="position:relative;"></div>').after(icon);
		icon.click(function(){
			$(this).parents('form').submit();
		});
	});
});

$(document).ready(function(){
	$('ul.tabs.ajax a').click(function() {
		$(this).parent().siblings('.active').removeClass('active');
		$(this).parent().addClass('active');
	});
	
});

$(document).ready(function(){
	emptytext.init();
});

$(document).ready(function(){
	$('form').submit(function(){
		if (typeof(actionchallenge) != 'undefined') {
			$(this).find('input[type="hidden"][name="actionchallenge"]').val(actionchallenge);
		}
	});
});


jQuery.fn.loaderContent = function() {
	this.css('position','relative').children().css('visibility','hidden');
	if (!this.data('resetOverflow')) { this.data('resetOverflow', this.css('overflow')); }
	this.css('overflow', 'hidden');
	var loader = $('<img class="loader" src="' +staticUrl+'/img/loader/loader.gif"/>');
	loader.css('position','absolute').css('left',this.width()/2-8).css('top',this.height()/2-8);
	this.append(loader);
}
jQuery.fn.resetLoader = function() {
	this.find('> img.loader').remove();
	this.children().css('visibility','visible');
	if (this.data('resetOverflow')) {
		this.css('overflow', this.data('resetOverflow')).removeData('resetOverflow');
	}
}

jQuery.fn.emptytext = function(revert) {
	this.data('emptytext_empty',1).data('emptytext_revert', revert).addClass(' emptytext emptytext_empty').val(revert);
}

jQuery.fn.val2 = function(v) {
	if (typeof v == 'undefined') {
		if (this.data('emptytext_empty')) { return ''; }
		else { return this.val(); }
	} else {
		if (v) { this.val(v).data('emptytext_empty', 0).removeClass('emptytext_empty'); }
		else { this.data('emptytext_empty', 1).val(this.data('emptytext_revert')).addClass('emptytext_empty'); }
	}
}

jQuery.fn.highlight = function(bgcolor) {
	if (!bgcolor) { bgcolor=this.css('background-color'); }
	this.css('background-color', '#fff8cc');
	this.animate({
		backgroundColor: bgcolor
	}, 3000);
}

jQuery.fn.showError = function(msg) {
	this.resetError();
	this.css({ marginTop: '3px', marginBottom: '3px' });
	this.css('height', 'auto').css('opacity', 1).show().html('<ul class="errors"><li>'+msg+'</li></ul>');
	this.animate({opacity:1}, 5000).animate({opacity:0, height:0, marginTop:0, marginBottom:0}, 1000, null, function() { $(this).hide() });
}
jQuery.fn.resetError = function() {
	this.stop().stop();
	this.hide().html('');
}

jQuery.preloadImages = function() {
	for(var i=0; i<arguments.length; i++) {
		jQuery("<img>").attr("src", arguments[i]); 
	}
}