

$(function() {
    setInterval( "slideSwitch()", 5000 );
});

function slideSwitch() {
    var $active = $('#slideshow DIV.active');

    if ( $active.length == 0 ) $active = $('#slideshow DIV:last');

    // use this to pull the divs in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow DIV:first');

    // uncomment below to pull the divs randomly
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}
	
$(function() {
	
	var name = $("#name"),
		email = $("#email"),
		address = $("#address"),
		contact_me = $("#contact_me"),
		allFields = $([]).add(name).add(email).add(address).add(contact_me),
		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 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: 350,
		modal: true,
		buttons: {
			'Download document': function() {
				var bValid = true;
				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");
									
				if (bValid) {
					
					download_id = $(".selectedDownload").attr("title");
					download_file = $(".selectedDownload").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,
						success: function() {
						   window.location.replace("http://grecian.digitminds.co.uk/downloads/" + download_file);
						}
					});

					$(this).dialog('close');
					$("a").removeClass("selectedDownload");
					
				}
				
			},
			Cancel: function() {
				$(this).dialog('close');
				$("a").removeClass("selectedDownload");
			}
		},
		close: function() {
			allFields.val('').removeClass('ui-state-error');
			$("a").removeClass("selectedDownload");
		}
	});
	
	
	$("#dialog_email").dialog({
						
		bgiframe: true,
		autoOpen: false,
		height: 350,
		modal: true,
		buttons: {
			'Send': function() {
				var bValid = true;
				allFields.removeClass('ui-state-error');

				bValid = bValid && checkLength(email_address,"email_address",6,80);
				bValid = bValid && checkRegexp(email_address,/^((([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");
									
				if (bValid) {
										
					var dataString = 'email_name='+ email_name.val() + '&email_address=' + email_address.val() + '&email_message=' + email_message.val();
					$.ajax({
						type: "GET",
						url: "/send-message.php",
						data: dataString,
						success: function() {
						   alert('Message sent');
						}
					});
					$(this).dialog('close');
				}
				
			},
			Cancel: function() {
				$(this).dialog('close');
			}
		},
		close: function() {
			allFields.val('').removeClass('ui-state-error');
		}
	});
	
	$('.download-document').click(function() {
		$('#dialog').dialog('open');
		$(this).addClass("selectedDownload");
		return false;			
	});


	$('.send_to_friend').click(function() {
		$('#dialog_email').dialog('open');
		return false;			
	});

});

