$(document).ready(function(){
						   
	// route description
	$('p.route-description').hide();
						   
	// route description
	$('h4.route-head a').click(function() {
		$(this).parent().next('p:first').slideToggle();
		return false;
	});
	
	// product tabs
	$('#project-list').tabs({
		fxAutoHeight: true,
        fxFade: true,
		fxSpeed: 'fast'
	});
						   
	// handle further profile texts
	$('div.profile-text-further').hide();
	$('a.lnk-close').hide();
	$('div.member-item-content p.more a.lnk-more').click(function() {
		$(this).parent().parent().children('div.profile-text-further').slideDown();
		$(this).parent().find('a.lnk-close').toggle();
		$(this).toggle();
		return false;
	});
	$('div.member-item-content p.more a.lnk-close').click(function() {
		$(this).parent().parent().children('div.profile-text-further').slideUp();
		$(this).parent().find('a.lnk-more').toggle();
		$(this).toggle();
		return false;
	});
	
	// handle validation
	$.validator.setDefaults({
		debug: true
	});	
	
	$('<div class="form-error"><h3>Bitte füllen Sie die gekennzeichneten Felder aus!</h3><ol></ol></div>').prependTo('div#form-state-subscribe');
	var subscribeContainer = $('div#form-state-subscribe div.form-error');
	$(subscribeContainer).hide();
	
	// handle newsletter form
	jQuery("form#frm-newsletter").submit(function() {
		$('span.error').remove();
	});
	var v = jQuery("form#frm-newsletter").validate({
		errorContainer: subscribeContainer,
		errorLabelContainer: $("ol", subscribeContainer),
		wrapper: 'li',
		rules: {
				vorname: { required: true },
				nachname: { required: true },
				email: { required: true, email: true }
			},
		errorPlacement: function(error, element) {
				$('<span class="' + this.errorClass + '">!</span>').insertAfter( element );
			},
		submitHandler: function(form) {
			jQuery(form).ajaxSubmit({
				after: function(result) {
					if(result.status) {
						v.showErrors(result.data);
						v.focusInvalid();
					}
				}, 
				beforeSubmit: function(formData, jqForm, options) {
					// add ajax flag
					formData.push({name: 'ajax', value: true});
				},
				success: function(responseText, statusText) {
					$('form#frm-newsletter').fadeOut('slow');
					$(responseText).prependTo('div#form-state-subscribe');
				} 
			});
		}
	});
	
	// handle newsletter unsubscribe form
	$('<div class="form-error"><h3>Bitte füllen Sie die gekennzeichneten Felder aus!</h3><ol></ol></div>').prependTo('div#form-state-unsubscribe');
	var unsubscribeContainer = $('div#form-state-unsubscribe div.form-error');
	$(unsubscribeContainer).hide();
	
	jQuery("form#frm-newsletter-unsubscribe").submit(function() {
		$('span.error').remove();
	});
	var v = jQuery("form#frm-newsletter-unsubscribe").validate({
		errorContainer: unsubscribeContainer,
		errorLabelContainer: $("ol", unsubscribeContainer),
		wrapper: 'li',
		rules: {
				email_unsubscribe: { required: true, email: true }
			},
		errorPlacement: function(error, element) {
				$('<span class="' + this.errorClass + '">!</span>').insertAfter( element );
			},
		submitHandler: function(form) {
			jQuery(form).ajaxSubmit({
				after: function(result) {
					if(result.status) {
						v.showErrors(result.data);
						v.focusInvalid();
					}
				}, 
				beforeSubmit: function(formData, jqForm, options) {
					// add ajax flag
					formData.push({name: 'ajax', value: true});
				},
				success: function(responseText, statusText) {
					$('form#frm-newsletter-unsubscribe').fadeOut('slow');
					$(responseText).prependTo('div#form-state-unsubscribe');
				} 
			});
		}
	});
	
	// print
	$('a.print-btn').click(function(){
		window.print();
		return false;
	});
	
	// replace search button with text link
	if($('form#search-options-form').length > 0) {
		var textLabel = $('form#search-options-form input#search-submit-btn').attr('value');
		$('form#search-options-form input#search-submit-btn').replaceWith('<a href="#" id="searchClickBtn"><span>' + textLabel + '</span></a>');
		$('a#searchClickBtn').click(function(){
			$('form#search-options-form').submit();
		});
	}
	
	// replace bubble client text with flash
	$('#client-bubble-text').css('padding', '20px 0 0 30px');
	var so = new SWFObject("/images/flash/clients.swf", "sotester", "430", "170", "8");
	so.addParam("wmode", "transparent");
	so.write("client-bubble-text");
	
});