
//Global Variables

/* var imageIndex=0; //global variable to count the image opened. Incrementation and decrementation must be obtained on functions */
/* var maxImageCount=5; //images' count */

//Ajax Routines

function initiateHTTPR(url, target) {
  document.getElementById(target).innerHTML = ' Loading data...';
  if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (req != undefined) {
    req.onreadystatechange = function() {answerHTTPR(url, target);};
    req.open("GET", url, true);
    req.send("");
  }
}

function answerHTTPR(url, target) {
  if (req.readyState == 4) { // only if req is "loaded"
    if (req.status == 200) { // only if "OK"
      document.getElementById(target).innerHTML = req.responseText;
    } else {
      document.getElementById(target).innerHTML=" AHAH Error:\n"+ req.status + "\n" +req.statusText;
    }
  }
}

//Functions

function ShowEffect(element){
       new Effect.Opacity(element, 
       {duration:3, from:0, to:1.0});
   }

function HideEffect(element){
       new Effect.Opacity(element, 
       {duration:3, from:1.0, to:0.1});
   }

function loadRandomImage (imageCount, container){
var randomImage =  document.getElementById(container);
var randomNumber = Math.random()*100;
var imageIndex = Math.floor(randomNumber);
HideEffect(randomImage);
if (imageIndex>imageCount){imageIndex = Math.floor(imageIndex/6)};
/*
if (imageIndex < 10){
var newImage = 'image0' + imageIndex + '.jpg'
} else {
};
*/
var newImage = 'image' + imageIndex + '.jpg'
var newImg = '<img src=\"images\/bg\/' + newImage + '\" width=\"100%\">';
randomImage.innerHTML = newImg;
ShowEffect(randomImage);
return;
}

function centreContent (element, container) {
	var getEl = document.getElementById(element);
	if (getEl != null){
	var elWidth = getEl.offsetWidth;
	var contWidth = window.outerWidth;
	
	if (contWidth < elWidth){
	getEl.style.marginRight = 0 + "px";
	getEl.style.marginLeft = 0 + "px";	
	return;
	} else {
	
	var correctMargin = Math.floor((contWidth - elWidth)/2);
	
	getEl.style.marginRight = correctMargin + "px";
	getEl.style.marginLeft = correctMargin + "px";
	
	return;
	}}
	
	alert ("è sbagliato");
	}
	
function centreInternal (element, container) {
	var getEl = document.getElementById(element);
	var getCon = document.getElementById(container);
	if (getEl != null){
	var elWidth = getEl.offsetWidth;
	var contWidth = getCon.offsetWidth;
	
	if (contWidth < elWidth){
	getEl.style.marginRight = 0 + "px";
	getEl.style.marginLeft = 0 + "px";	
	return;
	} else {
	
	var correctMargin = Math.floor((contWidth - elWidth)/2);
	
	getEl.style.marginRight = correctMargin + "px";
	getEl.style.marginLeft = correctMargin + "px";
	
	return;
	}}
	
	alert ("è sbagliato");
	}

function centreVerticalInternal (element, container) {
	var getEl = document.getElementById(element);
	var getCon = document.getElementById(container);
	if (getEl != null){
	var elHeight = getEl.offsetHeight;
	var contHeight = getCon.offsetHeight;
	
	if (contHeight < elHeight){
	getEl.style.marginTop = 0 + "px";
	getEl.style.marginBottom = 0 + "px";	
	return;
	} else {
	
	var correctMargin = Math.floor((contHeight - elHeight)/2);
	if (document.getElementById(container) == document.getElementById('header')){
	getEl.style.marginTop = (correctMargin-25) + "px";
	getEl.style.marginBottom = (correctMargin+25) + "px";
/* 	alert('questo è l\'header'); */
	}else{
/* 	alert('questo non è l\'header'); */
	getEl.style.marginTop = correctMargin + "px";
	getEl.style.marginBottom = correctMargin + "px";
	}
	return;
	}}
	
	alert ("è sbagliato");
	}

function setWindow () {
	var width = document.width;
	if (width != 1280)
	{
	window.resizeTo(1280,1024);
	}
//		alert (width);

}

function testWriteThere (divElement) {
	var width = window.outerWidth;
	var mcontent = document.getElementById(divElement);
document.getElementById(divElement).innerHTML = ('this page is ' + width + ' pixels large');
}

var popupIndex = 0; //global variable to count the popup opened. Incrementation and decrementation must be obtained on functions

function showPopup(popupContainer) {
	var xCursor= event.clientX;
	var yCursor= event.clientY;
	var elementWidth = document.getElementById(popupContainer).offsetWidth;
	var windowWidth = window.outerWidth;
	if (windowWidth <= elementWidth){
	xCursor= event.clientX;
	} else {
	xCursor = event.clientX - (Math.floor((windowWidth - elementWidth)/2));
	}
	if (popupIndex>0){
	xCursor += popupIndex*10;
	yCursor += popupIndex*10;
	}
		var newContent = document.createElement("div"); //it creates a new div to represent the popup and set attributes
		newContent.id = "popup" + popupContainer + popupIndex;
		newContent.className = "popup";
		newContent.style.position = "absolute";
		newContent.style.zIndex = "100";
		newContent.style.marginLeft = xCursor + "px";
		newContent.style.marginTop = yCursor + "px";
//		newContent.addEventListener('mousedown', function(f){movePopup(newContent.id, xCursor, yCursor, event.clientX, event.clientY); f.stopPropagation;}, false);
//	var newContentObj = document.getElementById(newContent);
	var aObj = document.createElement('a'); // it creates a new a tag and set attributes and add an event listener
	aObj.id = "aClose" + popupIndex;
	aObj.className = "ajaxCall";
	aObj.addEventListener('click', function(e){removePopupRelative(popupContainer, document.getElementById(aObj.id).parentNode.id); e.stopPropagation;}, false);
	
	var closeString = document.createTextNode('close');
	newContent.appendChild(aObj);
	aObj.appendChild(closeString);

	var popupContainerObj = document.getElementById(popupContainer);
	popupContainerObj.appendChild(newContent);
	popupIndex = ++popupIndex; //incremente global popup counter variable
//	alert (popupIndex);
	return;
}

