jQuery(document).ready(function() {
	
	// circle arrow on h2 anchors
	jQuery('#tertiary h2 a, #primary h2 a, #secondary h2 a').append('<span class="circle-arrow">&nbsp;</span>');
});

function calculateTimeout(currElement, nextElement, opts, isForward) {
	var index = opts.currSlide;
	return teasers[index].timeout;
}

function checkForOmnitureOutOfBounds(length, requested) {
	if (requested > length)
		return 1;
	else
		return requested;
}
var hp = {};
$(function(){
	$(".feature").each(function(){
		$(this).show();
		var curID = $(this).attr("id");
		// console.debug(curID);
		var cb = $(".contentbox",this);
		hp["#"+curID] = {};
		hp["#"+curID].message = cb.html();
		// it's been saved, remove it
		cb.remove();
	}).bind("show",function(){
		var curHref = "#"+$(this).attr("id"),
			cb2,cbWidth,theTitle,newSize;
		$(this)
			.stop(true)
			.addClass("active")
			.fadeIn(500)
			.siblings(".feature:visible")
			.removeClass("active")
			.stop(true)
			.fadeOut(500,function(){
				// hide other features and set opacity to nothing in case 
				$(".feature:hidden").hide().css("opacity","");
			});
		// animate the new feature content
		cb2 = $(".features .contentbox2");
		cb2.fadeTo(400,0,function(){ // fade out old content
			cb2.css("height","auto").html(hp[curHref].message); // remove previous height and put in new content
			// console.debug(cb2.find(".title"));
			// languages like german have long words that sometimes don't fit. Work some magic to make them fit
			theTitle = $(".features .title");
			while(theTitle[0].scrollWidth > theTitle.outerWidth()){
				newSize = (parseInt(theTitle.css("font-size"),10) - 1)+"px";
				theTitle.css({"font-size":newSize,"line-height":newSize});
				// console.debug("too big!!",theTitle.css("font-size"));
			}
			// now for keyword box tweaking
			cbWidth = $(".contentbox").outerWidth();
			$(".keyword").each(function(){
				// center keywords in the contentbox
				$(this).css("left",cbWidth/2 - $(this).outerWidth()/2);
				// make the top of the contentbox match the height of the keyword (in case it wraps due to lots of words or big words)
				$(".contentbox").css("paddingTop",$(this).outerHeight()+18);
			});
			
			// animate to new height then show
			$(".features .contentbox").animate({height:cb2.height()},function(){
				cb2.fadeTo(400,1);
			});
		});

	});
	$(".features .contentbox").html('<div class="contentbox2"></div>');
	$(".feature").hide();
	// hook up the side nav
	if($(".feature-nav").length){
		$(".feature-nav a").bind("click",function(e){
			$(this).parent().addClass("active").siblings().removeClass("active");
			e.preventDefault();
			var curHref = $(this).attr("href");
			$(curHref).trigger("show");
			$(".feature-nav a").not(this).fadeTo(200,0.7);
		}).eq(0).click(); // click the first item to make it active

		$(".feature-nav a").hover(function(){
			if(!$(this).parent("li").hasClass("active")){
				$(this).fadeTo(200,1);
			}
		},function(){
			if(!$(this).parent("li").hasClass("active")){
				$(this).fadeTo(200,0.7);
			}
		});
	} else {
		$(".feature").trigger("show");
	}

	
	// hook up any play buttons in the features
	$(".features a.videoplay").live("click",function(e){
		e.preventDefault();
		$(".videobox").mediaLoader("open",$(this).data("video"));
		$("div.video").fadeIn();
	});
});



/*!
 * lazyLoad
 * @description	loads images onto the page as you scroll down to them
 * @version		1.0  - 2011/11/16
 * @author		Aaron Barker
 * @requires	ui.core.js (1.8+)
 */
(function(b){b.widget("lds.lazyLoad",{options:{images:"img.lazy",threshold:300,timer:1E3,resizeDelay:100},_create:function(){var d=this.options,a=this;a.images=b(d.images);a.images.bind("update",function(c,b){a.checkLocation(this,b)}).each(function(){var c=a.getSrc(b(this));sessionStorage.getItem(c)&&b(this).trigger("update",true)});b(document).bind("scroll resize orientationchange",function(){clearTimeout(a.scrollTimer);a.scrollTimer=setTimeout(function(){var c=a.images.not(".done");a.viewportHeight=
b(window).height();a.viewportTopY=b(window).scrollTop();c.trigger("update",false)},d.resizeDelay)}).trigger("scroll");if(d.timer)a.loadEm=setInterval(function(){a.images.not(".done").eq(0).trigger("update",true);a.images.not(".done").length||clearInterval(a.loadEm)},d.timer);d.loadAll&&a.images.trigger("update",true)},checkLocation:function(d,a){var c=this.options,f=/iphone|ipod/i.test(navigator.userAgent.toLowerCase())?60:0,e=b(d);if((e.offset().top<this.viewportTopY+this.viewportHeight+c.threshold+
f||a)&&!e.hasClass("done")){if(c=this.getSrc(e))e.attr("src",c),sessionStorage.setItem(c,"loaded");e.addClass("done")}},getSrc:function(b){var a=this.options,c=b.data(a.mobile?"mobile":"desktop");a.mobile&&!c&&(c=b.data("desktop"));return c},destroy:function(){b.Widget.prototype.destroy.apply(this,arguments)}});b.extend(b.lds.lazyLoad,{version:"1.0"})})(jQuery);
// run lazy loader
$(function(){
	if($("img.lazy").length){
		$("body").lazyLoad();
	}
	/// now that the extra panels' images are loading, show the nav
	$(".feature-nav").fadeIn(1000);
});
