var nav_bottom = 0;
var thumbs_left = false;
var header_closed = false;
var timeout;
var freeTrialOpening = false;
var scrollUpperBounds = 494; //434
var scrollLowerBounds = 338; //278
var video_open = false;
var thumbs_animating = false;
var embedCode = '<div id="video-player-container"><iframe src="http://player.vimeo.com/video/{video_id}?title=0&amp;byline=0&amp;portrait=0&amp;color=9ed23c&amp;autoplay=1" width="466" height="263" frameborder="0" webkitAllowFullScreen allowFullScreen></iframe></div><p id="video-description"></p>';
if( $.browser.msie) {
	embedCode = '<div id="video-player-container"><object width="466" height="263"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id={video_id}&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=9ed23c&amp;fullscreen=1&amp;autoplay=1&amp;loop=0" /><embed src="http://vimeo.com/moogaloop.swf?clip_id={video_id}&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=9ed23c&amp;fullscreen=1&amp;autoplay=1&amp;loop=0" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="466" height="263"></embed></object></div><p id="video-description"></p>';
}
var isiPad = navigator.userAgent.match(/iPad/i) != null;
$(document).ready(function() {
	$(document).click(function(event) {
		if($("#video-player").hasClass("open")) {
			if(!$(event.target).hasClass("arrow-image")) $(this).trigger("close_video");
		}
	});
	$("#video-player-close").click(function(event) {
		$(document).trigger("close_video");
	});
	$("#main-video-play-button").click(function() {
		$(document).trigger("open_video");
	});
	$(".video-thumb").click(function() {
		$(document).trigger("close_video");
	});
	$(document).bind("open_video",function() {
		var featured;
		$(".video-thumb").each(function() {
			if($(this).css("top")=="0px") featured = $(this);
		});
		$("#video-player").html(embedCode.replace(/{video_id}/ig,featured.attr("rel")));
		$("#video-description").html(featured.find('.video-description').html());
		$("#video-description").hide()
		setTimeout(setVideoPlayerOpen,200);
		$("#video-player").show();
		$("#video-player-close").show();
		setTimeout(showVideoDescription,500);
	});
	$(document).bind("close_video",function() {
		if($("#video-player").hasClass("open")) {
			$("#video-player").removeClass("open");
			$("#video-player").empty();
		}
		$("#video-player-close").hide();
		$("#video-player").hide();
	});	
	$("#main-video-play-button img").fadeTo(0,.7);
	$("#main-video-play-button img").mouseenter(function() {
		$(this).fadeTo(75,1);
	});
	$("#main-video-play-button img").mouseleave(function() {
		$(this).fadeTo(75,.7);
	});
	$("#primary-navigation a").click(function() {
		$("#primary-navigation a").each(function() {
			$(this).removeClass("selected");
		});
		$(this).addClass("selected");
	});
	$("#footer-nav a").click(function() {
		if($("#nav-"+$(this).attr("href").replace("#","")).length > 0) {
			$("#primary-navigation a").each(function() {
				$(this).removeClass("selected");
			});
			$("#nav-"+$(this).attr("href").replace("#","")+" a").addClass("selected");
		}
	});
	
	$(".content-link").click(function(event) {
		event.preventDefault();
		var full_url = this.href;
		var parts = full_url.split("#");
		var trgt;
		(parts.length>1) ? trgt = parts[1] : trgt = parts[0];
		var target_offset = $("#"+trgt).offset();
		var target_top = target_offset.top;
		var target_distance = Math.abs(target_top - $(this).offset().top);
		var duration = target_distance/1.6;
		if(duration < 400) duration = 400;
		$('html, body').animate({scrollTop:target_top-88}, duration, 'easeOutCubic', function() {
		});
	});
	$("#logo-link").click(function(evt) {
		evt.preventDefault();
		var target_distance = Math.abs($(this).offset().top);
		$('html, body').animate({scrollTop:0}, target_distance/1.6, 'easeOutCubic');
	});
	$("#nav-watch a").click(function(evt) {
		evt.preventDefault();
		var target_distance = Math.abs($(this).offset().top);
		$('html, body').animate({scrollTop:0}, target_distance/1.6, 'easeOutCubic');
	});
	$("#footer-watch").click(function(evt) {
		evt.preventDefault();
		var target_distance = Math.abs($(this).offset().top);
		$('html, body').animate({scrollTop:0}, target_distance/1.6, 'easeOutCubic');
	});
	
	$(".colorbox").colorbox();
	
	/* Slideshow Controls */
	
	$(".slideshow").cycle({
		fx: 		'scrollHorz',
		speed:  	300, 
		timeout:	0,
		prev: 		'#tour-content .beta .slideshow-controls .left-arrow',
		next: 		'#tour-content .beta .slideshow-controls .right-arrow'
	});
	
	Cufon.replace('p');
	Cufon.replace('li');
	Cufon.replace('td');
	Cufon.replace('h3');
	Cufon.replace('h4');
	var useShadow = true;
	if($.browser.msie && $.browser.version < 9) useShadow = false;
	$('.carousel').carousel({
		hAlign:'center',
		vAlign:'center',
		hMargin:0.3,
		autoplay:false,
		directionNav:true,
		buttonNav:'bullets',
		frontWidth:466, 
        frontHeight:263,
        carouselWidth:1070, 
        carouselHeight:300,
        shadow:useShadow,
        before: function(carousel){
        	var featured = $(".video-thumb").eq(carousel.current).find('.video-description');
        	featured.fadeOut(200);        	
        },
        after: function(carousel){
        	var featured = $(".video-thumb").eq(carousel.current).find('.video-description');
        	featured.show().css("top","263px");
        	featured.css('width',"466px");
        	featured.animate({'top':(263-featured.height()-24)+"px"},350);
        }
	});   
	
	var featured = $(".video-thumb").eq(0).find('.video-description');
	featured.show().css("top","263px");
	featured.css('width',"466px");
	featured.animate({'top':(263-featured.height()-24)+"px"},250);
	/* 
	if(isiPad) {   
		$(window).resize(function() {
			clearTimeout(timeout);
			timeout = setTimeout(onResize,500);
		});      
		setTimeout(onResize,500);          
	}   
	*/  
	fixContentWidth();
	setTimeout(fixContentWidth,1000);
});

function fixContentWidth() {
	if(($("#content").width()) < ($(document).width())){
		$("#content").css("width",$(document).width()+"px");
		$("#top-wrapper").css("width",$(document).width()+"px");
		$(document).scrollLeft(($(document).width()-$(window).width())/2);
	} else {
		$("#content").css("width","100%");
		$("#top-wrapper").css("width","100%");
	}
}

$('window').load(function() {
	onResize();
});

$(window).resize(function() {
	clearTimeout(timeout);
	timeout = setTimeout(onResize,500);
});

function onResize() {
	clearTimeout(timeout);
	/*
	$('body').css("width",$("#top-wrapper").width()+190+'px');
	$('#video-area').css("width",$("#top-wrapper").width()+190+'px');
	$('#main-video-play-button').css("left",($("#top-wrapper").width()/2)+95+'px');
	*/
	fixContentWidth();
}

function setVideoPlayerOpen() {
	$("#video-player").addClass("open");
}

function showVideoDescription() {
	$("#video-description").slideDown(350);
}
