//simple modal calls
$(document).ready(function () {
							
	/* flashy goodness */
	$('#flash_video').flash({
    src: '/themes/bluehuecms/video/CMS.swf',
    width: 400,
    height: 243
	});
	
	$('#menu a.contact, #footer_menu a.contact, #content_blog a.contact, #content_loginForm a.contact').click(function (e) {
		e.preventDefault();
		$('#contactForm').modal();
	});
	
	$('#menu a.network').click(function (e) {
		e.preventDefault();
		$('#bluehueNetwork').modal();
	});
	
	if($("#content_blog .blog-rotator img").length > 0)
	{
		var t=setTimeout("rotateContentBlog()",5000);
	}
	
});

$(function() {
	
	// external class for links
	$(".external").attr("target","_blank");
		
	// Twitter feed
	$("#tweet").tweet();
	
	// equal heights for sub and main col
	var mainColHeight = $("#mainCol").height();
	var subColHeight = $("#subCol").height();
	if (mainColHeight > subColHeight) {
		$("#subCol").css("height",mainColHeight)
	}
	if (subColHeight > mainColHeight) {
		$("#mainCol").css("height",subColHeight)
	}
	
	// zebra stripe packages table
	$("#packagesTable tr:nth-child(odd)").addClass("zebra");
	
	// hide ajax errors
	$('#contactContent .alert').hide();
	
	//Login Form	
	if($.cookie("terms_accepted") == null) {
		$("#content_terms, #content_terms_accept").show();
		$("#termsLink").hide();
		$("#terms_accept").attr('checked',false);
	}
	else {
		$("#terms_accept").attr('checked',true);
	}
	
	$('#termsLink').click(function(event) {
		$("#content_terms").toggle();
	});
	
	$("#login_form").submit(function(){
		if($("#terms_accept").is(":checked")) {
			$.cookie("terms_accepted", "Yes");
			return true;
		}
		else {
			alert("You must accept the Terms and Conditions before logging in!");
			return false;
		}
	});
});

// contact form functions
$.fn.clearForm = function() {
  return this.each(function() {
 var type = this.type, tag = this.tagName.toLowerCase();
 if (tag == 'form')
   return $(':input',this).clearForm();
 if (type == 'text' || type == 'password' || tag == 'textarea')
   this.value = '';
 else if (type == 'checkbox' || type == 'radio')
   this.checked = false;
 else if (tag == 'select')
   this.selectedIndex = -1;
  });
};

function showAndHideContact ()
{
	if ($("#contactWrap").css("display") == "none") {
		$("#contactWrap form").clearForm()
		$('#contactContent .alert').hide();
		$("#contactWrap").slideDown("400");
	} else {
		$("#contactWrap").slideUp("400");
		$("#contactWrap form").clearForm()
		$('#contactContent .alert').hide();
	}
}

function hideContact () {
	$("#contactWrap").slideUp("400");
}

// regular expressions
function isEmail(strValue)
	{
		return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(strValue);
	}

function submitContact()
	{
	$('#contactContent .alert').hide();
	var valid = true;
	var source = $("input#source").val();
	var name = $("input#name").val();
	var company = $("input#company").val();
	var email = $("input#email").val();
	var phone = $("input#phone").val();
	var comments = $("textarea#comments").val();
	if(name == "")
		{
		valid = false;
		$("#contactContent #nameAlert").show();
		}
	if(company == "")
		{
		valid = false;
		$("#contactContent #companyAlert").show();
		}
	if(email == "")
		{
		valid = false;
		$("#contactContent #emailAlert").show();
		}
	if(email != "" && !isEmail(email))
		{
		valid = false;
		$("#contactContent #emailAlert2").show();
		}
	if(!valid)
		{
		return false;
		}
	else
		{
		
			//send the form
			var dataString = $("#BlueHueCMS_Contact").serialize();
			$.ajax({
				type: "POST",
				url: "/themes/bluehuecms/includes/contactProcess.php",
				data: dataString,
				success: function() {
					$('#contactForm').html("<div id='success'></div>");
					$('#success').html("<h3>Thank you for contacting Blue Hue Interactive!</h3>")
					.append("<p>We will respond to your inquiry in a prompt manner.</p>")
					.hide()
					.fadeIn(1500, function() {
						$('#success');
					});
					setTimeout(hideContact,4500);
			}
		});
			
	}
}

function submitEmailForm() {
		var valid = true;
	var contact = $("checkbox#chk_contact").val();
	var source = $("input#emailFormSource").val();
	var name = $("input#emailFormName").val();
	var company = $("input#emailFormCompanyname").val();
	var email = $("input#emailFormEmail").val();
	var phone = $("input#emailFormPhone").val();
	var url = $("input#emailFormUrl").val();
	if(name == "")
		{
		valid = false;
		alert("Please enter your first and last name");
		}
	if(company == "")
		{
		valid = false;
		alert("Please enter your company name");
		}
	if(email == "")
		{
		valid = false;
		alert("Please enter your email");
		}
	if(email != "" && !isEmail(email))
		{
		valid = false;
		alert("Please enter a valid email");
		}
	if(!valid)
		{
		return false;
		}
	else
		{
	var dataString = $("#form").serialize();
	$.ajax({
		type: "POST",
		url:  "/themes/bluehuecms/includes/contactProcess.php",
		data: dataString,
		success: function() {
			$("#form").html("Thank you for your interest in Blue Hue Email! Someone will be contacting you shortly.");
		}
	});
	}
}

var imgCount = 0;

function rotateContentBlog()
{
	imgCount++;
	var imgIndex = imgCount % 4;
	$("#content_blog .blog-rotator img").attr("src","/media/images/BHEmail_home"+imgIndex+".jpg");
	var t=setTimeout("rotateContentBlog()",5000);
}
