var path_assoluto;
function init(){
	menu();	
	my_accordion();
	/* VIRTUAL EXPLORE 
		delay=9000;
		virtual_explore(delay);
	-------------------------*/
	//active_gallery();
	$("form").submit(function (){return get_form_validation(this);}); //"#"+$(this).attr("id")
	$("#radio_group_1 :radio").click(function(){if(this.checked){$('#radio_group_1_val').val(this.id);}else{$('#radio_group_1_val').val("");}});
	$("#check_group_1 :checkbox").click(function(){if(this.checked){$('#check_group_1_val').val($('#check_group_1_val').val()+'['+this.id+']');}else{$('#check_group_1_val').val($('#check_group_1_val').val().replace('['+this.id+']',''));}});
	ws_json_form.init("form.post_ajax");
	if ($("#blocc_news_hp").size()>0){
		$("#blocc_news_hp").flash(
			{ src: 'mito.swf',
			  wmode: 'transparent',
			  width: 257,
			  height: 283 },
			{ version: 9 }
		);
	}
//	replace_flash_objects();	
}

/* Funzione per Menù 
- - - - - - - - - - - - - - - - - - - - */
function menu(){
	$("#menu_vert_3lev a.active").parent().parent().addClass("on");
	$("#menu_vert_3lev ul li ul").not(".on").hide();
	$("#menu_vert_3lev ul li a").click(function(){
		if($(this).attr("href")=="#"){
			if ($(this).next().attr("class") == "on") {
//				$(this).next().removeClass("on").slideUp("fast");
				// scriverlo due volte per trovare gli elementi successivi con classe .on
				$(this).parent().children().not("a").removeClass("on").slideUp("fast"); 
			} else {
				$(this).parent().parent().find("ul.on").removeClass("on").slideUp("fast");
				$(this).parent().children().not("a").addClass("on").slideDown("normal");
//				$(this).next().addClass("on").slideDown("fast");
//				$(this).next().next().addClass("on").slideDown("fast"); // serve per aprire l'UL fratello
			}
			return false;
		}
	});
	apriMenu();
	$("img[@src='']").src='/img/trasp.gif';

	
}
function apriMenu(){
	$("#menu_vert_3lev a.on").each(function(){
		var el=this;
		var conta=0;
		while(el !== null && conta<30){
			el=$(el).parent();
			if(el[0].tagName=="UL"){
//				$(el).show();
//				$(el).addClass("on");
				$(el).parent().children().not("a").show();
				$(el).parent().children().addClass("on");
				
			}else if (el[0].tagName=="LI"){
				conta++;
			}else if (el[0].tagName=="DIV"){
				el=null;
			}else{
				conta++;
			}
		}
	});
}
function active_gallery(){
	if (!$("#TB_Window").length && typeof(tb_init)=='function'){
//		imgLoader = new Image();// preload image
		//imgLoader.src = "/img/trasp.gif";
//		imgLoader.src = tb_pathToImage;
		tb_init('a.thickbox, area.thickbox, input.thickbox');//pass where to apply thickbox
//		$("#TB_Image").load(function(){ $("#TB_Load").hide();alert("caricata"); });
		
	}
}

function my_accordion(){
	$("h2 a[@rel='accordion']").click(function(){
		var thed=$(this).parent().next();
		$(".hidden:visible").not($(thed)).slideUp("fast");
		$(this).parent().next().slideDown("fast");
		return false;
	});
}

/* VIRTUAL EXPLORE */

if(typeof(Array.indexof)!=="function"){
	Array.prototype.indexOf = function(f, s) {
		if (typeof s == 'undefined') s = 0;
		for (var i = s; i < this.length; i++) {
			if (f === this[i]) return i;
		}
		return -1;
	}	
}

