$(document).ready(function(){
		$('#success_dialog').dialog({
			autoOpen: false,
			width: 400,
			buttons: {
				"": function() { 
					$(this).dialog("close"); 
				}
			},
			modal : true
		});
		
		$('#failure_dialog').dialog({
			autoOpen: false,
			width: 400,
			buttons: {
				"": function() {
					$(this).dialog("close"); 
				}
			},
			modal : true
		});
	});
		
	function validateContact()
	{
		valid = true;
		//set all fields to validate
		contactFields = new Array("contact_name", "contact_email","contact_reason","contact_message"); 
		//for reach fields
		for (f in contactFields)
		{
			var field = $('#'+contactFields[f]);
			//reset background color
			field.css('background-color', '#666666');
			if ($.trim(field.val()) == '')
			{
				valid = false;
				field.css('background-color', '#4A6D7F');
			}
			else if (f == 1)
			{
				var emailreg = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i
				var re = new RegExp(emailreg);
				if (!$.trim(field.val()).match(re)) {
					valid = false;
					field.css('background-color', '#4A6D7F');
				}
			}
		}
		return valid;
	}
	
	function clearContact()
	{
		//set all fields to clear
		contactFields = new Array("contact_name", "contact_email","contact_reason","contact_message"); 
		//for reach fields
		for (f in contactFields)
		{
			field = $('#'+contactFields[f]);
			field.val('');
		}
	}
	
	function sendContactMessage()
	{
		if (validateContact())
		{
			$.post("contact.php", { contact_name: $('#contact_name').val(), contact_email: $('#contact_email').val(), contact_reason: $('#contact_reason').val(), contact_message: $('#contact_message').val() }, 
			function(err) { 
				if (err == '0') {
					$('#success_dialog').html('<p>Thank you for contacting me.</p><p>I will be in touch soon.</p>');
					$('#success_dialog').dialog('open');
					$('#success_dialog').parent().parent().css('width',0);
					$('#success_dialog').parent().parent().animate( { width: 400 }, { duration: 'slow', easing: 'easeOutBounce' } );
					clearContact();
				}
				else
				{
					$('#failure_dialog').html('<p>Something went wrong!</p><p>Please try again later, or email me manually at<br> <a href="mailto:info@bdsgraphic.com" style=color:#000000;>info@bdsgraphic.com</a></p>');
					$('#failure_dialog').dialog('open');
					$('#failure_dialog').parent().parent().css('width',0);
					$('#failure_dialog').parent().parent().animate( { width: 400 }, { duration: 'slow', easing: 'easeOutBounce' } );
				}
			} );
		}
	}
	
	function downloadResume()
	{
		var field = $('#name_company');
		//reset background color
		field.css('background-color', '#666666');
		//validate name company is not empty
		if ($.trim(field.val()) == '')
		{
			field.css('background-color', '#4A6D7F');
		}
		else
		{
			$.post("resume.php", { name_company: $('#name_company').val() }, 
			function(err) {
				if (err == '0') {
					$('#success_dialog').html('<p>Thank you for downloading my resume.</p>');
					window.location = "download.php";
					$('#success_dialog').dialog('open');
					$('#success_dialog').parent().parent().css('width',0);
					$('#success_dialog').parent().parent().animate( { width: 400 }, { duration: 'slow', easing: 'easeOutBounce' } );
					field.val('');
				}
				else
				{
					$('#failure_dialog').html('<p>Oops! Something went wrong.</p><p>Please try again later.</p>');
					$('#failure_dialog').dialog('open');
					$('#failure_dialog').parent().parent().css('width',0);
					$('#failure_dialog').parent().parent().animate( { width: 400 }, { duration: 'slow', easing: 'easeOutBounce' } );
				}
			} );
		}
	}


	function rollover(item_name, image_name)
	{
		var new_image = "images/Nav/" + image_name;
		document.getElementById(item_name).src = new_image;
		return false;
	}

$(document).ready(function() {

        $('a[href*=#]').click(function() {

            if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '')

        && location.hostname == this.hostname) {

                var $target = $(this.hash);

                $target = $target.length && $target

          || $('[name=' + this.hash.slice(1) + ']');

                if (this.hash.slice(1).length != 0 && $target.length) {

                    var windowHeight = $(window).height();
                    var divHeight = $target.height();
                    if (windowHeight > divHeight) {
                        var difference = (windowHeight - divHeight) / 2;
                    }
                    else {
                        var difference = 0;
                    }

                    var targetOffset = $target.offset().top - difference;

                    $('html,body').animate({ scrollTop: targetOffset }, 1000);

                    return false;

                }

            }

        });

    });

$(document).ready(function()
{
	var new_divider_margin = Math.round($(window).height() / 2);
	$('.divider').css({'margin-top' : new_divider_margin, 'margin-bottom' : new_divider_margin});
	$('#nav').css('margin-bottom', new_divider_margin);
});
