var path_assoluto;
if(typeof($)=="function"){
	path_assoluto = location.href;
	$(document).ready (init_application);

}
function init_application(){
	if (String(path_assoluto+"&").toLowerCase().indexOf("co_id=18&") != -1) {
				$("div.allegati").hide();
				$("h2").contains("Brochures").hide();
	}
	$("form").append("<input type='hidden' name='jkspm' value='kspm' />");
	/*if (String(path_assoluto+"&").toLowerCase().indexOf("co_id=822&") != -1) {
				//$("div.allegati").hide();
				$("h2").contains("Brochures").hide();
	}
	
	if (String(path_assoluto+"&").toLowerCase().indexOf("co_id=823&") != -1) {
				//$("div.allegati").hide();
				$("h2").contains("Brochures").hide();
	}
	*/
	if(location.href.indexOf('aetnagroupvostok.ru')>0){
	}else{
		$("#menu_01 ul li a").each(function(){
				var h, ah,p, re;
				// Per WP il dominio deve rimanere quello corretto, per design sul dominio della filiale
				h = $(this).attr("href");
				if (h.indexOf('aetnagroup')<1){
					h = h.replace(/http:\/\/[^\/]+\//,"");
					$(this).attr("href",h);
				}
			});
	}
	$("#menu_03 ul li a[@href='viewdoc.asp?co_id=22']").attr('href','viewdoc.asp?co_id=14');
	mostra_link_email();
	media_player();
	mod_menu_prod();
	menu();
//	$(".pl_1 img, .pl_2 img, .pl_3 img").load( function (){ aggiusta_altezze_box();});
	//$(li).eq($(li).size()-1).load( function (){ aggiusta_altezze_box();});
//	$(li).eq(0).load( function (){ aggiusta_altezze_box();});
	$("#frm_contatti").submit(function() {return convalida_form_contatti();});
	$("#frm_curriculum").submit(function() {return get_form_validation("#frm_curriculum");});
	$("#frm_rivenditori_service").submit(function() {return get_form_validation("#frm_rivenditori_service");});
	$("#nel_mondo_it").each(function(){add_flash_nel_mondo("nel_mondo_it","nelmondo_IT.swf");});
	$("#nel_mondo_en").each(function(){add_flash_nel_mondo("nel_mondo_en","nelmondo_EN.swf");});
	link2worldwide_sites();
//	$(".menu_service_ra a[@href]*='" & cur_domain & "'").hide();
	//aggiusta_altezze_box();
//	convalida_form_contatti();
}

window.onload = function(){
	aggiusta_altezze_box();
	aggiusta_altezze_box2();
}

function link2worldwide_sites(){
	$("#worldwide_sites").change(function(){
			var url;
			url = $(this).val();
			if (url!==''){
				window.location = window.location.href='http://' + url + '/';
			}
		});
	}

/* Funzione per Menù 
- - - - - - - - - - - - - - - - - - - - */

function menu(){
	$("#menu_vert_pag a.active").parent().parent().addClass("on");
	$("#menu_vert_pag a.active").siblings("ul").addClass("on");
	$("#menu_vert_pag ul li ul").not(".on").hide();
//	$("#menu_vert_pag ul li a").each(function (){if ($(this).parent().children("ul").size()>0) $(this).attr("href","#");} )
	$("#menu_vert_pag 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).siblings("ul.on").removeClass("on").slideUp("fast");
			} else {
				$(this).parent().parent().find("ul.on").removeClass("on").slideUp("fast");
				$(this).siblings().addClass("on").slideDown("fast");
			}
			return false;
		}
	});
	apriMenu();
	my_accordion();
	$("img[@src='']").src='/img/trasp.gif';
}

