/*	name			: ClassBehaviours, the javascript framework based on class-name parsing	update			: 9.3.17	author			: Maurice van Creij	dependencies	: jquery.classbehaviours.js	info			: http://www.classbehaviours.com/

    This file is part of jQuery.classBehaviours.
    
    ClassBehaviours is a javascript framework based on class-name parsing.
    Copyright (C) 2008  Maurice van Creij

    ClassBehaviours is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    ClassBehaviours is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with ClassBehaviours. If not, see http://www.gnu.org/licenses/gpl.html.*/

	// create the jQuery object if it doesn't already exist
	if(typeof(jQuery)=='undefined') jQuery = function(){};
	
	// create the root classbehaviours object if it doesn't already exist
	if(typeof(jQuery.classBehaviours)=='undefined') jQuery.classBehaviours = function(){};
	
	// create the handlers child object if it doesn't already exist
	if(typeof(jQuery.classBehaviours.handlers)=='undefined') jQuery.classBehaviours.handlers = function(){}
	
	// Open an overlay as a popup window
	jQuery.classBehaviours.handlers.openLayerPopUp = {
		// properties
		name: 'openLayerPopUp',
		popUpClass: 'layerPopUp',
		step: 10,
		begin: 0,
		end: 50,
		index: 0,
		// methods
		start: function(node){
			// find the target layer
			targetPopUp = null;
			// the node's open button
			node.onclick = this.load;
			// open the popup immediately if required
			node.id = (node.id) ? node.id : this.name + 'Link' + this.index++ ;
			if(jQuery.classBehaviours.utilities.getClassParameter(node, 'auto', 'no')=='yes')
				setTimeout('jQuery.classBehaviours.handlers.openLayerPopUp.load(document.getElementById("'+node.id+'"))', 250);
		},
		fadeIn: function(id, amount){
			var olp = jQuery.classBehaviours.handlers.openLayerPopUp;
			node = document.getElementById(id);
			nodes = node.getElementsByTagName('div');
			nodeShadow = nodes[0];
			nodeContent = nodes[1];
			// if the amount is not 50
			if(amount<olp.end){
				// hide the popup content, but show the outside
				node.style.visibility = 'visible';
				nodeContent.style.visibility = 'hidden';
				// set the shadow's fade to the next step
				nodeShadow.style.display = 'block';
				if(typeof(nodeShadow.style.MozOpacity)!='undefined')	nodeShadow.style.MozOpacity = amount/100;
				if(typeof(nodeShadow.style.filter)!='undefined')		nodeShadow.style.filter = "alpha(opacity=" + amount + ")";
				if(typeof(nodeShadow.style.opacity)!='undefined')		nodeShadow.style.opacity = amount/100;
				// show the popup collection
				node.style.display = 'block';
				// repeat the fade
				setTimeout("jQuery.classBehaviours.handlers.openLayerPopUp.fadeIn('" + id + "'," + (amount+olp.step) + ")",10);
			}else{
				// show the popup content
				nodeContent.style.visibility = 'visible';
			}
		},
		fadeOut: function(id, amount){
			var olp = jQuery.classBehaviours.handlers.openLayerPopUp;
			node = document.getElementById(id);
			nodes = node.getElementsByTagName('div');
			nodeShadow = nodes[0];
			nodeContent = nodes[1];
			// if the amount is not 100
			if(amount>olp.begin){
				// hide the popup content
				nodeContent.style.visibility = 'hidden';
				// set the fade to the next step
				if(typeof(nodeShadow.style.MozOpacity)!='undefined')	nodeShadow.style.MozOpacity = amount/100;
				if(typeof(nodeShadow.style.filter)!='undefined')		nodeShadow.style.filter = "alpha(opacity=" + amount + ")";
				if(typeof(nodeShadow.style.opacity)!='undefined')		nodeShadow.style.opacity = amount/100;
				// repeat the fade
				setTimeout("jQuery.classBehaviours.handlers.openLayerPopUp.fadeOut('" + id + "'," + (amount-olp.step) + ")",10);
			}else{
				// hide the popup content
				node.style.display = 'none';
				// hide the popup's shadow
				nodeShadow.style.display = 'none';
			}
		},
		loadUrl: function(strXmlUrl, strHrefUrl, strId, strTitle, intStep, intBegin, intEnd){
			var olp = jQuery.classBehaviours.handlers.openLayerPopUp;
			// create a new link
			newA = document.createElement('a');
			newA.id = strId + 'Opener'
			newA.href = strXmlUrl + '?url=' + strHrefUrl;
			if(strTitle!=null) newA.title = strTitle;
			newA.className = 'openLayerPopUp id_' + strId; 
			if(intStep!=null) newA.className += 'step_' + intStep; 
			if(intBegin!=null) newA.className += 'begin_' + intBegin; 
			if(intEnd!=null) newA.className += 'end_' + intEnd; 
			newA.style.display = 'none';
			document.body.appendChild(newA);
			// trigger it like a click
			olp.load(document.getElementById(strId + 'Opener'), strId);
		},
		// events
		load: function(that){
			var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
			var olp = jQuery.classBehaviours.handlers.openLayerPopUp;
			// get the popup id
			popUpHref = (objNode.href) ? objNode.href : '' ;
			popUpId = (popUpHref.indexOf('#')>-1) ? popUpHref.split('#')[1] : jQuery.classBehaviours.utilities.getClassParameter(objNode, 'id', 'popUpWithScrollBar') ;
			// if the popup is allready loaded
			popUpObj = document.getElementById(popUpId);
			if(popUpObj){
				olp.show(objNode, popUpId);
			}else{
				jQuery.classBehaviours.ajax.addRequest(popUpHref, olp.insert, olp.wait, null, objNode);
			}
			// cancel the click
			return false;
		},
		wait: function(progress, referNode){
			var olp = jQuery.classBehaviours.handlers.openLayerPopUp;
			// it'd be fun to see a background image behind the link text be the progress-indicator
			referNode.parentNode.style.backgroundPosition = (100-progress*100) +'% 0px';
		},
		insert: function(importedObj, referNode, importedText){
			var olp = jQuery.classBehaviours.handlers.openLayerPopUp;
			//  get the popup's id
			olp.instance += 1;
			popUpId = jQuery.classBehaviours.utilities.getClassParameter(referNode, 'id', olp.name+olp.instance);
			popUpWidth = jQuery.classBehaviours.utilities.getClassParameter(referNode, 'width', '470');
			popUpHeight = jQuery.classBehaviours.utilities.getClassParameter(referNode, 'height', '300');
			// make a new node at the end of the page
			newNode = document.createElement('div');
			document.body.appendChild(newNode);
			// get the node and fill it with delicous html
			allDivs = document.getElementsByTagName('div');
			lastDiv = allDivs[allDivs.length-1];
			importedText = (importedText.indexOf('<body>')>-1) ? importedText.split('<body>')[1].split('</body>')[0] : importedText.replace('<?xml version="1.0"?>', '') ;
			importedText = importedText.replace(/{id}/gi, popUpId);
			importedText = importedText.replace(/{title}/gi, referNode.title);
			importedText = importedText.replace(/{url}/gi, referNode.href.split('?url=')[1]);
			importedText = importedText.replace(/{width}/gi, popUpWidth);
			importedText = importedText.replace(/{height}/gi, popUpHeight);
			lastDiv.innerHTML = importedText;
			// apply any classbehaviours
			jQuery.classBehaviours.parser.parseNode(lastDiv);
			// show the pop-up
			olp.show(referNode, popUpId);
		},
		center: function(popUpObj){
			if(popUpObj){
				// the dimensions can not be measured with "display:none;"
				popUpObj.style.display = 'block';
				// get the content segment
				popupContent = popUpObj.getElementsByTagName('DIV')[2];
				// how high is the content
				popupHeight = popupContent.offsetHeight;
				// how high is the screen
				screenHeight = (window.innerHeight) ? window.innerHeight : document.documentElement.clientHeight ;
				// center the popup
				popupContent.style.marginTop = (screenHeight>popupHeight) ? Math.round((screenHeight-popupHeight)/2) + 'px' : '30px' ;	
			}
		},
		show: function(openerObj, popUpId){
			var olp = jQuery.classBehaviours.handlers.openLayerPopUp;
			var submit = false;
			// adjust the fade parameters if it needs to open instantly
			if(openerObj){
				olp.step = parseInt(jQuery.classBehaviours.utilities.getClassParameter(openerObj, 'step', 10));
				olp.begin = parseInt(jQuery.classBehaviours.utilities.getClassParameter(openerObj, 'begin', 0));
				olp.end = parseInt(jQuery.classBehaviours.utilities.getClassParameter(openerObj, 'end', 50));
			}
			// get the popup
			popUpObj = document.getElementById(popUpId);
			// hide the popup for now
			popUpObj.style.visibility = 'hidden';
			// center the popup
			this.center(popUpObj);
			// if the link had a title and if the popup has a title. put the link title in the popup
			popUpTitles = popUpObj.getElementsByTagName('h1');
			if(openerObj) if(openerObj.title && popUpTitles.length>0) popUpTitles[0].innerHTML = openerObj.title;
			// load a given url in the iframe it it's not there allready
			popUpIframes = popUpObj.getElementsByTagName('iframe');
			if(popUpIframes.length>0 && openerObj.href.indexOf('?url=')>-1){
				if(popUpIframes[0].src!=openerObj.href.split('?url=')[1]) popUpIframes[0].src = openerObj.href.split('?url=')[1];
			}
			// find the close gadget
			popUpClosers = popUpObj.getElementsByTagName('a');
			if(popUpClosers.length>0){
				popUpClosers[0].onclick = olp.hide;
			}
			// remove the scroll bars
			if(navigator.appVersion.indexOf('MSIE 6')>-1 || navigator.appVersion.indexOf('MSIE 5')>-1){
				// manage the scroll positions
				if(popUpObj.className.indexOf('fullHeightPopUp')<0){
					// reset the scroll position
					document.documentElement.scrollLeft = 0;
					document.documentElement.scrollTop = 0;
					// hide the scrollbars if needed
					document.body.parentNode.style.overflow = "hidden";
					// reposition the popup according to the scroll position
					popUpObj.getElementsByTagName('DIV')[2].style.marginTop = Math.round(document.documentElement.scrollTop + (screenHeight-popupHeight)/2) + 'px' ;
				}
				// size the shadow under the popup
				popUpObj.getElementsByTagName('DIV')[0].style.height = document.body.offsetHeight + 'px';
				// hide the selects
				allSelects = document.getElementsByTagName('select');
				for(var a=0; a<allSelects.length; a++) allSelects[a].style.visibility = 'hidden';
			}
			// if there is a popup open already
			if(olp.opened!=null){
				// instantly hide the old one
				document.getElementById(olp.opened).style.display = 'none';
				// instantly show the new one
				document.getElementById(popUpId).style.display = 'block';
				document.getElementById(popUpId).style.visibility = 'visible';
			}else{
				// fade the popup in, but give the contents a little head start
				setTimeout('jQuery.classBehaviours.handlers.openLayerPopUp.fadeIn("'+popUpId+'", '+olp.begin+')', 1000);
			}
			// remember the open popup
			olp.opened = popUpId;
			// cancel the click
			return submit;
		},
		hide: function(that){
			var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
			var olp = jQuery.classBehaviours.handlers.openLayerPopUp;
			// the popup object
			popUp = objNode;
			while(popUp.className.indexOf(olp.popUpClass)<0){
				popUp = popUp.parentNode;
			}
			// fade the popup out
			olp.fadeOut(popUp.id, olp.end);
			// restore the scroll bars
			if(navigator.appVersion.indexOf('MSIE 6')>-1 || navigator.appVersion.indexOf('MSIE 5')>-1){
				// show the scrollbars
				if(popUp.className.indexOf('fullHeightPopUp')<0) document.body.parentNode.style.overflow = "";
				// show the selects
				allSelects = document.getElementsByTagName('select');
				for(var a=0; a<allSelects.length; a++) allSelects[a].style.visibility = 'visible';
			}
			// clear the iframe contents
			popUpIframes = popUp.getElementsByTagName('iframe');
			if(popUpIframes.length>0){
				popUpIframes[0].src = "";
			}
			// note that nothing is opened
			olp.opened = null;
			// cancel the click
			return false;
		}
	}
	
	// move the node to the bottom of the document
	jQuery.classBehaviours.handlers.layerPopUp = {
		// properties
		name: 'layerPopUp',
		// methods
		start: function(node){
			// if this node wasn't moved
			if(node.className.indexOf('doNotMove')<0) this.process(node);
		},
		process: function(objNode){
			// take this node
		 	removedChild = objNode.parentNode.removeChild(objNode);
		 	removedChild.className += ' doNotMove';
			// and put it at the end of the document
			document.body.appendChild(removedChild);
		}
	}
	
	// use a button to hide the popup layer
	jQuery.classBehaviours.handlers.closeLayerPopUp = {
		// properties
		name: 'closeLayerPopUp',
		// methods
		start: function(node){
			node.onclick = this.closePopUp;
		},
		closePopUp: function(that){
			var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
			// deal with framesets
			if(parent!=self){
				allPopUps = parent.jQuery.classBehaviours.utilities.getElementsByClassName('layerPopUp');
				for(var a=0; a<allPopUps.length; a++) parent.jQuery.classBehaviours.handlers.openLayerPopUp.hide(allPopUps[a]);
			}else{
				// get the root of this popup
				rootNode = jQuery.classBehaviours.utilities.rootNode(objNode, null, null, 'layerPopUp');
				// close the layer
				jQuery.classBehaviours.handlers.openLayerPopUp.hide(rootNode);
			}
		}
	}
	
	// add this addon to the jQuery object
	if(typeof(jQuery.fn)!='undefined'){
		// extend jQuery with this method
		jQuery.fn.openLayerPopUp = function(){
			return this.each(
				function(){
					jQuery.classBehaviours.handlers.openLayerPopUp.start(this);
				}
			);
		};
		// extend jQuery with this method
		jQuery.fn.layerPopUp = function(){
			return this.each(
				function(){
					jQuery.classBehaviours.handlers.layerPopUp.start(this);
				}
			);
		};
		// extend jQuery with this method
		jQuery.fn.closeLayerPopUp = function(){
			return this.each(
				function(){
					jQuery.classBehaviours.handlers.closeLayerPopUp.start(this);
				}
			);
		};
		// set the event handler for this jQuery method
		$(document).ready(
			function(){
				$(".openLayerPopUp").openLayerPopUp();
				$(".openLayerPopUp").layerPopUp();
				$(".openLayerPopUp").closeLayerPopUp();
			}
		);
	}