function movePopup(popup, x, y, startX, startY){ // to complete; don't use it
		var popupObj=document.getElementById(popup);
		var mouseX = event.clientX;
		var mouseY = event.clientY;
		var deltaX = startX - mouseX;
		var deltaY = startY - mouseY;
		if (deltaX >= 0){
		x = x+deltaX;
		popupObj.style.marginLeft = x + "px";
		} else {
			var probeX = x+deltaX;
			if (probeX <=0){
				return x;
			} else {
				x = probeX;
				popupObj.style.marginLeft = x + "px";
			}
		}
		}

function removePopup(popupContainer){
	var popupContainerObj = document.getElementById(popupContainer);
	var lastContent = popupContainerObj.lastChild; 
	popupContainerObj.removeChild(lastContent); 
	popupIndex = popupIndex--;
	}
	
function removePopupRelative(popupContainer, popupChild){
	var popupContainerObj = document.getElementById(popupContainer);
	var popupChildObj = document.getElementById(popupChild);
	popupContainerObj.removeChild(popupChildObj);
	popupIndex = --popupIndex;//Decrement global popup counter variable
	if (popupIndex<0){
	popupIndex = 0;
	}
	}
	
function loadContent(urlAddress, container) {
	initiateHTTPR(urlAddress, container);
	return false;
	}
	
function hideElement(element) {
if (document.getElementById(element) != undefined){
var elementHidden = document.getElementById(element).style;
elementHidden.setProperty('visibility','hidden')
return;
}
alert('errore');
return;
}

function showElement(element) {
if (document.getElementById(element) != undefined){
var elementHidden = document.getElementById(element).style;
elementHidden.setProperty('visibility','visible')
return;
}
alert('errore');
return;
}

function switchElement (element1, element2){
hideElement(element1);
showElement(element2);
return;
}

function fullscreen (divId){
if (!divId) return;
if (document.getElementById('fsContainer')!=null) return;
var fsDivId=document.getElementById(divId);
var fsContainer= document.createElement('div');
fsContainer.setAttribute ('id', 'fsContainer');
fsContainer.setAttribute ('style','background-color:#ffffff; background-repeat: repeat;background-image: url(file:///Volumes/My%20Passport%201/nomdeplume/UI/mwTemplate/images/popupTable_05.png); zindex:300; height:110%; width:110%; top:-5px; left:-5px; position:absolute;');	
var original= fsDivId.getAttribute ('style');
fsDivId.setAttribute ('style','background-repeat: repeat;background-image: url(file:///Volumes/My%20Passport%201/nomdeplume/UI/mwTemplate/images/popupTable_05.png);height:100%; width:100%;position:static;');	
document.getElementsByTagName ('body')[0].appendChild (fsContainer);
document.getElementById('fsContainer').appendChild (fsDivId);
var changeState = document.getElementById('fullscreen');
changeState.setAttribute('onclick', 'exitfullscreen(\'maincontent\');')
changeState.innerHTML = 'toggle fullscreen';

return;
}

function exitfullscreen (divId){
if (!divId) return;
if (!document.getElementById('fsContainer')) return;
var fsDivId=document.getElementById(divId);
fsDivId.setAttribute ('style','background-color: #FFF;height:560px; width:1100px;position:absolute;');
document.getElementById('pagecontent').appendChild(fsDivId);
var objectToBeRemoved = document.createElement('div');
var fsContainer= document.getElementById('fsContainer');
objectToBeRemoved.setAttribute('id', 'objectToBeRemoved');
document.getElementsByTagName ('body')[0].appendChild (objectToBeRemoved);
document.getElementById('objectToBeRemoved').appendChild (fsContainer);
document.getElementsByTagName ('body')[0].removeChild(objectToBeRemoved);
centreInternal('maincontent', 'pagecontent');
var changeState = document.getElementById('fullscreen');
changeState.setAttribute('onclick', 'fullscreen(\'maincontent\');')
changeState.innerHTML = 'fullscreen';
return;
}

function injectIframe (divId,url){
var divIdElement = document.getElementById(divId);
var childCount = divIdElement.childNodes;
if (childCount.length>0){
var answer = confirm('If you continue you\'ll loose your last search results. Do you want to continue?');
if (answer === true)
{
divIdElement.removeChild(divIdElement.lastChild);
} else {return;}
} else {return;}
var iFrame = document.createElement('iframe');
/* <iframe id="myIframe" frameborder="1" scrolling="yes" src="yourframe.html" width="600" height="400"></iframe> */
var iFrameSuffix = Math.random();
iFrame.setAttribute ('id', 'iFrame' + iFrameSuffix);
iFrame.setAttribute ('frameborder', '0');
iFrame.setAttribute ('scrolling', 'vertical');
iFrame.setAttribute ('src', url);
iFrame.setAttribute ('width', '100%');
iFrame.setAttribute ('height', '100%');
divIdElement.appendChild(iFrame);
return;
}

function makeDraggable(div, thisHandle){
var divElement = document.getElementById(div);
var divDragable = new Draggable (divElement,{handle: thisHandle});

var dragDestroy = function (){
divDragable.destroy();
return;
}
return;
}
	
