// JavaScript Document


/**
 * funzione per tinymce
 **/
function initTiny(element, type, siteUrl) {	
	switch(type) {
		
		case "small" : 
			tinyMCE.init({
				mode : "exact",
				elements : element,
				theme : "advanced",
				plugins : "safari,paste",
				theme_advanced_buttons1 : "bold,italic,link,unlink,formatselect,removeformat,pastetext,pasteword,|,code",
				theme_advanced_buttons2 : "",
				theme_advanced_buttons3 : "",		
				theme_advanced_toolbar_location : "top",
				theme_advanced_toolbar_align : "left",
				theme_advanced_statusbar_location : "bottom",
				theme_advanced_resize_horizontal : false,
				theme_advanced_resizing : true,
				extended_valid_elements : "a[name|href|title|class|style],img[class|src|alt|title|width|height|name],hr[class|style],span[class|style]"
			});
			break;
			
		case "full" :
			tinyMCE.init({
				mode : "exact",
				elements : element,
				theme : "advanced",
				plugins : "safari,paste,advlink,advimage,inlinepopups,media",
				theme_advanced_buttons1 : "bold,italic,underline,separator,justifyleft,justifycenter,justifyright,justifyfull,bullist,numlist,undo,redo,link,unlink,image,removeformat,anchor,code,pastetext,pasteword",
				theme_advanced_buttons2 : "",
				theme_advanced_buttons3 : "",
				theme_advanced_toolbar_location : "top",
				theme_advanced_toolbar_align : "left",
				theme_advanced_statusbar_location : "bottom",
				theme_advanced_resize_horizontal : false,
				theme_advanced_resizing : true,
				width : "640",
				height : "300",
				extended_valid_elements : "a[name|href|title|class|style],img[class|src|alt|title|width|height|name],hr[class|style],span[class|style]",
				apply_source_formatting : true,
				relative_urls : false,
				remove_script_host : false,
				document_base_url : siteUrl,
				language : "it",		
				convert_newlines_to_brs : false,
				force_br_newlines : false,
				force_p_newlines : false,
				file_browser_callback : "ajaxfilemanager"
			});	
			break;
	}
}

/**
 * funzione per l'ajax file manager
 **/
function ajaxfilemanager(field_name, url, type, win) {
	switch (type) {
		case "image":
			ajaxfilemanagerurl += "&type=img";
			break;
		case "media":
			ajaxfilemanagerurl += "&type=media";
			break;
		case "flash": //for older versions of tinymce
			ajaxfilemanagerurl += "&type=media";
			break;
		case "file":
			ajaxfilemanagerurl += "&type=files";
			break;
		default:
			return false;
	}
	
	var fileBrowserWindow = new Array();
	fileBrowserWindow["file"] = ajaxfilemanagerurl;
	fileBrowserWindow["title"] = "Ajax File Manager";
	fileBrowserWindow["width"] = "782";
	fileBrowserWindow["height"] = "440";
	fileBrowserWindow["resizable "] = "yes";
	fileBrowserWindow["inline"] = "yes";
	fileBrowserWindow["close_previous"] = "no";
	fileBrowserWindow["language"] = "it";
	tinyMCE.activeEditor.windowManager.open(fileBrowserWindow, {
		window : win,
		input : field_name
	});

	return false;
}	


/**
 * funzione per mostrare/nascondere la modifica dell'url della pagina in frontend
 * DA RIVEDER PER MOOTOOLS 1.2
 **/
function setSefUrl(sefEl, titleEl){
	if ($(sefEl)) {
		var seturlFx = new Fx.Slide(sefEl);
		seturlFx.hide();		
		var seturlBtn = (new Element('a').setProperty('href', '#').setText('Modifica URL titolo')).injectAfter($(titleEl));
		seturlBtn.addEvent('click', function(){
			seturlFx.toggle();	
		});	
	}
}



/**
 *	Funzione per la gestione del mouseover/mouseout
 *  sulla navbar (compatibilità con ie6)
 *	@require mootools core
 *	@version 1.0
 */
function loadNavbar(els)
{
    $$(els).addEvents({
        'mouseover': function(e){
            e = new Event(e).stop();
            this.addClass('hover');
        },
        'mouseout': function(e){
            e = new Event(e).stop();
            this.removeClass('hover');
        }
    }); 
}


