$(function(){ 
    $(".dropdown").mouseover(function() {
        $(this).find("ul").css("display","block");    
    }).mouseout(function() {
        $(this).find("ul").css("display","none");     
    });    
    $('#slider').orbit();    
    $("button").mouseover(function() {
        $(this).css("background-position","0px -29px");    
    }).mouseout(function() {
        $(this).css("background-position","0px 0px");    
    });
    $(".gal").mouseover(function() {
        $(this).css("background","#d47f08").css("border-color","#d47f08");     
        $(this).find("div").css("border-color","#d47f08").css("background","#d47f08");
    }).mouseout(function() {
        $(this).css("background","#3153A2").css("border-color","#3153A2");     
        $(this).find("div").css("border-color","#3153A2").css("background","#3153A2");
    });
    $("#password").focus();
    $(".img").draggable({
			cancel: "a.delete", // clicking an icon won't initiate dragging
			revert: "invalid", // when not dropped, the item will revert back to its initial position
			helper: "clone",
			cursor: "move",
            start: function(event, ui) {
                
            }

    });
    $(".gal a").fancybox();
    $(".cont").droppable({
			accept: ".img",
			activeClass: "custom-state-active",
			drop: function( event, ui ) {
			     redraw($(this),ui.draggable);	
			}
    });
    function redraw($drag,$item) {
        $item.parent().append($drag.find(".img"));
        $drag.empty();
        $drag.append($item);    
    }  
    $(".news>div").each(function() {
        var text = $(this).html();
        if(text.length > 100) {
		  text = text.substring(0, 200);
		  $(this).html(text+"...");
	   } 
    });
    $(".news>div").mouseover(function() {
       $(this).css("background-color","#FFFF99"); 
    }).mouseout(function() {
       $(this).css("background-color","#fff");  
    }).click(function() {
        var href=$(this).attr("rel");
        window.location.href='news#'+href;    
    })
    $('#testo').keypress(function() {
	   var text = $(this).val();
	   if(text.length > 200) {
		  text = text.substring(0, 200);
		  $(this).val(text);
	   }
       $("#rest").html(200-text.length);
    });
});
function saveGallery(ap) {
    var str="";
    $(".img").each(function() {
        str+=$(this).attr("rel")+'|';
    });
    str+="";
    $.ajax({
      type: "POST",
      url: "index.php",
      data: "ajax=1&class=helper&method=saveGallery&params="+ap+";"+str,
      success: function(data) {
        alert(data); 
      }
    });
}
function sendMail() {
    var nome=$("#nome").val();
    var telefono=$("#telefono").val();
    var email=$("#email").val();
    var testo=$("#testo").val();
    if(nome=='') { alert('Non hai inserito il tuo nome!'); return false; }
    if(telefono=='') { alert('Non hai inserito un numero di telefono!'); return false; }
    if(email=='' | !checkEmail(email)) { alert('Email inserita non valida!'); return false; }
    if(testo=='') { alert('Non hai inserito alcun messaggio!'); return false; }
    $.ajax({
      type: "POST",
      url: "index.php",
      data: "ajax=1&class=helper&method=mailAdmin&params=nome="+nome+";telefono="+telefono+";email="+email+";testo="+testo,
      success: function(data) {
        alert(data);
        $("#preventivo").html("Messaggio inviato con successo. Verrai ricontattato al più presto."); 
      }
    });
}

function checkEmail(email) {
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!filter.test(email)) {
        return false;
    } else {
        return true;
    }
}

tinyMCE.init({
			mode : "exact",
			elements : "tinymce",
			theme : "advanced",
			plugins : "advimage,advlink,media,contextmenu,autosave",
			theme_advanced_buttons1_add : "fontsizeselect",
			theme_advanced_buttons2_add : "separator,forecolor,backcolor",
			theme_advanced_buttons3_add : "media",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			extended_valid_elements : "hr[class|width|size|noshade]",
			file_browser_callback : "ajaxfilemanager",
			paste_use_dialog : false,
			theme_advanced_resizing : true,
			theme_advanced_resize_horizontal : true,
			apply_source_formatting : true,
			force_br_newlines : true,
			force_p_newlines : false,	
			relative_urls : true,
            document_base_url : "/impretecno/",
            language: "it",
            content_css : "css/tinymce_styles.css",
		});

		function ajaxfilemanager(field_name, url, type, win) {
			var ajaxfilemanagerurl = "../../../../js/tiny_mce/plugins/ajaxfilemanager/ajaxfilemanager.php";
			var view = 'detail';
			switch (type) {
				case "image":
				view = 'thumbnail';
					break;
				case "media":
					break;
				case "flash": 
					break;
				case "file":
					break;
				default:
					return false;
			}
            tinyMCE.activeEditor.windowManager.open({
                url: "../../../../js/tiny_mce/plugins/ajaxfilemanager/ajaxfilemanager.php?view=" + view,
                width: 782,
                height: 440,
                inline : "yes",
                close_previous : "no"
            },{
                window : win,
                input : field_name
            });
            
/*            return false;			
			var fileBrowserWindow = new Array();
			fileBrowserWindow["file"] = ajaxfilemanagerurl;
			fileBrowserWindow["title"] = "Ajax File Manager";
			fileBrowserWindow["width"] = "782";
			fileBrowserWindow["height"] = "440";
			fileBrowserWindow["close_previous"] = "no";
			tinyMCE.openWindow(fileBrowserWindow, {
			  window : win,
			  input : field_name,
			  resizable : "yes",
			  inline : "yes",
			  editor_id : tinyMCE.getWindowArg("editor_id")
			});
			
			return false;*/
		}






















