$(document).ready(function(){

	$('.radio-button').click(function(){AttachAction();});
	
	$('#send-button').click(function(){SendEmail();});
	
	$('#submit-link').click(function(){ShowSubmit();});
	
	$('#gallery-link').click(function(){window.location = 'http://www.animalsoffission.com/gallery.php'});
	
	$('#contest-rules').click(function(){window.location = 'http://www.animalsoffission.com/filmschoolcontestrules.pdf'});
	
	$('#contest-rules-text').click(function(){window.location = 'http://www.animalsoffission.com/filmschoolcontestrules.pdf'});
	
	$('#back-button').click(function(){BackSubmit();});
	
	$('#twitter-link').click(function(){window.open('http://twitter.com/Filmschoolmusic')});
	
	$('#fb-link').click(function(){window.open('http://www.facebook.com/filmschoolmusic?ref=ts')});
	
	$('#ms-link').click(function(){window.open('http://www.myspace.com/filmschool')});
	
	$('#fs-link').click(function(){window.open('http://www.filmschoolmusic.com/')});
	
	$('#itunes-link').click(function(){window.open('http://itunes.apple.com/us/preorder/fission-bonus-version/id386020118')});
	
	$('#album-art-main').click(function(){window.open('http://www.hispeedsoul.com/')});
	
	$('#fission-dates').click(function(){window.open('http://www.hispeedsoul.com/')});
	
	$('#head-img').click(function(){location.reload(true)});
	
	$('#hold-text').click(function(){window.location = 'http://www.animalsoffission.com'});
	
	$('#gallery-header').click(function(){window.location = 'http://www.animalsoffission.com/gallery.php'});
}
)
;

function checkMail(email){
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(email)) {
		return true;
	}
	return false;
}

function checkURL(url){
	var filter = /(http):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
	if (filter.test(url)) {
		return true;
	}
	return false;
}

function AttachAction(){
	if ( $("input[name='attach']:checked").val() == 'file'){
		if ( $('.url-attach').css('display') == 'block'){$('.url-attach').fadeOut(300);};
		$('.file-attach').fadeIn(300);
	};
	if ( $("input[name='attach']:checked").val() == 'url'){
		if ( $('.file-attach').css('display') == 'block'){$('.file-attach').fadeOut(300);};
		$('.url-attach').fadeIn(300);
	};
};

function SendEmail(){
	var subFilename = "";
	subName = $('#name-option').val();
	subEmail = $('#email-option').val();
	subTitle = $('#title-option').val();
	if ($("input[name='attach']:checked").val() == 'file'){
		subFilename = $('#file-input').val();
	};
	if ($("input[name='attach']:checked").val() == 'url'){
		subFilename = $('#url-input').val();
		if ( checkURL(subFilename) != true){
			var subFilename = "";
			alert('Invalid URL');
		};
	};
	
	if (subName.length > 0){ 
		if(subEmail.length > 0){
			if (subTitle.length > 0){
				if (subFilename.length > 0){
					if ( checkMail(subEmail) == true){
						$('#file-form').submit();
					}
					else {
						alert('You haven\'t submitted a valid email address.');
					}; 
				}
				else {
					alert('You haven\'t attached either a file or url.');
				}; 
			
			}
			else {
				alert('You haven\'t completed the submission form! Please add the title of your design, and make sure you\'ve.attached either a file or url.');
			};
			 
		}
		else {
			alert('You haven\'t completed the submission form! Please add your email address, and any other fields still empty.');
		};
	
	}
	else {
		alert('You haven\'t completed the submission form! Please add your name, and any other fields still empty.');
	};
	//

};

function ShowSubmit(){
	$('#draw-shade').fadeIn(300, function(){$('#submit-window').fadeIn(300);});
};

function BackSubmit(){
	$('#submit-window').fadeOut(300, function(){$('#draw-shade').slideUp(1000);});
};
