/**
	PAM is the global namespace container
	@requires jQuery.js
	@namespace
*/

var PAM = (function($) {

	var templateURL = '';

	/**
		Blah blah blah
		@name general
		@memberOf HT
		@namespace
	*/
	var general = {
		/**
			Set site globals
			@private
		*/
		init: function() {
			general.setGlobals();
		},
		/**
			Set site globals
			@private
		*/
		setGlobals: function() {
			$('a[rel=external]').attr('target','_blank');
		}
		
	};
	
	/**
		Handling of the search toggling
		@name searchBar
		@memberOf PAM
		@namespace
	*/
	var searchBar = {
	
		defaultText: 'Search',
		input: false,
	
		/**
			Initialize the search bar
			@private
		*/
		init: function() {
			searchBar.input = $('input[name=s]');
			searchBar.input.each( function() {
				searchBar.checkValue('', $(this));
			});
			searchBar.input.live('blur', function(e) {
				searchBar.checkValue('blur', $(this));
			});
			searchBar.input.live('focus', function(e) {
				searchBar.checkValue('focus', $(this));
			});
			$('.searchform').submit( function() {
				if ( searchBar.validates($(this)) ) {
					return true;
				} else {
					alert('Oops. Try again.');
					return false;
				}
			});
		},
	
		/**
			Check the value of the input
			@private
		*/
		checkValue: function(t, $i) {
			var v = $.trim( $i.val() );
			if (t=='focus') {
				if (v == searchBar.defaultText) $i.val('');
			} else {
				if (v == '') $i.val( searchBar.defaultText );
			}
		},
	
		/**
			Validate the form
			@private
		*/
		validates: function($form) {
			var v = $.trim( $form.find('input[name=s]').val() ).toLowerCase();
			if ( v == '' || v == searchBar.defaultText.toLowerCase() ) {
				return false;
			} else {
				return true;
			}
		}
		
	};

	/**
		Initialize slide
		@name slides
		@memberOf PAM
		@namespace
	*/
	var slides = {
	
		/**
			Initialize the slides
			@private
		*/
		init: function() {
			var $s = $('#slides');
			if ($s.length > 0) {
				$s.slides({
					preload: true,
					preloadImage: 'http://www.pamarin.org/dev/wp-content/themes/IHSS-Theme/slideshow/img/loading.gif',
					play: 5000,
					pause: 2500,
					hoverPause: true,
					animationStart: function(current){
						$('.caption').animate({
							bottom:0
						},100);
						if (window.console && console.log) {
							// example return of current slide number
							console.log('animationStart on slide: ', current);
						};
					},
					animationComplete: function(current){
						$('.caption').animate({
							bottom:0
						},200);
						if (window.console && console.log) {
							// example return of current slide number
							console.log('animationComplete on slide: ', current);
						};
					},
					slidesLoaded: function() {
						$('.caption').animate({
							bottom:0
						},200);
					}
				});
			}
		}
		
	};

	/**
		Initialize commentForm handler
		@name commentForm
		@memberOf PAM
		@namespace
	*/
	var commentForm = {
	
		/**
			Initialize the slides
			@private
		*/
		init: function() {
		
			var $cf = $('#commentform');
			if ($cf.length > 0) {
			
				$is = $cf.find('input[type=text]');
				$is.each( function() {
					commentForm.checkEmpty( $(this) );
				});
				$is.live('focus', function() {
					commentForm.checkFilled( $(this) );
				}).live('blur', function() {
					commentForm.checkEmpty( $(this) );
				});
				
			}
			
			$cf.live('submit', function() {
				$cf.find('input[type=text]').each( function() {
					commentForm.checkFilled( $(this) );
				});
			});

		},
		
		/**
			Check if input is filled
			@private
		*/
		checkFilled: function($i) {
			var v = $.trim( $i.val().toLowerCase() ),
				l = $i.siblings('label').text().toLowerCase();
			if (v == l) {
				$i.val('');
			}
		},
		
		/**
			Check if input is empty
			@private
		*/
		checkEmpty: function($i) {
			var v = $.trim( $i.val().toLowerCase() ),
				l = $i.siblings('label').text();
			if (v == '') {
				$i.val(l);
			}
		}
		
	};

		
	
	/**
		return for public functions
	*/
	return {
		
		init: function() {
			general.init();
			slides.init();
			searchBar.init();
			commentForm.init();
		}
	
	}

	

})(jQuery);
