/*
	name			: ClassBehaviours, the javascript framework based on class-name parsing
	update			: 9.11.9
	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){
			// cosmetic adjustments for MSIE 6
			if(navigator.userAgent.indexOf('MSIE 6')>-1)
				node.getElementsByTagName('DIV')[0].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;
			if(jQuery.classBehaviours.utilities.getClassParameter(node, 'auto', 'no')=='yes') this.auto(node);
		},
		auto: function(node){
			var opl = jQuery.classBehaviours.handlers.openPopUpLayer;
			var acn = jQuery.classBehaviours.handlers.animatedClassName;
			// if there is an iframe transfer the url
			if(node.getAttribute('target')!=null){
				document.getElementById(node.getAttribute('target')).src = node.href;
				frames[node.getAttribute('target')].location.replace(node.getAttribute('href'));
			}
			// trigger the open animation
			node.className = node.className.replace('click_', 'load_');
			acn.start(node);
			// trigger the open event
			opl.open(node);
		},
		// events
		open: function(that){
			var node = (typeof(this.nodeName)=='undefined') ? that : this ;
			// tell the body that a layer is open (to avoid select elements shining through in MSIE 6)
			jQuery.classBehaviours.utilities.setClassParameter(document.body, 'popup', 'open');
			// get the target node, if we can't find the ID, we are probably going to find in in the link
			linkTarget = (node.href!=null) ? (node.href.indexOf('#')>-1) ? node.href.split('#')[1] : null : null;
			linkTarget = jQuery.classBehaviours.utilities.getClassParameter(node, 'id', linkTarget);
			// transfer the title of the link to the popup
			if(node.getAttribute('title')!=null && node.getAttribute('title')!='') document.getElementById(linkTarget).getElementsByTagName('H1')[0].innerHTML = node.getAttribute('title');
			// if there is no animation defined, open it the easy way
			if(node.className.indexOf('animatedClassName')<0) document.getElementById(linkTarget).style.display = 'block';
		}
	}

	// closes a popup from within an iframe
	jQuery.classBehaviours.handlers.closePopUpLayer = {
		// properties
		name: 'closePopUpLayer',
		// methods
		start: function(node){
			node.onmousedown = (parent!=self) ? this.closeInParent : this.closeInSelf;
		},
		// events
		closeInSelf: function(that){
			var node = (typeof(this.nodeName)=='undefined') ? that : this ;
			// get the target node, if we can't find the ID, we are probably going to find in in the link
			linkTarget = (node.href!=null) ? (node.href.indexOf('#')>-1) ? node.href.split('#')[1] : null : null;
			linkTarget = jQuery.classBehaviours.utilities.getClassParameter(node, 'id', linkTarget);
			// tell the body that a layer is closed (to avoid select elements shining through in MSIE 6)
			jQuery.classBehaviours.utilities.setClassParameter(document.body, 'popup', 'close');
			// trigger the close link of the target
			targetNode = document.getElementById(linkTarget).getElementsByTagName('A')[0];
			if(targetNode.className.indexOf('animatedClassName')<0) document.getElementById(linkTarget).style.display = 'none'
			else if(targetNode!=node) jQuery.classBehaviours.handlers.animatedClassName.clickToPlay(targetNode);
		},
		closeInParent: function(that){
			var node = (typeof(this.nodeName)=='undefined') ? that : this ;
			// get the target node, if we can't find the ID, we are probably going to find in in the link
			linkTarget = (node.href!=null) ? (node.href.indexOf('#')>-1) ? node.href.split('#')[1] : null : null;
			linkTarget = jQuery.classBehaviours.utilities.getClassParameter(node, 'id', linkTarget);
			// tell the body that a layer is closed (to avoid select elements shining through in MSIE 6)
			parent.jQuery.classBehaviours.utilities.setClassParameter(parent.document.body, 'popup', 'close');
			// trigger the close link of the target
			targetNode = parent.document.getElementById(linkTarget).getElementsByTagName('A')[0];
			if(targetNode.className.indexOf('animatedClassName')<0) parent.document.getElementById(linkTarget).style.display = 'none';
			else parent.jQuery.classBehaviours.handlers.animatedClassName.clickToPlay(targetNode);
		}
	}

	// print the contents of the popup layer
	jQuery.classBehaviours.handlers.printPopUpLayer = {
		// properties
		name: 'printPopUpLayer',
		// methods
		start: function(node){
			node.onclick = this.print;
		},
		// events
		print: function(that){
			var node = (typeof(this.nodeName)=='undefined') ? that : this ;
			// get the target node, if we can't find the ID, we are probably going to find in in the link
			linkTarget = (node.href!=null) ? (node.href.indexOf('#')>-1) ? node.href.split('#')[1] : null : null;
			linkTarget = jQuery.classBehaviours.utilities.getClassParameter(node, 'id', linkTarget);
			// if this is an iframe
			iFrames = document.getElementById(linkTarget).getElementsByTagName('IFRAME');
			if(iFrames.length>0){
				// print the contents of the iframe
				frames[iFrames[0].name].focus();
				frames[iFrames[0].name].print();
			// else
			}else{
				// print the page
				window.print();
			}
		}
	}

	// 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.openPopUpLayer = function(){
			return this.each(
				function(){
					jQuery.classBehaviours.handlers.openPopUpLayer.start(this);
				}
			);
		};
		jQuery.fn.closePopUpLayer = function(){
			return this.each(
				function(){
					jQuery.classBehaviours.handlers.closePopUpLayer.start(this);
				}
			);
		};
		jQuery.fn.printPopUpLayer = function(){
			return this.each(
				function(){
					jQuery.classBehaviours.handlers.printPopUpLayer.start(this);
				}
			);
		};
		// set the event handler for this jQuery method
		$(document).ready(
			function(){
				$(".popUpLayer").popUpLayer();
				$(".openPopUpLayer").openPopUpLayer();
				$(".closePopUpLayer").closePopUpLayer();
				$(".printPopUpLayer").printPopUpLayer();
			}
		);
	}