/**
 * funzioni per il crop
 * versione 0.5
 * usare all'interno di window.addEvent('load') perchè occorre che le immagini siano caricate
 * DA RIVEDER PER MOOTOOLS 1.2
 **/
function doCrop(crop, path, xmin, ymin, xmax, ymax){
	if (crop) {
	
		// inizializzo gli array
		var thumbsImg = $$('img[id^=thumb]');
		var thumbsPrev = $$('img[id^=prev]');
		var thumbsCrop = [];
		
		// ciclo su tutte e thumbnails
		thumbsImg.each(function(el, i){
			
			// instanzio gli oggetti MooCrop
			thumbsCrop[i] = new MooCrop(el,{
				'maskColor' : '#ffffff',
				'maskOpacity' : '.8',
				'handleColor' : '#666666',
				'handleWidth' : '4px',
				'handleHeight' : '4px',
				'cropBorder' : '1px dashed #cccccc',
				'showHandles' : true,
				'min' : { 'width' : xmin, 'height' : ymin },
				'max' : { 'width' : xmax, 'height' : ymax }
			});
			
			// aggiungo gli eventi sulle miniature
			thumbsCrop[i].addEvent('onDblClk', function(img,crop,bound){
				var url = "inc/imgresize.inc.php";
				var file = el.getProperty('rel');
				new Ajax(url,{
					method : 'post',
					data : 'check=true&path='+ path +'&file='+ file +'&w='+ crop.width +'&h='+ crop.height +'&x='+ crop.left +'&y='+ crop.top +'&size=thumb',
					onSuccess : function(){
						thumbsPrev[i].setProperty('src', url +'?check=true&action=prev&path='+ path +'&file='+ file +'&w='+ crop.width +'&h='+ crop.height +'&x='+ crop.left +'&y='+ crop.top +'&size=thumb');
					}
				}).request();				
			});
		
		}); // end of thumbsImg.each()
	
	}
			
}

/**
 * funzioni per l'aggiunta di un record 
 * versione 0.1
 * DA RIVEDER PER MOOTOOLS 1.2
 **/
function addRecord(azione){
	var url = thisUrl;
    var btn = $('addRc');
	var form = $('form1');
	var rcSelect = $('categorie');

    switch (azione) {
		case 'new':		
			var rcTitle = form.rcTitle.value; 
			var dati = "check=true&azione=" + azione + "&rcTitle=" + encodeURIComponent(rcTitle);
			if (rcTitle != "") {
				new Ajax(url, {
					method: 'post',
					data: dati,
					onSuccess: function(){						
						var reply = this.response.text.split("\t");									
						switch (reply[0]) {
							case 'ok':
								var newRcOpt = new Element('option', {
									'value': reply[1],
									'selected': 'selected'
								});
								newRcOpt.appendText(rcTitle);
								rcSelect.appendChild(newRcOpt);
								form.rcTitle.value = "";
								btn.setHTML('apri');
								btn.setProperty("onclick", "addRecord('open')");
								newRc.slideOut();
							break;
							
							case 'errore':
								alert(reply[1]);
							break;
						}	
					}
				}).request();
				
			} else {
				alert('Inserire un titolo.');
			}
		break;
		
		case 'del':		
			var rcId = rcSelect.value;
			var dati = "check=true&azione=" + azione + "&rcId=" + encodeURIComponent(rcId);
			if (rcId != "" && rcId != 0 && confirm("Eliminare la categoria ?")) {
				new Ajax(url, {
					method: 'post',
					data: dati,
					onSuccess: function(){						
						var reply = this.response.text.split("\t");									
						switch (reply[0]) {
							case 'ok':
								for (i=0; i<rcSelect.options.length; i++) {
									if (rcSelect.options[i].value == rcId) {
										rcSelect.removeChild(rcSelect.options[i]);
									}
								}
								btn.setHTML('apri');
								btn.setProperty("onclick", "addRecord('open')");
								newRc.slideOut();
							break;
							
							case 'errore':
								alert(reply[1]);
							break;
						}	
					}
				}).request();
				
			} 
		break;
		
		case 'open':
			form.rcTitle.value = "";
			newRc.slideIn();
			btn.setHTML('chiudi');
			btn.setProperty("onclick", "addRecord('close')");
			break;
    
		case 'close':
			newRc.slideOut();
			form.rcTitle.value = "";
			btn.setHTML('apri');
			btn.setProperty("onclick", "addRecord('open')");
		break;	
		
		default:
		break;
		
    }
	
}  



