(function($) {
	$.fn.equalHeights = function(minHeight, maxHeight) {
		tallest = (minHeight) ? minHeight : 0;
		this.each(function() {
			if($(this).height() > tallest) {
				tallest = $(this).height();
			}
		});
		if((maxHeight) && tallest > maxHeight) tallest = maxHeight;
		return this.each(function() {
			$(this).height(tallest).css("overflow","visible");
		});
	}
})(jQuery);

$(document).ready(function(){
	var $cookieOptions = { path: '/', expires: 30 };

	// Font size
	/*
	$('#fontSmall, #fontMedium, #fontBig').css('cursor','pointer').click(function(){
		var $fontSize = $(this).attr('size');
		$.cookie('fontSize', $fontSize, $cookieOptions);
		if($.browser.msie)
			$('body').css({fontSize:$fontSize});
		else
			$('body').animate({fontSize:$fontSize}, 1000);
		$adjustLayout();
	});
	var $fontSize = $.cookie('fontSize');
	if($fontSize)
	{
		$('body').css('font-size', $fontSize);
		$.cookie('fontSize', $fontSize, $cookieOptions);
	}
	*/


	/// ### Login Box
	// hide login box an show on request (show always of its forgot password form)
	$('#hd .tx-felogin-pi1').css('visibility','visible');

	if(($('.tx-felogin-pi1').length > 1) || ($('#hd .tx-felogin-pi1:has(input[name=user])').length > 0))
		$('#hd .tx-felogin-pi1').hide();

	$('#hd #login_btn').click(function(){
		//$(this).toggle(300);
		$('#hd .tx-felogin-pi1').toggle(300);
		return false;
	});
	$('#bd').click(function(){
		$('#hd .tx-felogin-pi1').hide(300);
	});

	// destroy logout-box an display its contents inline
	$('#hd .tx-felogin-pi1:has(.loginbox_logged_in)').removeClass('tx-felogin-pi1');
	$('#hd .tx-felogin-pi1:has(h3 + div):not(:has(form))').removeClass('tx-felogin-pi1').find('h3').hide();
	// hide login boxes without controls after 5sek
	$('#hd .tx-felogin-pi1:not(:has(form))').animate({opacity:0.8}, 5000, function(){ $(this).hide(300); });



	/// ### Memorial Pages
	$('#form_memo > h3:not(:has(a[href]))').contents().wrap('<a href="#"></a>');
	if($().accordion)
	{
		$('#form_memo').accordion({
			header: 'h3',
			autoHeight: false,
			collapsible: true,
			navigation: true,
			active:false,
			change: function(event, ui) {

				//move helptext (if any) to old content block
				if(ui.oldContent.length)
					ui.oldContent.parent().find('.helptext_left').replaceWith($('#content_left .helptext_left').hide().clone(true));

				// move new helptext to left column
				if(ui.newContent.length)
					$('#content_left .helptext_left').replaceWith(ui.newContent.parent().find('.helptext_left').clone(true).addClass('bodytext').show());
				// or just clear the box
				else
					$('#content_left .helptext_left').empty();

				$('#content_left, #content_main, #content_right').css('height','auto').equalHeights();
			},
			animated: $.browser.msie ? false : 'slide'
		}).find('.ui-accordion-header').bind('click', function(event) {
			//set STEP-field onchange
			//console.log($(this));
		    var $index = $(this).prevAll('.ui-accordion-header').length;
			$('#form_memo').find(':input[name="tx_rkmemo_pi1[step]"]').val($index+1);
		});
		// show first accordion or the one set by cookie
		$('#form_memo').accordion( 'activate' , $.cookie('step') ? ($.cookie('step')-1) : false);
		$('#form_memo').find(':input[name="tx_rkmemo_pi1[step]"]').val(($.cookie('step') || 1));
	}

	$('.kondolenzen_liste > h3:not(:has(a[href]))').contents().wrap('<a href="#"></a>');
	if($().accordion)
	{
		$('.kondolenzen_liste').accordion({
			header: 'h3',
			autoHeight: false,
			collapsible: true,
			navigation: true,
			active: 0,
			animated: $.browser.msie ? false : 'slide'
		})
		// show first accordion or the one set by cookie
		/*.accordion('activate', $.cookie('step') ? ($.cookie('step')-1) : 0)*/;
	}




	$('#form_memo').each(function(){
    	var $form = $(this);
    	var $css_id = $form.find('#input_css_id').val() || $form.find('.input_css_id:checked').val();
    	var $region_id = $form.find('#input_region_id').val() || $form.find('.input_region_id:checked').val();

    	// change region
    	$form.find('.input_region_id').change(function(){
    		if($(this).is(':checked'))
    			// select a css in this region
    			if(!$(this).nextAll('.list_css').find('.input_css_id:checked').trigger('change').length)
   					$(this).nextAll('.list_css').find('.input_css_id:first').attr('checked', 'true').trigger('change');
    	});
    	// change css
    	$form.find('.input_css_id').change(function(){
    		if($(this).is(':checked'))
   				$(this).closest('.list_css').css('background-image', 'url('+$(this).closest('.cssitem').find('.css_screenshot img').attr('src')+')');
    	});
    	$form.find('.css_name, .css_screenshot').click(function(){
    		$(this).siblings('.input_css_id').click();
    	});
    	$form.find('.css_name, .css_screenshot').hover(
			function(){
				$(this).closest('.list_css').css('background-image', 'url('+$(this).closest('.cssitem').find('.css_screenshot img').attr('src')+')');
			},
			function(){
			}
    	);


		// expand active region
    	$form.find('.list_regions .region').hide();
    	//$form.find('.list_regions .region .list_css').hide();
    	$form.find('.list_regions .region[region_id='+$region_id+']').show().addClass('ui-state-active').find(' .list_css').show().siblings('.input_region_id').click().trigger('change');
    	// check active css-id
    	$form.find('#input_css_id_'+$css_id+'').attr('checked', 'true').click();



		// generate region list from headers
		// open region on click
		$region_name_container = $('<div class="list_regions ui-corner-left">').appendTo($('.list_regions').parent().find('.helptext_left'));
    	$form.find('.list_regions > .region').each(function(){
    		var $region = $(this);
    		if($region.hasClass('ui-state-active'))
    			$region.find('.region_name').addClass('ui-state-active');
    		// click region name
    		$region.find('.region_name').css('cursor', 'pointer').click(function(){
    			$(this).addClass('ui-state-active').siblings().removeClass('ui-state-active');
				$region.addClass('ui-state-active').show().siblings().removeClass('ui-state-active').hide().find('.list_css:visible').slideUp(100);
				$region.find('.list_css:not(:animated)').slideDown(100);

				// change radio.button
				$region.find('.input_region_id').click().trigger('change');
			}).appendTo($region_name_container); // add region to container (in helptext)
    	});


    	$form.find('.region').hover(
    		function(){ $(this).filter(':not(.ui-state-active)').addClass('ui-state-hover'); },
    		function(){ $(this).removeClass('ui-state-hover'); }
		);


    	// Biografie Editor
		$('#input_lifestory').width(710).height(300).tinymce({
				// Location of TinyMCE script
				script_url : $('base').attr('href') + 'fileadmin/js/tinymce/tiny_mce.js',

				// General options
				theme : "advanced",
				plugins : "save,safari,advlink,paste,fullscreen,noneditable,nonbreaking,xhtmlxtras",

				theme_advanced_buttons1 : "cut,copy,paste,pastetext,pasteword,|,bold,italic,strikethrough,|,sub,sup,|,forecolor,code,fullscreen",
				theme_advanced_buttons2 : "justifyleft,justifycenter,justifyright,justifyfull,|,hr,removeformat,|,bullist,numlist,|,undo,redo,|,link,unlink",
				theme_advanced_buttons3 : "formatselect,fontselect,fontsizeselect",
				theme_advanced_buttons4 : "",
				theme_advanced_toolbar_location : "top",
				theme_advanced_toolbar_align : "left",
				theme_advanced_statusbar_location : "none",
				theme_advanced_resizing : false

				//content_css : 'creator_base.css'
			});
	});

	// memo input helptext_left
	$('#form_memo .ui-accordion-content .helptext_left').hide();
	$('#content_left').append($('<div class="helptext_left bodytext">'));




	// Listen
	var $detailWin;
$('.memo_link_detail a, a[target="memo_detail"]').click(function(){
		$detailWin = window.open(this.href, '_blank');
		$detailWin.focus();
		return false;
	});

	$('#content_right .tx-rkmemo-pi1 .newest .memo_item').css('cursor', 'pointer').click(function(){
		$(this).find('.memo_link_detail a').click();
	});
	$('.delete a, .btn_delete').click(function(){
		return confirm('Löschen bitte bestätigen!');
	});

	// Gallerielisten
	$('.tx-edgallery-pi1 .list .item').each(function(){
		var $item = $(this);
		$item.find('.facebox').removeClass('facebox').click(function(){
			var $popup = $('<div class="photo_popup"><img src="'+$(this).attr('href')+'"></div>');
			var $details = $item.find('.details').clone();
			$details.children().filter(':has(.value:empty)').remove();
			$details.children().show();
			$popup.append($details).width(800);

			$.facebox($popup);
			return false;
		});

	});




	// Lightbox
	if($.facebox)
	{
		$('a.facebox, .facebox_link a').facebox();
		$('a.facebox_reload_on_close').click(function(){
			$(document).one('close.facebox', function() { location.href = location.href; });
		});
	}
	else
	{
		$('a.facebox, .facebox_link a').css('cursor','default').click(function(){ return false; });
	}




	// Lightbox with Iframe instead of Ajax
	$('a[target="facebox_iframe"]').click(function(){
		$.facebox('<iframe frameborder="0" src="'+$(this).attr('href')+'"></iframe>');
		return false;
	});

	// Print-Button
	$('a[target=print]').click(function(){
		print();
		return false;
	});

	// close facebox
	$('a[target="close_facebox"]').click(function(){
		top.$(top.document).trigger('close.facebox');
		return false;
	});





	// tooltip
	$livetipcontainer = $('body');
	$livetip = $('<div id="livetip"></div>').hide().appendTo($livetipcontainer);

	var tipTitle = '';
	$('body:not(:has(div.iframe))').bind('mouseover', function(event) {
		var $elem = $(event.target).closest('[title],[orgtitle]', event.target);
		if ($elem.length && typeof $elem.attr('title') == 'string') {
			var elem = $elem[0];
			tipTitle = elem.title;
			elem.title = '';
			$elem.attr('orgtitle', tipTitle);

			var $css = { left:'auto', right:'auto', top:'auto', bottom:'auto' };
			if(event.pageX > $(window).width() - 200)
				$css.right = $livetipcontainer.width() - event.pageX - 12 - $livetipcontainer.position().left;
			else
				$css.left = event.pageX + 12 - $livetipcontainer.position().left;

			if(event.pageY > $(window).height() - 200 + $(window).scrollTop())
				$css.bottom = $livetipcontainer.height() - event.pageY - 12 - $livetipcontainer.position().top;
			else
				$css.top = event.pageY + 12 - $livetipcontainer.position().top;

			$livetip.css($css).html('<div>' + tipTitle + '</div>').show();
		}
	}).bind('mouseout', function(event) {
		var $elem = $(event.target).closest('[title],[orgtitle]', event.target);
		if ($elem.length) {
			$elem.attr('title', tipTitle);
			$livetip.hide();
		}
	}).bind('mousemove', function(event) {
		if ($(event.target).closest('[title],[orgtitle]', event.target).length) {

			var $css = { left:'auto', right:'auto', top:'auto', bottom:'auto' };
			if(event.pageX > $(window).width() - 200)
				$css.right = $livetipcontainer.width() - event.pageX - 12 - $livetipcontainer.position().left;
			else
				$css.left = event.pageX + 12 - $livetipcontainer.position().left;

			if(event.pageY > $(window).height() - 200 + $(window).scrollTop())
				$css.bottom = $livetipcontainer.height() - event.pageY - 12 - $livetipcontainer.position().top;
			else
				$css.top = event.pageY + 12 - $livetipcontainer.position().top;

			$livetip.css($css);
		}
	});

	/* Ausklappboxen */
	$('.expandable:not(.autoexpand)').addClass('collapsed').find('.body').hide();
	$('.expandable .btn_collapse').click(function(){
		var $this = $(this);

		$this.closest('.expandable')
			.toggleClass('collapsed')
			.find('.body')
			.animate({ height: 'toggle', opacity: 'toggle' }, 250);

		if($this.hasClass('hide_on_expand'))
			$this.animate({ height: 'toggle', opacity: 'toggle' }, 250);

		return false;
	});



	$('#content_left, #content_main, #content_right').equalHeights();

	$('.new_e').click(function(){
		$('.tx-edcomments-pi1 #form_comment .body').css("display","block");
	});

});
