var map; 

function sepCoord(crd) {
	var arr = new Array();
	arr[0] = crd.substr(0,crd.indexOf(","));
	arr[1] = crd.substr(crd.indexOf(",")+1,crd.length);
	//alert("c0: " + arr[0] + "\nc1: " + arr[1]);
	return arr;
}

function initialize(ctr,mrkr) {
	if (ctr == "") {ctr = "41.886584,-87.629183"; }
	if (mrkr == "") { mrkr = "41.886584,-87.62583"; }
	var cLL = sepCoord(ctr);
	var mLL = sepCoord(mrkr);
	
	var ltMapStyles = [
		{
			featureType: "all",
			stylers: [
				{saturation:-100},
				{hue:"#333333"},
				{lightness:20}
			]
		}
	];
		
	var ltMapType = new google.maps.StyledMapType(ltMapStyles, {name: "LiquidThread"});

  var mapOptions = {
    zoom: 16,
    center: new google.maps.LatLng(cLL[0],cLL[1]),
    zoomControl: false,
    streetViewControl: false,
    panControl: false,
    mapTypeControlOptions: {mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'lt_maps']}
   };
   
  map = new google.maps.Map(document.getElementById('top'), mapOptions);
  
  map.mapTypes.set('lt_maps', ltMapType);
  map.setMapTypeId('lt_maps');
  
 	var image = "images/lt-mapmarker.png";
	var markerLatLng = new google.maps.LatLng(mLL[0],mLL[1]);
	var ltMarker = new google.maps.Marker({
		position: markerLatLng,
		map: map,
		icon: image
	});

}

function sendMsg(params) {
	var success_msg = "Your message has been sent<br /><br />Someone will reach out to you shortly";

	$.ajax({
		//dataType: "json",
		type: "POST",
		url: "scripts/contact_form.php",
		data: params,
		cache: false,
		success: function(html) {
			$("#sending").fadeOut(400,function(){$("#msg").html(success_msg).fadeIn(400).delay(1500).fadeOut(1000,function(){$("#address").fadeIn(500,function(){$("#your_name").val(""); $("#your_message").val(""); $("#email").val("");$("#form_container").addClass("sent");});});});
			//$("#sending").fadeOut(400,function(){$("#msg").html(html).fadeIn(500);});
		},
		error: function(jqXHR, textStatus, errorThrown) {
			$("#sending").fadeOut(400,function(){$("#msg").html("send error: \njqXHR: " + jqXHR + "\ntextStatus: " + textStatus + "\nerrorThrown: " + errorThrown).fadeIn(300);});
		}
	});
} // end function sendMsg

