/*
 * jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
 *
 * Uses the built in easing capabilities added In jQuery 1.1
 * to offer multiple easing options
 *
 * TERMS OF USE - jQuery Easing
 * 
 * Open source under the BSD License. 
 * 
 * Copyright Â© 2008 George McGinley Smith
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of 
 * conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list 
 * of conditions and the following disclaimer in the documentation and/or other materials 
 * provided with the distribution.
 * 
 * Neither the name of the author nor the names of contributors may be used to endorse 
 * or promote products derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 *  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
 * OF THE POSSIBILITY OF SUCH DAMAGE. 
 *
*/

// t: current time, b: begInnIng value, c: change In value, d: duration
jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend( jQuery.easing,
{
	def: 'easeOutQuad',
	swing: function (x, t, b, c, d) {
		//alert(jQuery.easing.default);
		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
	},
	easeInQuad: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	easeInOutQuad: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return -c/2 * ((--t)*(t-2) - 1) + b;
	},
	easeInCubic: function (x, t, b, c, d) {
		return c*(t/=d)*t*t + b;
	},
	easeOutCubic: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t + 1) + b;
	},
	easeInOutCubic: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t + b;
		return c/2*((t-=2)*t*t + 2) + b;
	},
	easeInQuart: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t + b;
	},
	easeOutQuart: function (x, t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	},
	easeInOutQuart: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
		return -c/2 * ((t-=2)*t*t*t - 2) + b;
	},
	easeInQuint: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t*t + b;
	},
	easeOutQuint: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t*t*t + 1) + b;
	},
	easeInOutQuint: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
		return c/2*((t-=2)*t*t*t*t + 2) + b;
	},
	easeInSine: function (x, t, b, c, d) {
		return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
	},
	easeOutSine: function (x, t, b, c, d) {
		return c * Math.sin(t/d * (Math.PI/2)) + b;
	},
	easeInOutSine: function (x, t, b, c, d) {
		return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
	},
	easeInExpo: function (x, t, b, c, d) {
		return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
	},
	easeOutExpo: function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	},
	easeInOutExpo: function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	},
	easeInCirc: function (x, t, b, c, d) {
		return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
	},
	easeOutCirc: function (x, t, b, c, d) {
		return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
	},
	easeInOutCirc: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
		return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
	},
	easeInElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
	},
	easeOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	},
	easeInOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
		return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
	},
	easeInBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*(t/=d)*t*((s+1)*t - s) + b;
	},
	easeOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
	},
	easeInOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158; 
		if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
		return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
	},
	easeInBounce: function (x, t, b, c, d) {
		return c - jQuery.easing.easeOutBounce (x, d-t, 0, c, d) + b;
	},
	easeOutBounce: function (x, t, b, c, d) {
		if ((t/=d) < (1/2.75)) {
			return c*(7.5625*t*t) + b;
		} else if (t < (2/2.75)) {
			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
		} else if (t < (2.5/2.75)) {
			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
		} else {
			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		}
	},
	easeInOutBounce: function (x, t, b, c, d) {
		if (t < d/2) return jQuery.easing.easeInBounce (x, t*2, 0, c, d) * .5 + b;
		return jQuery.easing.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;
	}
});

