jQuery(document).ready(function() {
	doSlideshows();
	doSliders();
});

window.onload = function() {
	Shadowbox.init({skipSetup:true});
	vimeoInits();
};

function doSlideshows() {
	var slideshows = jQuery('.slideshow');	// Grab the slideshows

	// Loop over slideshows and activate
	if( slideshows.size() > 0 ) {
		slideshows.each(function(index) {
			var slideshow = jQuery(this);
			var args = {fx: slideshowGetEffect(slideshow),
							timeout: slideshowGetDuration(slideshow),
							speed: slideshowGetSpeed(slideshow),
							pager: jQuery('.slideshow-pager', slideshow),
							pagerAnchorBuilder: slideshowAnchorBuilder,
							slideExpr: '.slide'};
			slideshow.cycle(args);
		});
	}
}

function doSliders() {
	var sliders = jQuery('.slider');	// Grab the slider

	// Loop over slideshows and activate
	if( sliders.size() > 0 ) {
		sliders.each(function(index) {
			var slider = jQuery(this);
			var slideContainer = slider.parent();
			var args = {
					start: sliderGetStartSlide(slideContainer),
					scroll: sliderGetScrollSize(slideContainer),
					animation: sliderGetAnimation(slideContainer),
					auto: sliderGetAuto(slideContainer),
					wrap: sliderGetWrap(slideContainer),
					vertical: sliderGetOrientation(slideContainer),
					buttonPrevHTML: '<div>&nbsp;<span class="hide">Prev</span></div>',
					buttonNextHTML: '<div>&nbsp;<span class="hide">Next</span></div>'
				};
			slider.jcarousel(args);
		});
	}
}

/**
 * Initialize Vimeo videos on the page
 */
function vimeoInits() {
	// Get all vimeo configs embedded in page
	var vimeoConfigs = jQuery('div.vimeo_params');
	if( vimeoConfigs.size() > 0 ) {
		vimeoConfigs.each(function() {
			var c = jQuery(this);
			Shadowbox.setup( jQuery('.vimeo_selector', c).text(), {
				  height: jQuery('.vimeo_height', c).text(),
				  width: jQuery('.vimeo_width', c).text(),
				  player: "swf",
				  flashVars:  {
						clip_id: jQuery('.vimeo_clip_id', c).text(),
						autoplay:  jQuery('.vimeo_autoplay', c).text()
				  }
			 });
		});
	}
	vimeo_post_init_callback();
}

/*
 * Do stuff after Vimeo inits
 *
 * Specifically, we want to find the video titled "Now is the Time"
 * and automatically launch it if the user has not seen it before
 * (based on cookie).
 */
function vimeo_post_init_callback() {
	var vidseen_cookie = getCookie('focus_seen_hp_vid');
	if(vidseen_cookie == "") {
		var time_now_vid = vimeo_get_time_now_video();
		if(time_now_vid != {}) {
			Shadowbox.open(time_now_vid);	
			setCookie("focus_seen_hp_vid", "yes", 1);	// set cookie 1 day
		}
	}
}

/**
 * Get "Time is now" video from SB cache
 */
function vimeo_get_time_now_video() {
	var vid = {};
	for (var propertyName in Shadowbox.cache) {
		if (!(Shadowbox.cache[propertyName] instanceof Function)) {
			if(Shadowbox.cache[propertyName].title == "Now is the Time") {
				vid = Shadowbox.cache[propertyName];
			}
		}
	}
	return vid;
}

function slideshowGetEffect(slideshow) {
	var returnVar = 'fade';
	var effect = jQuery('.effect', slideshow).text();
	if( effect != "" ) {
		returnVar = effect;
	}
	return returnVar;
}

function slideshowGetDuration(slideshow) {
	var returnVar = 7000;
	var duration = parseInt(jQuery('.duration', slideshow).text());
	if( duration > 0 ) {
		returnVar = (duration * 1000);	// we need to return milliseconds
	}
	return returnVar;
}

function slideshowGetSpeed(slideshow) {
	var returnVar = 2000;
	var speed = parseInt(jQuery('.speed', slideshow).text());
	if( speed > 0 ) {
		returnVar = (speed * 1000);	// we need to return milliseconds
	}
	return returnVar;
}

function slideshowAnchorBuilder(idx, slide) {
	return '<a href="#">&nbsp;<span class="hide">Slide '+idx+'</span></a>';
}

function sliderGetAnimation(slideContainer) {
	var returnVar = 1000;
	var animation = parseInt(jQuery('.animation', slideContainer).text());
	if( animation >= 0 ) {
		returnVar = (animation * 1000);	// we need to return milliseconds
	}
	return returnVar;
}

function sliderGetOrientation(slideContainer) {
	var returnVar = false;
	var orientation = jQuery('.orientation', slideContainer).text();
	if( orientation == 'true' ) {
		returnVar = true;
	}
	return returnVar;
}

function sliderGetWrap(slideContainer) {
	var returnVar = 'both';
	var wrap = jQuery('.wrap', slideContainer).text();
	if( wrap.length > 0 ) {
		returnVar = wrap;
	}
	return returnVar;
}

function sliderGetStartSlide(slideContainer) {
	var returnVar = 1;
	var start = parseInt(jQuery('.start_slide', slideContainer).text());
	if( start > 0 ) {
		returnVar = start;
	}
	return returnVar;
}

function sliderGetScrollSize(slideContainer) {
	var returnVar = 1;
	var scrollSize = parseInt(jQuery('.scroll_size', slideContainer).text());
	if( scrollSize > 0 ) {
		returnVar = scrollSize;
	}
	return returnVar;
}

function sliderGetAuto(slideContainer) {
	var returnVar = 7;
	var auto = parseInt(jQuery('.auto', slideContainer).text());
	if( auto >= 0 ) {
		returnVar = auto;
	}
	return returnVar;
}
