/*	name			: ClassBehaviours, the javascript framework based on class-name parsing	update			: 9.2.2	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(){}

	// resizes an iframed popup to the document's height
	jQuery.classBehaviours.handlers.popUpLayer = {
		// properties
		name: 'popUpLayer',
		// methods
		start: function(node){
			// if this popup is an iframe
			if(navigator.userAgent.indexOf('MSIE 6')>-1 && node.nodeName=='IFRAME') node.style.height = document.body.offsetHeight + 'px';
		}
	}
	
	// resizes the background of the popup to the document's height
	jQuery.classBehaviours.handlers.popUpBackground = {
		// properties
		name: 'popUpBackground',
		// methods
		start: function(node){
			if(navigator.userAgent.indexOf('MSIE 6')>-1) node.style.height = document.body.offsetHeight + 'px';
		}
	}
	
	// closes a popup from within an iframe
	jQuery.classBehaviours.handlers.closePopUpLayer = {
		// properties
		name: 'closePopUpLayer',
		// methods
		start: function(node){
			if(parent!=self) node.onmousedown = this.closeInParent;
		},
		// events
		closeInParent: function(that){
			var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
			// get the timeout
			timeToWait = jQuery.classBehaviours.utilities.getClassParameter(objNode, 'wait');
			// hide the parent frame of this popup
			allPopUps = parent.jQuery.classBehaviours.utilities.getElementsByClassName('popUpLayer');
			for(var a=0; a<allPopUps.length; a++)
				if(allPopUps[a].nodeName=='IFRAME')
					setTimeout("parent.document.getElementById('" + allPopUps[a].id + "').style.display = 'none'; parent.document.getElementById('" + allPopUps[a].id + "').src = ''", timeToWait);
			// cancel the click
			return false;
		}
	}
	
	// closes a popup from within an iframe
	jQuery.classBehaviours.handlers.openPopUpLayer = {
		// properties
		name: 'openPopUpLayer',
		// methods
		start: function(node){
			// set the event handler for the button
			node.onclick = this.openInFrame;
			node.setAttribute('target', '_self');
			// if "auto" is set, open the popup immediately
			autoOpen = jQuery.classBehaviours.utilities.getClassParameter(node, 'auto');
			if(autoOpen=='yes') this.openInFrame(node);
		},
		// events
		openInFrame: function(that){
			var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
			// if the pop iframe doesn't exist
			iframeName = jQuery.classBehaviours.utilities.getClassParameter(objNode, 'id');
			iframeObject = document.getElementById(iframeName);
			if(iframeObject==null){
				// make a new iframe
				newIframe = document.createElement('IFRAME');
				newIframe.setAttribute('name', iframeName); 
				newIframe.setAttribute('id', iframeName);
				newIframe.setAttribute('class', 'popUpLayer');
				newIframe.setAttribute('allowtransparency', 'true');
				newIframe.setAttribute('scrolling', 'no');
				newIframe.setAttribute('frameborder', 'no');
				newIframe.setAttribute('src', objNode.href);
				// for internet explorer
				newIframe.className = 'popUpLayer';
				newIframe.allowTransparency = true;
				// add it to the page
				document.body.appendChild(newIframe);
				// parse its classbehaviour
				jQuery.classBehaviours.handlers.popUpLayer.start(document.getElementById(iframeName));
			}else{
				// show the iframe
				document.getElementById(iframeName).style.display = 'block';
				// set the iframe url
				window.frames[iframeName].document.location.href = objNode.href;
			}
			// cancel the click
			return false;
		}
	}
	
	// add this addon to the jQuery object
	if(typeof(jQuery.fn)!='undefined'){
		// extend jQuery with this method
		jQuery.fn.popUpLayer = function(){
			return this.each(
				function(){
					jQuery.classBehaviours.handlers.popUpLayer.start(this);
				}
			);
		};
		jQuery.fn.popUpBackground = function(){
			return this.each(
				function(){
					jQuery.classBehaviours.handlers.popUpBackground.start(this);
				}
			);
		};
		jQuery.fn.closePopUpLayer = function(){
			return this.each(
				function(){
					jQuery.classBehaviours.handlers.closePopUpLayer.start(this);
				}
			);
		};
		jQuery.fn.openPopUpLayer = function(){
			return this.each(
				function(){
					jQuery.classBehaviours.handlers.openPopUpLayer.start(this);
				}
			);
		};
		// set the event handler for this jQuery method
		$(document).ready(
			function(){
				$(".popUpLayer").popUpLayer();
				$(".popUpBackground").popUpBackground();
				$(".closePopUpLayer").closePopUpLayer();
				$(".openPopUpLayer").openPopUpLayer();
			}
		);
	}
