
var plgalleries = new Array();
var playerurl = 'http://baldeagle.com/wp-content/plugins/nggallery/';

// Pierrick

function getPageScroll(){
	var yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}
	arrayPageScroll = new Array('',yScroll);
	return arrayPageScroll;
}

function getPageSize(){
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
}

var screenMask = {
	scrmask: false,
	init: function() {
		this.scrmask = document.createElement('div');
		this.scrmask.id = 'scrmask';
		document.getElementsByTagName("body").item(0).appendChild(this.scrmask);
	},
	show: function() { this.scrmask.style.display = 'block'; },
	hide: function() { this.scrmask.style.display = 'none'; },
	height: function(x) { this.scrmask.style.height = x; }
}; 


var gPopup = {
	html: false,
	div: false,
	close: false,
	filepath: false,
	title: false,
	content: false,
	titles: new Array(),
	contents: new Array(),
	onfocus: new Array(),

	init: function() {
		var _this = this;
		this.div = document.createElement('div');
		this.div.id = 'popup';
	
		this.content = document.createElement('div');
		this.content.id = 'popupContent';
		this.div.appendChild(this.content);

		this.close = document.createElement('a');
		this.close.href = '#';
		this.close.innerHTML = 'close';
		this.close.id='closePopup';
		this.close.onclick = function() { _this.hide(true); return false; };
		this.div.appendChild(this.close);

		this.title = document.createElement('div');
		this.title.id = 'popupTitle';
		this.div.appendChild(this.title);		

		this.navlinks = document.createElement('div');
		this.navlinks.id = 'popupGnav';
		this.div.appendChild(this.navlinks);

		document.getElementById("content").appendChild(this.div);
	},
	setTitle: function(t, c) {
		this.title.innerHTML = t ? t : 'Titre';
		this.div.className = c ? c : '';
	},
	setLinks: function(gindex, findex, ftotal) {
		var tmpcnt = '';
		tmpcnt += 'image&nbsp;'+(findex+1)+'&nbsp;of&nbsp;'+ftotal;
		if(findex > 0) tmpcnt += '&nbsp;&nbsp;&nbsp;&nbsp;<a onclick="gPopup.clear(); gPopup.load(\''+gindex+'\','+(findex - 1)+');">&lt;&nbsp;Prev</a>&nbsp;';
		if((findex +1) < ftotal) tmpcnt += '&nbsp;&nbsp;<a onclick="gPopup.clear(); gPopup.load(\''+gindex+'\','+(findex + 1)+');">Next&nbsp;&gt;</a>';
		this.navlinks.innerHTML = tmpcnt;
	},
	load: function(gindex, findex) {
		this.filepath = plgalleries[gindex].files[findex];
		var extension = this.filepath.substr( this.filepath.lastIndexOf(".") +1);
		this.setTitle(plgalleries[gindex].titles[findex]);
		if(extension.substr(0,3) == 'flv'){
			if(extension.substr(9) != ''){
				var sizes = extension.substr(9).split('x');
			}else{
				var sizes = new Array('550','306');
			}
			var s1 = new SWFObject(playerurl+'flvplayer.swf','mpl',sizes[0],sizes[1],'7');
			s1.addVariable("height",sizes[1]);
			s1.addVariable("width",sizes[0]); 
			s1.addVariable("streamName", this.filepath.substring(0, this.filepath.lastIndexOf(".") ) );
			s1.addVariable("skinName",playerurl+'clearskin');
			s1.addVariable("autoPlay",'true');
			s1.write("popupContent");
			gPopup.resize();
		}else{
			var gimage = new Image();
			gimage.src = this.filepath;
			gimage.onclick = this.close.onclick;
			gimage.onload = function(){
				gPopup.resize();
			};
			this.content.appendChild(gimage);
			gPopup.resize();
		}
		this.setLinks(gindex, findex, plgalleries[gindex].files.length);
	},
	show: function(nc) {
		this.div.style.visibility = 'hidden';
		this.div.style.display = 'block';
		this.close.style.display = (nc==true)?'none':'block';
		this.resize();
		this.div.style.visibility = 'visible';
		screenMask.show();
	},
	hide: function(ma) {
		this.clear();
		this.div.style.display = 'none';
		if(ma)	screenMask.hide();
	},
	resize: function() {
		var arrayPageSize = getPageSize();
		var arrayPageScroll = getPageScroll();
		if(this.div.offsetHeight > arrayPageSize[3] && false){
			this.div.style.top = '5px';
		}else{
			this.div.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 15 - this.div.offsetHeight) / 2) + 'px');
		}
		if(this.div.offsetWidth > arrayPageSize[0] && false){
			this.div.style.left = '5px';
		}else{
			this.div.style.left = (((arrayPageSize[0] - this.div.offsetWidth) / 2) + 'px');
		}
		screenMask.height(arrayPageSize[1] + 'px');
	},
	clear: function() {
		var f = null;
		while(f = this.content.firstChild) this.content.removeChild(f);
		while(f = this.navlinks.firstChild) this.navlinks.removeChild(f);
	}
};

function galleryinit(){
	plgalleries = new Array();
	var gallery = false;
	var j = 0;
	var liens = document.getElementsByTagName('a');
	for(i=0; i< liens.length ; i++) {
		if(liens[i].className == 'thickbox' || liens[i].className.substring(0,10) == 'shutterset' ){
			if(!gallery || liens[i].rel != gallery.name){
				if(gallery)	plgalleries[gallery.name] = gallery;
				gallery = new Object();
				gallery.name = liens[i].rel;
				gallery.files = new Array();
				gallery.titles = new Array();
				j = 0;
			}
			gallery.files[j] = liens[i].href;
			gallery.titles[j] = liens[i].getElementsByTagName('img').item(0).title;
			liens[i].fileid = j;
			liens[i].onclick = function(){
				gPopup.show();
				gPopup.load(this.rel, this.fileid);
				return false;
			};
			j++;
		}
	}
	plgalleries[gallery.name] = gallery;
}

(function($){
$(document).ready(function() {
	screenMask.init();
	gPopup.init();
	galleryinit();
});
})(jQuery);