$(document).ready(function(){
    
    $('.rollover').each(function() {
	rollsrc = $(this).attr('src');
	rollON = rollsrc.replace(/.png$/ig,'_over.png');
	$("<img>").attr("src", rollON);
    });
    
    $('.rollover').mouseover(function() {
        imgsrc = $(this).attr('src');
        if (!imgsrc.match(/_over/)) {
            $(this).attr('src', imgsrc.replace(/.png$/ig,'_over.png'));
        }
    });
    
    $('.rollover').mouseout(function() {
        $(this).attr("src", imgsrc);
    });
          
    // load the modal window
    var compatibleOverlay, ie6 = !window.XMLHttpRequest;
    $('body').append(
        $([
            overlay = $('<div id="mask" />')[0]
        ]).css('display','none')
    );
    $('div#contact').css('display','none');
        
    $('.contactme').click(function(){       

        // scroll to top
        $('html, body').animate({
            scrollTop:0
        }, 'fast');

        // before showing the modal window, reset the form incase of previous use.
        $('.success, .error, .sendError').hide();
        $('form#contactForm').show();
		
        // Reset all the default values in the form fields
        $('#name').val('');
        $('#addr').val('');
        $('#comment').val('');                    

        //show the mask and contact divs
        compatibleOverlay = ie6 || (overlay.currentStyle && (overlay.currentStyle.position != "fixed"));
	if (compatibleOverlay) overlay.style.position = "absolute";
        $(overlay).css("opacity",0.7).fadeIn(400); 
        $('div#contact').css("opacity",1.0).fadeIn(400);

        // stop the modal link from doing its default action
        return false;
    });

    // close the modal window is close div or mask div are clicked.
    $('div#close, div#mask').click(function() {
        $('div#contact, div#mask').stop().fadeOut('slow');

    });

    // when the Submit button is clicked...
    $('#contactForm').submit(function() {
        
        $('.error').hide().remove();
        
        //Inputed Strings
        var email = $('#addr').val();
        //var phone = $('#tel').val();
        var comment = $('#comment').val();
        
        var error = '';
		
        //Regex Strings
        var email_regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/; 

				
        //Test Email
        if (email != '') {
            if(!email_regex.test(email)) {
                error += 'Invalid email entered.</br>';
            }
        }
        
        //Email and Telephone?
        //if (email == '' && phone == '') {
        //    error += 'Please enter either an email or a telephone no.</br>';
        //}
        
        if (email == '') {
            error += 'Please enter an email address.</br>';
        }
			
        //Blank Comment?
        if(comment == '') {
            error += 'Please enter a comment.</br>';
        }
			
        //No Errors?
        if (error == '') {
            $.ajax({
                
                type: 'post',
                url: $('form#contactForm').attr('action'),
                dataType: 'html',
                data: $('form#contactForm').serialize(),
                
                error: function() {
                    $('.error').hide();
                    $('#sendError').slideDown('slow');
                },
                
                success: function ($data) {
                    //alert($data);
                    if ($data == "OK") {
                        $('.error').hide();
                        $('.success').slideDown('slow');
                        $('form#contactForm').fadeOut('slow');
                    } else {
                        $('.success').hide();
                        $('#contact_header').after('<p class=error>There has been an error sending your email.</p');
                        $('.error').slideDown('slow');
                        $('form#contactForm').fadeOut('slow');
                    }
                }				
            });	
        } else {
            $('#contact_header').after('<p class=error>' + error + '</p>');
            $('.error').slideDown('slow');
        }
			
        return false;
    });
	
});