$(document).ready(function(){
	
	function swapImg(img,ver) {
		var new_img;
		var img_part;
		if ( ver == "on" ) { img_part = img.substr(0,img.indexOf(".")); } else { img_part = img.substr(0,img.indexOf("-over")); }
		if ( ver == "on" ) { new_img = img_part + "-over.png"; } else { new_img = img_part + ".png"; }
		//alert("img: " + img + "\nnew_img: " + new_img + "\nimg_part: " + img_part);
		return new_img;
	}
	
	function splitVals(str) {
		var arr = new Array();
		arr[0] = str.substr(0,str.indexOf("||"));
		arr[1] = str.substr(str.indexOf("||")+2,(str.indexOf("|-|")-str.indexOf("||")-2));
		arr[2] = str.substr(str.indexOf("|-|")+3,str.length);
		return arr;
	}

	$(".citylink").click(function(){
		$(".citylink").removeClass("active");
		$(this).addClass("active");
		
		var el = splitVals($(this).attr("rel"));
		var addr = "#" + el[2];
		
		if ( $("#frame_content").hasClass("active") ) {
			//$("#top_container").css({"background":newprop});
			initialize(el[1],el[0]);
			$("div.office").fadeOut(500).delay(800).removeClass("active");
			$(addr).fadeIn(500);
			if (addr=="#chicago") { $("#to_email").text("erin.vogel@liquidthread.com"); } else { $("#to_email").text("carly.feinstein@liquidthread.com"); }
			if ( $("#form_container").hasClass("sent") ) { $("#form_container").fadeIn(500); }
			$("#from_office").text(addr);
		} else { 
			$("#flashobject").fadeOut(500,function(){
				initialize(el[1],el[0]);
				//$("#top_container").css({"background":newprop});
				//$("#frame").css("margin-top","20px");
				$("#frame_content").fadeIn(700).addClass("active");
				$(addr).fadeIn(500);
				if (addr=="#chicago") { $("#to_email").text("erin.vogel@liquidthread.com"); } else { $("#to_email").text("carly.feinstein@liquidthread.com"); }
				if ( $("#form_container").hasClass("sent") ) { $("#form_container").fadeIn(500); }
				$("#from_office").text(addr);
			});
			
		} // end if righPane.hasclass(active)
	});
	
	$("#your_name").focus(function(){
		if ( $(this).hasClass("error") ) { $(this).removeClass("error"); }
		if ( $(this).val() == "Your Name" || $(this).val() == "Required" ) { $(this).val(""); }
	});
	
	$("#email").focus(function(){
		if ( $(this).hasClass("error") ) { $(this).removeClass("error"); }
		if ( $(this).val() == "Your Email" || $(this).val() == "Required" ) { $(this).val(""); }
	});

	$("#your_message").focus(function(){
		if ( $(this).hasClass("error") ) { $(this).removeClass("error"); }
		if ( $(this).val() == "Your Message" || $(this).val() == "Required" ) { $(this).val(""); }
	});
	
	$("#sendMsgBtn").hover(function(){$(this).css("cursor","pointer");},function(){$(this).css("cursor","default");});
	
	$("#sendMsgBtn").click(function(){
		var error = true;

		var eml = new RegExp("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$");
		var name = $("#your_name").val();
		var email = $("#email").val();
		var mesg = $("#your_message").val();
		var to_email = $("#to_email").text();

		var params = $("#contact").serialize();
		params += "&your_email=" + email + "&to_email=" + to_email + "&subject=Message from LiquidThread Website (" + $("#from_office").text() + ")" ;
		//alert(params);

		if ( name == "" || name == "Your Name" || name == "Required" ) { $("#your_name").addClass("error").val("Required"); error = false; }
		if ( email == "" || email == "Your Email" || email == "Required"  ) { $("#email").addClass("error").val("Required"); error = false; }
		if ( !eml.test(email) ) { $("#email").addClass("error").val("Required"); error = false; }
		if ( mesg == "" || mesg == "Your Message" || mesg == "Required"  ) { $("#your_message").addClass("error").val("Required"); error = false; }
		
		if (error == false) {return false;}
		
		$("#address").fadeOut(400);
		$("#form_container").fadeOut(400,function(){$("#sending").delay(400).fadeIn(400); $("#wait").delay(300).fadeIn(200); sendMsg(params);});
		
		
	});
	
	$(".social_link").hover(function(){
			var img = "";
			var rel = "";
			if ( $(this).hasClass("img") ) {
				img = swapImg($(this).find("img").attr("src"),"on");
				rel = "#" + $(this).attr("id");
			}
			if ( $(this).hasClass("txt") ) {
				rel = "#" + $(this).find("a").attr("rel");
				img = swapImg($(rel).find("img").attr("src"),"on");			
			}
			$(rel).find("img").attr("src",img);			
		},function(){
			var img = "";
			var rel = "";
			if ( $(this).hasClass("img") ) {
				img = swapImg($(this).find("img").attr("src"),"off");
				rel = "#" + $(this).attr("id");
			}
			if ( $(this).hasClass("txt") ) {
				rel = "#" + $(this).find("a").attr("rel");
				img = swapImg($(rel).find("img").attr("src"),"off");			
			}
			$(rel).find("img").attr("src",img);			
		});
	
	$("#clear_fields").hover(function(){$(this).css({"cursor":"pointer","text-decoration":"underline"});},function(){$(this).css({"cursor":"default","text-decoration":"none"});});

	$("#clear_fields").click(function(){
		$("#your_name").val("");
		$("#your_message").val("");
		$("#email").val("");
	});	
	
});