var ve_pages = new Array();
function virtual_explore(delay){
//	return;
	if (window.location.href.indexOf("&ve=1")>0){
		document.body.innerHTML+="<a href='"+window.location.href.replace('&ve=1','&ve=0')+"' style='position:fixed;top:0px;left:404px;z-index:100;padding:12px;border:1px solid #ff7777;background-color:#770000;color:#fff;'>stop virtual explore</a>";
		
		$("#menu_vert_3lev a").each(
					function(){
						if(this.href !=="#" && this.href.indexOf("?")>1 && this.href.indexOf("http://")<1)ve_pages.push(this.href+"&ve=1");
					});
		
		var i=0;
		//alert(window.location.href);
		i=ve_pages.indexOf(window.location.href);
		setTimeout(function(delay){if (i+1<ve_pages.length){window.location.href=ve_pages[i+1];}else{window.location.href=ve_pages[0];}},delay);
	} else if (window.location.href.indexOf("&ve=0")>0){
		document.body.innerHTML+="<a href='"+window.location.href.replace('&ve=0','&ve=1')+"' style='position:fixed;top:0px;left:404px;z-index:100;padding:12px;border:1px solid #ff7777;background-color:#770000;color:#fff;'>start virtual explore</a>";
	}
}

/* GALLERY NOTHICK
-------------------------------------------*/
function gallery_nothick(){
	var tg, gscreen, current,top, playing;
	tg = $("a.nothickbox");
	top = $(tg).size()-1;
	current=0;
	playing=0;
	var partenza = false;
	gscreen = $("img.gallery_screen");
	$(tg).click(function(){
						 	$(gscreen).attr("src",this.href); 
							current=$(tg).index(this); 
							$(tg).removeClass("current_img");
							$(this).addClass("current_img");
							return false;
							});
	$("a.gprevious").click(function(){
				var i, ni;
				$(tg).removeClass("current_img");
				if (top<0 ){return false;}
				if(current>0){
					i=current-1;
				}else{
					i=top;
				}
				current = i;
				ni=$(tg).eq(i).attr("href");
				if(ni!=="undefined"){
					$(gscreen).attr("src",ni);
					$(tg).eq(i).addClass("current_img");
				}
				return false;
			});
	$("a.gnext").click(function(){
				var ni,i;
				$(tg).removeClass("current_img");
				if (top<0 ){return false;}
				if(current<top){
					i=current+1;
				}else{
					i=0;
				}
				current = i;
				ni=$(tg).eq(i).attr("href");
				if(ni!=="undefined"){
					$(gscreen).attr("src",ni);
					$(tg).eq(i).addClass("current_img");
				}
				return false;
			});
	$("body").append("<img src='" + $(tg).eq(0).attr("href") + "' alt='' class='hidden_loader' style='display:none' /> ");
//	$("body img.hidden_loader").eq(0).load(function(){$(gscreen).attr("src",$(this).attr("src")); playNext();});
	nothick_playNext = function(){
		if(playing==1){
			if (current<=top){
					//$(gscreen).attr("src",$(tg).eq(current));
					$(tg).removeClass("current_img");
					current++;
					if (current>top){current=0;}
					$("body img.hidden_loader").eq(0).attr("src",$(tg).eq(current).attr("href"));
					$(tg).eq(current).addClass("current_img");
			}

			partenza=window.setTimeout(nothick_playNext,3000);
		}else{
			partenza=false;
		}
	}
	nothick_play=function(){
			playing=1;
			$("body img.hidden_loader").eq(0).load(activePlay);
			$(".nothick_play").removeClass("on");
			$(".nothick_stop").addClass("on");
			nothick_playNext();
			return false;
		}
	nothick_stop=function(){
			playing = 0;
			partenza = false;
			$("body img.hidden_loader").eq(0).unbind("load");
			$(".nothick_play").addClass("on");
			$(".nothick_stop").removeClass("on");
			return false;		
		}
	activePlay = function(){
		var ni=$(this).attr("src");
		$(gscreen).fadeOut("fast",function(){$(gscreen).attr("src",ni)}).fadeIn("fast");

	}
	$(gscreen).click(function(){if (playing==0){nothick_play();}else{nothick_stop()}});
	$(".nothick_play").click(nothick_play);
	$(".nothick_stop").click(nothick_stop);
}


