/*
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 v1.0

Modified By: Nelle (www.parkside.at)
Date: 27.02.2009 v1.2 
	no longer dependant on prototype.js
	
Modified By: Nelle (www.parkside.at)
Date: 10.04.2009 v1.3
	now uses ajax instead of iframe to load content
	
	the url is now fixed to ajax.<link href>
	
	can be redirected using .htacces to a cfc
		RewriteRule ^ajax\.(.*)\.([0-9]*)\.([0-9]*)\.htm$ custom/ajax_components.cfc?method=$1&menu_id=$2&key_value=$3

	the cfc must/should have the following structure
	<cffunction name="method_name" access="remote" returntype="struct">	
		<!--- params --->
		
		<cfset ajax_result=structNew()>
		<cfset ajax_result.html = "some html">
		
		<cfreturn ajax_result>
	</cffunction>

	following html must be included in every page (x_pframe_init.cfm):
	<div id="pframeOverlay"></div>
	<div id="pframeContainer">
		<div id="pframeTopCorner"></div>
		<div id="pframeContent"></div>
		<div id="pframeBottomCorner"></div>
	</div>
	
	links:
	<a onclick='this.call(parkside.PFrame.activate);' href=''></a>

*/

if(typeof parkside == "undefined") var parkside = new Object();

parkside.Browser = {
    IE:     !!(window.attachEvent && !window.opera),
    Opera:  !!window.opera,
    WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1,
    Gecko:  navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1,
    MobileSafari: !!navigator.userAgent.match(/Apple.*Mobile.*Safari/)
};
  
  
var $ = function(element) {
	if (typeof element == "string")
		return document.getElementById(element);
	else 
		return element;
	throw ("element not found");
}

/////////////////// 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 = function(destination, source) {
  for (var property in source)
    destination[property] = source[property];
  return destination;
};

var Element = {
	getWidth: function(element) {
	   	element = $(element);
	   	return element.offsetWidth; 
	},
	getHeight: function(element) {
	   	element = $(element);
	   	return element.offsetHeight; 
	},
	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();

parkside.PFrame = {

	yPos : 0,
	xPos : 0,
	content : "",
	width : 0,
	height : 0,
	processing: false,
	overlayActive: false,
	
	resizeTimeout: 0,
	
	addCloseButton : function() {
		var objCloseButton 			= document.createElement('a');
		objCloseButton.href 		= "javascript:parkside.PFrame.deactivateOverlay();";
		objCloseButton.className 	= "pframe-btn-close";
		objCloseButton.id 			= "pframeBtnClose";
		
		document.getElementById('pframeContent').appendChild( objCloseButton );
	},
	
	/// Activates the popup
	/// activate method is called as a link method through object masquerading (don't ask me why), so the "this" pointer is actually the link and not the pframe object
	activate : function(mouseevent) {	

		if (parent && parent.parkside.PFrame && parent.parkside.PFrame.overlayActive) {
			parent.lightboxIframeHistory.push( parent.parkside.PFrame.content );
			parent.parkside.PFrame.content = this.href.substring(0, this.href.lastIndexOf('/')) + "/ajax." + this.href.substring(this.href.lastIndexOf('/') + 1, this.href.length);
			parent.parkside.PFrame.loadInfo();
		} else {
			parkside.PFrame.content = this.href.substring(0, this.href.lastIndexOf('/')) + "/ajax." + this.href.substring(this.href.lastIndexOf('/') + 1, this.href.length);
			parkside.PFrame.activateOverlay();
		}
	},
	
	/// Changes active URL
	changeURL : function() {
		
		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() {
	
		this.getScroll();
		//this.prepareIE('100%', 'hidden');
		this.setScroll(0,0);

		/// Hide Flash Elements
		this.hideFlash();
		
		// stretch overlay to fill page and fade in
		parkside.PFrame.setOverlayHeight();

		/// 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) e = window.event;
			var clickObj = e.target ? e.target.id : e.srcElement.id;
			if ( clickObj == 'pframeOverlay') {
				parkside.PFrame.deactivateOverlay();
			}
		};
		
		/// Add mouse handler on container when the user clicks outside the area
		document.getElementById('pframeContainer').onclick = function(e) {
			if(!e) e = window.event;
			var clickObj = e.target ? e.target.id : e.srcElement.id;
			if ( clickObj == 'pframeContainer') {
				parkside.PFrame.deactivateOverlay();
			}
		};
		
		document.getElementById('pframeContainer').style.display = 'block';
		document.getElementById('pframeContent').style.visibility = 'visible';
		this.loadInfo();
		
		this.resizeTimeout = setInterval( "parkside.PFrame.setOverlayHeight()", 100);
	},
	
	/// Sets widthand height of the iframe and the surrounding content div
	loadInfo: function() {	 
		//document.getElementById('pframeContent').style.width = this.width + "px";
		
		if( typeof http == "undefined" ) {
			alert("JSMX not included!");
			return;
		}
		/// falls wir etwas mitschicken möchten
		var params = new Object();
		params.dummy = 1;
	
		document.getElementById('pframeContent').innerHTML = "<img src='images/loading.gif' border='0' alt='' /><br/><br/>";
		parkside.PFrame.addCloseButton();
		
		http( "POST" , this.content, this.onInfoLoaded,  params ); 
	},
	
	onInfoLoaded: function(result) {
		document.getElementById('pframeContent').innerHTML = "";
		parkside.PFrame.addCloseButton();
		document.getElementById('pframeContent').innerHTML += result.html;
		/// Execute javascripts using eval (i am aware of the security risks)
		/// used for sifr, videoplayer, photogallery and all other flash replacing elements
		var x = document.getElementById('pframeContent').getElementsByTagName("script");   
    	for(var i=0;i<x.length;i++) {  
	        eval(x[i].text);  
	    }
		/// Resize overlay AFTER content has been loaded
		parkside.PFrame.setOverlayHeight();
	},
	
	/// Hides everything
	deactivateOverlay : function() {
		try {
			if (g_use_scriptaculous) {
				new Effect.Fade('pframeContainer', { duration: 0.3 } );
				new Effect.Fade('pframeOverlay', { duration: 0.3, afterFinish: parkside.PFrame.showFlash() } );
			} else {
				document.getElementById('pframeContainer').style.display = "none";
				document.getElementById('pframeOverlay').style.display = "none";
				parkside.PFrame.showFlash();				
			}
		} catch(e) {
			document.getElementById('pframeContainer').style.display = "none";
			document.getElementById('pframeOverlay').style.display = "none";
			parkside.PFrame.showFlash();
		}
		//this.prepareIE('auto', 'auto');
		parkside.PFrame.setScroll(0,parkside.PFrame.yPos);
		clearInterval(parkside.PFrame.resizeTimeout);
	},
	
	// 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';
		}
	},
	
	// hide 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';
			objects[i].style.visibility = 'hidden';
		}
	},
	
	/// 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); 
	},
	///
	setOverlayHeight : function() {
		// stretch overlay to fill page and fade in
		var arrayPageSize = parkside.PFrame.getPageSize();
		Element.setHeight('pframeOverlay', arrayPageSize[1]);
	},
	//
	// 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 if (document.documentElement && document.documentElement.scrollHeight > document.documentElement.offsetHeight){ // Explorer 6 strict mode
			xScroll = document.documentElement.scrollWidth;
			yScroll = document.documentElement.scrollHeight;
		} else { // Explorer Mac...would also work in 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;
	}
}

