/*
INLINE POPUP SCRIPT

Created By: Oy B&H Ventures Ltd
Website: http://www.bhventures.fi
Date: 2006-09-11

Inspired by the Lightbox v2.02 implementation found at http://www.huddletogether.com/projects/lightbox2/

Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
*/

//
//	Configuration
//
var fileTopNavCloseImage = "skins/_default/images/icons/cross.gif";	

// -----------------------------------------------------------------------------------

//
//	Additional methods for Element added by SU, Couloir
//	- further additions by Lokesh Dhakar (huddletogether.com)
//
Object.extend(Element, {
	getWidth: function(element) {
	   	element = $(element);
	   	return element.offsetWidth; 
	},
	setWidth: function(element,w) {
	   	element = $(element);
    	element.style.width = w +"px";
	},
	setHeight: function(element,h) {
   		element = $(element);
    	element.style.height = h +"px";
	},
	setTop: function(element,t) {
	   	element = $(element);
    	element.style.top = t +"px";
	},
	setSrc: function(element,src) {
    	element = $(element);
    	element.src = src; 
	},
	setHref: function(element,href) {
    	element = $(element);
    	element.href = href; 
	},
	setInnerHTML: function(element,content) {
		element = $(element);
		element.innerHTML = content;
	}
});

// -----------------------------------------------------------------------------------

//
//	InlinePopup Class Declaration
//	- initialize()
//	- start()
//	- enableKeyboardNav()
//	- disableKeyboardNav()
//	- keyboardNavAction()
//	- preloadNeighborImages()
//	- end()
//
//	Structuring of code inspired by Scott Upton (http://www.uptonic.com/)
//
var InlinePopup = Class.create();

