$(function(){
  var page = $("body").attr("id");
  switch (page) {
	case "portfolio":
		portfolio();
		break;
	case "press":
		press();
		break;
	case "contact":
		contact();
		break;
	default:
		home();
		break;
  }
});

function home() {
	$("#kicker").hide();
	$("#content img").hide().fadeIn(2000, function() {
		$("#kicker").fadeIn(500);
	});
}

function portfolio() {
  $("#gallery").ready(function(){
	$("#furniture").parent().addClass("location");
	$.ajax({
	  type: "GET",
	  url: "gallerybuild.php",
	  data: "go=1&which=0",
	  success: function(msg) {
		$("#gallery").append(msg).fadeIn(500);
		buildButtons(0);
	  }
	});

	$.ajax({
	  type: "GET",
	  url: "gallerytext.php",
	  data: "go=1&which=0",
	  success: function(msg) {
		$(".gallerytext").text(msg);
	  }
	});
	$(".info").html('For More Information: Edward Ferrell &mdash; Lewis Mittman P: 336-841-3028 <a href="mailto:info@ef-lm.com" title="Email Edward Ferrell - Lewis Mittman">info@ef-lm.com</a>');
  });
  
  $("#furniture").click(function(){
	this.blur();
	$("#left h2").html("The Roger Thomas Collection<br />For Edward Ferrell - Lewis Mittman");
	$(this).parent().parent().find(".location").removeClass("location");
	$(this).parent().addClass("location");
	$("#gallery").empty();
	$.ajax({
	  type: "GET",
	  url: "gallerybuild.php",
	  data: "go=1&which=0",
	  success: function(msg) {
		$("#gallery").append(msg).fadeIn(500);
		buildButtons(0);
	  }
	});
	$("#right img").attr("src","images/gallery/furniture/1.jpg");
	$("#right .gallerylink a").attr("href","http://www.edwardferrell.com/ef/designercollection.aspx?id=3").css("text-decoration","underline");


	$.ajax({
	  type: "GET",
	  url: "gallerytext.php",
	  data: "go=1&which=0",
	  success: function(msg) {
		$(".gallerytext").text(msg);
	  }
	});
	$(".info").html('For More Information: Edward Ferrell &mdash; Lewis Mittman P: 336-841-3028 <a href="mailto:info@ef-lm.com" title="Email Edward Ferrell - Lewis Mittman">info@ef-lm.com</a>');
	return false;
  });

  $("#trimmings").click(function(){
	this.blur();
	$("#left h2").html("The Roger Thomas Collection<br />For Samuel & Sons Passementerie");
	$(this).parent().parent().find(".location").removeClass("location");
	$(this).parent().addClass("location");
	$("#gallery").empty();
	$.ajax({
	  type: "GET",
	  url: "gallerybuild.php",
	  data: "go=1&which=2",
	  success: function(msg) {
		$("#gallery").append(msg).fadeIn(500);
		$("#left div").remove();
	  }
	});

	$("#right img").attr("src","images/gallery/misc/16.jpg");
	$("#right .gallerylink a").attr("href","http://www.samuelandsons.com/").css("text-decoration","underline");

	$.ajax({
	  type: "GET",
	  url: "gallerytext.php",
	  data: "go=1&which=5",
	  success: function(msg) {
		$(".gallerytext").text(msg);
	  }
	});
	$(".info").html('For More Information: Samuel & Sons P: (212) 704-8000 <a href="mailto:info@samuelandsons.com" title="Email Samuel & Sons">info@samuelandsons.com</a>');
	return false;
  });

  	$("#mirrors").click(function(){
		this.blur();
		$("#left h2").html("The Roger Thomas Collection<br />For A.P.F. Master Framemakers");
		$(this).parent().parent().find(".location").removeClass("location");
		$(this).parent().addClass("location");
		$("#gallery").empty();
		$.ajax({
			type: "GET",
			url: "gallerybuild.php",
			data: "go=1&which=1",
			success: function(msg) {
				$("#gallery").append(msg).fadeIn(500);
				$("#left div").remove();
			}
		});

		$("#right img").attr("src","images/gallery/mirrors/1.jpg");
		$("#right .gallerylink a").attr("href","http://www.apfmunn.com").css("text-decoration","underline");

		$.ajax({
			type: "GET",
			url: "gallerytext.php",
			data: "go=1&which=1",
			success: function(msg) {
				$(".gallerytext").text(msg);
			}
		});

		$(".info").html("For More Information: A.P.F. Master Framemakers P: 914-665-5400 ext. 19 <a href=\"mailto:info@apfmunn.com\" title=\"Email A.P.F. Master Framemakers\">info@apfmunn.com</a>");
		return false;
	});

$("#lighting").click(function(){
		this.blur();
		$("#left h2").html("The Roger Thomas Collection<br />For Niedermaier");
		$(this).parent().parent().find(".location").removeClass("location");
		$(this).parent().addClass("location");
		$("#gallery").empty();
		$.ajax({
			type: "GET",
			url: "gallerybuild.php",
			data: "go=1&which=4",
			success: function(msg) {
				$("#gallery").append(msg).fadeIn(500);
				$("#left div").remove();
			}
		});

		$("#right img").attr("src","images/gallery/lighting/1.jpg");
		$("#right .gallerylink a").attr("href","mailto:della@niedermaier.com").css("text-decoration","underline");

		$.ajax({
			type: "GET",
			url: "gallerytext.php",
			data: "go=1&which=4",
			success: function(msg) {
				$(".gallerytext").text(msg);
			}
		});

		$(".info").html("For More Information: Niedermaier P: 312-492-9400 <a href=\"mailto:della@niedermaier.com\" title=\"Email Neidermaier\">della@niedermaier.com</a>");
		return false;
	});

  $("#misc").click(function(){
	this.blur();
	$("#left h2").html("The Roger Thomas Collection<br />For Trevi");
	$(this).parent().parent().find(".location").removeClass("location");
	$(this).parent().addClass("location");
	$("#gallery").empty();
	$.ajax({
	  type: "GET",
	  url: "gallerybuild.php",
	  data: "go=1&which=2",
	  success: function(msg) {
		$("#gallery").append(msg).fadeIn(500);
		buildButtons(2);
	  }
	});
	$("#right img").attr("src","images/gallery/misc/1.jpg");
	$("#right .gallerylink a").attr("href","http://www.trevicollection.com/").css("text-decoration","underline");

	$.ajax({
	  type: "GET",
	  url: "gallerytext.php",
	  data: "go=1&which=2",
	  success: function(msg) {
		$(".gallerytext").text(msg);
	  }
	});
	$(".info").html('For More Information: Trevi Manufacturing P: 702-564-3030 <a href="mailto:info@trevi.com" title="Email Trevi">info@trevi.com</a>');
	return false;
  });

  $("#carpet").click(function(){
	this.blur();
	$("#left h2").html("The Roger Thomas Collection<br />For Natural Carpet Company");
	$(this).parent().parent().find(".location").removeClass("location");
	$(this).parent().addClass("location");
	$("#gallery").empty();
	$.ajax({
	  type: "GET",
	  url: "gallerybuild.php",
	  data: "go=1&which=3",
	  success: function(msg) {
		$("#gallery").append(msg).fadeIn(500);
		$("#left div").remove();
		$("#gallery li:last").remove();
	  }
	});
	$("#right img").attr("src","images/gallery/carpet/1.jpg");
	$("#right .gallerylink a").attr("href","http://www.naturalcarpetcompany.com").css("text-decoration","underline");

	$.ajax({
	  type: "GET",
	  url: "gallerytext.php",
	  data: "go=1&which=3",
	  success: function(msg) {
		$(".gallerytext").text(msg);
	  }
	});
	$(".info").html('For More Information: Natural Carpet Company P: 310-664-1421 <a href="mailto:info@naturalcarpetcompany.com" title="Email Natural Carpet">info@naturalcarpetcompany.com</a>');
		
	return false;
  });
}

