/*
	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(){}

	// 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(node.nodeName=='IFRAME'){
				// adjust the size for MSIE
				if(navigator.userAgent.indexOf('MSIE 6')>-1) node.style.height = document.body.offsetHeight + 'px';
				// unhide the layer
				setTimeout("document.getElementById('"+node.id+"').style.visibility = 'visible';", 200);
			}
		}
	}

	// 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';
		}
	}

	// a (hidden) link for opening a popup layer
	jQuery.classBehaviours.handlers.openPopUpLayer = {
		// properties
		name: 'openPopUpLayer',
		// methods
		start: function(node){
			node.onmouseup = this.open;
		},
		// events
		open: function(node){
			jQuery.classBehaviours.utilities.setClassParameter(document.body, 'popup', 'open');
		}
	}

	// opens a popup from within an iframe
	jQuery.classBehaviours.handlers.openPopUpIframe = {
		// properties
		name: 'openPopUpIframe',
		// 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') + '0';
			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('style', 'visibility:hidden;');
				newIframe.setAttribute('src', objNode.href);
				// for internet explorer
				newIframe.className = 'popUpLayer';
				newIframe.allowTransparency = true;
				// add it to the page
				document.body.appendChild(newIframe);
				// reveal the iframe
				jQuery.classBehaviours.handlers.popUpLayer.start(document.getElementById(iframeName));
			}else{
				// hide the iframe
				document.getElementById(iframeName).style.visibility = 'hidden';
				// set the iframe url
				window.frames[iframeName].document.location.href = objNode.href;
				// reveal the iframe
				jQuery.classBehaviours.handlers.popUpLayer.start(document.getElementById(iframeName));
			}
			// tell the body that the popup is open
			jQuery.classBehaviours.utilities.setClassParameter(document.body, 'popup', 'open');
			// cancel the click
			return false;
		}
	}

	// 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
			else node.onmousedown = this.closeInSelf;
		},
		// 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.visibility = 'hidden';" +
						"parent.document.getElementById('" + allPopUps[a].id + "').src = '';",
						timeToWait
					);
			// tell the body that the popup is open
			jQuery.classBehaviours.utilities.setClassParameter(parent.document.body, 'popup', 'close');
			// cancel the click
			return false;
		},
		closeInSelf: function(that){
			var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
			// tell the body that the popup is open
			jQuery.classBehaviours.utilities.setClassParameter(document.body, 'popup', 'close');
			// 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.openPopUpLayer = function(){
			return this.each(
				function(){
					jQuery.classBehaviours.handlers.openPopUpLayer.start(this);
				}
			);
		};
		jQuery.fn.openPopUpIframe = function(){
			return this.each(
				function(){
					jQuery.classBehaviours.handlers.openPopUpIframe.start(this);
				}
			);
		};
		jQuery.fn.closePopUpLayer = function(){
			return this.each(
				function(){
					jQuery.classBehaviours.handlers.closePopUpLayer.start(this);
				}
			);
		};
		// set the event handler for this jQuery method
		$(document).ready(
			function(){
				$(".popUpLayer").popUpLayer();
				$(".popUpBackground").popUpBackground();
				$(".openPopUpLayer").openPopUpLayer();
				$(".openPopUpIframe").openPopUpIframe();
				$(".closePopUpLayer").closePopUpLayer();
			}
		);
	}
