/* accordion menù prodotti ====================================================================== */

$(document).ready(function(){
	$("ul.prodMenu ul").hide();
	$("ul.prodMenu a.parentLink").click(function(){
	$(this).next("ul").slideToggle("fast")
		.siblings("ul:visible").slideUp("fast");
	$(this).toggleClass("current");
	
	return false;
	});
	$("ul.prodMenu a.current").next("ul").show();
});


/* tabella scheda prodotto ================================== */

$(document).ready(function(){
	
	var inox = $("#tabella").hasClass("inox");
	
	$("#tabella table td").each(function () {

	    var valore = $(this).html();
	    if (valore.length > 0 && valore == "-") {
	        $(this).html("");
	        $(this).addClass("circle");
	    } else
		if (valore.length > 0 && valore != "&nbsp;") {
			$(this).attr("title",valore); //copio il valore della cella nel title
			$(this).tooltip(); //jquery tooltip
			if (valore.toLowerCase().indexOf("</p>")==-1) {
				//valore singolo
				if(inox) { 
					$(this).addClass("squareblue");
				} else {
					$(this).addClass("squaregreen");
				}
			} else {
				//valori multipli
				if(inox) { 
					$(this).addClass("squarewhiteblue");
				} else {
					$(this).addClass("squarewhitegreen");
				}
			}
		}
		
		
	});
});

$(document).ready(function(){
	$(".productTableArs tr:odd").each(function () {
		$(this).addClass("alt");									
	});
});



/* fondino homepage ================================== */

//$(document).ready(function(){
//	var h1 = $("#leftcol").height();
//	var h2 = $("#glossario").height();
//	var h3 = $("#areariservata").height();
//	if (h2 == null) h2 = -110;
//	var h = h1 - h2 - h3 + 21;
//	if ( $.browser.msie )
//	{
//		h = h - 77;
//		$("#imgFondino").css("display", "none");
//		$("#fondino").height(h);
//		$("#imgFondino").attr("src","/ThumbImage.aspx?image=/images/fondino.png&width=238&height="+h+"&mode=crop");
//		$("#imgFondino").css("display", "block");
//		
//	} else {
//		$("#imgFondino").attr("src","/ThumbImage.aspx?image=/images/fondino.png&width=238&height="+h+"&mode=crop");
//	}
// });
 

/* JSTarget function by Roger Johansson, www.456bereastreet.com ================================== */

var JSTarget = {
	init: function(att,val,warning) {
		if (document.getElementById && document.createElement && document.appendChild) {
			var strAtt = ((typeof att == 'undefined') || (att == null)) ? 'class' : att;
			var strVal = ((typeof val == 'undefined') || (val == null)) ? 'non-html' : val;
			var strWarning = ((typeof warning == 'undefined') || (warning == null)) ? ' (opens in a new window)' : warning;
			var oWarning;
			var arrLinks = document.getElementsByTagName('a');
			var oLink;
			var oRegExp = new RegExp("(^|\\s)" + strVal + "(\\s|$)");
			for (var i = 0; i < arrLinks.length; i++) {
				oLink = arrLinks[i];
				if ((strAtt == 'class') && (oRegExp.test(oLink.className)) || (oRegExp.test(oLink.getAttribute(strAtt)))) {
					oWarning = document.createElement("em");
					oWarning.appendChild(document.createTextNode(strWarning));
					oLink.appendChild(oWarning);
					oLink.onclick = JSTarget.openWin;
				}
			}
			oWarning = null;
		}
	},
	openWin: function(e) {
		var event = (!e) ? window.event : e;
		if (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) return true;
		else {
		    var oWin = window.open(this.getAttribute('href'), '_blank');
			if (oWin) {
				if (oWin.focus) oWin.focus();
				return false;
			}
			oWin = null;
			return true;
		}
	},
	/*
	addEvent function from http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
	*/
	addEvent: function(obj, type, fn) {
		if (obj.addEventListener)
			obj.addEventListener(type, fn, false);
		else if (obj.attachEvent) {
			obj["e"+type+fn] = fn;
			obj[type+fn] = function() {obj["e"+type+fn]( window.event );}
			obj.attachEvent("on"+type, obj[type+fn]);
		}
	}
};
JSTarget.addEvent(window, 'load', function(){JSTarget.init("rel","external","");});


/* funzione anteprima di stampa ================================== */

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent( function(){ add_print_link( 'nav' ) } );

function add_print_link( id ){
  if( !document.getElementById ||
      !document.getElementById( id ) ) return;

  // add extra functions to page tools list
  var print_page = document.getElementById( id );

  // create print link
  var print_function = document.createElement('p');
  print_function.className = 'print-link';
  print_function.onclick = function(){ print_preview(); return false; };
  print_function.appendChild( document.createTextNode( 'Print the Page' ) );

}

function print_preview() {
	// Switch the stylesheet
	setActiveStyleSheet('Print Preview');
	
	// Create preview message
	add_preview_message();

	// Print the page
	window.print();
}

function add_preview_message(){
var main_content = document.getElementById('contentmaincol');
var main_body = main_content.parentNode;

	if (document.getElementById){
		
		var preview_message = document.createElement('div');
		preview_message.id = 'preview-message';
	
		// Create Heading
		var preview_header = document.createElement('h3');
		var preview_header_text = document.createTextNode('Versione stampabile della pagina');
		preview_header.appendChild(preview_header_text);
		
		// Create paragraph
		var preview_para = document.createElement('p');
		var preview_para_text = document.createTextNode("Questo messaggio scomparira' nella stampa finale. ");
		
		var cancel_function_link = document.createElement('a');
			cancel_function_link.onclick = function(){ cancel_print_preview(); return false; };
			cancel_function_link.setAttribute('href', '#');	
		var cancel_function_link_text = document.createTextNode('Torna alla pagina del prodotto');
		cancel_function_link.appendChild(cancel_function_link_text);
		preview_para.appendChild(preview_para_text);
		preview_para.appendChild(cancel_function_link);
		
		// Put it all toegether
		preview_message.appendChild(preview_header); 
		preview_message.appendChild(preview_para);
		main_body.insertBefore(preview_message, main_content);

	}
}

function cancel_print_preview() {
	// Destroy the preview message
	var print_preview = document.getElementById('preview-message');
	var main_body = print_preview.parentNode;
	main_body.removeChild(print_preview);
	
	// Switch back stylesheet
	setActiveStyleSheet('default');
}

function setActiveStyleSheet(title) {
   var i, a, main;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     if(a.getAttribute("rel").indexOf("style") != -1
        && a.getAttribute("title")) {
       a.disabled = true;
       if(a.getAttribute("title") == title) a.disabled = false;
     }
   }
}

/* avvia print preview (jquery */

$(document).ready(function(){
				  $(".printLink").click(function(){
					print_preview(); 
					return false;
				  });
});
