if (typeof PICPEEPS === 'undefined') {
	var PICPEEPS = {};
}

// adds 'hover' class to the li to deal with 
// ie6's lack of :hover on anything but links
PICPEEPS.fixNav = function(options) {
	var config = {
		$li: $('#navigation_global').children('li.beyond_session')
	};
	
	//allow caller to override the selector
	$.extend(config, options);

	config.$li.hover(function () {
		$(this).toggleClass('hover');
	});
};

/* Handles showing and hiding bits of content.
 * For example, the terms & conditions on the job application
 */
PICPEEPS.toggleContent = {
	config: {
		control: '.toggle_control',
		headerClass: 'toggle_open',
		contentClass: 'visuallyhidden'
	},

	/* The 'options' argument is an object literal that can
	 * be used to override the default options in the config object
	 */
	init: function (options) {
		var that = PICPEEPS.toggleContent;

		// allow for augmenting the config object if needed
		$.extend(that.config, options);

		//store the jQuery objects for later reuse
		that.control = $(that.config.control);
		
		if (that.control.length) {
			// open/close on click
			that.control.live('click', that.toggleContent);		
		}
	},
	
	toggleContent: function (event) {
		var that = PICPEEPS.toggleContent;
		var el = $(event.target);
	
		el.toggleClass(that.config.headerClass).
			siblings('div.toggle_content').toggleClass(that.config.contentClass);
	}
};

/* handles all the slide shows on the site using http://jquery.malsup.com/cycle/
 * @param [options] object				pass in an object to init to augment or override the 
 *										defaults in the config object
 * @param [options.slideOpts] object	Allows for all the options from the cycle plugin
 */
PICPEEPS.createSlideShow = {
	config: {
		slideContainer: '#hero_slideshow',
		slideOpts: {
			timeout: 5500,
			pager: '#slideshow_pager',
			pagerAnchorBuilder: function (idx, slide) {
				return '<li><a href=" ' + slide.src + ' ">' + (idx + 1) + '</a></li>';
			},
			pauseOnPagerHover: 1,
			containerResize: 0
		}
	},

	init: function (options) {
		var that = PICPEEPS.createSlideShow;
		
		// allow for augmenting the config object if needed
		$.extend(true, that.config, options);
		
		// store the jQuery object for later reuse
		that.container = $(that.config.slideContainer);
		
		if (that.container.length) {
			that.wireSlideShow(that);
		}
	},
	
	wireSlideShow: function (that) {		
		that.container.cycle(that.config.slideOpts);
	}
};

/* Adds support for HTML5 for browsers that don't support it 
 * Function code from Nathan Smith's formalize library
 * http://github.com/nathansmith/formalize
 */
PICPEEPS.placeholder = function() {
	var PLACEHOLDER_SUPPORTED = 'placeholder' in document.createElement('input');

	if (PLACEHOLDER_SUPPORTED || !$(':input[placeholder]').length) {
		// Exit if placeholder is supported natively,
		// or if page does not have any placeholder.
		return;
	}
	
	$(':input[placeholder]').each(function() {
		var el = $(this);
		var text = el.attr('placeholder');
	
		function add_placeholder() {
			if (!el.val() || el.val() === text) {
				el.val(text).addClass('placeholder_text');
			}
		}
	
		add_placeholder();
	
		el.focus(function() {
			if (el.val() === text) {
				el.val('').removeClass('placeholder_text');;
			}
		}).blur(function() {
			add_placeholder();
		});
	
		// Prevent <form> from accidentally
		// submitting the placeholder text.
		el.closest('form').submit(function() {
			if (el.val() === text) {
				el.val('');
			}
		}).bind('reset', function() {
			setTimeout(add_placeholder, 50);
		});
	});
};

PICPEEPS.duplicateRow = {
	config: {
		container: '#add_children',
		control: 'p.add_row_control',
		row: 'div.row_content',
		limit: 4
	},
	
	init: function(options) {
		var that = this;
		
		// allow for augmenting the config object if needed
		$.extend(true, that.config, options);
			
		that.container = $(that.config.container);
		
		// no point in continuing if there is no container div
		if (!that.container.length) {
			return
		}
		
		that.control = that.container.find(that.config.control);
		that.row = that.container.find(that.config.row);
		that.rowcount = that.row.length;
			
		that.control.bind('click', function(event) {
			that.duplicate(that);
		});
	
	},
	
	clearContent: function (row, that) {
		var updateAttr = function (index, attr) {
			return attr.replace(/_[\d]+/, '_' + that.rowcount);
		};
	
		//clear inputs an increment the attributes
		row.find('input').val('').
			attr({
				checked: false,
				name: updateAttr, 
				id: updateAttr
			}).
			siblings('label').attr('for', updateAttr);
	
		return row;
	},
	
	duplicate: function(that) {
		var clonedRow;
		that.rowcount += 1;
	
		// if we have a limited number of rows, check the limit
		if (that.config.limit) {
			if (that.rowcount === that.config.limit) {
				that.control.addClass('visuallyhidden');
			} else if (that.rowcount > that.config.limit) {
				return;
			}
		} 
		
		clonedRow = that.row.clone();
		clonedRow = that.clearContent(clonedRow, that);	
				
		that.control.before(clonedRow);
	}	
};


/* Remove "no-js" class from <html> element */
(function (window, doc) {
	var docElement = doc.documentElement;
	docElement.className = docElement.className.replace(/\bno-js\b/,'') + ' js';
	
})(this, this.document);

PICPEEPS.fixNav();
PICPEEPS.placeholder();
PICPEEPS.toggleContent.init();
PICPEEPS.duplicateRow.init();

/* big slideshow on the homepage */
PICPEEPS.createSlideShow.init();


/* mini gallery slideshows, e.g. the get creative ideas page */
(function () {
	var containers = $('.content_primary div.mini_gallery');
	
	/* TODO: Incorporate this loop into the createSlideShow function itself */
	if (containers.length) {
		containers.each(function() {
			var $this = $(this);
		
			PICPEEPS.createSlideShow.init({
				slideContainer: $(this).find('div.snapshot'),
				slideOpts: { 
					pager: $(this).find('ol.gallery_paging'),
					timeout: 0
				}
			});
		});
	}
})();
