function striptags( str )
{
	return str.replace(/<\/?[^>]+>/gi, '');
}

function trim( str )
{
	return str.replace(/\s+/g,' ')
}

function validate( email ) 
{
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   
   if( reg.test(email) == false) 
   {
      return false;
   }
   return true;
}

function photobook( amain, adist, avisible )
{
	var domain = "http://www.slamfm.nl/";
	var main = amain || "#photobook-carrousel";

	var visible = avisible || 1;
	
	var numItems = $( main + " .scroller a").size();
	
	
	if ( numItems <= visible )
	{
		
		$( main + " .back").hide();
		$( main + " .next").hide();
		
	} else {
		/*
			settings
		*/
		
		var duration = 300;
		var scrollEaseType = "easeInOutExpo";
		var distance = adist || 576;
		var currentIndex = $( "#currentIndex" ).val() || 0;
		var previousIndex = 0;

		var max = numItems - visible;
	
		var dimmed = "dimmed";
		
		function scroll()
		{	
			var x = -(currentIndex * distance) + "px";
				
			$( main + " .back").removeClass(dimmed);
			$( main + " .next").removeClass(dimmed);
			
			if ( currentIndex == 0 ) $( main + " .back").addClass( dimmed );
			if ( currentIndex == max ) $( main+ " .next").addClass( dimmed );
			
			updateValue();
			
			$( main + " .scroller").animate( { left: x }, { duration: duration } );
			
			
			if ( $(".photobook-mail-a-friend .thankyou").is(":visible") )
			{
				$(".photobook-mail-a-friend .thankyou").hide();
			}
		}
		
		
		function updateValue()
		{
			var p = $( main + " .scroller a:eq(" + currentIndex + ")" );
			
			var path = trim( striptags( p.attr("href") ) );
			
			path = path.split("/big/").join("/original/");
			
			$(".photobook-actions .save-picture").attr( "href", path );
			
			var id = parseInt( p.attr("rel") );
			
			$( "#currentIndex" ).val( currentIndex );
			$( "#picture" ).val( path );
		}
		
		/*
			next & previous
		*/
		
		$( main + " .back").addClass("dimmed");
		
		$( main + " .back").click(
			
			function(e)
			{
				previousIndex = currentIndex;
				currentIndex--;
				currentIndex = currentIndex <= 0 ? 0 : currentIndex;
				
				scroll();
				
				e.preventDefault();
			}
		);
		
		$( main + " .next").click(
			
			function(e)
			{
				previousIndex = currentIndex;
				currentIndex++;
				currentIndex = currentIndex >= max ? max : currentIndex;
				
				scroll();
				
				e.preventDefault();
			}
		);
	
	
		/*
			rollovers
		*/
		
		$( main + " .scroller a").each(
		
			function(i)
			{
				$(this).css( { position: "absolute", top: 0, left: (i * distance ) + "px" } );
				
				$(this).click(
					function(e)
					{
						e.preventDefault();
					}
				);
			}
		);
		
		
	
		scroll( );
		
		
		/*
			form
		*/
		
		$(".photobook-mail-a-friend").hide();
		
		$(".photobook-actions .send-through").click(
			function(e)
			{
				
				$(".photobook-mail-a-friend .thankyou").hide();
				$(".photobook-mail-a-friend").slideDown();
				$(".photobook-mail-a-friend fieldset").show();
				$(".photobook-mail-a-friend .close").show();
				e.preventDefault();
			}
		);
		
		$(".photobook-actions .save-picture").click(
			function(e)
			{
				window.open( domain + $( "#picture" ).val() );
					
				e.preventDefault();
			}
		);
		$(".photobook-mail-a-friend .close").click(
			function(e)
			{
				$(".photobook-mail-a-friend").slideUp();
				
				e.preventDefault();
			}
		);
		
		$(".photobook-mail-a-friend .thankyou").hide();
		
		$(".photobook-mail-a-friend").submit(
			
			function(e)
			{
				e.preventDefault();
				
				var url = domain + $(".photobook-mail-a-friend #url").val() + parseInt( currentIndex );
				
				var id_picture 	= $(".photobook-mail-a-friend #id_picture").val();
				var picture	 	= $(".photobook-mail-a-friend #picture").val();
				
				var your_name 	= $(".photobook-mail-a-friend #your_name").val();
				var your_email 	= $(".photobook-mail-a-friend #your_email").val();
				var friend_name = $(".photobook-mail-a-friend #friend_name").val();
				var friend_email = $(".photobook-mail-a-friend #friend_email").val();
				
				id_picture = parseInt( id_picture );
				
				picture 		= trim( striptags( picture ) );
				your_name 		= trim( striptags( your_name ) );
				your_email 		= trim( striptags( your_email ) );
				friend_name 	= trim( striptags( friend_name ) );
				friend_email 	= trim( striptags( friend_email ) );
				
				var errors = [];
				
				if ( your_name == "" ) errors.push( $(".photobook-mail-a-friend #your_name") );
				if ( friend_name == "" ) errors.push( $(".photobook-mail-a-friend #friend_name") );
				if ( !validate(your_email) ) errors.push( $(".photobook-mail-a-friend #your_email") );
				if ( !validate(friend_email) ) errors.push( $(".photobook-mail-a-friend #friend_email") );
				
				if ( errors.length > 0 )
				{
					for( var i = 0; i < errors.length; i++ )
					{
						errors[i].css( "border" , "1px solid red" );
					}
					
				} else {
					
					$.post(
						"/cron/photobook.php",
						{
							url : url,
							picture : picture,
							your_name : your_name,
							your_email : your_email,
							friend_name : friend_name,
							friend_email : friend_email
						},
						function(data)
						{
							$(".photobook-mail-a-friend #your_name").val("");
							$(".photobook-mail-a-friend #your_email").val("");
							$(".photobook-mail-a-friend #friend_name").val("");
							$(".photobook-mail-a-friend #friend_email").val("");
							
							$(".photobook-mail-a-friend fieldset").hide();
							
							$(".photobook-mail-a-friend .thankyou").text( "Bedankt! We hebben de foto doorgestuurd naar " + friend_name );
							$(".photobook-mail-a-friend .thankyou").show();
							$(".photobook-mail-a-friend .close").hide();
							
							
						}
						
					);
				}
				
				
			}
			
		);
		
	}
}