/* CONVALIDA FORM
-------------------------------------------*/
function ws_validate(){}
ws_validate.prototype.isEmail = function (what)
{
   var i=new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
   if(!i.test(what)) { return false}   
   return true;
}
ws_validate.prototype.isDate = function (what)
{
   var i=new RegExp("^([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{4})$");
   if(i.test(what))
   {
	datePart=what.split("/");
	giorno=(datePart[0]>=1) && (datePart[0]<=31);
	mese=(datePart[1]>=1) && (datePart[1]<=12);
	anno=(datePart[2]>=1900) && (datePart[2]<=9999);
	if(!giorno || !mese || !anno) { return false; }
   }else{
	return false;
   }
   return true;
}

ws_validate.prototype.isTelFax = function(what)
{
//   var i=new RegExp("([0-9]{2,})\/([0-9]{5,})");
   var i=new RegExp("([0-9]{5,})");
   if(!i.test(what)) { return false;}   
   return true;
}

ws_validate.prototype.notZero = function(what)
{
   if(what=="0") { return false;}   	
   return true;
}
ws_validate.prototype.field = function (what, type){
	try{
		switch (type) {
		  case "isEmail":
			return this.isEmail($(what).val());
			break; 
		  case "isDate":
			return this.isDate($(what).val());
			break; 
		  case "isTelFax":
			return this.isTelFax($(what).val());
			break; 
		  case "isChecked":
		  	return $(what).is(":checked");
			break; 
		  case "notZero":
			return this.notZero($(what).val());
			break; 
		  case "isText":
			return ($.trim($(what).val()).length>0);
			break; 
		  case "isNumeric":
			return (!isNaN($(what).val()) && ($.trim($(what).val()).length>0));
			break; 
		  default: 	
			return ($(what).val().length>0);
		}
		
	}
	
	catch (e){
		alert(e);
	}
}
var validate = new ws_validate;

function get_form_validation(frm_id){
	var oval,valid,validation_string;
	oval=$(frm_id)[0]._validation;
	for (nc in oval){
		if( validate.field("#"+nc,oval[nc])==false ){
			$("#"+nc).parent().addClass("bg_error");
			valid=false;
		}else{
			$("#"+nc).parent().removeClass("bg_error");
		}
	}
	$(".bg_error input, .bg_error select, .bg_error textarea").eq(0).focus();
	$(frm_id)[0].valid = valid;
	return valid;
}
/* REPLACE FLASH OBJECTS*/
function replace_flash_objects()
{
	$(".replace_flash_objects").each(function ()
		{
			var f, re,p;
			f = String($(this).children("noscript").html()).replace("&lt;","<").replace("&gt;",">");

			while(f.indexOf("&lt;")>=0){
				f = f.replace("&lt;","<");
			}
			while(f.indexOf("&gt;")>=0){
				f = f.replace("&gt;",">");
			}
/*
			p="<[^>]{0,1}object[^>]*>|<[^>]{0,1}param[^>]*>";
			re = new RegExp();
			re.pattern=p;
			f.replace(re,"");
			re.pattern="&lt;";
			f.replace(re,"<");
			re.pattern="&gt;";
			f.replace(re,">");
*/
			if(f.length<10){f = $(this).children(".embed").html()}
//			$(this).prepend(f);
			this.innerHTML=f;
		}
	 );
}

