$(document).ready(function() {
	//this retains the field values, but hides them when people click on the input. !Need to work on not clearing user data!
	$("#contact-form input, #contact-form textarea,#license-request input,#license-request textarea").focus(function() {
		$(this).removeClass("error");
		var inputValue = $(this).val();
		$(this).val("");
		$(this).blur(function() {
			var newValue = $(this).val()
			if(newValue == ""){
				$(this).val(inputValue);
			}else{
				$(this).val(newValue);
			}
		});
	});
	//this changes the checkbox text.
	$("#checkbox").click(function() {
		if($(this).attr("checked")){
			$("#join-text").html("Thank you for your interest in us. You will be added to our mailing list.")
		}else{
			$("#join-text").html("Would you like to join our newsletter?");
		}
	});
	//this checks for valid Email & Subject for the contact form
	$("#submit").click(function() {
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		var emailValue = $("#email").val();
		var nameValue = $("#name").val();
		var textValue = $("#text").val();
		var checkValue = $("#checkbox").attr("checked");
		if(emailValue == "your email"){
			$("#email").addClass('error');
			$("#email").val("Please enter a valid email address");
			hasError = true;
		}
		else if(!emailReg.test(emailValue)){
			$("#email").addClass('error');
			$("#email").val("Please enter a valid email address");
			hasError = true;
		}
		if(nameValue == "your name"){
			$("#name").addClass('error');
			$("#name").val("Please enter your name.");
			hasError = true;
		}
		if(textValue == "what would you like to know?"){
			$("#text").addClass('error');
			$("#text").val("Don't forget to tell us something.");
			hasError = true;
		}
		//alert(checkValue);
		//alert(hasError);
		if(hasError == false){
			$(this).hide();
			$("#loading").fadeIn('fast');
			$.ajax({
				url: "_includes/contactform.php",
				type: "POST",
				data:'name=' + nameValue + "&email=" + emailValue + "&text=" + textValue + "&checkbox=" + checkValue + "&submit=true",		
				dataType:"JSON",
				success: function(data) {
					var obj = JSON.parse(data);
					//alert(obj.checkbox);
					console.log(data);
					if(obj.checkbox == "true"){
						$("#success h1").html("The form was sent successfully. Thank you for signing up for our newsletter.")
					}
					$("#contact-form ol").fadeOut('fast', function() {
						$("#success").fadeIn('slow');
					});
				},
				error: function(result) {
					$("#error").show();
				}
			});		
		}
		return false;
	});
	//this checks for valid Email & Subject for the request form
	$("#requestsubmit").click(function() {
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		var emailValue = $("#requestemail").val();
		var nameValue = $("#requestname").val();
		var textValue = $("#requesttext").val();
		//var checkValue = $(".checkbox input").attr("checked");
		var proSingle = $("#prosingle:checked").val();
		var proMultiple = $("#prosmultiple:checked").val();
		var pvpHD = $("#pvphd:checked").val();
		var prosingleupgrade = $('#prosingleupgrade:checked').val();
		var proconversion = $('#proconversion:checked').val();
		var prositeupgrade = $('#prositeupgrade:checked').val();
		var prosingleupgrade = $('#prosingleupgrade:checked').val();
		if(emailValue == "your email"){
			$("#requestemail").addClass('error');
			$("#requestemail").val("Please enter a valid email address");
			hasError = true;
		}
		else if(!emailReg.test(emailValue)){
			$("#requestemail").addClass('error');
			$("#requestemail").val("Please enter a valid email address");
			hasError = true;
		}
		if(nameValue == "your name"){
			$("#requestname").addClass('error');
			$("#requestname").val("Please enter your name.");
			hasError = true;
		}
		if(textValue == "Is there anything else you'd like to say?"){
			$("#requesttext").addClass('error');
			$("#requesttext").val("Don't forget to tell us something.");
			hasError = true;
		}
		/*
		if(checkValue != true){
			$("#select").addClass('error');
			$("#select").html('Please select an option.')
			hasError = true;
		}
		*/
		if(hasError == false){
			$("#request").fadeOut();
			$("#loading").fadeIn('fast');
			$.ajax({
				url: "_includes/requestform.php",
				type: "POST",
				data:'requestname=' + nameValue + "&requestemail=" + emailValue + "&requesttext=" + textValue + "&prosingle=" + proSingle + "&promultiple=" + proMultiple + "&pvphd=" + pvpHD + "&prosingleupgrade=" + prosingleupgrade + "&proconversion=" + proconversion + "&prosingleupgrade=" + prosingleupgrade + "&prositeupgrade=" + prositeupgrade + "&submit=true",		
				dataType:"JSON",
				success: function(data) {
					$("#request").fadeOut('fast', function() {
						$("#thankyou").fadeIn('slow');
					});
				},
				error: function(result) {
					$("#error").show();
				}
			});		
		}
		return false;
	});
	//tooltips
	$("#player-navigation a").easyTooltip({
			tooltipId: "tooltip",
			content: '<p>click!</p>',
			xOffset: -25,
			yOffset: 30
	});
	//this sets the links for those who use Javascript. Others will be redirect to the actual .html page
	$("#story > a").attr('href','#story-box');
	$("#contact-us > a").attr('href','#contact-box');
	$("#our-clients > a").attr('href','#client-box');
	$("#player").removeClass('hide');
	$("#film-nav").attr('href','film');
	$("#environmental-nav").attr('href','environmental');
	$("#media-nav").attr('href','media');
	$("#story-box,#contact-box,#client-box").addClass('hide');
	//this is for pretty photo
	$("a[rel^='prettyPhoto']").prettyPhoto();
	$(".prettyPhoto").prettyPhoto();
	//this is for the navigation tabs
	$("#story a,#contact-us a,#our-clients a").click(function() {
		if($("div").is(':animated')){
			return false;
		}
		$("#navigation li").removeClass('selected');
		var href = $(this).attr('href');
		$(href).siblings('div').hide(); //this hides all but the box needed to be shown
			if($(href).css("display") == "block"){
				$(href).slideUp();
			}
			else{
				$(href).slideDown('slow');
				$(this).parent().addClass('selected');
			}
		return false;
	});
	//this is for slideshows
	$('#player-film,#player-environmental,#player-media').cycle({
		speed: 2000
	});
	//this sets up the hover for the nav
	$('#hover-slides-media, #hover-slides-environmental, #hover-slides-film').hide();
	$('#player-navigation a').hover(function() {
		var href = $(this).attr('href'); //this pulls the 'href' value from each anchor;
		var image = $("#hover-slides-"+href); //this identifies the player unique to each anchor, example -> #player-film
		$('#hover-logo').hide();
		$(image).show();
	}, function() {
		var href = $(this).attr('href'); //this pulls the 'href' value from each anchor;
		var image = $("#hover-slides-"+href); //this identifies the player unique to each anchor, example -> #player-film
		$(image).hide();
		$('#hover-logo').show();
	});
	//this is for slideshow tabs
	$("#player-navigation a").click(function() {
		if($("div").is(':animated')){
			return false;
		}
		$("#player ul").hide();
		$("#player-navigation a").removeClass('selected');
		//set variables for easier access
		var href = $(this).attr('href'); //this pulls the 'href' value from each anchor;
		var playerbox = $("#player-"+href); //this identifies the player unique to each anchor, example -> #player-film
		var metabox = $("#"+href); //this identifies the meta box unique to each anchor, example -> #film;
		playerbox.fadeIn('fast');
		$(metabox).siblings().hide();
		if(metabox.css("display") == "block"){
			metabox.slideUp("fast");
			playerbox.hide();
			$('#player-default').fadeIn('fast');
		}
		else{
			metabox.slideDown('slow');
			$(this).addClass('selected');
		}
		return false;
	});
});
