
var overlayOpacity = 0.3;
var animate = true;
var resizeSpeed = 7;
var pageArray = new Array;
var activeImage;
var borderSize = 1;
var paddingSize = 10;
var noClose = false;

if(animate == true) {
	overlayDuration = 0.2;
	if(resizeSpeed > 10){ resizeSpeed = 10;}
	if(resizeSpeed < 1){ resizeSpeed = 1;}
	resizeDuration = (11 - resizeSpeed) * 0.15;
} else {
	overlayDuration = 0;
	resizeDuration = 0;
}


var Popup = {

	active: true,
	opened: false,
	loaded: false,
	openit: null,
	defaultWidth:320,
	defaultHeight:200,

	initialize: function() {

	},

	load: function() {

		var objBody = document.getElementsByTagName("body").item(0);

		var objOverlay = new Element('div',{'id':'popupOverlay'});
		objOverlay.style.display = 'none';
		objBody.appendChild(objOverlay);

		var objPopup = new Element("div", {'id':'popup'});
		objPopup.style.display = 'none';
		objBody.appendChild(objPopup);

		var objPopupContentContainer = new Element("div", {'id':'popupContentContainer'});
		objPopup.appendChild(objPopupContentContainer);
		if(animate){
			Element.setStyle('popupContentContainer', {width:"250px"});
			Element.setStyle('popupContentContainer', {height:"1px"});
		} else {
			Element.setStyle('popupContentContainer', {width:"250px"});
			Element.setStyle('popupContentContainer', {height:"1px"});
		}

		var objPopupContent = new Element("div", {'id':'popupContent', 'border':0});
		objPopupContentContainer.appendChild(objPopupContent);

		var objPopupContentPage = new Element("iframe", {'id':'popupContentPage', 'scrolling':'no'});
		if(Prototype.Browser.IE) objPopupContentPage.frameBorder = 0;
		objPopupContent.appendChild(objPopupContentPage);

		var objHoverNav = new Element("div", {'id':'popupHoverNav'});
		objPopupContentContainer.appendChild(objHoverNav);

		var objPrevLink = document.createElement("a");
		objPrevLink.setAttribute('id','popupPrevLink');
		objPrevLink.setAttribute('href','#');
		objHoverNav.appendChild(objPrevLink);

		var objNextLink = document.createElement("a");
		objNextLink.setAttribute('id','popupNextLink');
		objNextLink.setAttribute('href','#');
		objHoverNav.appendChild(objNextLink);

		var objCloseLink = new Element("a", {'id':'popupCloseLink', 'href': '#'});
		objCloseLink.onclick = function() { Popup.end(); return false; }
		objHoverNav.appendChild(objCloseLink);

		Element.setStyle('popupContent',{padding:paddingSize+"px"});

		$$('a[rel^=popup]').each(function(anchor) {
			Event.observe(anchor,'click', function(ev) {Popup.open(ev);});
		});

		this.loaded = true;
		if(this.openit) {
			this.start(this.openit);
		}
	},

	activate: function () {
		Popup.active = true;
	},

	deactivate: function () {
		Popup.active = false;
	},

	open: function (ev) {
		if(ev) Event.stop(ev);
		if(Popup.active) {
			var el = Event.element(ev);
			el = el.up('a') ? el.up('a') : el;
			Popup.start(el);
		}
	},

	start: function(el, notClosable) {
		
		if(!this.loaded) {
			
			this.openit = el;
			
		} else {
		
			noClose = (notClosable) ? true : noClose;
			if(noClose) {
				Element.hide('popupCloseLink');
				$('popup').onclick = function(e) {}
			} else {
				Element.show('popupCloseLink');
				$('popup').onclick = function(e) {
					if (!e) var e = window.event;
					var clickObj = Event.element(e).id;
					if ( clickObj == 'popup') Popup.end();
				};
			}
			if($('popupPrevLink')) Element.hide('popupPrevLink');
			if($('popupNextLink')) Element.hide('popupNextLink');
	
			$$('select').each(function(e) {Element.hide(e);});
			$$('object').each(function(e) {Element.hide(e);});
			$$('embed').each(function(e) {Element.hide(e);});
			
			// stretch overlay to fill page and fade in
			var arrayPageSize = getPageSize();
			Element.setStyle('popupOverlay', {width:arrayPageSize[0]+'px'});
			Element.setStyle('popupOverlay', {height:arrayPageSize[1]+'px'});
			new Effect.Appear('popupOverlay', { duration: 0.2, from: 0.0, to: overlayOpacity });
			//Element.show('popupOverlay');
	
			pageArray = [];
			pageNum = 0;
	
			var arrayPageScroll = getPageScroll();
			var popupLeft = arrayPageScroll[0];
			var popupTop = arrayPageScroll[1];
	
			if((Element.readAttribute(el, 'rel') == 'popup')) {
				if(Element.readAttribute(el, 'rev')) {
					var aDim = Element.readAttribute(el,'rev').match(/[0-9]+/g);
					var pageWidth = (aDim && (parseInt(aDim[0]) > 0)) ? parseInt(aDim[0]) : 0;
					var pageHeight = (aDim && (parseInt(aDim[1]) > 0)) ? parseInt(aDim[1]) : 0;
					popupTop += ((arrayPageSize[3]-pageHeight)/3);
				} else {
					var pageWidth = Popup.defaultWidth;
					var pageHeight = Popup.defaultHeight;
					popupTop += ((arrayPageSize[3]-pageHeight)/3);
				}
				pageArray.push(new Array(Element.readAttribute(el, 'href'), Element.readAttribute(el,'title'), pageWidth, pageHeight));
			} else {
				$$(el.tagName).each(function(anchor) {
					if (Element.readAttribute(anchor, 'href') && (Element.readAttribute(anchor,'rel') == Element.readAttribute(el, 'rel'))) {
						if(Element.readAttribute(anchor, 'rev')) {
							var aDim = Element.readAttribute(anchor, 'rev').match(/[0-9]+/g);
							var pageWidth = (aDim && (parseInt(aDim[0]) > 0)) ? parseInt(aDim[0]) : 0;
							var pageHeight = (aDim && (parseInt(aDim[1]) > 0)) ? parseInt(aDim[1]) : 0;
							//popupTop += ((arrayPageSize[3]-pageHeight)/3);
						} else {
							var pageWidth = Popup.defaultWidth;
							var pageHeight = Popup.defaultHeight;
							//popupTop += ((arrayPageSize[3]-pageHeight)/3);
						}
						pageArray.push(new Array(Element.readAttribute(anchor, 'href'), Element.readAttribute(anchor, 'title'), pageWidth, pageHeight));
					}
				});
				pageArray.removeDuplicates();
				while(pageArray[pageNum][0] != Element.readAttribute(el, 'href')) { pageNum++;}
				popupTop += ((arrayPageSize[3]-pageArray[pageNum][3])/3);
			}
			Element.setStyle('popup', {left:popupLeft+'px'});
			Element.setStyle('popup', {top:popupTop+'px'});
	
			Element.show('popup');
			Popup.opened = true;
			Popup.changePage(pageNum);
		}
	
	},

	changePage: function(pageNum) {

		Element.hide('popupHoverNav');
		activePage = pageNum;
		Popup.resize(pageArray[activePage][2], pageArray[activePage][3]);

	},

	resize: function( pageWidth, pageHeight) {

		Element.hide('popupContentPage');
		Element.writeAttribute('popupContentPage', 'src', '');

		Popup.widthCurrent = Element.getWidth('popupContentContainer');
		Popup.heightCurrent = Element.getHeight('popupContentContainer');

		var widthNew = (pageWidth  + (paddingSize * 2));
		var heightNew = (pageHeight  + (paddingSize * 2));

		Popup.xScale = ( widthNew / Popup.widthCurrent) * 100;
		Popup.yScale = ( heightNew / Popup.heightCurrent) * 100;

		wDiff = Popup.widthCurrent - widthNew - (2*borderSize);
		hDiff = Popup.heightCurrent - heightNew - (2*borderSize);

		if(!( hDiff == 0)){ new Effect.Scale('popupContentContainer', Popup.yScale, {scaleX: false, duration: resizeDuration}); }
		if(!( wDiff == 0)){ new Effect.Scale('popupContentContainer', Popup.xScale, {scaleY: false, delay: resizeDuration, duration: resizeDuration}); }

		if((hDiff == 0) && (wDiff == 0)){
			//if (navigator.appVersion.indexOf("MSIE")!=-1){ pause(250); } else { pause(100);}
		}

		Element.setStyle('popupHoverNav', {width:widthNew+'px'});
		Element.setStyle('popupHoverNav', {height:heightNew+'px'});
		new Effect.Appear('popupContentContainer', {delay: resizeDuration, duration: resizeDuration, afterFinish:function(){Popup.setPage(pageWidth, pageHeight);} });

	},

	setSize: function( pageWidth, pageHeight) {

		Popup.widthCurrent = Element.getWidth('popupContentContainer');
		Popup.heightCurrent = Element.getHeight('popupContentContainer');

		var widthNew = (pageWidth  + (paddingSize * 2));
		var heightNew = (pageHeight  + (paddingSize * 2));

		Popup.xScale = ( widthNew / Popup.widthCurrent) * 100;
		Popup.yScale = ( heightNew / Popup.heightCurrent) * 100;

		wDiff = Popup.widthCurrent - widthNew - (2*borderSize);
		hDiff = Popup.heightCurrent - heightNew - (2*borderSize);

		if(!( hDiff == 0)){ new Effect.Scale('popupContentContainer', Popup.yScale, {scaleX: false, duration: resizeDuration}); }
		if(!( wDiff == 0)){ new Effect.Scale('popupContentContainer', Popup.xScale, {scaleY: false, delay: resizeDuration, duration: resizeDuration}); }

		Element.setStyle('popupHoverNav', {width:widthNew+'px'});
		Element.setStyle('popupHoverNav', {height:heightNew+'px'});
		Element.setStyle('popupContentPage', {width:pageWidth+'px'});
		Element.setStyle('popupContentPage', {height:pageHeight+'px'});

	},

	setPage: function(pageWidth, pageHeight) {

		Element.show('popupContentPage');
		Element.setStyle( 'popupContentPage', {width:pageWidth+'px'});
		Element.setStyle( 'popupContentPage', {height:pageHeight+'px'});
		var url = (parseUri(pageArray[activePage][0]).query!="") ? pageArray[activePage][0]+"&popup=true" : pageArray[activePage][0]+"?popup=true";
		Element.writeAttribute('popupContentPage', 'src', url);
		Element.show('popupHoverNav');

		if(activePage > 0) {
			Element.show('popupPrevLink');
			Event.observe($('popupPrevLink'),'click', function(ev) {Popup.changePage(activePage-1);});
		} else {
			Element.hide('popupPrevLink');
		}
		if(activePage < (pageArray.length - 1)) {
			Element.show('popupNextLink');
			Event.observe($('popupNextLink'),'click', function(ev) {Popup.changePage(activePage+1);});
		} else {
			Element.hide('popupNextLink');
		}

		this.enableKeyboardNav();

	},

	enableKeyboardNav: function() {
		document.onkeydown = Popup.keyboardAction;
	},

	disableKeyboardNav: function() {
		document.onkeydown = '';
	},

	keyboardAction: function(e) {

		if (e == null) { // ie
			keycode = event.keyCode;
			escapeKey = 27;
		} else { // mozilla
			keycode = e.keyCode;
			escapeKey = e.DOM_VK_ESCAPE;
		}
		key = String.fromCharCode(keycode).toLowerCase();
		if((key == 'x') || (key == 'o') || (key == 'c') || (keycode == escapeKey)){	// close popup
			Popup.end();
		} else if((key == 'p') || (keycode == 37)){	// display previous page
			if(activePage != 0){
				Popup.disableKeyboardNav();
				Popup.changePage(activePage - 1);
			}
		} else if((key == 'n') || (keycode == 39)){	// display next page
			if(activePage != (pageArray.length - 1)){
				Popup.disableKeyboardNav();
				Popup.changePage(activePage + 1);
			}
		}

	},

	end: function() {
		Popup.disableKeyboardNav();
		Popup.opened = false;
		Element.hide('popup');
		Element.hide('popupOverlay');
		//new Effect.Fade('popupOverlay', { duration: overlayDuration});
		$$('select').each(function(e) {Element.show(e);});
		$$('object').each(function(e) {Element.show(e);});
		$$('embed').each(function(e) {Element.show(e);});
	}

};

//------------------------------------------------------------------------------
// Parsing
//------------------------------------------------------------------------------

function parseUri(str) {
	var	o   = parseUri.options,
	m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
	uri = {},
	i   = 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});

	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};

//------------------------------------------------------------------------------
// Graphics
//------------------------------------------------------------------------------

function getPageScroll() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;
	}
	arrayPageScroll = new Array(xScroll,yScroll)
	return arrayPageScroll;
}

function getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = window.innerWidth + window.scrollMaxX;
		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
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth;
		} else {
			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;
	}
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}
	if(xScroll < windowWidth){
		pageWidth = xScroll;
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}


function getKey(e) {
	if (e == null) { // ie
		keycode = event.keyCode;
	} else { // mozilla
		keycode = e.which;
	}
	key = String.fromCharCode(keycode).toLowerCase();

	if(key == 'x'){
	}
}

function listenKey () {	document.onkeypress = getKey; }