function contact() {
  $("#left div").hide();
  $("#left h3").click(function() {
	$("#left div").hide();
	$("#left h3").css("color","#333")
	$(this).css("color","#a01e21");
	$(this).next("div").fadeIn();
  });
  
  $("form").submit(function(){
	error = checkForm();
	if (error == 1) { return false; }
	var values = $("input[@type=text], textarea").serialize();
	$.ajax({
	  type: "POST",
	  url: "mail.php",
	  data: values,
	  complete: function() {
		$("#errors").remove();
		$("#contactform").hide("slow");
		$("<p>Thanks for your interest. We'll get back to you as soon as possible.</p>").appendTo("#right").fadeIn();
	  }
	});
	return false;
  });
}

function rollover(target) {
  image = $("#img"+target);
  src = image.attr("src");
  src2 = src.replace(/off/,"hover");
  image.attr("src", src2);
}

function rolloff(target) {
  image = $("#img"+target);
  src = image.attr("src");
  src2 = src.replace(/hover/,"off");
  image.attr("src", src2);
}

function swap(target,gallery) {
  image = $("#img"+target).attr("src");
  gallery = getGallery(gallery);
  image2 = image.replace(/thumbs\/hover\//,"");
  $("#right img").attr("src",image2).fadeIn(500);
  
  $.ajax({
	type: "GET",
	url: "gallerytext.php",
	data: "go="+target+"&which="+gallery,
	success: function(msg) {
		$(".gallerytext").text(msg);
	}
  });
}

function getGallery(path) {
  var gallery;
  if (path == "furniture") {
	gallery = "0";
  } else if (path == "mirrors") {
	gallery = "1";
  } else if (path == "misc") {
	gallery = "2";
  } else if (path == "carpet") {
	gallery = "3";
  } else if (path == "lighting") {
	gallery = "4";
  } else if (path == "trimmings") {
	gallery = "5";
  } else {
	gallery = "0";
  }
  return gallery;
}

function pdf(target) {
  image = $("#pdf"+target).attr("href");
  $("#cover").attr("src",image).fadeIn(500);
  return false;
}

function buildButtons(which) {
  $("#prev").remove();
  $("#next").remove();
  $("#left div").remove();
  $("#left").append("<div></div>");
  go = $("#gallery li:last img").attr("src");
  go = parseInt(go.match(/\d+/));
  num = $("#gallery li").length;
  
  prev = go - (num + 11);
  next = go + 1;
  
  if (go >= 13) {
	$("#left div").append('<button id="prev"><img src="images/back.png" alt="" /> Previous</button>');
	$("#prev").click(function(go){
	  $("#gallery").empty();
	  $.ajax({
		type: "GET",
		url: "gallerybuild.php",
		data: "go="+prev+"&which="+which,
		success: function(msg) {
		  $("#gallery").append(msg).fadeIn(500);
		  buildButtons(which);
		}
	  });
	});
  }
  if (num >= 12) {
	$("#left div").append('<button id="next">Next <img src="images/forth.png" alt="" /></button>');
	$("#next").click(function(go){
	  $("#gallery").empty();
	  $.ajax({
		type: "GET",
		url: "gallerybuild.php",
		data: "go="+next+"&which="+which,
		success: function(msg) {
		  $("#gallery").append(msg).fadeIn(500);
		  buildButtons(which);
		}
	  });
	});
  }
}

//VALIDATES EMAIL ADDRESS
function validEmail(str) {
  return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}


function checkForm() {
  $("#errors").remove();
  //SET ERRORS TO 0
  error = 0;
  phoneErr = 0;
  
  //GET USER SUPPLIED DATA
  name = $.trim($("#name").val());
  address = $.trim($("#address").val());
  phone1 = $.trim($("#phone1").val());
  phone2 = $.trim($("#phone2").val());
  phone3 = $.trim($("#phone3").val());
  email = $.trim($("#email").val());
  comment = $.trim($("#comment").val());
  
  $("#right h3").after('<ul id="errors"></ul>');
  
  if (name.length < 2) {
	$("#name").css("border-color","#a01e21");
	$("#errors").append("<li>Please fill in your name.</li>").fadeIn();
	error = 1;
  }
  
  if (address.length < 2) {
	$("#address").css("border-color","#a01e21");
	$("#errors").append("<li>Please fill in your address.</li>");
	error = 1;
  }
  
  if (phone1.length != 3 || isNaN(phone1)) {
	$("#phone1").css("border-color","#a01e21");
	phoneErr = 1;
  }
  
  if (phone2.length != 3 || isNaN(phone2)) {
	$("#phone2").css("border-color","#a01e21");
	phoneErr = 1;
  }
  
  if (phone3.length != 4 || isNaN(phone3)) {
	$("#phone3").css("border-color","#a01e21");
	phoneErr = 1;
  }
  
  if (phoneErr) {
	$("#errors").append("<li>Please fill in your phone number.</li>");
	error = 1;
  }
  
  if (email.length < 8) {
	$("#email").css("border-color","#a01e21");
	$("#errors").append("<li>Please fill in your email.</li>");
	error = 1;
  } else if (! validEmail(email)) {
	$("#email").css("border-color","#a01e21");
	$("#errors").append("<li>Please fill in a valid email address.</li>");
	error = 1;
  }
  
  return error;
}