InlinePopup.prototype = {
	
	//
	// initialize()
	//
	initialize: function() {	
	
		// The rest of this code inserts html at the bottom of the page that looks similar to this:
		//
		//	<div id="inlinePopup">
		//		<div id="inlinePopup_topNav">
		//			<div id="inlinePopup_topNavClose">
		//				<a href="#" id="inlinePopup_topNavCloseLink">
		//					<img src="images/closelabel.gif">
		//				</a>
		//			</div>
		//		</div>
		//		<div id="inlinePopup_html">
		//			<div id="inlinePopup_htmlContent">
		//			</div>
		//		</div>
		//	</div>


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


		var objOverlay = document.createElement("div");
		objOverlay.setAttribute('id','inlinePopup_overlay');
		objOverlay.style.display = 'none';
		objOverlay.onclick = function() { myInlinePopup.end(); return false; }
		objBody.appendChild(objOverlay);

		

		var objInlinePopup = document.createElement("div");
		objInlinePopup.setAttribute('id','inlinePopup');
		objInlinePopup.style.display = 'none';
		objBody.appendChild(objInlinePopup);



		var objTopNav = document.createElement("div");
		objTopNav.setAttribute('id','inlinePopup_topNav');
		objTopNav.style.display = 'none';
		objInlinePopup.appendChild(objTopNav);

		var objTopNavClose = document.createElement("div");
		objTopNavClose.setAttribute('id','inlinePopup_topNavClose');
		objTopNavClose.style.display = 'none';
		objTopNav.appendChild(objTopNavClose);

		var objTopNavCloseLink = document.createElement("a");
		objTopNavCloseLink.setAttribute('id','inlinePopup_topNavCloseLink');
		objTopNavCloseLink.setAttribute('href','#');
		objTopNavCloseLink.onclick = function() { myInlinePopup.end(); return false; }
		objTopNavClose.appendChild(objTopNavCloseLink);
	
		var objTopNavCloseImage = document.createElement("img");
		objTopNavCloseImage.setAttribute('src', fileTopNavCloseImage);
		objTopNavCloseLink.appendChild(objTopNavCloseImage);





		var objHTML = document.createElement("div");
		objHTML.setAttribute('id','inlinePopup_html');
		objHTML.style.display = 'none';
		objInlinePopup.appendChild(objHTML);

		var objHTMLContent = document.createElement("div");
		objHTMLContent.setAttribute('id','inlinePopup_htmlContent');
		objHTMLContent.style.display = 'none';
		objHTML.appendChild(objHTMLContent);
	},
	
	//
	//	start()
	//
	start: function(ajaxLink) {	

		hideSelectBoxes();

		// stretch overlay to fill page and fade in
		var arrayPageSize = getPageSize();
		Element.setHeight('inlinePopup_overlay', arrayPageSize[1]);
		Element.show('inlinePopup_overlay');

		// calculate top offset for the lightbox and display 
		var arrayPageSize = getPageSize();
		var arrayPageScroll = getPageScroll();
		var inlinePopupTop = arrayPageScroll[1] + (arrayPageSize[3] / 15);

		Element.setTop('inlinePopup', inlinePopupTop);

		Element.setHeight('inlinePopup', ajaxLink.getAttribute('inlinepopup-height'));
		Element.setWidth('inlinePopup_html', ajaxLink.getAttribute('inlinepopup-width'));
		Element.setHeight('inlinePopup_html', ajaxLink.getAttribute('inlinepopup-height')-30); // Room for inlinePopup_topNav
		Element.setWidth('inlinePopup_htmlContent', ajaxLink.getAttribute('inlinepopup-width')-30); // Room for parents scrollbar
		Element.setHeight('inlinePopup_htmlContent', ajaxLink.getAttribute('inlinepopup-height')-30-40); // Room for parents scrollbar and inlinePopup_topNav
		Element.setWidth('inlinePopup_topNav', ajaxLink.getAttribute('inlinepopup-width'));

		Element.show('inlinePopup');
		Element.show('inlinePopup_topNav');
		Element.show('inlinePopup_topNavClose');
		Element.show('inlinePopup_html');
		Element.show('inlinePopup_htmlContent');

		getAXAH(ajaxLink.getAttribute('href'), 'inlinePopup_htmlContent');

		this.enableKeyboardNav();
	},

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

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

	//
	//	keyboardAction()
	//
	keyboardAction: function(e) {
		if (e == null) { // ie
			keycode = event.keyCode;
		} else { // mozilla
			keycode = e.which;
		}

		key = String.fromCharCode(keycode).toLowerCase();
		
		if((key == 'x') || (key == 'o') || (key == 'c')){	// close lightbox
			myLightbox.end();
		} else if(key == 'p'){	// display previous image
			if(activeImage != 0){
				myLightbox.disableKeyboardNav();
				myLightbox.changeImage(activeImage - 1);
			}
		} else if(key == 'n'){	// display next image
			if(activeImage != (imageArray.length - 1)){
				myLightbox.disableKeyboardNav();
				myLightbox.changeImage(activeImage + 1);
			}
		}


	},

	//
	//	end()
	//
	end: function() {
		this.disableKeyboardNav();
		Element.hide('inlinePopup');
		Element.hide('inlinePopup_overlay');
		showSelectBoxes();
	}
}

// -----------------------------------------------------------------------------------

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
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;
}

// -----------------------------------------------------------------------------------

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
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;
}

// -----------------------------------------------------------------------------------

//
// getKey(key)
// Gets keycode. If 'x' is pressed then it hides the lightbox.
//
function getKey(e){
	if (e == null) { // ie
		keycode = event.keyCode;
	} else { // mozilla
		keycode = e.which;
	}
	key = String.fromCharCode(keycode).toLowerCase();
	
	if(key == 'x'){
	}
}

// -----------------------------------------------------------------------------------

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

function showSelectBoxes(){
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
}

// ---------------------------------------------------

function hideSelectBoxes(){
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
}

// ---------------------------------------------------





function initInlinePopup() { myInlinePopup = new InlinePopup(); }
