// JavaScript Document

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

function selectActive(myClick) {
	var myLi = myClick.parentNode;
	var allLi = myLi.parentNode.getElementsByTagName("li");
	for (i=0; i<allLi.length; i++) {
		if (allLi[i] == myLi) {
			allLi[i].className = "active";
		} else {
			allLi[i].className = "";
		}
	}
	return false;
}

function showFullscreenItem(myItem) {
	$(myItem.parentNode).dequeue().stop();
	if ($(myItem).data('isOver') === true) {
		$(myItem.parentNode).animate({
			width: "69px",
		}, 500 );
	} else {
		$(myItem.parentNode).animate({ 
			width: "30px",
		}, 500 );
	}
}      

function KeyCheck(e) {
   var KeyID = (window.event) ? event.keyCode : e.keyCode;
   
   switch(KeyID)
   {
      case 122:
   		isFullScreen = !isFullScreen;
   		cambiaStato();
      	break;
  }
}

function cambiaStato() {
   var myElement = document.getElementById("f11").getElementsByTagName("a")[0];	
  
  if (isFullScreen) {
	  myElement.className = "backToNormalScreen";
	  myElement.title = "Torna a visualizzazione precedente";
  } else {
	  myElement.className = "goToFullScreen";
	  myElement.title = "Visualizza a schermo intero";	
  }
  windowHeight = parseInt(document.documentElement.clientHeight);
  containerHeight = 720;
  if (windowHeight>containerHeight) {
	var myMargin = Math.floor((windowHeight-containerHeight)/2);
  	document.getElementById('container').style.margin = myMargin + "px auto";
  } else {
  	document.getElementById('container').style.margin = "0px auto";
  }
}

var isFullScreen = false;

function checkOnLoad() {
	var windowWidth = 0;
	var windowHeight = 0;
	if (document.documentElement.clientWidth) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else {
		windowWidth = window.innerWidth;
		windowHeight = window.innerHeight;
	}
	if ((screen.width-windowWidth<27) && (screen.height-windowHeight<24)) {
		isFullScreen = true;
		cambiaStato();
	}
}

function mostraPrivacy() {
	var privacyBG = document.createElement('div');
	var newAttr = document.createAttribute('class');
	newAttr.nodeValue = "privacyBG";
	privacyBG.setAttributeNode(newAttr);
	
	var privacyContainer = document.createElement('div');
	newAttr = document.createAttribute('class');
	newAttr.nodeValue = "privacyContainer";
	privacyContainer.setAttributeNode(newAttr);
	privacyContainer.innerHTML = "<h1>Trattamento dati personali</h1>Inviandovi questo modulo da me compilato, ai sensi e per gli effetti degli articoli 7, 13 e 23 del <a href=\"privacy/privacy.pdf\" target=\"_blank\" title=\"Leggi il decreto sulla privacy\" style=\"color:#555;\">D.Lgs 196/2003</a>, dichiaro di aver preso conoscenza dell'informativa di <strong>giuseppeferrari.net</strong> per il trattamento dei miei dati personali, di essere esaurientemente informato in merito ai miei diritti e di conoscere come e dove esercitare gli stessi, pertanto PRESTO IL MIO CONSENSO a <strong>giuseppeferrari.net</strong> per il trattamento delle informazioni rilasciate per le finalit&agrave; indicate nella predetta informativa.";
	
	var chiudiPrivacy = document.createElement('a');
	newAttr = document.createAttribute('href');
	newAttr.nodeValue = "#";
	newAttr = document.createAttribute('class');
	newAttr.nodeValue = "chiudiPrivacy";
	chiudiPrivacy.setAttributeNode(newAttr);
	chiudiPrivacy.onclick = function() {
		$('.privacyBG').remove();
		$('.privacyContainer').remove();
	}
	chiudiPrivacy.innerHTML = '[chiudi]';
	
	$(privacyContainer).append(chiudiPrivacy);
	$('body').append(privacyBG);
	$('body').append(privacyContainer);
	
	return false;
}

function AjaxRequest(file, container) {
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	} else { // code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4 && xmlhttp.status==200) {
				if (container.innerHTML!=xmlhttp.responseText) {
					calendarDelay = 60000;
					container.innerHTML = xmlhttp.responseText;
				}
			}
		}
	xmlhttp.open("GET",file,true);
	xmlhttp.send();
}

function cambiaFotoLinks(objsPerPage, numbersOfObjs) {
	for (i=primoObj; i<numbersOfObjs; i++) {
		$('#li_thumb_'+i).hide();
	}
	primoObj = primoObj+objsPerPage;
	objsPerPage = Math.abs(objsPerPage);
	if (primoObj>=numbersOfObjs) {
		primoObj = 0;
	} else if (primoObj<0) {
		primoObj = Math.floor(numbersOfObjs/objsPerPage)*objsPerPage;
		if (primoObj==numbersOfObjs) {
			primoObj = (Math.floor(numbersOfObjs/objsPerPage)-1)*objsPerPage;
		}
	}
	for (i=primoObj; i<(primoObj+objsPerPage); i++) {
		$('#li_thumb_'+i).fadeIn();
	}
	var ultimaFoto = ((primoObj+objsPerPage)<numbersOfObjs)?(primoObj+objsPerPage):numbersOfObjs;
	$('#counterLinks_Pics').html((primoObj+1) + ' - ' + ultimaFoto + ' (totale: ' + numbersOfObjs + ')');
	return false;
}

