var validatorGoogleVoice;
var validatorContactUs;
$.validator.addMethod("phoneFormat",  function(value, element) { //method for phone number
	var phoneLength = 0;
	$(element).closest('.phoneNumber').find('input').each(function() {
		phoneLength = phoneLength + $(this).val().length;
	});
	if (phoneLength == 10) {
		return true;
	} else if (phoneLength == 0) {
		return true;
	} else {
		return false;
	}
}, '<span class="fieldsMarker">&nbsp;</span>');

$(window).load(function(){
	$('body').addClass('bodyBg');
	$(".portfolio ul").load("portfolio.php",'',
		function(){
			$("#slider").easySlider({
				continuous: true
			});
			$('div.portfolio .ajaxLoad').addClass('hidden');

			$('#prevBtn a,#nextBtn a').mousedown(function(){
				$(this).parent().addClass('click');
			})
			$(document).mouseup(function(){
				$('#prevBtn,#nextBtn').removeClass('click');
			})
			pressedBt();
			ieShadowText();
		}
		)
});


$(document).ready(function (){
	eventTextArea();
	
	$(".content").load("contact_us.php",'',function(){
		initialization();
	})

	$('input.text, .textarea textarea').live('focus',function(){
		$(this).addClass('focus');
		$(this).parent('div').addClass('focus');
		$(this).parents('tr').find('label').addClass('focus');
	    
	})

	$('div.input').live('focus',function(){
		$(this).find('input.text').focus();
	})

	$("input.text, .textarea textarea").live('blur',function(){
		$(this).removeClass('focus');
		$(this).parent('div').removeClass('focus');
		$(this).parents('tr').find('label').removeClass('focus');
	})

	$('#url').live('blur',function(){
		var label = '<label class="valid invalid"><span class="fieldsMarker">&nbsp;</span></label>';
		if ($.trim($(this).val()) != '') {
			if ($(this).parent().find('label').is('label') === false) {
				$(this).after(label);
			}
		}else {
			$(this).parent().find('label').remove();
		}
	})

	$('.bt-contactUs').bind('click', function(){
		scrollWin();
	})
	
	ieShadowText();
})


function ieShadowText(){
	if($.browser.msie){
		var arr = $('.dropShadow');
		$.each(arr,function(key,value){
			var obj = $(this);
			if ((!obj.prev().hasClass('shadowElem'))) {
				var parent = obj.parent();
				var cssShadow = this.currentStyle["text-shadow"].split(' ');
				var colorShadow = cssShadow[3];
				var widthObj = obj.width();
				
				if (parent.css('position') != 'absolute') {parent.css('position','relative')}
				
				obj.clone().insertBefore(this)
					.removeClass('dropShadow')
					.addClass('shadowElem')
					.css('line-height',obj.css('line-height'))
					.css('color',colorShadow)
					.css('padding-left',parent.css('padding-left'))
					.width(widthObj);
				if (obj.css('position') == 'static') {obj.css('position','relative')}
			}
		})
	}
}


function eventTextArea() {
	$('.textarea textarea').live('click',function(){
		var elem = $('.textarea textarea');
		var parent = elem.closest('.textarea');
		
		parent.css({
			height: "150px"
		});
		elem.css({
			height: "140px"
		});
		elem.blur(function(){
			parent.css({
				height: "90px"
			});
			elem.css({
				height: "87px"
			});
		});
	});
	$('.textarea textarea').live('focus',function(){
		$(this).click()
		});
}

