$(function(){
	$('#tri-survey ul.cycle').cycle({
		fx:			'fade',
		speed:		'fast',
		timeout:	0,
		next:		'.nextH',
		prev:		'.prevH'
	});
});

$(document).ready(function(){
	var slide	= -1;
	var go		= true;
	var lastSlide = -1;
	
	// Function that moves the slide Foward (y = 1) or Backward (y = 0).
	function progress(y){
		if(go){
			var total = $('.progress').find('li:not(.button)').size() - 2;
			
			if (y){
				slide = slide + 1;
				$('.nextH').click();
				$('.progress li:not(.button)').eq(slide).toggleClass('done');
				go = false;
			}
			else{
				$('.progress li:not(.button)').eq(slide).toggleClass('done');
				$('.prevH').click();
				slide = slide - 1;
			}
			
			
			//Controls the Apperance of thje start and submit buttons.
			if(slide > total){
				$('.progress li:not(.start, .end)').toggle();
				$('.end').fadeToggle();
			}
			if(slide > 0){
				$('.prev').removeClass('no')
			}
			else{
				$('.prev').addClass('no')
			}
		}
		else alert("Please answer the question.");
	}
	
	$('li.start').click(function(){
		$(this).toggle();
		$('.progress li:not(.start, .end)').fadeToggle();
		$('li.next').click();
	});
	
	//Buttons to go foward or backward, checks to see if the question was answered.
	
	$('li.next').click(function(){
		var val = $('#tri-survey ul.cycle li').eq(slide + 1).find('input.ready:checked').val();
		if(val == 'ready'){
			go = true;
		}
		progress(1);	
	});
	
	$('li.prev').click(function(){
		go = true;
		progress(0);
	});
	
	//Progress Bar functions
	
	$('li.slide-b').click(function(){
		var bnum = $(this).index() -2;
		var slidenum = 0;
		
		if (bnum > slide){
			alert("You can't skip ahead");
		}
		else{
			slidenum = slide - bnum;
			go = true;
			for (i = slidenum;i>=1;i--) progress(0);
		}
	});	
	//Gives the goahead to procedd along to next slide
	$('#tri-survey form input, #tri-survey form textarea').change(function(){
		$(this).parent().find('input.ready').attr('checked', 'checked');
	});

	function showModal(){
		if($(document).find('.modal')){
			//Modal
			var modWidth	= $('.modal').outerWidth();
			var modHeight	= $('.modal').outerHeight();
			var docHeight	= $(document).height();
			var docWidth	= $(document).width();
			var top			= ((docHeight/6)) - (modHeight/2);
			var left		= (docWidth - modWidth)/2;
			
			$('.modal').css({
				marginTop:top,
				marginLeft:left
			});
			$('.modal').show('fade');
			//Curtain
			$('<div class="curtain on"></div>').insertBefore('.modal');
			$('div.curtain').height(docHeight);
		}
	}
	
	showModal();
	
	function hideModal(){
		//Modal
		$('.modal').hide('fade');
		//Curtain
		$('.curtain').hide('fade', function(){
			$('.curtain').remove();
		});
	}
	
	function modalAccept(){
		//hideModal();
		//document.location = 'http://tri-supreme.com/survey.asp';
	}
	
	function modalDecline(){
		hideModal();
		document.location = 'http://tri-supreme.com?survey=no';
	}
	
	if(location.pathname =='/survey.asp') showModal();
	
	$('.modal .yes').click(function(){
		modalAccept();
	});

	$('.modal .no').click(function(){
		modalDecline();
	});
	
	//Ajax Post
	/*$('#tri-survey form').submit(function(){
		var action	= $(this).attr('action');
		var answers = $(this).serialize();
		$.post(action, answers, function(){
		$('#tri-survey').html('<h1>Thank you for taking our survey, your input is greatly appreciated!</h1>');
		});
		return false;
	});
		*/
});