function viewDatasheet(id_subject) {
	var newDiv = document.createElement('div');
	var newAttr = document.createAttribute('id');
	newAttr.nodeValue = 'datasheetBG';
	newDiv.setAttributeNode(newAttr);
	$('div.sidebar2 > div.subjectList').append($(newDiv));
	//$(newDiv).click(function() {$(this).fadeOut().remove();});
	$(newDiv).load('datasheet.php', {'id_subject':id_subject}, function () {		
		$('#datasheetBG > div > div#subjectClose').click(function() {$('#datasheetBG').fadeOut(function() {$('#datasheetBG').remove();})});
		$(newDiv).fadeIn('slow');
	});
	return false;
}

var message = "I testi e le immagini di questo sito sono protetti dalle leggi italiane e internazionali sul diritto d'autore.\nOgni riproduzione, traduzione o adattamento sono proibiti senza l'espressa autorizzazione scritta dell'autore.\n"+unescape("%A9")+" 2010-2011 Giuseppe Ferrari";

//function clickIE4(){
//	if (event.button==2/* || event.button==3*/){
//		window.alert(message);
//		return false;
//	}
//}
//
//function clickNS4(e){
//	if (document.layers || document.getElementById && !document.all){
//		if (e.which==2 || e.which==3){
//			window.alert(message);
//			return false;
//		}
//	}
//}
//
//function clickAll(e) {
//	if (e.which==2 || e.which==3 || event.button==2 || event.button==3){
//		window.alert(message);
//		return false;
//	}
//}
//
//if (document.layers){
//	document.captureEvents(Event.MOUSEDOWN);
//	document.onmousedown = clickNS4;
//} else if (document.all && !document.getElementById){
//	document.onmousedown = clickIE4;
//}
/* else {
	document.captureEvents(Event.MOUSEDOWN);
	document.onmousedown = clickAll;
}*/

document.oncontextmenu = new Function("return false");
document.onselectstart = new Function("return false");
document.ondragstart = new Function("return false");

$.fn.extend({ 
	disableSelection : function() { 
		return this.each(function() { 
			this.onselectstart = function() { return false; }; 
			this.unselectable = "on"; 
			$(this).css('user-select', 'none'); 
			$(this).css('-o-user-select', 'none'); 
			$(this).css('-moz-user-select', 'none'); 
			$(this).css('-khtml-user-select', 'none'); 
			$(this).css('-webkit-user-select', 'none'); 
		}); 
	} 
});

$.fn.tagName = function() {
    return this.each(function() {
        return this.tagName;
    });
}

$(function() {
	if (!document.all || window.opera) {
		$(this).bind("contextmenu", function(e) {
			e.preventDefault();
		});
		$('img').bind("contextmenu", function(e) {
			window.alert(message);
			e.preventDefault();
		});
	} else {
		$('img').bind("contextmenu", function(e) {
			window.alert(message);
			e.preventDefault();
		});
	}
});

$(document).ready(function(){
	
	//document.onkeydown = KeyCheck;
	//checkOnLoad();
	//if(window.navigator.vendor && (window.navigator.vendor.match("Apple"))) {
	   //$(document.getElementById("f11")).hide();
	//}
	if ($('#emailReport')) {
	   setTimeout(function () {$('#emailReport').fadeOut(1500);}, 5000);
	}
	if ($('div.breadcrumb') && $('div.breadcrumb').get(0)) {
	   var myDiv = $('div.breadcrumb').get(0);
	   if (myDiv.scrollWidth>$(myDiv).width()) {
		   $('<div id="dots">&#0133;</div>').appendTo(myDiv);
	   }
	   //window.alert(myDiv.innerHTML);
	}
	/*if ($('div.containerGoogleMap') && $('div.containerGoogleMap iframe')) {
		var myIframe = $('div.containerGoogleMap iframe');
		if (myIframe.length>0) {
			myIframe = myIframe[0];
			setTimeout(function() {
				var bodies = myIframe.contentDocument.document.body.innerHTML;									
				window.alert(bodies);
			}, 1000);
		}
	}*/
	//$('div.gallery.openThumbs li a').click(function() {
								//openThumbs(this);
								//return false;
										  //});
	
	//$('body *').disableSelection();
	/*if (document.all && !window.opera && $('img').get()) {
		var imagesTags = $('img').toArray();
		for (i=0; i<imagesTags.length; i++) {
			imagesTags[i].oncontextmenu = new Function("window.alert(message); return false;");
		}
	}*/
	
	/* LEFT MENU TRANSITION */
/*	if ($('#left-menu').get(0)) {
		$('#left-menu').bind('mouseover', function() {
			//					   
		});
	}*/
	
});
