$(document).ready(
	function() {
		$('#nav li').hover(
			function(){
				$(this).children('ul').slideDown(200);
			}, function(){
				$(this).children('ul').slideUp(200);
			}
		);
		
		if($('.slideshow').length > 0){	
			$('.slideshow-content').cycle({
				speed: 700
			});
		}
		
		$('#txtName').focus(
			function() {
				if($(this).val()=="Name") {
					$(this).val('');
				}
			}
		);
		$('#txtName').blur(
			function() {
				if($(this).val()=="") {
					$(this).val('Name');
				}
			}
		);
		
		$('#txtCompany').blur(
			function() {
				if($(this).val()=="") {
					$(this).val('Company');
				}
			}
		);
		$('#txtCompany').focus(
			function() {
				if($(this).val()=="Company") {
					$(this).val('');
				}
			}
		);
		
		$('#txtPhone').blur(
			function() {
				if($(this).val()=="") {
					$(this).val('Phone');
				}
			}
		);
		$('#txtPhone').focus(
			function() {
				if($(this).val()=="Phone") {
					$(this).val('');
				}
			}
		);
		
		$('#txtEmail').blur(
			function() {
				if($(this).val()=="") {
					$(this).val('Email');
				}
			}
		);
		$('#txtAddress').focus(
			function() {
				if($(this).val()=="Address") {
					$(this).val('');
				}
			}
		);
		
		$('#txtAddress').blur(
			function() {
				if($(this).val()=="") {
					$(this).val('Address');
				}
			}
		);
		$('#txtEmail').focus(
			function() {
				if($(this).val()=="Email") {
					$(this).val('');
				}
			}
		);
		
		$('#txtEnquiry').blur(
			function() {
				if($(this).val()=="") {
					$(this).val('Description of Enquiry');
				}
			}
		);
		$('#txtEnquiry').focus(
			function() {
				if($(this).val()=="Description of Enquiry") {
					$(this).val('');
				}
			}
		);
		
		$('form.ajax-submit').submit(function(){
			formId = $(this).attr('id');
			url = $(this).attr('action');
		
//			$('#'+formId+' input,'+' #'+formId+' textarea,'+' #'+formId+' select').attr('disabled','disabled');	
			
			submitBtn=$('#'+$(this).attr('id')+' input#Submit');	
			submitTxt=submitBtn.val();
			submitBtn.val('Saving form..');
	
			submitData=new Object();
			submitData['_SubmitTxt'] = submitTxt;
			submitData['_FormId'] = formId;
			submitData['_Ajax'] = true;
			
			$('#'+formId+' input,'+' #'+formId+' textarea,'+' #'+formId+' select').each(function(){
				type=$(this).attr('type');
				if(type=='radio' || type=='checkbox'){
					if($(this).is(":checked")){
						submitData[$(this).attr('name')]=$(this).val();
					}
				}
				else {
					submitData[$(this).attr('name')]=$(this).val();
				}
			});
	
			$.ajax({
				type: "POST",
				url: url,
				data:submitData,
				dataType:"json",
				error: function(){
					$('form#'+formId).unbind('submit');
					$('form#'+formId).submit();
				},
				success: function(response){
					errMsg='';
					for(i in response.errors){
						if(response.errors[i]){
							errMsg+=' - '+response.errors[i]+'\n';
						}
					}
					
					if(errMsg){
						alert(errMsg);
						$('form#'+response.formId+' input#Submit').val(response.SubmitTxt);
						$('#'+response.formId+' input,'+' #'+response.formId+' textarea,'+' #'+response.formId+' select').removeAttr('disabled');
					}
					else {
						if(response.evaluate){
							eval(response.evaluate);
						}
						else {
							$('form#'+response.formId).unbind('submit');
							$('form#'+response.formId).submit();
						}
					}
				}
			});			
			return false;
		});
	}
);
