// coding: utf-8
var myhost = window.location.hostname;
var newsListContent = new Array();

var winW = 0;
var winH = 0;
if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    winW = window.innerWidth;
    winH = window.innerHeight;
} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    winW = document.documentElement.clientWidth;
    winH = document.documentElement.clientHeight;
}
var scrW = screen.availWidth;
var scrH = screen.availHeight;
//~ alert("this window: "+winW+" x "+winH+"\nscreen (avail.): "+scrW+" x "+scrH);

function getImgSize(img_uri, rtn) {
    var pic = new Image();
    pic.src = img_uri;
    var h = pic.height;
    var w = pic.width;
    
    var res = 0;
    switch(rtn) {
	case 'wh':
	    res =  w+'|'+h; //on récup en faisant .split('|');
	    break;
	case 'hw':
	    res =  h+'|'+w;
	    break;
	case 'w':
	    res =  w;
	    break;
	case 'h':
	    res =  h;
	    break;
	default:
	    res =  w+'|'+h;
    }
    return res;
}

function EventsControl() {
    var myAs = document.getElementsByTagName('a');
    for (var a = 0; a < myAs.length; ++a) {
	//liens externes en target _blank (émulé)
	if(myAs[a].hostname != myhost) {
	    myAs[a].target = '_blank';
	}
	// liens retour page -1 / forward sur images / suite XMLHttpRequest
	if(myAs[a].className == 'back' || myAs[a].className == 'forward' || myAs[a].className == 'suite') {
	    
	    myAs[a].boxTxt = (myAs[a].className == 'back') ? 'retour' : ( (myAs[a].className == 'forward') ? 'détails' : 'suite' );
	    myAs[a].hasImg = (myAs[a].firstChild.nodeName == 'IMG') ? true : false;
		
	    if( typeof(titContent) == 'undefined' ) {
		var titContent = ''; var altContent = '';
		//on crée un div pour afficher le titre de manière plus élaborée (bouge avec la souris, etc)
		var linkTitBox = document.createElement('div');
		var linkTitID = document.createAttribute('class');
		linkTitID.nodeValue = 'infoPop';
		linkTitBox.setAttributeNode(linkTitID);
		linkTitBox.appendChild(document.createTextNode('notset'));
		document.body.appendChild(linkTitBox);
		
		var TopPos =  -25;
		var LeftPos = 0;
	    }
	    if(myAs[a].hasImg) {
		myAs[a].onmouseover = function() {
		    if(this.title) {
			titContent = this.title;
			this.title = '';
		    }
		    linkTitBox.childNodes[0].nodeValue = this.boxTxt;
		    //~ linkTitBox.style.display = 'block';
		    //pour lt IE8, obligé de virer le alt sinon il l'affiche ce fdp!
		    if(this.firstChild.alt) {
			altContent = this.firstChild.alt;
			this.firstChild.alt = '';
		    }
		};
		myAs[a].onmousemove = function(event) {
		    event = event || window.event;
		    linkTitBox.style.top = event.clientY + (document.body.scrollTop || document.documentElement.scrollTop) - TopPos+'px';
		    linkTitBox.style.left = event.clientX - LeftPos+'px';
		};
		myAs[a].onmouseout = function() {
		    if(this.title) this.title = titContent;
		    if(this.firstChild.alt) this.firstChild.alt = altContent;
		    linkTitBox.style.display = 'none';
		};
	    }
	    myAs[a].onclick = function() {
		linkTitBox.style.display = 'none';
		//~ this.blur();
		if(this.className == 'back') {
		    var hostURL = 'http://'+myhost;
		    if(document.referrer.substr(0, hostURL.length) == hostURL) {
			if(window.history.length > 1) {
			    window.history.back();
			    return false;
			}
			else return true;
		    }
		    else return true;
		}
		/**********************************************************
		 requete serveur pour avoir la suite d'une news si [break]
		***********************************************************/
		else if(this.className == 'suite') {
		    var nReq = null;
		    if(window.XMLHttpRequest) nReq = new XMLHttpRequest();
		    else if(window.ActiveXObject){
			try {
			    nReq = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
			    nReq = new ActiveXObject("Microsoft.XMLHTTP");
			}
		    } else {
			alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest.");
			nReq = false;
		    }
		    if(!nReq) return true;
		    
		    var a = this.href.lastIndexOf('-') + 1;
		    var b = this.href.lastIndexOf('#');
		    var news_id = (b != -1) ? this.href.substring(a, b) : this.href.substring(a);
		    var ok = 0;
		    var container = document.getElementById('newsId'+news_id);
		    
		    //on vérifie si la news a deja été révélée (contenu 'listing' de la news enregistré dans tableau indexé sur id)
		    if(!newsListContent[news_id] || newsListContent[news_id] == '') {
			//getFullNews_ voir .htaccess
			nReq.open("GET", 'getFullNews_'+news_id, true);
		    
			nReq.onreadystatechange = function() {
			    if(nReq.readyState == 4) {
				if (nReq.status == 200) {
				    //~ eval(nReq.responseText.substring(nReq.responseText.length - 8));
				    //~ if(ok) container.innerHTML = nReq.responseText.substring(0, nReq.responseText.length - 8);
				    newsListContent[news_id] = container.innerHTML;
				    container.innerHTML = nReq.responseText;
				    //pour interpréter le contenu nouvellement généré
				    EventsControl();
				}
			    }
			};
			nReq.send(null);
		    }
		    else {
			container.innerHTML = newsListContent[news_id];
			newsListContent[news_id] = '';
			EventsControl();
		    }
		    
		    return false;
		}
		else return true;
	    };
	}
	/*
	// affiche le titre du projet (dans le <li><a><span>) sur la photo
	else if(myAs[a].className == 'reftit') {
	    myAs[a].onmouseover = function() {
		this.firstChild.style.display = 'block';
	    };
	    myAs[a].onmouseout = function() {
		this.firstChild.style.display = 'none';
	    };
	}
	*/
    }
}

window.onload = function() {
    EventsControl();
    //voir diaporama.js inclus AVANT
    rotationInit('rotimg');
};