function initialization() {
	pressedBt();
	
	$('#field1').keyup(function(e) {
		var key = e.keyCode;
		$("#googleVoiceForm").validate().element('#field3');
		if (3 == $(this).val().length && ((key>47 && key<58) || (key>95 && key<106))) {
			$('#field2').focus();
		}
	});

	$('#field2').keyup(function(e) {
		var key = e.keyCode;
		$("#googleVoiceForm").validate().element('#field3');
		if (3 == $(this).val().length && ((key>47 && key<58) || (key>95 && key<106))) {
			$('#field3').focus();
		}
	});

	$('#phone1').keyup(function(e) {
		var key = e.keyCode;
		$("#feedbackForm").validate().element('#phone3');
		if (3 == $(this).val().length && ((key>47 && key<58) || (key>95 && key<106))) {
			$('#phone2').focus();
		}
	});

	$('#phone2').keyup(function(e) {
		var key = e.keyCode;
		$("#feedbackForm").validate().element('#phone3');
		if (3 == $(this).val().length && ((key>47 && key<58) || (key>95 && key<106))) {
			$('#phone3').focus();
		}
	});

	$('.bt-googleVoice').click(function(){
		if ($('#googleVoiceForm').hasClass('hidden')) {
			$('#googleVoiceForm').removeClass('hidden');
			setTimeout(function(){ieShadowText()},100);
		}
		else {
			$('#googleVoiceForm').addClass('hidden');
		}
		
	})

	$('.feedback .bt-form').click(function() {
		  
		$('#feedbackForm').ajaxSubmit({
			success: function(responseText) {
				if (responseText == "ok=true") {
					$('#feedbackForm .ajaxLoadConnecting').addClass('hidden');
					$('#feedbackForm .messageSend').removeClass('hidden');
					$.ga.trackPageview('/Contact-Form-Thanks');
				}
				else {
					$('#feedbackForm .ajaxLoadConnecting').addClass('hidden');
					$('#feedbackForm .errorSend').removeClass('hidden');
				}

			},
			beforeSubmit: function () {
				if (validatorContactUs.form()) {
					$('#feedbackForm table').addClass('hidden');
					$('#feedbackForm .ajaxLoadConnecting').removeClass('hidden');
					return true;
				}
				else {
					return false;
				}
			}
		});
	});

	validatorContactUs = $("#feedbackForm").validate({
		success: function(label) {
			label.addClass("valid").html('<span class="fieldsMarker">&nbsp;</span>')
		},
		errorClass: "invalid",
		rules: {
			name: "required",
			email: {
				required: true,
				email: true
			},
			phone1: {
				number: true,
				minlength: 3
			},
			phone2: {
				number: true,
				minlength: 3
			},
			phone3: {
				phoneFormat: true,
				number: true,
				minlength: 4
			}
		},
		messages: {
			name: {
				name: '<span class="fieldsMarker">&nbsp;</span>',
				required: '<span class="fieldsMarker">&nbsp;</span>'
			},
			email: {
				required: '<span class="fieldsMarker">&nbsp;</span>',
				email: '<span class="fieldsMarker">&nbsp;</span>'
			},
			phone1: {
				phone1: '<span class="fieldsMarker">&nbsp;</span>',
				minlength:'<span class="fieldsMarker">&nbsp;</span>'
			},
			phone2: {
				phone2: '<span class="fieldsMarker">&nbsp;</span>',
				minlength:'<span class="fieldsMarker">&nbsp;</span>'
			},
			phone3: {
				phone3: '<span class="fieldsMarker">&nbsp;</span>',
				minlength:'<span class="fieldsMarker">&nbsp;</span>'
			}
		}
	});

	$('.googleVoice .bt-form').click(function() {
			
		$('#googleVoiceForm').ajaxSubmit({
			success: function(responseText) {
				if (responseText == "ok=true") {
					$('#googleVoiceForm .ajaxLoadConnecting').html('<h2>Calling Your Phone!</h2>');
				}
				else {
					$('#googleVoiceForm .ajaxLoadConnecting').html('<h2>Error, please call us at 646.652.6842</h2>');
				}
			},
			beforeSubmit: function () {
				if (validatorGoogleVoice.form()) {
					$('#googleVoiceForm table').addClass('hidden');
					$('#googleVoiceForm .ajaxLoadConnecting').removeClass('hidden');
					return true;
				}
				else {
					return false;
				}
			}
		});

			
	});

	var options = {
		target: "#output",
		timeout: 3000
	};

	validatorGoogleVoice = $("#googleVoiceForm").validate({
		success: function(label) {
			label.addClass("valid").html('<span class="fieldsMarker">&nbsp;</span>')
		},
		errorClass: "invalid",
		submitHandler: function(form) {
			$('#googleVoiceForm table').addClass('hidden');
			$('#googleVoiceForm .ajaxLoadConnecting').removeClass('hidden');
		},
		rules: {
			name: "required",
			field1: {
				required: true,
				number: true,
				minlength: 3
			},
			field2: {
				required: true,
				number: true,
				minlength: 3
			},
			field3: {
				required: true,
				number: true,
				minlength: 4
			}
		},
		messages: {
			name: {
				name: '<span class="fieldsMarker">&nbsp;</span>',
				required: '<span class="fieldsMarker">&nbsp;</span>'
			},
			field1: {
				field1: '<span class="fieldsMarker">&nbsp;</span>',
				required: '<span class="fieldsMarker">&nbsp;</span>',
				minlength:'<span class="fieldsMarker">&nbsp;</span>'
			},
			field2: {
				field2: '<span class="fieldsMarker">&nbsp;</span>',
				required: '<span class="fieldsMarker">&nbsp;</span>',
				minlength:'<span class="fieldsMarker">&nbsp;</span>'
			},
			field3: {
				field3: '<span class="fieldsMarker">&nbsp;</span>',
				required: '<span class="fieldsMarker">&nbsp;</span>',
				minlength:'<span class="fieldsMarker">&nbsp;</span>'
			}
		}
	});

}

function pressNumber(e){
	var key = (typeof e.charCode == 'undefined' ? e.keyCode : e.charCode);
	if (e.ctrlKey || e.altKey || key < 32) return true;
	key = String.fromCharCode(key);
	return /[\d]/.test(key);
}

function pressedBt(){
	$('.bt').mousedown(function(){
		$(this).addClass('pressed');
	})
	$('.bt').mouseup(function(){
		$(this).removeClass('pressed')
	})
	$('.bt').mouseout(function(){
		$(this).removeClass('pressed')
	})
  
}

function scrollWin(){
	$('html, body').animate({
		scrollTop: $(".content").offset().top
	}, 1000);
}

function bindEvent() {
	$('.bt-contactUs').next().next().bind('click', function(){
		scrollWin();
	})
}

