
// Initialization commands

$(function($){
	Site.addFieldFocus();
	$('#submit').click(function(){
		var f = $('#contact-form>form');
		Site.submitContact(f);
	});
	
	$('#contact-submit').click(function(){
		var f = $('#site-contact>form');
		Site.submitContact(f);
	});
	
	$('#move-in').focus(function(){$(this).val('')});
	
	$('#floorplan-link>a>img').hover(
		function(){$(this).attr({src:'/images/button-floorplan-over.gif'})},
		function(){$(this).attr({src:'/images/button-floorplan.gif'})}
	);
	$('#nats-link>a>img').hover(
		function(){$(this).attr({src:'/images/nats-link-over.gif'})},
		function(){$(this).attr({src:'/images/nats-link.gif'})}
	);
	
	var loc = String(window.location).substr(String(window.location).lastIndexOf('/')+1);
	if(loc!=''){
		$('#topnav>ul>li>a').each(function(){
			var re = new RegExp(loc,"i");
			if(this.href.match(re)){
				$(this).addClass('active');
			}
		});
	}
});

$.fn.trim = function(s){
	return s.replace(/^\s+|\s+$/,'');	
}

var Site = {
	addFieldFocus:function(){
		$('#contact-form,#site-contact').find('input,select').each(function(){		
			$(this).focus(function(){$(this).addClass('input-active')}).blur(function(){$(this).removeClass('input-active')});			
		});	
	},
	submitContact:function(whichForm){
		
		var whichForm = $(whichForm);

		var eRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

		var f = $.trim($('#first-name').val());
		var l = $.trim($('#last-name').val());
		var e = $.trim($('#email').val());
		var p = $.trim($('#phone').val());
		var m = $.trim($('#move-in').val());

		var empty = (f =='' || l =='' || e =='' || p =='' || m =='');

		var defaultsMatch = (
			f == $('#first-name').get(0).defaultValue || 
			l == $('#last-name').get(0).defaultValue || 
			e == $('#email').get(0).defaultValue
		)

		if(empty || defaultsMatch){
			alert('Please fill out all required fields.');
			return;
		}

		if(!e.match(eRegEx)){
			alert('Please provide a valid email address.');
			return;
		}
		if(!m.match(/^\d{1,2}\/\d{1,2}\/\d{4}$/)){
			alert('Move-in date must be in thist format: mm/dd/yyyy');
			return;			
		}

		$.blockUI({opacity:.25});

		var params = 'ajax=submitcontact&'+whichForm.serialize();

		$.ajax({
			type	: "POST",
			url		: "/lib/ajax.php",
			dataType: 'text',
			async	: true,
			data	: params,
			success: function(updated){

				if(updated == 'SUCCESS'){				
					
					pageTracker._trackPageview('/thanks.html');
					
					document.write = function(s){
						$(document.body).append(s);
					}
					
					var scr = $('<scr'+'ipt>');
					scr.attr({ type:"text/javascript", src:"http://www.googleadservices.com/pagead/conversion.js"});
					$(document.body).append(scr);
					
					whichForm.html('<div id="thanks">We appreciate your interest in Onyx on First, the premier address in Washington, DC&#0146;s most exciting new neighborhood. A leasing agent will contact you shortly with more information. </div>');
					
				}else{
					alert('An error occurred updating this record.  Please contact webmaster for support.' + updated);
				}	

				$.unblockUI();		
			}
		});
	},
	
	floorplans:{
		
		open:function(whichPlan){
			var bw = document.documentElement.offsetWidth;
			var bh = document.documentElement.offsetHeight;
			var fp = window.open('/floorplans/?p='+whichPlan,'fp_win','width=700,height=650,screenX='+(bw/2-350)+',left='+(bw/2-350)+',screenY='+(bh/2-325)+',top='+(bh/2-325));
			fp.focus();
		},
		close:function(){
			self.close();
		}		
	},
	playVideo:function(whichVideo,flvpath){
		var bw = document.documentElement.offsetWidth;
		var bh = document.documentElement.offsetHeight;
		var vwin = window.open('/flash/'+whichVideo+'?videopath=/flash/'+flvpath,'vidwin','width=533,height=360,screenX='+(bw/2-265)+',left='+(bw/2-265)+',screenY='+(bh/2-130)+',top='+(bh/2-130));		
	}
}
