(function($p, $x)
{
	function _showMainPictureDescr() 
	{
		$('#mainPicture .transparency').css({ 'background-color' : '#666' });
		$('#mainPicture .text *').css({ 'color' : '#fff' });
		$('#changeMainPicture').show();
	}

	function _hideMainPictureDescr() 
	{
		$('#mainPicture .transparency').removeAttr('style');
		$('#mainPicture .text *').removeAttr('style');
		$('#changeMainPicture').hide();
	}
	
	function _showSidebarEdit() 
	{
		$('#editSidebarPictures').show();
	}

	function _hideSidebarEdit() 
	{
		$('#editSidebarPictures').hide();
	}

	function _goToFotoalbenMain(e) 
	{
		if(e.target.id == 'changeMainPicture') {
			$.modal('/' + vars.user + '/profile/main-picture/', {'iframe' : true});
		} else {
			$x.goTo('fotoalben');
		}
	}
	
	function _goToFotoalbenSidebar() 
	{
		$x.goTo('fotoalben');
	}
	
	function _showSidebarPictures() 
	{
		$(this).css({'opacity' : 1.0});
	}
	
	function _opacitySidebarPictures() 
	{
		$(this).fadeTo(500, 0.85);
	}
	
	function _createEntry() 
	{
		var t = this;
		$.get('/' + vars.user + '/profile/new-entry', function(html) {
			$(t).hide();
			$(t).parent().after(html);
			
			//var intervalId = _autoSave('#createEntry');
			
			_attachEditHandlers('#createEntry', function() {
				$('#createEntry').remove();
				$('#newEntry').show();
				//clearInterval(intervalId);
			});
		}, 'html');
		
	}
	
	function _editEntry(html) 
	{
		var action = this.url.split('/')[3];
		var editPanel = '#editPanel\\.' + this.data.split('=')[1];
		if('edit-entry' == action) {
			var selector = '#editEntry\\.' + this.data.split('=')[1];
			$(editPanel).parent().hide().after(html);
			$(editPanel).parent().prev().hide();
			
			//var intervalId = _autoSave(selector);
			
			_attachEditHandlers(selector, function() {
				$(selector).prev().show();
				$(selector).prev().prev().show();
				$(selector).remove();
				//clearInterval(intervalId);
			});
			
			$(selector + ' .wysiwyg iframe').css({'width' : 620});
		} else if('delete' == action) {
			$(editPanel).parent().prev().remove();
			$(editPanel).parent().remove();
		} else if('delete-comment' == action) {
			$(editPanel).parent().parent().prev().remove();
			$(editPanel).parent().parent().remove();
		} else if('public-comment' == action || 'private-comment' == action) {
			alert('Die Änderungen wurden übernommen. Die Seite muss neu geladen werden, damit sie sichbar werden.');
		} else {
			$x.refresh();
		}
	}
	
	function _attachEditHandlers(selector, closeCallback) 
	{
		$.scrollTo($(selector).position().top - 120, 1500);
		
		$(selector + ' [name=text]').wysiwyg();
		$('.wysiwyg iframe, .wysiwyg').css({'width' : 620});
		
		$(selector + ' [name=category_id]').change(function() {
			if($(this).val() == 'newCategory')
				$.modal('/' + vars.user + '/profile/category/');
		});
		
		$(selector + ' [name=publishDate]').datepicker();
		$(selector + ' [name=publish]').change(function() {
			if($(this).val() == '2')
				$(selector + ' #wysiwygPublish').slideToggle();
			else
				$(selector + ' #wysiwygPublish').slideUp();
		});
		
		$(selector + ' .save').click(function() {
			$.post( '/' + vars.user + '/profile/save-entry', 
				$(selector + ' *').serializeArray(), 
				function(html){
					if(selector == '#createEntry') {
						$('#createEntry').next().after(html);
					} else {
						$(selector).prev().replaceWith(html);
					}
					closeCallback(); 
				}, 
				'html'
			);
		});
		$(selector + ' .close').click(closeCallback);
	}
	
	/**
	 * Enable autosave every 2 minutes
	 * unfinished...
	 * 
	 * @param String selector
	 * @return int {interval id}
	 */
	function _autoSave(selector) 
	{
		return setInterval(function() {
			$(selector + ' .save').trigger('click');
		}, 10000);
	}

	function _editCalendar() 
	{
		$.modal('/' + vars.user + '/profile/calendar/');
		return false;
	}

	function _editKontakt() 
	{
		$.modal('/' + vars.user + '/profile/kontakt/');
		return false;
	}
	
	function _editAboutMe() 
	{
		$.modal('/' + vars.user + '/profile/about-me/');
		return false;
	}
	
	function _editBlogroll() 
	{
		$.modal('/' + vars.user + '/profile/blogroll/');
		return false;
	}
	
	function _editSidebarPictures() 
	{
		$.modal('/' + vars.user + '/profile/sidebar-pictures/');
	}
	
	function _saveNewProfileComment(e) 
	{
		return $x.saveNewComment(this, '/profile/new-comment/profile_entry_id/');
	}
	
	function _goToFotoalbumSidebar() 
	{
		window.location = '/' + $(this).attr('id').split('.')[1] + '/fotoalben/index/album/' + $(this).attr('id').split('.')[2] + '/picture/' + $(this).attr('id').split('.')[3];
	}
	
	function _goToFotoalbumAttached() 
	{
		window.location = '/' + $(this).parent().attr('id').split('.')[1] + '/fotoalben/index/album/' + $(this).parent().attr('id').split('.')[2] + '/picture/' + $(this).attr('id').split('.')[1];
	}
	
	$p.init = function() 
	{
		$('#mainPicture').hover(_showMainPictureDescr, _hideMainPictureDescr);
		$('#mainPicture').click(_goToFotoalbenMain);
		$('#sidebarAlbum').hover(_showSidebarEdit, _hideSidebarEdit);
		$('#sidebarAlbum img').hover(_showSidebarPictures, _opacitySidebarPictures);
		$('#sidebarAlbum img').click(_goToFotoalbumSidebar);
		$('#editSidebarPictures').click(_editSidebarPictures);
		$('.attachedAlbum img').click(_goToFotoalbumAttached);
		
		$('#newEntry').click(_createEntry);
		$('[id^="showComments."]').live('click', $x.toggleComments);
		$('[id^="newComment."]').live('click', $x.toggleNewComment);
		$('[id^="newCommentContent."] button').live('click', _saveNewProfileComment);

		$('#editCalendar').click(_editCalendar);
		$('#editKontakt').click(_editKontakt);
		$('#editAboutMe').click(_editAboutMe);
		$('#editBlogroll').click(_editBlogroll);
		
		$('.editPanel').live('click', function() {
			$x.editPanel(this, '/' + vars.user + '/profile/', '', _editEntry);
		});
	};
	
	$p.triggerSocialButtons = function() {
		$('.socialIt').trigger('click');
	}
})(xhochn.profile = {}, xhochn);