/* JSON FORM */
var ws_json_form = {
		theForm: "" ,
		init: function(p){
			try{
			$("body").after("<iframe name=\"ws_ifr_upload_file\" id=\"ws_ifr_upload_file\" style=\"display:none\" rows=\"1\" cols=\"1\" src=\"\"></iframe>");
			$(p).each(function(){this.target="ws_ifr_upload_file";});
			$(p).submit(ws_json_form.ajax_submit);
			}
			catch(e){alert(e);}
		},
		ajax_submit: function(){
			var post_data,ifr_target;
			try{
				ws_json_form.the_form = this;
				if(ws_json_form.the_form.valid==false){return false;}
				if($(this).attr("enctype")=="multipart/form-data"){
				/* upload files BEGIN */
				ifr_target = $("#ws_ifr_upload_file");
				if ($(ifr_target).size()==0){
					$("body").after("<iframe name=\"ws_ifr_upload_file\" id=\"ws_ifr_upload_file\" src=\"\"></iframe>");
				}
				this.target="ws_ifr_upload_file";
				$("#ws_ifr_upload_file").load(function(){
					   var d,s;
					   s="success";
					   d=eval("(" + frames['ws_ifr_upload_file'].document.body.innerHTML +")" );
					   ws_json_form.callback_json(d,s);
					});
				return true;
				/* upload files END*/
				}else{
					post_data = $(this).serializeArray();
					$.post($(this).attr("action"), post_data, ws_json_form.callback_json, "json");
				}
			}
			catch(e){
				self.status = (e);
			}
			return false;
		},
		callback_json:	function(vdata, textStatus){
			var d;
			try{
				d = vdata;
				if ($(ws_json_form.the_form).children("div.response").size()==0 ){$(ws_json_form.the_form).prepend("<div class=\"response\"></div>")}

				if(d.status=="success"){
					$(ws_json_form.the_form).children("div.response").html(d.data);
				}else{
					$(ws_json_form.the_form).children("div.response").html(d.error);
				}
				if(parseInt($(ws_json_form.the_form).height())>parseInt($(ws_json_form.the_form).children("div.response:eq(0)").height())){
					$(ws_json_form.the_form).children("div.response").css({"height": $(ws_json_form.the_form).height()+"px"});
				}
				$(ws_json_form.the_form).children("fieldset").fadeOut("fast",function(){$(ws_json_form.the_form).children("div.response").fadeIn("fast");});
				$(ws_json_form.the_form).children("div.response").hide();
			}
			catch(e){
				self.status = (e);
				alert(e);
			}
		},
		back_and_complete: function (chi){
			$(chi).parent().fadeOut('slow',function(){
				$(chi).parent().parent().children('fieldset').fadeIn('slow');
				return false;
			});
		}
	}

/* LANCIA INIT */
if(typeof($)=="function"){
	path_assoluto = location.href;
	$(document).ready (init);

}


/* plugin scrolling
- - - - - - - - - - - - - - - - - - - - */
(function($) {
  $.fn.scrollIt = function(options) {
		var opts = $.extend({}, $.fn.scrollIt.defaults, options);
		return this.each(function() {
			$this = $(this);
						
			var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
			
			var elWidth = $this.find(o.el).width() + o.extra;
			var elTot = $this.find(o.el).size();
			var current = 0;
			var $scroll = $this.find(".scroll");
			if(elTot==0){$(this).hide(); return false;} 
			var $wrap = $this.find(".wrap");
			var $sx = $this.find(".scroll_sx");
			var $dx = $this.find(".scroll_dx");

			$wrap.css({width:(elWidth*(elTot )) + "px"}); // + 2
			

			//if (($wrap.width()+o.extra) < $scroll.width()) {
			if (elTot <= o.elVis ) {
				// eliminare scroll e padding
/*
				$sx.hide();
				$dx.hide();
				*/
				$sx.click(function(){return false;});
				$dx.click(function(){return false;});
				$sx.children('a').css({'cursor':'not-allowed'});
				$dx.children('a').css({'cursor':'not-allowed'});
				$wrap.width($this.width());
				$scroll.width($this.width() - $sx.width() - $dx.width() );
				$wrap.css({paddingLeft:"0px"});
			} else {
			
				
				function muovi(current){
					$scroll.animate({scrollLeft:(o.elStep*elWidth*(current))+"px"},400);
				}
				
				$sx.click(function(){
					if (current != 0) {
						current = current-1;
					}
					muovi(current);
					return false;
				});
				
				$dx.click(function(){
//					if (current < (elTot-o.elVis-(o.elStep/o.elVis))) {
					if ((current * o.elStep) < (elTot-o.elVis)) {
						current=current+1;
					}
					muovi(current);
					return false;
				});
				
			}
			var curr_index = $this.find(o.el).index($this.find(o.el + ".current"));
			if(curr_index>0){
				$($scroll).each(function(){
						current =  Math.floor(curr_index / o.elStep);
						this.scrollLeft = (current * o.elStep * elWidth);  
					});
			}
		});
  };
	
	// default
	$.fn.scrollIt.defaults = {
		elVis : 2,
		elStep : 1,
		el : "div",
		extra : 0
  };
})(jQuery);