var API = (function ( $, window, document, undefined ) {
	
	var oPrivateMethods = {
		
		quickExpressions : {
			name      : /^([a-zA-Z0-9\.\'\`\/\\\- \u00c0-\u0233])+$/,
			email      : /^[a-zA-Z0-9\!\#\$\%\&\'\.\*\+\/\=\?\^\_\`\{\|\}\~\-\[\]]+@([a-zA-Z0-9\-\.]*\.)?[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]\.[a-zA-Z]{2,6}$/,
			message : /^.*$/m
		},
				
		setupIntroBoxes : function() {
			
			$sheetAnchors.bind('click.introsheets', function() {
				
				var $this = $(this), 
				$currentSheet = $this.prev(), 
				$parentColl = $this.parents('.collumn'), 
				$otherSheet = $parentColl.prev().not('h1').length > 0 ? $parentColl.prev().find('.introSheetDetails') : $parentColl.next().find('.introSheetDetails'),
				$currentDeco = $parentColl.prev().not('h1').length > 0 ? $parentColl.prev().find('.sheetDeco') : $parentColl.next().find('.sheetDeco'),
				$otherDeco = $parentColl.find('.sheetDeco'),
				$otherLink = $parentColl.prev().not('h1').length > 0 ? $parentColl.prev().find('.sheetAnchor') : $parentColl.next().find('.sheetAnchor'),
				$currentHeading = $parentColl.find('.sheetHeading'),
				$otherHeading = $parentColl.prev().not('h1').length > 0 ? $parentColl.prev().find('.sheetHeading') : $parentColl.next().find('.sheetHeading');
				
				if( $currentSheet.data('opened') ) {
					
					$otherHeading.show(0);
					hideIntroSheet( $currentSheet, $currentDeco );
					$this.removeClass('sheetAnchorOff');
					
				} else {
					
					if( $otherSheet.data('opened') ) {
						
						$currentHeading.show(0);
						hideIntroSheet( $otherSheet, $otherDeco );
						
						$otherLink.removeClass('sheetAnchorOff');
					}
					
					$otherHeading.hide(0);
					showIntroSheet( $currentSheet, $currentDeco );
					
					$this.addClass('sheetAnchorOff');
				}
				
				return false;
			});	
			
			function showIntroSheet( $sheet, $currentDeco ) {
			
				$sheet.data('opened', true)
					.show(0, function() {
						
						var iBaseWidth = $currentDeco.width(), ibaseHeight = $currentDeco.height();
						
						$currentDeco.animate({ 
							width : iBaseWidth / 2,
							height : ibaseHeight / 2,
							opacity : 0.4
						}, {
							duration : 150,
							complete : function() {
								generateBlur( $(this) );
							}
						}); 
					});
			};
			
			function hideIntroSheet( $sheet, $currentDeco ) {
				
				$sheet.data('opened', false)
					.hide(0, function() {
						
						destroyBlur();
						
						var iBaseWidth = $currentDeco.width(), ibaseHeight = $currentDeco.height();
						
						$currentDeco.animate({ 
							width : iBaseWidth * 2,
							height : ibaseHeight * 2,
							opacity : 1
						}, {
							duration : 150
						});
						
					});
			};
			
			function generateBlur( $element ) {
				
				var $clone = $element.clone(), xPos = $element.position().left, yPos = $element.position().top;
				
				$clone.addClass('sheetClone')
					.animate({
						opacity : 0.4,
						left : xPos + 1,
						top : yPos + 1
					}, {
						duration : 0
					}).insertAfter( $element );
			};
			
			function destroyBlur() {
				$('.sheetClone').remove();
			};		
		},
		
		feedSwitch : function() {
			
			$('.mediaFeedNav li a').click(function() {
				var curChildIndex = $(this).parent().prevAll().length + 1;
				$(this).parent().parent().children('.active').removeClass('active');
				$(this).parent().addClass('active');
				$('.mediaFeedNavLink').toggleClass('active');
				$(this).parent().parent().next('.mediaFeedTabContainer').children('.current').fadeOut('fast',function() {
					$(this).removeClass('current');
					$(this).parent().children('div:nth-child('+curChildIndex+')').fadeIn('normal',function() {
						$(this).addClass('current');
					});
				});
				return false;
			});			
		}, 
		
		loadFeeds : function() {
			
			$("#js-facebookFeed").load("/assets/scripts/facebook.php");
			$("#js-twitterFeed").load("/assets/scripts/twitter.php");		
		}, 
		
		initCompanySlides : function() {
			
			$companySlides.slideShow({

				isNavigationEnabled : true,
				isPaginationEnabled : false,

				slideDuration  : 700,
				slideEasing : 'easeOutQuad',

				slideDirection : 'ltr',

				selectors : {

					selectorDeck : '.chapterDeck',
					selectorClip : '.chaptersClip',
					selectorSlides : '.chapter'
				},

				elements : {

					navButtonPrevious : {

						domElement : '<a href="#">Vorige</a>',

						domID : 'js-chaptersPrev',
						domClass : 'slideBookNavBtn chaptersPrev',

						styles : {}
					},

					navButtonNext : {

						domElement : '<a href="#">Volgende</a>',

						domID : 'js-chaptersNext',
						domClass : 'slideBookNavBtn chaptersNext',

						styles : {}
					}
				},

				hooks : {

					onInit : function() {
						
						var $slides = $('#js-navMain a');
						
						$slides.each(function(index, element) {
							
							$this = $(this);
							
							$this.bind('click.slideto', function() {
								$companySlides.slideShow('slideTo', index);
								return false;
							});
						});
						
						var oSettings = this.data('settings'), iActiveSlide = this.data('activeSlide');
						
						if( iActiveSlide === 0 ) {
							$('#js-chaptersPrev').hide(0);
						} 

					},

					onSlideComplete : function() {

						var oSettings = this.data('settings'), iActiveSlide = this.data('activeSlide'), iTotalSlides = $('#js-navMain li').length ;
						
						if( iActiveSlide === 0 ) {
							$('#js-chaptersPrev').hide(0);
						} else if( iActiveSlide === iTotalSlides ) {
							$('#js-chaptersNext').hide(0);
						}
						else {
							$('#js-chaptersPrev, #js-chaptersNext').show(0);
						}
					}
				}
			});
		},
		
		validateForm : function( $form ) {
			
			var $formElements = $('input[type=text]', $form);

			$form.bind('submit', function(){

				 var  i, 
				 max = $formElements.length, 
				 aErrorElements = [];

				  for( var i = 0; i < max; i++ ) {
					  var isValid = validateFormElement( $formElements[i] );

					  if( ! isValid ) {
						  aErrorElements.push( $formElements[i] );
					  }
				  }

				  if( aErrorElements.length > 0 ) {

						var j, maxErrors = aErrorElements.length;

						for( j = 0; j < maxErrors; j++ ) {
							$(aErrorElements[j]).addClass('errorMessage')
								.unbind('.aftererror')
									.bind('focus.aftererror', function(){
										$(this).removeClass('errorMessage');
									});
						}

						return false;  
				  } else {
					
					if( this.getAttribute('data-ajax') === 'true' ) {
						
						var sData = $form.serialize();

						$.ajax({ url : this.getAttribute('action'), type : this.getAttribute('method'), data : sData, success : function( sResponse ){
							
							var $response = $('<p class=\"response\">Dank u voor uw inschrijving!</p>');
							
							$form.parent()
								.html($response);
							
						}});
						
						return false;
					} else {
						return true;
					}
				  }
			 });

			 function validateFormElement( elFormElement ) {

				 var sRequired =  elFormElement.getAttribute('data-required'),
				 sRegEx = elFormElement.getAttribute('data-regex'),
				 sValue = elFormElement.value,
				 sBaseValue = elFormElement.getAttribute('data-placeholder');

				 if( sRequired === 'true' ) {

					 if( sValue !== sBaseValue && sValue !== '' ) {

						if( ! oPrivateMethods.quickExpressions[sRegEx].test(sValue) ) {
							return false;
						} else {
							return true;	
						}
					} else {
						return false;	
					}
				 }
			};
		}
	},
	
	oPublicMethods = {
		
		Project : {
			
			name  : 'Bouwmeester van Leeuwen',
			major : '0',
			minor : '1'
		},
		
		showElement : function( $element, callback ) {
			$element.show(0, function(){
				if( typeof callback === 'function' ) {
					callback.call( $element );
				}
			});
		},
		
		hideElement : function( $element, callback ) {
			$element.hide(0, function(){
				if( typeof callback === 'function' ) {
					callback.call( $element );
				}
			});
		},
		
		isEmptyObject : function( obj ) {
			
			for( var prop in obj ) return false;
			return true;
		},
		
		countTotalDOMelments : function() {
			return $('*').length;
		},
		
		fnExist : function( fnName ) {
			return $.isFunction( $[fnName] );
		}
	};
	
	var $companySlides = $('#js-chapterSections'),
	$navLinks = $('#js-navMain li a'), 
	$sheetAnchors = $('.sheetAnchor'),
	$mediaFeed = $('#js-mediaFeed'),
	$sheetDecoA = $('#js-figureSheetA'), 
	$sheetDecoB = $('#js-figureSheetB'), 
	$sheetHeadingA = $('.sheetHeadingA'), 
	$sheetHeadingB = $('.sheetHeadingB'),
	$sheetAnchors = $('.sheetAnchor'),
	$introSheets = $('.introSheetDetails'),
	$newsletterForm = $('#js-newsletterSignUp').find('form'),
	$formElements = $('form').find('input');
	
	if( $sheetAnchors.length > 0 ) {
		oPrivateMethods.setupIntroBoxes();
	}
	
	if( $companySlides.length > 0 ) {
		oPrivateMethods.initCompanySlides();
	}
	
	if( $mediaFeed.length > 0 ) {
		oPrivateMethods.feedSwitch();
		oPrivateMethods.loadFeeds();	
	}
	
	if( $newsletterForm.length > 0 ) {
		oPrivateMethods.validateForm( $newsletterForm );
	}
	
	if( $formElements.length > 0 ) {
		
		var i, formElementsLength = $formElements.length;
		for( i = 0; i < formElementsLength; i ++ ) {
			$formElements.eq(i)
				.bind({
					
					focus : function() {
						
						var sVal = this.value, sBaseVal = this.getAttribute('data-placeholder');
						
						if( sVal === sBaseVal ) {
							this.value = '';
						} else {
							this.value = sBaseVal;
						}
					},
					
					blur : function() {
						
						var sVal = this.value, sBaseVal = this.getAttribute('data-placeholder');
						
						if( sVal === '' ) {
							this.value = sBaseVal;
						} 
					}
				});
		}
	}
				
	return oPublicMethods;
	
}( jQuery, this, this.document ));
