$( "document" ).ready( function(){
// gallery slider	
	$( "#sliderImpressionen2009 .sliderViewport img").wrap( '<a href="http://www.flickr.com/photos/icrossing_de/sets/72157626174603662/" target="_blank"></a>' );
	$( "#sliderImpressionen2010 .sliderViewport img").wrap( '<a href="http://www.flickr.com/photos/icrossing_de/sets/72157626173781638/" target="_blank"></a>' );
	$( "#sliderImpressionen2011 .sliderViewport img").wrap( '<a href="http://www.flickr.com/photos/icrossing_de/sets/72157626187565260/" target="_blank"></a>' );

	$( "#sliderLocation ul" ).prop( { maxLeft: 0, minLeft: -672, distance: 252 } );
	$( "#sliderImpressionen2009 ul" ).prop( { maxLeft: 0, minLeft: -1278, distance: 710 } );
	$( "#sliderImpressionen2010 ul" ).prop( { maxLeft: 0, minLeft: -709, distance: 710 } );
	$( "#sliderImpressionen2011 ul" ).prop( { maxLeft: 0, minLeft: -1278, distance: 710 } );
	
	$( ".bt-left" ).css( "opacity", 0.4 )
				   .click( function(){
						var slider = $( this ).parent().find( "ul" );
						if( $( slider ).queue().length !=0 ) return;
						var sliderPositionLeft = $( slider ).position().left;
						var sliderMaxLeft = $(slider).prop('maxLeft');
						var movingX = $(slider).prop('distance');
						
						var newSliderLeft = ( sliderPositionLeft + movingX <= sliderMaxLeft ) ? sliderPositionLeft + movingX : sliderMaxLeft ;
						$( slider ).animate( {left: newSliderLeft }, { easing: 'easeInOutCubic'} );
						
						if( newSliderLeft == sliderMaxLeft ) $( this ).css( "opacity", 0.4 );
						$( this ).parent().find(".bt-right" ).css( "opacity", 1 );
						
					});
	$( ".bt-right" ).click( function(){
						var slider = $( this ).parent().find( "ul" );
						if( $( slider ).queue().length !=0 ) return;
						var sliderPositionLeft = $( slider ).position().left;
						var sliderMinLeft = $(slider).prop('minLeft');
						var movingX = $(slider).prop('distance');

						var newSliderLeft = ( sliderPositionLeft - movingX > sliderMinLeft ) ? sliderPositionLeft - movingX : sliderMinLeft ;
						$( slider ).animate( {left: newSliderLeft }, { easing: 'easeInOutCubic'} );
						
						if( newSliderLeft == sliderMinLeft ) $( this ).css( "opacity", 0.4 );
						$( this ).parent().find(".bt-left" ).css( "opacity", 1 );
					});	

// Application form 
	$( "#sendApplicationForm" ).click( function(){
		var msg = "Bitte füllen Sie alle Pflichtfelder korrekt aus!";
		var error = false;
		$( "#firstname,#lastname,#company,#email,#mobil" ).each( function( i,el ){
			$( el ).removeClass( 'redBorder' );
			if( $.trim( $( el ).val() ) == '' )
			{
				error = true;
				$( el ).addClass( 'redBorder' );
			}
		});

		var emailCheckPattern=/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
		
		if(!emailCheckPattern.test($('#email').val())) 
		{
			error = true;
			$('#email').addClass( 'redBorder' );
		}

		
		if( !error )$( "#applicationForm" ).submit();
		else alert( msg );
	});
	
	$( "#applicationForm" ).iframePostForm({
        iframeID : "applicationFormIframe",
        json : true,
        post: function(){},
        complete : function ( response ) {
          if( response.msg )
		  { 
        	  alert( response.msg );
		  }
        }
     });	
});
