/*
	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(){}

	// controls a movable divider between two content panes
	jQuery.classBehaviours.handlers.sectionDivider = {
		// properties
		name: 'sectionDivider',
		dragBar: null,
		mouse: null,
		index: 0,
		// methods
		start: function(node){
			if(document.body.className.indexOf('divided')>-1 && navigator.userAgent.indexOf('MSIE 6')<0){
				// give the node an id if none is present
				node.id = (node.id) ? node.id : this.name + this.index++ ;
				// set event handlers here
				node.onmousedown = this.pickUp;
				jQuery.classBehaviours.utilities.addEvent(document, 'mouseup', this.dropDown);
				jQuery.classBehaviours.utilities.addEvent(document, 'mousemove', this.moveAround);
				// for the faulty text selecting in internet explorer
				document.onselectstart = this.cancelSelect;
				jQuery.classBehaviours.utilities.addEvent(document, 'selectstart', this.cancelSelect);
				// change the cursor
				node.style.cursor = 'pointer';
				// retore any save settings
				this.restore(node);
			}
		},
		restore: function(node){
			var sdv = jQuery.classBehaviours.handlers.sectionDivider;
			// get the settings from the cookie
			cookieSettings = jQuery.classBehaviours.cookies.getCookie(node.id);
			// if there was cookie
			if(cookieSettings!=null){
				// get the sizes
				cookieParams = cookieSettings.split(',');
				// store the sizes
				jQuery.classBehaviours.utilities.setClassParameter(node, 'bottom', cookieParams[3] + '-' + cookieParams[4]);
				jQuery.classBehaviours.utilities.setClassParameter(node, 'top', cookieParams[5] + '-' + cookieParams[6]);
				// update the dimensions
				document.getElementById(cookieParams[0]).style.top = cookieParams[1] + '%';
				document.getElementById(cookieParams[0]).style.bottom = cookieParams[2] + '%';
				document.getElementById(cookieParams[3]).style.top = cookieParams[4] + '%';
				document.getElementById(cookieParams[5]).style.bottom = cookieParams[6] + '%';
			}
		},
		// events
		pickUp: function(that){
			var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
			var sdv = jQuery.classBehaviours.handlers.sectionDivider;
			// store the target node
			sdv.dragBar = objNode;
			sdv.mouse = null;
			// cancel the mouse drag
			return false;
		},
		dropDown: function(that){
			var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
			var sdv = jQuery.classBehaviours.handlers.sectionDivider;
			// clear the target node
			sdv.dragBar = null;
			sdv.mouse = null;
		},
		moveAround: function(that){
			var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
			var sdv = jQuery.classBehaviours.handlers.sectionDivider;
			// if there is a target picked up
			if(sdv.dragBar!=null){
				// get the previous mouse position
				oldMouse = sdv.mouse;
				// store the mouse new position
				sdv.mouse = (typeof(event)!='undefined') ? event.screenY : that.screenY;
				// if a valid amount of movement occurred
				if(oldMouse!=null){
					// get the bottom properties
					bottomParams = jQuery.classBehaviours.utilities.getClassParameter(sdv.dragBar, 'bottom', 'channelItem-41').split('-');
					bottomPos = parseFloat(bottomParams[1]);
					bottomObj = document.getElementById(bottomParams[0]);
					// get the top properties
					topParams = jQuery.classBehaviours.utilities.getClassParameter(sdv.dragBar, 'top', 'channelItems-61').split('-');
					topPos = parseFloat(topParams[1]);
					topObj = document.getElementById(topParams[0]);
					// get the parameter properties
					dividerSize = parseFloat(jQuery.classBehaviours.utilities.getClassParameter(sdv.dragBar, 'size', '2'));
					dividerMin = parseFloat(jQuery.classBehaviours.utilities.getClassParameter(sdv.dragBar, 'min', '10'));
					dividerMax = parseFloat(jQuery.classBehaviours.utilities.getClassParameter(sdv.dragBar, 'max', '10'));
					dividerOff = parseFloat(jQuery.classBehaviours.utilities.getClassParameter(sdv.dragBar, 'off', '0'));
					// calculate the sensitivity
					delta =  (bottomPos + topPos) / (bottomObj.offsetHeight + topObj.offsetHeight + dividerOff) * (sdv.mouse - oldMouse);
					// calculate the new positions
					newTopPos = topPos - delta;
					newBottomPos = bottomPos + delta;
					// if these are valid values
					if(newTopPos>dividerMax && newBottomPos>dividerMin){
						// set the new position
						sdv.dragBar.style.top = (newBottomPos - dividerSize) + "%";
						sdv.dragBar.style.bottom = (newTopPos - dividerSize) + "%";
						bottomObj.style.top = (newBottomPos) + "%";
						topObj.style.bottom = (newTopPos) + "%";
						// store the new position
						jQuery.classBehaviours.utilities.setClassParameter(sdv.dragBar, 'bottom', bottomParams[0] + '-' + newBottomPos);
						jQuery.classBehaviours.utilities.setClassParameter(sdv.dragBar, 'top', topParams[0] + '-' + newTopPos);
						// store the positions in a cookie
						cookieExpiration = jQuery.classBehaviours.cookies.getDaysFromNow(30);
						cookieValue = sdv.dragBar.id + ',' + (newBottomPos - dividerSize) + ',' + (newTopPos - dividerSize) + ',' + bottomParams[0] + ',' + newBottomPos + ',' + topParams[0] + ',' + newTopPos;
						jQuery.classBehaviours.cookies.setCookie(sdv.dragBar.id, cookieValue, cookieExpiration, null, null, null);
					}
				}
			}
		},
		cancelSelect: function(that){
			var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
			var sdv = jQuery.classBehaviours.handlers.sectionDivider;
			// cancel the selection
			return (sdv.dragBar==null) ? true : false;
		}
	}

	// add this addon to the jQuery object
	if(typeof(jQuery.fn)!='undefined'){
		// extend jQuery with this method
		jQuery.fn.sectionDivider = function(){
			return this.each(
				function(){
					jQuery.classBehaviours.handlers.sectionDivider.start(this);
				}
			);
		};
		// set the event handler for this jQuery method
		$(document).ready(
			function(){
				$(".sectionDivider").sectionDivider();
			}
		);
	}
