$(document).ready(function() {
    if($('#asToContact') && $('#asToContact').length == 1) {
        $('#asToContact').click(function(){
            $('.asVideosDiv').animate({
                left: '+=920',
                opacity: 0
            }, 'slow');
            $('#asContact').animate({
                left: '+=920',
                opacity: 1
            }, 'slow');
           return false;
        });

        $('#asToContactForm').click(function(){
            $('#asContact').animate({
                left: '+=920',
                opacity: 0
            }, 'slow');
            $('#asContactForm').animate({
                left: '+=920',
                opacity: 1
            }, 'slow');
           return false;
        });

        if (swfobject.getFlashPlayerVersion().major) {
            var closeVideo = function(container, callBack) {
                container.fadeOut('fast', function() {
        		    var videoNode = $(this).children().last();
        		    videoNode.replaceWith($('<div id="'+videoNode.attr('id')+'"/>'));
            		if (typeof callBack == 'function') {
            		    callBack();
            		}
        		});
            }		
            
            			    							
        	$('#asVideo_thumbnail_1').click(function() {
        	    closeVideo($('#asVideo2Container'), function() {
            		$('#asVideo1Container').each(function() {
            		    if($(this).is(":visible")) {
            		        closeVideo($(this));
            		    } else {
                		    $(this).fadeIn('fast');
                            swfobject.embedSWF("project/media/videos/Zehnder film Martin Olsson_2.swf", "asVideo1", "480", "270", "8");
                  		    }
            		}); 
        	    });
        	});
        	$('#asVideo_thumbnail_2').click(function() {
        	    closeVideo($('#asVideo1Container'), function() {
            		$('#asVideo2Container').each(function() {
            		    if($(this).is(":visible")) {
            		        closeVideo($(this));
            		    } else {
                		    $(this).fadeIn('fast');
                            swfobject.embedSWF("project/media/videos/Zehnder film TNT web_1.swf", "asVideo2", "480", "270", "8");
            		    }
            		}); 
        	    });
        	});
        	$('.asVideoClose').click(function() {
        		closeVideo($(this).parent());
        	});
    	} else {
            $('#asVideo_thumbnail_1, #asVideo_thumbnail_2').click(function() {
        		alert('Please install Adobe Flash player');
        	});
    	}

        var validateForm = function(form) {
            var error;
            form.find('.required').each(function() {
                if (!$(this).val()) {
                    var errorString = $(this).attr('data-error-missing');
                    error = errorString ? errorString : 'Please fill out all required fields.';
                    $(this).addClass('ui-state-error').attr('title', error).change(function() {
                        if ($(this).val()) {
                            $(this).removeClass('ui-state-error').removeAttr('title');
                        }
                    });
                }
            });
            if (error) {
                $('#errorMessage').html('<p>Please fill out all required fields.</p>');
                return false;
            }
            return true
        }
    	
    	$('#commentform').submit(function(){
    	    return validateForm($(this));
    	});
	}
});
