/*	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(){}

	// move a link's click event to the parent node
	jQuery.classBehaviours.handlers.clickOnParent = {
		// properties
		name: 'clickOnParent',
		// methods
		start: function(node){
			// what node is the click supposed to go on?
			parentCount = parseInt(jQuery.classBehaviours.utilities.getClassParameter(node, "parent", "2"));
			targetNode = node;
			for(var a=0; a<parentCount; a++) targetNode = targetNode.parentNode;
			// get the target of the link
			linkTarget = node.href;
			// set the click event
			targetNode.onclick = this.load;
			// set the cursor
			targetNode.style.cursor = 'pointer';
		},
		// events
		wait: function(waitStatus, waitNode, waitError){
			// progress indicator
			waitNode.innerHTML = (waitStatus<0) ? '<h2>error: ' + waitError + '</h2>' : '<h2>loading: ' + (waitStatus * 100) + '%</h2>' ;
		},
		insert: function(insertXML, insertNode, insertTxt){
			var cop = jQuery.classBehaviours.handlers.clickOnParent;
			// get the replacement content from the new document
			insertHTMLparts = insertTxt.split('<!-- ' + insertNode.id + ' -->');
			// insert the new content
			insertNode.innerHTML = insertHTMLparts[1] + insertHTMLparts[3];
			// initiate the classbehaviours
			jQuery.classBehaviours.parser.parseNode(insertNode);
		},
		// events
		load: function(that){
			var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
			var cop = jQuery.classBehaviours.handlers.clickOnParent;
			// find the link
			allLinks = (objNode.nodeName=='A') ? new Array(objNode) : objNode.getElementsByTagName('A');
			for(var a=0; a<allLinks.length; a++) if(allLinks[a].className.indexOf('clickOnParent')>-1) loadLink = allLinks[a];
			// get the url
			loadUrl = loadLink.href;
			// if an ajax request is required
			if(jQuery.classBehaviours.utilities.getClassParameter(loadLink, 'ajax', 'no')=='yes'){
				// get the target node
				loadId = loadUrl.split('#')[1];
				loadNode = document.getElementById(loadId);
				// make it an ajax request
				jQuery.classBehaviours.ajax.addRequest(loadUrl, cop.insert, cop.wait, null, loadNode);
				// cancel the click
				return false;
			// else load the url normally
			}else{
				document.location.href = loadUrl;
			}
		}
	}
			
	// add this addon to the jQuery object
	if(typeof(jQuery.fn)!='undefined'){
		// extend jQuery with this method
		jQuery.fn.clickOnParent = function(){
			return this.each(
				function(){
					jQuery.classBehaviours.handlers.clickOnParent.start(this);
				}
			);
		};
		// set the event handler for this jQuery method
		$(document).ready(
			function(){
				$(".clickOnParent").clickOnParent();
			}
		);
	}


