
$(function() {
	
	var name = $("#name"),
		email = $("#email"),
		address = $("#address"),
		agree = $("#agree"),
		allFields = $([]).add(name).add(email).add(address).add(agree),
		tips = $("#validateTips");


	var email_name = $("#email_name"),
		email_address = $("#email_address"),
		email_message = $("#email_message"),
		allFields = $([]).add(email_name).add(email_address).add(email_message),
		email_tips = $("#email_validateTips");
	
	function getContactMe() {
		//Set contact me to 1 if set or 0 if undefined
		if (contact_me = $("#contact_me:checked").val() == 1) {
			contact_me = 1;
		}
		else {
			contact_me = 0;
		}
	}

	function updateTips(t) {
		tips.text(t).effect("highlight",{},1500);
	}


	function checkLength(o,n,min,max) {
		if ( o.val().length > max || o.val().length < min ) {
			o.addClass('ui-state-error');
			updateTips("Length of " + n + " must be between "+min+" and "+max+".");
			return false;
		} else {
			return true;
		}
	}
	

	function checkRegexp(o,regexp,n) {
		if ( !( regexp.test( o.val() ) ) ) {
			o.addClass('ui-state-error');
			updateTips(n);
			return false;
		} else {
			return true;
		}
	}
	
	
	
	
	$("#dialog").dialog({
						
		bgiframe: true,
		autoOpen: false,
		height: 450,
		width: 256,
		modal: true,
		buttons: {
			'Download document': function() {
				var bValid = true;
				var download_success = false;
				allFields.removeClass('ui-state-error');

	
	

				bValid = bValid && checkLength(email,"email",6,80);
				bValid = bValid && checkRegexp(email,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"eg.  name@email.com");
				bValid = bValid;
						
						
				if (bValid) {
					//Reget the thingy
					getContactMe();
					
					
					download_id = $(".selectedDownload > a").attr("title");
					download_file = $(".selectedDownload > a").attr("alt");
						
					var dataString = 'name='+ name.val() + '&email=' + email.val() + '&address=' + address.val() + '&contact_me=' + contact_me + '&download_id=' + download_id;
					$.ajax({
						type: "GET",
						url: "/download-ajax.php",
						data: dataString,
						async: false,
						success: function(msg) {
							download_success = true;
							//alert('Yep');
							//blah blah balh
						},
						error: function(msg) {
							//alert('Nope');
						}
					});

					//alert(download_success);
					
					if(download_success == true) {
						 // alert('Download');
						  window.location.replace("http://visionifp.co.uk/downloads/" + download_file);
					}

					$(this).dialog('close');
					
					//Remove class from A tag
					$("a").removeClass("selectedDownload");
					
					//Remove class from P n li tag
					$("p").removeClass("selectedDownload");
					$("li").removeClass("selectedDownload");
					
				}
				
			},
			Cancel: function() {
				$(this).dialog('close');
				$("a").removeClass("selectedDownload");
				$("p").removeClass("selectedDownload");
				$("li").removeClass("selectedDownload");
			}
		},
		close: function() {
			allFields.val('').removeClass('ui-state-error');
			$("a").removeClass("selectedDownload");
			$("p").removeClass("selectedDownload");
			$("li").removeClass("selectedDownload");
		}
	});
	
	$('.download-document').click(function() {
		$('#dialog').dialog('open');
		$(this).addClass("selectedDownload");
		return false;			
	});	


});

var mygallery=new simpleGallery({
	wrapperid: "splash_images", //ID of main gallery container,
	dimensions: [822, 365], //width/height of gallery in pixels. Should reflect dimensions of the images exactly
	imagearray: [
		["/images/slideshow/header-1.jpg", "", "", ""],
		["/images/slideshow/header-2.jpg", "", "", ""],
		["/images/slideshow/header-3.jpg", "", "", ""],
		["/images/slideshow/header-4.jpg", "", "", ""]
	],
	autoplay: [true, 2500, 2], //[auto_play_boolean, delay_btw_slide_millisec, cycles_before_stopping_int]
	persist: false, //remember last viewed slide and recall within same session?
	fadeduration: 1000, //transition duration (milliseconds)
	oninit:function(){ //event that fires when gallery has initialized/ ready to run
		//Keyword "this": references current gallery instance (ie: try this.navigate("play/pause"))
	},
	onslide:function(curslide, i){ //event that fires after each slide is shown
		//Keyword "this": references current gallery instance
		//curslide: returns DOM reference to current slide's DIV (ie: try alert(curslide.innerHTML)
		//i: integer reflecting current image within collection being shown (0=1st image, 1=2nd etc)
	}
})// JavaScript Document

