$(document).ready(function(){
	$('div#contact').removeClass('loading').css('display', 'none');
    $('div#pic').removeClass('loading');
    $('div#page-intro').removeClass('loading');
	$('div#contact-toggle a').click(function(){
		var state = $('div#contact-toggle a').attr('class');
		var height = $('div#contact-toggle').css('height');
		if(state == 'down') {
			$('div#contact-toggle').css('height', '47px');
			$('div#contact-toggle a').removeClass('down');
			$('div#contact-toggle a').addClass('up');
			
		} else {
            $('div#footer').css('height', '600px');
            $('div#contact-toggle a').removeClass('up');
			$('div#contact-toggle a').addClass('down');
		}
		$('div#contact').slideToggle('slow', function() {
			if(height == '47px') {
				$('div#contact-toggle').css('height', '46px');
                $.scrollTo({top:'-=450px'}, 400);
                $('div#footer').animate( { height:"248px" }, 400 );

			} else {
                $.scrollTo({top:'+=450px'}, 400);
            }
		});
	});
	
	$('img#btt').click(function(){
		$.scrollTo('0', 400);
	});
	
	// contact form
		var options = { 
			target:        '#target',   // target element(s) to be updated with server response 
			beforeSubmit:  validate,  // pre-submit callback 
			resetForm: true,
			success:       function(){
				$('#target').show();
				$('#contact-form').hide();
				$('#target').fadeOut(4000, function() {
					$('#contact-form').fadeIn('fast');
				});
			}  // post-submit callback 
	 
			// other available options: 
			//url:       url         // override for form's 'action' attribute 
			//type:      type        // 'get' or 'post', override for form's 'method' attribute 
			//dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
			//clearForm: true        // clear all form fields after successful submit 
			//resetForm: true        // reset the form after successful submit 

		}; 
			
		$('#contact-form').ajaxForm(options); 
	
		function validate(formData, jqForm, options) { 
			
			var nameValue = $('input#contact-name').fieldValue(); 
			var emailValue = $('input#contact-email').fieldValue(); 
			var messageValue = $('textarea#contact-message').fieldValue();
            var companyValue = $('input#contact-company').fieldValue();
			var errorSet = 0;
			
			// reset error messages
			$('span.error-name').hide();
			$('span.error-email').hide();
			$('span.error-message').hide();
			
			if (!nameValue[0]) {
				$('span.error-name').show();
				errorSet = 1;
			}
			
			if (!emailValue[0]) {
				$('span.error-email').show();
				errorSet = 1;
			} 
				
			validateEmail(emailValue);
			
			function validateEmail(email) {
			   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			   var address = email;
			   if(reg.test(address) == false) {
				  $('span.error-email').show();
				  errorSet = 1;
			   }
			}
			
			if (!messageValue[0]) {
				$('span.error-message').show();
				errorSet = 1;
			}
            
            if(companyValue[0]) {
                errorSet = 1;
            }
			
			if (errorSet == 1) {
				return false;
			}
		}
		
		$('div.additional-message').click( function() {
			$(this).hide();
			$('#form-response').hide();
			$('#contact-form').show();
		});
		
		$('span#clear button').click( function() {
			// reset error messages
			$('span.error-name').hide();
			$('span.error-email').hide();
			$('span.error-message').hide();
            $('div#footer').css('height', '600px');
            $('div#contact-toggle a').removeClass('up');
			$('div#contact-toggle a').addClass('down');
            $('div#contact').slideToggle('slow', function() {
				$('div#contact-toggle').css('height', '46px');
                $.scrollTo({top:'-=450px'}, 400);
                $('div#footer').animate( { height:"248px" }, 400 );
            });
		});
        
        
    // homepage blog carousel
    $('#hp-blog #recent-entries ul').cycle(
            {
                fx:'scrollVert',
				speed:500,
				timeout:8500,
                prev:'#hp-blog span.prev a',
                next:'#hp-blog span.next a',
                pager:'#hp-blog span.pagination',
                pagerAnchorBuilder:function(idx,slide){
                    return '<a href="#"><img src="http://media.charlotteobserver.com/static/design/blank.gif" alt=""><\/a>';
                }
                //after: portAfter
            }
        );
    
    // change background of photo container on hover
    $('#hp-blog li').mouseover(function() {
        $(this).addClass('active');
        $('#hp-blog #recent-entries ul').cycle('pause');
    }).mouseout(function(){
        $(this).removeClass('active');
        $('#hp-blog #recent-entries ul').cycle('resume');
    });

});
