google.setOnLoadCallback(function(){
	if($.browser.safari) $('#banners img').css('margin-right', '4px');

	$('#menu img').hover(function(){
		$('#menu-hover').attr('class', 'menu-hover-'+$(this).attr('class'));
	},function(){
		$('#menu-hover').attr('class', '');
	});

	$('#footer img').preload({
		find: '/base/',
		replace: '/hover/'
	});
	$('#footer img').hover(function(){
		this.src = this.src.replace('\/base\/','/hover/');
	},function(){
		this.src = this.src.replace('\/hover\/','/base/');
	});
	
	$.getJSON('/websites/index.txt', function(data) {
		data.sort(function() {return 0.5 - Math.random()});
		var textToAppend = [];
		$.each(data, function(i, img) {
			var url = img.substring(0, (img.length-4));
//			textToAppend.push('<span><a href="http://www.'+ url +'?iframe=true&amp;width=100%&amp;height=100%" rel="modal"><img src="http://websites.cubicdesign.it/'+ img +'" width="560" height="170" border="0" /></a> <a href="http://www.'+ url +'?iframe=true&amp;width=100%&amp;height=100%" rel="modal">http://www.'+ url +'</a></span>');
			textToAppend.push('<span><a href="http://www.'+ url +'?iframe=true&amp;width=100%&amp;height=100%" rel="modal"><img src="http://websites.cubicdesign.it/'+ img +'" width="560" height="170" border="0" /></a> <a href="http://www.'+ url +'?iframe=true&amp;width=100%&amp;height=100%" rel="modal">http://www.'+ url +'</a></span>');
		});
		$('#website').append(textToAppend.join(''));
		$('#website').cycle();
		$("a[rel^='modal']").prettyPhoto({theme: 'dark_rounded', modal: 'true'});
	});

	$('#dialog-contatto').dialog({
		bgiframe: true,
		autoOpen: false,
		height: 490,
		width: 350,
		modal: true,
		buttons: {
			'Invia richiesta': function() {
				$("#contatto").submit();
			},
			Annulla: function() {
				$(this).dialog('close');
			}
		}
	});


	$('#indirizzo').click(function() {
/*
		$.get('dialog-contatto.html', function(data){
			$('body').prepend(data);
*/
			$("#contatto").validate({
				errorClass: 'ui-state-error',
				
				// JDR: rules/messages are for the validation
				rules: {
					nome: "required",
					email: {
						required: true,
						email: true
					}
				},
				
				// JDR: our form submit
				submitHandler: function(form) {
					$('#dialog-contatto').dialog('close');
					$('#dialog-contatto-inviato').dialog('open');
				 }
			}); 
			

			$('#dialog-contatto').dialog('open');
			
			$.get('dialog-contatto-inviato.html', function(data){
				$('body').prepend(data);
				$('#dialog-contatto-inviato').dialog({
					bgiframe: true,
					autoOpen: false,
					modal: true,
					buttons: {
						Ok: function() {
							$(this).dialog('close');
						}
					}
				});
			});
/*		});*/
		
		return false;
	});
});
