$(document).ready(function(){
	$('ul.sf-menu').superfish();
	Cufon.replace('h1, h2, h3, div#headline, div#subhead, strong.hline');

	$('a.email').each(function(){
		e = this.rel.replace('/','@');
		this.href = 'mailto:' + e;
		$(this).text(e);
	});
	
	
	  $('.rounded').click(function() {
	    if (this.value == this.defaultValue) {
	      this.value = '';
	    }
	  });
	  
	$('.rounded').blur(function() {
		if (this.value == '') {
		  this.value = this.defaultValue;
		}
	});
	  
	$('div.inputs').corner("round 3px");
	$('#expand, #order-offer').corner("tl 3px, tr 3px");
	  
	$("a[rel^='prettyPhoto']").prettyPhoto();
	
	$('#newsfader').innerfade({
		animationtype: 'fade',
		speed: 1750,
		timeout: 6000,
		type: 'sequence',
		containerheight: '1em'
	});

	//$('#expand').click(function() {
	  //$('#content-home').slideToggle('slow', function() {
	    // Animation complete.
	  //});
	//});
	$('#gmap').click(function() {
		$("#mappi").prepend('<img width="400" height="400" src="http://maps.google.com/maps/api/staticmap?center=Muehlenbruecke%205a,Luebeck&zoom=15&size=400x400&maptype=roadmap&markers=color:green|label:S|53.861272,10.68928&sensor=true" />');
	});

});

$(window).load(function() {
$('#slider').nivoSlider({
	effect:'sliceDown',
	animSpeed:500,
	pauseTime:4000
});

$('#green_btn').live('click', function() {
    Formular.sendData();
});



if('#formular'){


	$('#formular_form  input.required').each(function(){
		$(this).change(function(){
			if($(this).val() !="" && $(this).val()!= $(this).attr('rel')){
				$(this).removeClass('error');
			}
		});
	});
	
	$('.formular_links a').each(function(){
		$(this).click(function(){
			$('.formular_links a').each(function(){		
				$(this).removeClass('selected');	
			});
			$(this).addClass('selected');
			$('#formular_subject').val($(this).text());			
			if($('#formular_right').css('display') == 'none'){
				$('#formular_right').fadeIn(250);
			}
		});
	});

	$('#formular input[type=text]').each(function(){
		if($(this).attr('rel')!=""){
			$(this).click(function(){
				if($(this).attr('rel') == $(this).val()){
					$(this).val('');
				}
			});
			$(this).blur(function(){
				if($(this).val()=='' || $(this).val()==' '){
					$(this).val($(this).attr('rel'));
				}
			});		
		}
	});

}

});

function Formular(){}
Formular.show = function(){
	if($('#formular_right').css('display') == 'none'){
		$('#formular_right').fadeIn(250);
	}else{
			$('#formular_right').fadeOut(250);
	}
}

Formular.addKeyword = function(){
	Formular.count('hide');
	var div = $('<div>',{
		'class':'delete_input'
	});
	var span = $('<span>',{
		'class':'delete',
		'onclick':"Formular.deleteKeyword(this);"
	});	
	var input = $('<input>',{
		'type':'text',
		'class':'text',
		'name':'formular_keyword'
	});		
	$('#keywords_container').append('<div class="delete_input"><span onclick="Formular.deleteKeyword(this);" class="delete"></span><input type="text" name="formular_keyword[]" class="text" /></div>');
}

Formular.deleteKeyword = function(el){
	console.log(el);
	var count = $('.delete_input').length;
	if(count > 0){
		$(el).parent().remove();
		Formular.count('show');
	}
}

Formular.count = function(way){
	var count = $('.delete_input').length;
	if(count == 4 && way=='hide'){
		console.log('hide');
		$('#black_btn').css('display','none');	
	}
	if(count == 4 && way=='show'){
		console.log('show');
		$('#black_btn').css('display','block');	
	}	
}

Formular.sendData = function(){



	$('#formular_form  input.required').each(function(){
		if($(this).val() == "" || ($(this).attr('rel')!= undefined &&  $(this).val() == $(this).attr('rel') )){
			$(this).addClass('error');
		}
	});
	if($('#formular_form  input.error').length == 0){
		var data = $('#formular_form').serialize();
		$.ajax({
			type:'POST',
			url: '/send.php',
			data: data,
			success: function(data){
				if(data == 'sent'){
					alert('Message sent!');
					$('#formular_right').fadeOut(250);
				}
			}
		});
	}

}