function apriMenu(){
	$("#menu_vert_pag a.active").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).siblings("ul").show().addClass("on");
			}else if (el[0].tagName=="LI"){
				conta++;
			}else if (el[0].tagName=="DIV"){
				el=null;
			}else{
				conta++;
			}
		}
	});
	$("#menu_vert_pag a.onindex").parent().find("ul:eq(0)").show();
}
function mod_menu_prod () {
	var li3, li2, i;
	
	if (String(location.href).indexOf("/www.aetnagroupusa.com/")>0){
		/* Rimuovo le categorie che non hanno prodotti all'interno */
		for(i = 0; i < 3 ; i++ ){
			$("#menu_vert_pag.mod_menu_prod ul li.cat").each(function(){
					if($(this).find("ul").size()==0){
						$(this).remove();
					}else if($(this).find("li").size()==0){
						$(this).remove();
					}
				});
		}
	}
	if ($("#menu_vert_pag.mod_menu_prod").size()>0){
		li3 = $("#menu_vert_pag ul li ul li ul li ul li");
		li2 =  $("#menu_vert_pag ul li ul li ul li");
		$(li3).each(function(){
				if($(this).find("ul").size()==0 ){
					$(this).clone(true).appendTo($(this).parent().parent().parent());
					$(this).remove();
				}else{
					$(this).find("ul").children("li").clone(true).appendTo($(this).parent().parent().parent());
					$(this).children("ul li").remove();
				}
			})
		$(li2).each(function(){
				if($(this).children("ul").size()>0){
					$(this).remove();
				}
			});
	}
}

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");
		$("h2 a[@rel='accordion_ex']").each(function(){this.my_state='collapsed';});
		return false;
	});
	$("h2 a[@rel='accordion_ex']").each(function(){this.my_state=null; });
	$("h2 a[@rel='accordion_ex']").click(function(){
			if(this.my_state=='collapsed'){
				$("#cont_blocchetti .hidden:hidden").slideDown("normal"); //slideDown
				this.my_state='expanded';
			}else if(this.my_state=='expanded'){
				$("#cont_blocchetti .hidden:visible").slideUp("normal");// slideUp
				this.my_state='collapsed';
			}else{
				$("#cont_blocchetti .hidden:hidden").slideDown("normal"); //slideDown
				this.my_state='expanded';				
			}
			/*
			if ($(this).html()=="-") {
				$(this).html("+");
			}else{
				$(this).html("-");
			}
			*/
			return false;
		});
	$("#cont_blocchetti.template_ra div.hidden").show(); // mostra  Links e brochures (Allegati)  aperti in area riservata
}
/*
NEL MONDO
*/

function add_flash_nel_mondo(thediv,theflash){
	var w,h, versione;
	w= 428;
	h=172;
	versione="7";
	var dst = document.getElementById(thediv );
	var s1 = new SWFObject(theflash,"single",w,h,versione);//"400","300"
	s1.addParam("allowfullscreen","true");
	s1.addParam("wmode","transparent");
	s1.addVariable("file",theflash);
	//s1.addVariable("showicons","false");
	s1.addVariable("backcolor","0x004268");
	s1.addVariable("frontcolor","0xc5d5dc");
	s1.addVariable("lightcolor","0xffffff");
	s1.addVariable("screencolor","0xc5d5dc");
	s1.addVariable("displayheight",h);
	s1.addVariable("showdigits","false");
	//s1.addVariable("largecontrols","true");
	
	//s1.addVariable("image","jw_flv_player/preview.jpg");
	s1.addVariable("width",w);
	s1.addVariable("height",h);
	s1.write(dst);
}

/* LINK EMAIL
-------------------------------------------*/

function mostra_link_email(){
	var n, t, e, p;
	p = "mailto:"
	e = Array({a:"aetnagroup",d:"aetnagroup.com"},{a:"robopac",d:"aetnagroup.com"},{a:"robopacsistemi",d:"aetnagroup.com"},{a:"dimac",d:"aetnagroup.com"},{a:"ricercapersonale",d:"aetnagroup.com"},{a:"",d:""});
	for (n=1;n<6;n++){
		t = $("span.mailto_"+n);
		$(t).before("<a href=\"" + p + e[n-1].a + "@" + e[n-1].d + "\" class=\"mailto\">" + e[n-1].a + "@" + e[n-1].d + "</a>");
		$(t).remove();
	}
}

/* MEDIA PLAYER 
-------------------------------------------*/
function media_player(){
			var np=0;
			var players = $(".media_player");
			if (players.size()){
				$.ajax({
   					type: "GET",
   					url: "jw_flv_player/swfobject.js",
					error: function(a , b ){
						self.status = b + ": plugin video NON caricato! " + a + " File Not Found: " + this.url ;
					},
					success: function(data){
									if (typeof deconcept !== "object" ){eval(data);}
									$(players).each(function() {
										var w,h;
										w = "400"; // Larghezza Player
										h = "300"; // Altezza Player
										var v=$(this).attr("href");		
										$(this).parent().html("<div id=\"player"+np+"\">video player</div>");
										var dst = document.getElementById("player" + np );
										var s1 = new SWFObject("jw_flv_player/flvplayer.swf","single",w,h,"7");//"400","300"
										s1.addParam("allowfullscreen","true");
										s1.addParam("wmode","transparent");
										s1.addVariable("file",v);
										//s1.addVariable("showicons","false");
										s1.addVariable("backcolor","0x004268");
										s1.addVariable("frontcolor","0xc5d5dc");
										s1.addVariable("lightcolor","0xffffff");
										s1.addVariable("screencolor","0xc5d5dc");
										s1.addVariable("displayheight",h);
										s1.addVariable("showdigits","false");
										//s1.addVariable("largecontrols","true");
										
										//s1.addVariable("image","jw_flv_player/preview.jpg");
										s1.addVariable("width",w);
										s1.addVariable("height",h);
										s1.write(dst);
										//if(dst) dst.innerHTML = s1.getSWFHTML();
										np+=1;
									});			
   								}
					 		});
					 
				
			}
			
		}
		
