/*******************************************************************************

	CSS on Sails Framework
	Title: John Barrett Salon
	Author: XHTMLized (http://www.xhtmlized.com/)
	Date: June 2011

*******************************************************************************/

$(document).ready(function() {
	JBS.init();
});

var JBS = {
	init: function () {
		this.editorial();
		this.archive();
		this.story();
		this.staff();
		this.salonToStaff();
		this.inlineHTMLPopup();
		this.videoPopup();
	},

	/* Closes editorial after 5s */
	editorial: function () {
		var $e = $('#editorialHome');
		if ( $e.size() ) {
			var eH = 0;
			if ( typeof editorialHeight !== 'undefined' ) {
				eH = editorialHeight;
			} else {
				// try to estimate desired height based on view button position
				var $v = $e.find('.view');
				if ( $v.size() ) {
					eH = $v.position().top + $v.height() + 67;
				}
			}
			if ( eH > 0 ) {
				setTimeout(function(){
					$e.animate({
						'height': eH
					},"slow");
				},0000);
				$e.find('img').toggle(function(){
					$e.animate({
						'height': $(this).height()
					},"slow");
				},function(){
					$e.animate({
						'height': eH
					},"slow");
				});
			}
		}
	},


	
	/* Closes editorial after 5s */
	editorial: function () {
		var $e = $('#editorial');
		if ( $e.size() ) {
			var eH = 0;
			if ( typeof editorialHeight !== 'undefined' ) {
				eH = editorialHeight;
			} else {
				// try to estimate desired height based on view button position
				var $v = $e.find('.view');
				if ( $v.size() ) {
					eH = $v.position().top + $v.height() + 67;
				}
			}
			if ( eH > 0 ) {
				setTimeout(function(){
					$e.animate({
						'height': eH
					},"slow");
				},3000);
				$e.find('img').toggle(function(){
					$e.animate({
						'height': $(this).height()
					},"slow");
				},function(){
					$e.animate({
						'height': eH
					},"slow");
				});
			}
		}
	},
	
	/* Show more/close more entry content */
	archive: function () {
		$('.entry-content .buttons .show').click(function(){
			$(this).hide().closest('.entry-content').children('.more-content').slideDown();
		});
		$('.entry-content .buttons .close').click(function(){
			var $c = $(this).closest('.entry-content');
			$c.children('.more-content').slideUp();
			$c.find('.buttons .show').show();
		});
	},
	
	/* Handles stories Home lightbox */
	storyHome: function () {
		if ( $.fn.colorbox ) {
			$('.entry-content, #editorial-contentHome').each(function(i,e){
				var t = $(this).find('h2').text();
				$(this).find('.story-contentHome a').attr('rel','storyHome-'+i).attr('title',t);
			});
			$('.entry-content .buttons .storyHome, #editorial-contentHome .storyHome').click(function(){
				$(this).closest('.entry-content, #editorial-contentHome').find('.story-contentHome a:first').click();
			});
			$('.story-contentHome a').colorbox({
				current: "{current}/{total}",
				maxHeight: '100%',
				maxWidth: '100%',
				opacity: 0.85,
				fixed: true
			});
		}
	},
	
	/* Handles stories lightbox */
	story: function () {
		if ( $.fn.colorbox ) {
			$('.entry-content, #editorial-content, .module-content').each(function(i,e){
				var t = $(this).find('h2').text();
				$(this).find('.story-content a').attr('rel','story-'+i).attr('title',t);
			});
			$('.entry-content .buttons .story, #editorial-content .story, .module-content .story').click(function(){
				$(this).closest('.entry-content, #editorial-content, .module-content').find('.story-content a:first').click();
			});
			$('.story-content a').colorbox({
				current: "{current}/{total}",
				maxHeight: '100%',
				maxWidth: '100%',
				opacity: 0.95,
				fixed: true
			});
		}
	},
	
	
	/* Handles inline HTML lightbox (bio) */
	inlineHTMLPopup: function(){
		if ( $.fn.colorbox ) {
			$('.inline-html').colorbox({
				rel: 'nofollow',
				maxHeight: '100%',
				maxWidth: '100%',
				innerWidth: 800,
				inline: true,
				opacity: 0.90,
				fixed: true,
				onComplete: function(){
					$.colorbox.resize({
						innerHeight: '80%'
					});
				}
			});
		}
	},
	
	/* Handles video lightbox */
	videoPopup: function(){
		if ( $.fn.colorbox ) {
			$('.video').colorbox({
				rel: 'nofollow',
				maxHeight: '100%',
				maxWidth: '100%',
				innerWidth: 960,
				innerHeight: 540,
				iframe: true,
				opacity: 0.95,
				fixed: true
			});
		}
	},
	
	/* Staff scrollable */
	staff: function () {
		var $s = $('.members .scrollable');
		if ( $s.size() ) {
			$s.find('.items > li').slice(1).hide();
			var oldIndex = -1;
			
			$s.scrollable({
				circular: true,
				onBeforeSeek: function (ev, index) {
					oldIndex = this.getIndex();
					if ( !( index == this.getSize()-1 && oldIndex == -1 ) ) {
						var $o = this.getItems().eq(index).show();
						var $w = this.getItemWrap();
						if ( oldIndex > index && !(oldIndex == 0 && index == -1)) {
							$w.css('left',$w.position().left-$o.width());
						}
					}
				},
				onSeek: function (ev, index) {
					if ( !( index == this.getSize()-1 && oldIndex == -1 ) ) {
						location.hash = $('.member-list a').eq(index).attr('href');
						var $o = this.getItems().eq(oldIndex);
						if ( oldIndex < index && oldIndex > -1) {
							var $w = this.getItemWrap();
							$w.css('left',$w.position().left+$o.width());
						}
						if ( oldIndex != index ) {
							$o.hide();
						}
					}
				}
			}).navigator({
				navi: '.member-list'
			});
			
			$s.find('.items > li.cloned').show();
			
			var sc = $s.data('scrollable');
			
			if ( location.hash ) {
				var ind = $('.member-list a[href="'+location.hash+'"]').parent().index();
				sc.getItemWrap().css('left',-sc.getItems().eq(ind).show().position().left);
				sc.seekTo(ind);
			} else {
				sc.seekTo(0);
			}
			
			$('.staff-choose select').change(function(){
				if ( $(this).val() ) {
					$s.data('scrollable').seekTo($('.member-list a[href="'+$(this).val()+'"]').parent().index());
				}
			});
		}
	},
	
	/* Staff select on the Salon page */
	salonToStaff: function () {
		$('.salon-team-area .selects select').change(function(){
			if ( $(this).val() ) {
				location = $(this).val();
			}
		});
	}
}

