/******************************************************************************************************************/
// TABS HANDLE
/******************************************************************************************************************/

function handleTabs(){
	var sfEls = document.getElementById("menu").getElementsByTagName("LI");
	for (var i=0; i < sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}

/******************************************************************************************************************/
// On document load...
/******************************************************************************************************************/

$(function(){

	handleTabs();

	var button4 = $('#button4'), interval;
	if ($("#button4").length > 0) {
		new AjaxUpload(button4,{
			action: '/wp-content/avatars/upload_avatar.php',
			name: 'myfile',
			data: { prefix : Math.floor(Math.random()*101) },
			onSubmit : function(file, ext){
				// change button text, when user selects file			
				button4.text('Caricando');
				
				// If you want to allow uploading only 1 file at time,
				// you can disable upload button
				this.disable();
				
				// Uploding -> Uploading. -> Uploading...
				interval = window.setInterval(function(){
					var text = button4.text();
					if (text.length < 13){
						button4.text(text + '.');					
					} else {
						button4.text('Carica');				
					}
				}, 200);
			},
			onComplete: function(file, response){
				button4.text('Carica');
				window.clearInterval(interval);
				this.enable();
				$('#avatar_preview').html('<img id="preview" src="/wp-content/uploads/thumbs/phpThumb.php?src=../../avatars/tmp/' + response + '" /><input type="hidden" name="avatar" value="' + response + '" />');
			}
		});
	}

	var check1 = $('#editprofile #frm-input'), interval;
	if (check1.length > 0) {
		// validate signup form on keyup and submit
		check1.validate({
			errorElement: "small",
			rules: {
				email: {
					required: true,
					email: true
				},
				aaaa: {
					checkData: true
				},
				sesso: {
					required: true
				},
				categoria: {
					required: true
				},
				url: {
					url: true
				},
				facebook: {
					url: true
				},
				youtube: {
					url: true
				},
				twitter: {
					url: true
				},
				blog: {
					url: true
				},
				oggetto: {
					required: true
				},
				pass1: {
					minlength: 5
				},
				pass2: {
					equalTo: "#user_pass"
				},
				messaggio: {
					required: true
				}
			},
			messages: {
				email: {
					required: "Inserisci un indirizzo email",
					email: "Email non valida"
				},
				sesso: {
					required: "Specifica sesso"
				},
				categoria: {
					required: "Seleziona categoria"
				},
				oggetto: {
					required: "Scrivi un oggetto descrittivo"
				},
				pass1: {
					minlength: "La password deve essere di almeno 5 caratteri alfanumerici"
				},
				pass2: {
					equalTo: "La password riscritta non corrisponde a quella inserita"
				},
				url: {
					url: "Url non valido"
				},
				facebook: {
					url: "Url non valido"
				},
				youtube: {
					url: "Url non valido"
				},
				twitter: {
					url: "Url non valido"
				},
				blog: {
					url: "Url non valido"
				},
				messaggio: {
					required: "Scrivi un messaggio"
				},
				aaaa: {
					checkData: "Inserisci la tua data di nascita"
				}
			}
		});
	}

	var check2 = $('#frm-login'), interval;
	if (check2.length > 0) {
		$("#frm-login").validate({
			errorElement: "small",
			rules: {
				log: "required",
				pwd: "required"
			},
			messages: {
				log: "Inserisci il tuo username",
				pwd: "Inserisci la tua password"
			}
		});
	}

	$.validator.addMethod("checkData", function(value, element) {
		return ($('#mm').is(":filled") && $('#gg').is(":filled") && $('#aaaa').is(":filled"));
	}, "Inserisci la tua data di nascita");

	var check3 = $('#description'), interval;
	if (check3.length > 0) {
		$('#description').wysiwyg({
		    controls: {
		      subscript : { visible : false },
		      superscript     : { visible : false },
		      
		      separator00 : { visible : true },
		      
		      undo : { visible : false },
		      redo : { visible : false },
			  removeFormat : { visible : false }
		      
		    }
		  });
	}

	var check6 = $('#testo'), interval;
	if (check6.length > 0) {
		$('#testo').wysiwyg({
			controls : {
			separator01 : { visible : false },
			separator02 : { visible : false },
			separator03 : { visible : false },
			separator04 : { visible : false },
			separator05 : { visible : false },
			separator06 : { visible : false },
			separator00 : { visible : false },
			insertOrderedList : { visible : true },
			insertUnorderedList : { visible : true },
			indent : { visible: false },
			outdent : { visible: false },
			undo: { visible : true },
			redo: { visible : true },
			justifyLeft: { visible : false },
			justifyCenter: { visible : true },
			justifyRight: { visible : false },
			justifyFull: { visible : true },
			strikeThrough: { visibile: false },
			underline: { visibile: true },
			createLink: { visible: true},
			insertImage: { visible: true},
			subscript: { visible : false },
			superscript: { visible : false },
			h1: { visible : false },
			h2: { visible : false },
			h3: { visible : false },
			h1mozilla: { visible : false },
			h2mozilla: { visible : false },
			h3mozilla: { visible : false }
			}
		  });
	}

	var check7 = $('#frm-segnala-articolo'), interval;
	if (check7.length > 0) {

		$.validator.addMethod("checkSelect", function(value, element) {
			if ($('#cat option:selected').val()==0)
				return false;
			else
				return true;
		}, "Assegna una categoria all&#39;articolo");


		$("#frm-segnala-articolo").validate({
			errorElement: "small",
			errorPlacement: function(error, element) {
				if ( element.is(":checkbox") )
					error.appendTo ( element.parent() );
				else
					error.insertAfter( element );
			},
			rules: {
				titolo: {
					required: true,
					minlength: 5
				},
				cat: {
					checkSelect: true
				},
				testo: {
					required: true,
					minlength: 1000
				},
				check1: {
					required: true
				}
			},
			messages: {
				titolo: {
					required: "Assegna un titolo all&#39;articolo",
					minlength: "La lunghezza minima &egrave; di 5 caratteri"
				},
				cat: {
					checkSelect: "Assegna una categoria all&#39;articolo"
				},
				testo: {
					required: "Scrivi il testo l&#39;articolo",
					minlength: "La lunghezza minima &egrave; di 1000 caratteri"
				},
				check1: {
					required: "Accetta le condizioni di utilizzo"
				}
			}
		});
	}
});

function Cerca() {
	testo = $('#s').val();
	testo = testo.replace(/ /g, "+");
	parole2 = testo.replace(/'/g, "");
	page = "/cerca/" + testo;
	location=(page);
	return false;
}
