$(document).ready(function(){
	resetSpencer();
	isLDClickable = true;
	
	$('#slides') 
		.before('<div class="nav">') 
		.cycle({ 
			fx:     'fade', 
			speed:  'fast',
			speed:    300,
			timeout:  5000,
			pager:  '.nav'
	});
	$('#slides').cycle('pause');
	
	
	$('.findOutMore').livequery(function() {
		$(this).append('<span class="hoverer"></span>')
		var $span = $('> span.hoverer', this).css('opacity', 0);
		$(this).hover(function(){
			$span.stop().fadeTo(200, 1);
		}, function() { 
			$span.stop().fadeTo(200, 0);
		});
	});
	
	$('.liveDemo').livequery(function() {
		$(this).append('<span class="hoverer"></span>')
		var $span = $('> span.hoverer', this).css({'opacity':0});
		$(this).hover(function(){
			if ( isLDClickable == true ){
				$(this).css({cursor: 'pointer'});
				$span.stop().fadeTo(200, 1);
			}
		}, function() { 
			if ( isLDClickable == true ){
				$(this).css({cursor: 'default'});
				$span.stop().fadeTo(200, 0);
			}
		});
	});

	var keephtml;
	$('.findOutMore').live('click',function(){
		$('.videoContainer').html(keephtml);
		$('.videoContainer').animate({
			height: 272
		}, 500).show();
	});

	$('.back').live('click',function(){
		$('.videoContainer').animate({
			height: 0
		}, 500).fadeOut();
		keephtml = $('.videoContainer').html();
		$('.videoWrap').remove();
	});

	$('.tourMenuItem').click(function() {
		$("#tourMenuCurrentItem").removeAttr("id");
		$(this).attr("id", "tourMenuCurrentItem");
		if ( ($(document).height() - $(window).height()) < ($(this).offset().top) ) {
			scrollToHere = ($(document).height() - $(window).height());
		} else {
			scrollToHere = ($(this).offset().top) - 5;
		}
		if ( ( $('html').scrollTop() != scrollToHere ) && ( $('body').scrollTop() != scrollToHere ) ) {
			$('html,body').animate({scrollTop: scrollToHere}, {duration: 500, easing: 'easeInOutQuint' });
		}
	});
	
	$('.tourMenuItem').not('.tmi5').click(function(){
		isLDClickable = true;
		resetSpencer();
		$('.liveDemo').css({height:62, overflow:'auto'}).animate({opacity:1});
	});
	$('.tmi1').click(function() {
		$("#currentTourItem").removeAttr("id");
		$('.tourItem1').attr("id", "currentTourItem");
		$('.liveDemo').fadeIn();
	});
	$('.tmi2').click(function() {
		$("#currentTourItem").removeAttr("id");
		$('.tourItem2').attr("id", "currentTourItem");
	});
	$('.tmi3').click(function() {
		$("#currentTourItem").removeAttr("id");
		$('.tourItem3').attr("id", "currentTourItem");
	});
	$('.tmi4').click(function() {
		$("#currentTourItem").removeAttr("id");
		$('.tourItem4').attr("id", "currentTourItem");
	});
	$('.tmi5').click(function() {
		isLDClickable = false;
		$('.fix').remove();
		$("#currentTourItem").removeAttr("id");
		$('.tourItem5').attr("id", "currentTourItem");
		$('.spguy').animate({opacity:1, marginLeft:-30}, {duration: 500, easing: 'easeInOutQuint'});
		$('.liveDemo').css({height:62, overflow:'auto'}).animate({opacity:0});
	});

	$('.liveDemo, .liveDemoLink').click(function(){
		$('.liveDemo').animate({opacity:0}, 200, function(){
			$('.liveDemo').css({height:0, overflow:'hidden'});
		});
		$('.liveDemo').find('.hoverer').animate({opacity:0});
		isLDClickable = false;
		$("#tourMenuCurrentItem").removeAttr("id");
		$('.tmi5').attr("id", "tourMenuCurrentItem");
		$('.fix').remove();
		$("#currentTourItem").removeAttr("id");
		$('.tourItem5').attr("id", "currentTourItem");
		$('.spguy').animate({opacity:1, marginLeft:-30}, {duration: 500, easing: 'easeInOutQuint'});
		if ( ($(document).height() - $(window).height()) < ($('#tourMenuCurrentItem').offset().top) ) {
			scrollToHere = ($(document).height() - $(window).height());
		} else {
			scrollToHere = ($('#tourMenuCurrentItem').offset().top) - 5;
		}
		if ( ( $('html').scrollTop() != scrollToHere ) && ( $('body').scrollTop() != scrollToHere ) ) {
			$('html,body').animate({scrollTop: scrollToHere}, {duration: 500, easing: 'easeInOutQuint' });
		}
	});
	
/*
	$("#getGoingForm").validate({
		rules: {
			name: "required",
			phone: {
				required: true
				},
			email: {
				required: true,
				email: true
			},
			company: "required"
		},
		messages: {
			name: "<p class='fix'>Please enter your name</p>",
			phone: "<p class='fix'>Please enter a valid phone number</p>",
			email: "<p class='fix'>Please enter a valid email address</p>",
			company: "<p class='fix'>Please enter a company name</p>"
		},			
		submitHandler: function(form) {
			completeForm();
		}
	});
	
	$("#phone").keypress(function (e) {
		if( e.which!=46 && 
			e.which!=47 && 
			e.which!=40 && 
			e.which!=41 && 
			e.which!=43 && 
			e.which!=45 && 
			e.which!=8 && 
			e.which!=0 && 
			(e.which<48 || e.which>57)) 
		{
			return false;
		}
	});
*/


		

});

$(window).load(function(){
	$('#slides').cycle('resume');
});

function resetSpencer() {
	$('.spguy').css({opacity:0, marginLeft: -200});
}

function completeForm() {
	var postname = $('.name').val();
	var postphone = $('.phone').val();
	var postemail = $('.email').val();
	var postcompany = $('.company').val();
	var postindustry = $('.industry').val();
	var postpos = $('.pos').val();
	var postlaunchDate = $('.launchDate').val();
	
	$.post("contact.php",{
		name: postname,
		phone: postphone,
		email: postemail,
		company: postcompany,
		industry: postindustry,
		pos: postpos,
		launchDate: postlaunchDate
	}, function(data){
		$('.getGoingWrap').html(data);
	});
}