/* AGGIUSTA ALTEZZE BOX
-------------------------------------------*/
function aggiusta_altezze_box(){
	var box=$(".pl_1 h3 a, .pl_2 h3 a, .pl_3 h3 a");
	var max_height=20;
	$(box).each(function (){
		max_height = Math.max(max_height,$(this).height());});
		$(".pl_1 h3 a, .pl_2 h3 a, .pl_3 h3 a").css({display:"block"});
//		$(".pl_1 h3 a, .pl_2 h3 a, .pl_3 h3 a").height(max_height);
		$(".pl_1 h3 a, .pl_2 h3 a, .pl_3 h3 a").css("height",max_height + "px");
	// fin qui aggiusta il link dentro gli h3
//	box=$(".pl_1 a img, .pl_2  a img, .pl_3 a img");
	box=$(".pl_1, .pl_2, .pl_3");
	max_height=80;
	$(box).each(function (){
		var a=$(this).parent();
		max_height = Math.max(max_height,$(this).height());
//		max_height = Math.max(max_height,$(a).find("img").height() + $(a).prev().height() + $(a).prev().next().height() + 40 );
						  });

$(".pl_1, .pl_2, .pl_3").css("height", max_height + "px");
//$("#menu_vert_pag").attr("title",max_height);

	// fin qui aggiusta i box .pl_1, .pl_2, .pl_3
}

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;
	}	
}

/* AGGIUSTA ALTEZZE BOX 2
-------------------------------------------*/
function aggiusta_altezze_box2(){
	var box=$(".pi_1 h3 a, .pi_2 h3 a, .pi_4 h3 a");
	var max_height=20;
/*
	$(box).each(function (){
		max_height = Math.max(max_height,$(this).height());});
		$(".pi_1 h3 a, .pi_2 h3 a, .pi_3 h3 a, .pi_4 h3 a").css({display:"block"});
		$(".pl_1 h3 a, .pl_2 h3 a, .pl_3 h3 a, .pi_4 h3 a").css("height",max_height + "px");
	// fin qui aggiusta il link dentro gli h3
*/
	box=$(".pi_1, .pi_2, .pi_3, .pi_4");
	max_height=80;
	$(box).each(function (){
		var a=$(this).parent();
		max_height = Math.max(max_height,$(this).height());
						  });

$(".pi_1, .pi_2, .pi_3, .pi_4").css("height", max_height + "px");

	// fin qui aggiusta i box .pl_1, .pl_2, .pl_3
}

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;
	}	
}
/* 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 "atLeastOneSibling:checked" :
			return $(what + ':checked, ' + what + ' ~:checked').size()>0;
			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 convalida_form_contatti(){
	var f, valid, msg, af,ae
	valid = true;
	msg="";
	f=$("#frm_contatti");
	if (!f){return true;}
	af = Array("#ut_azienda isText","#ut_indirizzo isText","#ut_cap isText","#ut_telefono isTelFax","#ut_email isEmail","#ut_citta isText","#ut_nome isText","#ut_cognome isText", "#ut_nazione notZero", "#ut_informativa isChecked");
	for ( var el=0;el<af.length;el++){
		ae=af[el].split(" ");
		if( validate.field(ae[0],ae[1])==false ){
			$(ae[0]).parent().addClass("bg_error");
			valid=false;
		}else{
			$(ae[0]).parent().removeClass("bg_error");
		}
	}
	$(".bg_error input, .bg_error select, .bg_error textarea").eq(0).focus();
	return valid;
}

function get_form_validation(frm_id){
	var oval,valid,validation_string;
//	validation_string="{ut_nome:'isText',ut_cognome:'isText',ut_email:'isEmail'}";
//	eval("oval="+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();
	return valid;
}