$(document).ready(function(){
	//$("#blocc_hp dl dt").addClass("png_bg");
	$("#contenuto dl:eq(2)").css("margin","0px");
	$("#contenuto dl:eq(5)").css("margin","0px");
	$("#contenuto dl:eq(8)").css("margin","0px");	
	$("#blocc_news_hp dl").addClass("png_bg");
	$("#blocc_hp_it dl:eq(0)").css("background","url(../img/sf_blocc_sicurezza02.jpg) no-repeat top left #3d3b3b");
	$("#blocc_hp_it dl:eq(1)").css("background","url(../img/sf_blocc_edilizia02.jpg) no-repeat top left #3d3b3b");
	$("#blocc_hp_it dl:eq(2)").css("background","url(../img/sf_blocc_arredamento02.jpg) no-repeat top left #3d3b3b");
	$("#blocc_hp_it dl:eq(3)").css("background","url(../img/sf_blocc_contatti02.jpg) no-repeat top left #3d3b3b");
	$("#blocc_hp_it dl:eq(3)").css("padding-left","60px");
	$("#blocc_hp_it dl:eq(3)").css("width","149px");
	$("#blocc_hp_it dt").addClass("png_bg");
	$("#blocc_hp_en dl:eq(0)").css("background","url(../img/sf_blocc_sicurezza02_en.jpg) no-repeat top left #3d3b3b");
	$("#blocc_hp_en dl:eq(1)").css("background","url(../img/sf_blocc_edilizia02_en.jpg) no-repeat top left #3d3b3b");
	$("#blocc_hp_en dl:eq(2)").css("background","url(../img/sf_blocc_arredamento02_en.jpg) no-repeat top left #3d3b3b");
	$("#blocc_hp_en dl:eq(3)").css("background","url(../img/sf_blocc_contatti02_en.jpg) no-repeat top left #3d3b3b");
	$("#blocc_hp_en dl:eq(3)").css("padding-left","60px");
	$("#blocc_hp_en dl:eq(3)").css("width","149px");
	$("#blocc_hp_en dt").addClass("png_bg");
	//$("#blocc_hp dl dt:eq(0)").css("background","url(../img/tit_blocc_sicurezza_hp.png) no-repeat top left");
	//$("#blocc_hp dl dt:eq(1)").css("background","url(../img/tit_blocc_edilizia_hp.png) no-repeat top left");
	//$("#blocc_hp dl dt:eq(2)").css("background","url(../img/tit_blocc_arredamento_hp.png) no-repeat top left");
	//$("#blocc_hp dl dt:eq(3)").css("background","url(../img/tit_blocc_contatti_hp.png) no-repeat top left");
	var extra_height = 20;
	if($.browser.msie){extra_height = 0;}
	ab = { left:0, top: $(window).height() - $('.always_bottom').height() - extra_height}; // $(document).height() // document.clientHeight //parseInt(window.innerHeight)
	
	$('.always_bottom').css({'position':'absolute','top': ab.top + 'px', 'left': ab.left + 'px'});
	//$("#galleria_img div.scroll").scrollable({vertical:false, size:4, prev:'.prev', next:'.next', items:'.wrap'});
	$("#galleria_img").scrollIt({elStep:3, el:".dt_image", extra:11, elVis:6});
	$("#galleria_img2").scrollIt({elStep:3, el:"a.thickbox", extra:11, elVis:6});
	$("#float_form_container").remove(); // rimuovere questa riga per PROPONI UN'IDEA
	floating_form();
});

function floating_form(){
		$("a#float_form").click(function(){
			if($("#floating_form").size()>0){
				$("#floating_form").show("normal");
			}else{
				$("#float_form_container").prepend("<div id='floating_form'><div><a href='#' title='close' class='floating_form_close'>chiudi</a></div><iframe src='proponi_idea.asp' scrolling='no' frameborder='0'></iframe></div>");

				$("#floating_form").slideDown("normal").animate({left:"189px", top:"128px", width:"313px",opacity: "0.6"},"normal").animate({opacity: 1}, 'fast');
				$(".floating_form_close").click(function(){$("#floating_form").animate({top:"189px", left:"0px", width:"150px", opacity: 0},"normal").slideUp("normal", function(){$("#floating_form").remove();});  return false;});
			}
			return false;
		});
}

function thickbox_show_url(title, url, group){
	tb_show(title, url, group);
	return false;
	}