/*
Created By: Chris Campbell
Modified By: Noah Winecoff (http://www.findmotive.com)
Website: http://particletree.com
Date: 2/1/2006

Modified By: Nelle (www.parkside.at)
Date: 01/04/2008

*/

/////////////////// Global configuration
/// Remember to include the scriptaculous in x_scripts when changing this setting
/// 			and not to use background image
var g_use_scriptaculous = false; /// FadeIn/FadeOut

//
//	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;
	}
});


///// Implementation of the PFrame
if(typeof parkside == "undefined") var parkside = new Object();
if(typeof parkside.PFrame == "undefined") parkside.PFrame = new Object();

// Constructor
parkside.PFrame = function() {
}

parkside.PFrame.prototype = {

	yPos : 0,
	xPos : 0,
	content : "",
	width : 0,
	height : 0,
	processing: false,
	overlayActive: false,

	/// Adds HTML markup
	initialise : function() {
		bod = document.getElementsByTagName('body')[0];

		/// Overlay
		var objOverlay 				= document.createElement("div");
		objOverlay.id 				= 'pframeOverlay';
		objOverlay.style.display 	= 'none';
		
		/// Container DIV
		var objContainer			= document.createElement('div');
		objContainer.id				= 'pframeContainer';
			
		/// Content DIV
		var objContent 				= document.createElement('div');
		objContent.id 				= 'pframeContent';
			
		/// Create an IFrame
		var objIFrame 				= document.createElement('iframe');
		objIFrame.id 				= 'pframeIFrame';
		try {
			if (objIFrame.scrolling)
				objIFrame.scrolling = 'vertical';
		} catch(e) {
			objIFrame.scrolling 	= 'auto';
		}
		
		objIFrame.frameBorder 		= "none";
		objIFrame.style.border 		= 'none';
		
		
		var objTopCorner 			= document.createElement('div');
		objTopCorner.id 			= 'pframeTopCorner';
		objTopCorner.className 		= 'pframe-top-corner';
		
		var objBottomCorner 		= document.createElement('div');
		objBottomCorner.id 			= 'pframeBottomCorner';
		objBottomCorner.className 	= 'pframe-btm-corner';
			
		var objCloseButton 			= document.createElement('a');
		objCloseButton.href 		= "#";
		objCloseButton.className 	= "pframe-btn-close";
		objCloseButton.onclick 		= this.deactivateOverlay;
	
		objContent.appendChild( objCloseButton );
		objContent.appendChild( objIFrame );

		objContainer.appendChild ( objTopCorner );
		objContainer.appendChild ( objContent );
		objContainer.appendChild ( objBottomCorner );
		
		bod.appendChild(objContainer);
		
		bod.appendChild(objOverlay);
	},
	
	/// Activates the popup
	activate : function(event, ctrl) {
		if (!ctrl) 
			return;
		
		/// get the address
		this.content = ctrl.rel;
		
		/// get the window size
		var splitted = ctrl.id.split('_');

		this.width = splitted[1];
		this.height = splitted[2];
		
		if (parent && parent.pFrame && parent.pFrame.overlayActive)
			this.changeURL();
		else
			this.activateOverlay();
	},
	
	/// Changes active URL
	changeURL : function() {
		parent.lightboxIframeHistory.push( parent.document.getElementById("pframeIFrame").src );
		parent.document.getElementById("pframeBackButton").onclick = parent.staticBackButton;
		parent.document.getElementById("pframeBackButton").style.visibility = 'visible';
		parent.document.getElementById("pframeBackButton").style.display = "block";
		parent.document.getElementById("pframeIFrame").src = this.content;	
	},
	
	/// Shows the popup
	activateOverlay : function() {
	
		if (Prototype.Browser.IE) {
			this.getScroll();
			this.prepareIE('100%', 'hidden');
			this.setScroll(0,0);
		}

		/// Hide Flash Elements
		this.hideFlash();
		
		// stretch overlay to fill page and fade in
		var arrayPageSize = this.getPageSize();
		Element.setHeight('pframeOverlay', arrayPageSize[1]);

		/// use scriptaculous
		try {
			if (g_use_scriptaculous) {
				new Effect.Appear('pframeOverlay', { duration: 1.0, from: 0.0, to: 0.8, queue: 'end' });
			} else {
				document.getElementById('pframeOverlay').style.display = 'block';
			}
		} catch(e) {
			/// or not
			document.getElementById('pframeOverlay').style.display = 'block';
		}

		/// Add mouse handler on overlay when the user clicks outside the area
		$('pframeOverlay').onclick = function(e) {
			if (!e) var e = window.event;
			var clickObj = Event.element(e).id;
			if ( clickObj == 'pframeOverlay') {
				pFrame.deactivateOverlay();
			}
		};
		
		/// Add mouse handler on container when the user clicks outside the area
		$('pframeContainer').onclick = function(e) {
			if (!e) var e = window.event;
			var clickObj = Event.element(e).id;
			if ( clickObj == 'pframeContainer') {
				pFrame.deactivateOverlay();
			}
		};

		$('pframeContainer').style.display = 'block';
		this.loadInfo();
	},
	
	/// Sets widthand height of the iframe and the surrounding content div
	loadInfo: function() {	 
		$('pframeContent').style.width = this.width + "px";
		
		$('pframeIFrame').style.width = this.width + "px";
		$('pframeIFrame').style.height = this.height + "px";
		$('pframeIFrame').width = this.width;
		$('pframeIFrame').height = this.height;
		$('pframeIFrame').src = this.content;
	},
	
	/// Hides everything
	deactivateOverlay : function() {
		try {
			if (g_use_scriptaculous) {
				new Effect.Fade('pframeContainer', { duration: 0.3 } );
				new Effect.Fade('pframeOverlay', { duration: 0.3, afterFinish: this.showFlash() } );
			} else {
				document.getElementById('pframeContainer').style.display = "none";
				document.getElementById('pframeOverlay').style.display = "none";
				this.showFlash();				
			}
			$('pframeIFrame').src = "";
		} catch(e) {
			document.getElementById('pframeContainer').style.display = "none";
			document.getElementById('pframeOverlay').style.display = "none";
			this.showFlash();
			$('pframeIFrame').src = "";
		}
		if (Prototype.Browser.IE) {
			this.prepareIE('auto', 'auto');
			this.setScroll(0,this.yPos);
		}
	},
	
	// Ie requires height to 100% and overflow hidden or else you can scroll down past the lightbox_iframe
	prepareIE: function(height, overflow){
		bod = document.getElementsByTagName('body')[0];
		bod.style.height = height;
		bod.style.overflow = overflow;
  
		htm = document.getElementsByTagName('html')[0];
		htm.style.height = height;
		htm.style.overflow = overflow; 
	},
	
	// show all flash elements
	showFlash : function() {			
/*		var embeds = document.getElementsByTagName('embed');
		for(i = 0; i < embeds.length; i++) {
			embeds[i].parentNode.style.visibility = 'visible';
		}
		
		var selects = document.getElementsByTagName('select');
		for(i = 0; i < selects.length; i++) {
			selects[i].style.visibility = 'visible';
		}
		
		objects = document.getElementsByTagName('object');
		for(i = 0; i < objects.length; i++) {
			objects[i].parentNode.style.visibility = 'visible';
		}*/
	
		document.getElementById('infobox_obj').style.visibility = 'visible';
		document.getElementById('main-menu').style.visibility = 'visible';
		var contfeat = document.getElementById('content_feature');
		if(contfeat){ 
			contfeat.style.visibility = 'visible';
		}
		
		if (typeof(sIFR) != "undefined")
			do_sIFR();
	},
	
	// show all flash elements
	hideFlash : function() {			
/*		var embeds = document.getElementsByTagName('embed');
		for(i = 0; i < embeds.length; i++) {
			embeds[i].parentNode.style.visibility = 'hidden';
		}
		
		var selects = document.getElementsByTagName('select');
		for(i = 0; i < selects.length; i++) {
			selects[i].style.visibility = 'hidden';
		}
		
		objects = document.getElementsByTagName('object');
		for(i = 0; i < objects.length; i++) {
			objects[i].parentNode.style.visibility = 'hidden';
		}*/
		
		document.getElementById('infobox_obj').style.visibility = 'hidden';
		document.getElementById('main-menu').style.visibility = 'hidden';
		var contfeat = document.getElementById('content_feature');
		if(contfeat){ 
			contfeat.style.visibility = 'hidden';
		}
		
		if (typeof(sIFR) != "undefined")
			sIFR.rollback();
	},
	
	/// determines the scroll position of the browser
	getScroll : function() {			
		if (self.pageYOffset) {
			this.yPos = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop) {
			this.Ypos = document.documentElement.scrollTop; 
		} else if (document.body) {
			this.Ypos = document.body.scrollTop;
		}
	},
	
	/// sets the scroll position of the browser
	setScroll : function( x, y ) {			
		window.scrollTo(x, y); 
	},
	
	//
	// getPageSize()
	// Returns array with page width, height and window width, height
	// Core code from - quirksmode.org
	// Edit for Firefox by pHaez
	//
	getPageSize: function(){	
		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;
	}
}

/// Singelton only one instance pro page
var pFrame = new parkside.PFrame();

/*-----------------------------------------------------------------------------------------------*/
// Onload, make all links that need to trigger active
function initializePFrame(){
	pframeLinks = document.getElementsByClassName('pframeLink');
	
	if (pframeLinks.length) {
		pFrame.initialise();
	}
		
	for(i = 0; i < pframeLinks.length; i++) {
		Event.observe(pframeLinks[i], 'click', pFrame.activate.bindAsEventListener(pFrame, pframeLinks[i]), false);
		pframeLinks[i].onclick = function() {return false;};	
	}
}

Event.observe(window, 'load', initializePFrame, false);