/**
 * Sliding Panles 1.0.2 - jQuery plugin for accessible, unobtrusive Sliding Panels
 * @requires jQuery 1.2.1
 *
 * http://www.andreacfm.com/jquery-plugins
 *
 * Copyright (c) 2007 Andrea Campolonghi (andreacfm.com)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
/**
 * The underlying HTML has to look like this:
 *
 * <p>Some Text</p>
 * <div> Text in the sliding Panel</div>
 *
 * The element can be any html valid element. By default the plugin looks for the very next 
 * element to be identify as the TARGET element. 
 *
 * @example jQuery('.slide').slidePanel();
 * @desc Create the default panel with the target element closed in the load. 
 * <p class="slide">Some Text</p>
 * <div> Text in the sliding Panel</div>
 *
 * @example  jQuery('.slide').slidePanel({status:'open', target:'#myTarget#'});
 * @desc The target element will be displayed opened onLoad and will be idetified by the ID myTarget
 *		and not by being the very next element
 * 		<p class="slide">Some Text</p>.................
 * 		<h4 id="myTarget"> Text in the sliding Panel</div>
 *
 * @example  jQuery('.slide').slidePanel({remote:'true'});
 * @desc text is loaded via Ajax. The panel element must be provide of an a Child with the href
 * 	used by the ajax call.
 * 		<p class="slide"><a href="text.htm">Some Text</a></p>
 * 		<div></div>
 *
 * @option String status 		default:closed. Attr: open. 
 *								Target element will be hide or not on load
 * @option jQueryselector target  	Default: next. Attr: any jQuery valid selector. 
 *								Identify the target element.
 * @option boolean remote 		Default:false.
 *                              Target element is poulated by Ajax call.
 *								If element status is 'open' the call is made on load, else is made at the first 
 *								click event.
 *								The panel element must have a child A element with the href used to make the call.
 *								This makes panels completely unbintrusive.
 *@option string role 			Default:normal.
 *                              Render dependant element in motion on call of a master element.
 *								Set role:'dep' for dependant and role:'master' for the main element.
 *								Master element must be one and must be declared after all the dependant.
 *								Single element preserve their own settings as individual panels with the follwing
 *								limitations regarding Ajax content:
 *								On dependant target element the Ajax loading can be set but will not be fired from the "master" click,
 *								will respond on his own element click.
 *								On Master target element no ajax content loading is allowed.
 

 * @type jQuery
 * @name slidingPanels
 * @author Andrea Campolonghi/andrea@andreacfm.com
 */
var jQuerypanArray;
var jQuerytargArray;
var jQuerypanAcc;

jQuery.fn.slidePanel = function(parameters){
	settings = {
		status: 'closed',
		speed: 'normal',
		target: 'next',
		remote:'false',
		role:'normal'
		};
	//extend settings in jQuery object
	jQuery.extend(settings, parameters);
	
	//Initialize any panel and keep chinable
		return this.each(function(){
			var jQuerypanel = jQuery(this);
			//set target variable to the required target panel, default is next
			if (settings.target == 'next'){
					var jQuerytarget = jQuerypanel.next();
				}
				else{
					var jQuerytarget = jQuery(settings.target);
				}
					
				//Add general class to panel element
				//if target start closed add the .panel-closed class			
			if (settings.status == 'closed'){
					jQuerytarget.addClass('panel-target panel-closed');
					jQuerypanel.addClass('panel panel-up');
				}
				else{
					jQuerytarget.addClass('panel-target');
					jQuerypanel.addClass('panel panel-down');
							
				}
			if(settings.role == 'accordion'){
						panelAcc(jQuerytarget);
						if(jQuerypanAcc.length == '1'){
							jQuerypanAcc[0].removeClass('panel-closed');
							}												
						jQuerypanel.click(function(){
							for (var j = 0; j < jQuerypanAcc.length; j++){
								if(jQuerypanAcc[j] != jQuerytarget){
									jQuerypanAcc[j].slideUp();
									jQuerypanAcc[j].prev().removeClass('panel-down').addClass('panel-up');
								 }			
								}
								jQuerytarget.slideDown();
								jQuerytarget.prev().addClass('panel-down').removeClass('panel-up');							
							});							
					
			}					
			// if Role is Master bind the function to the master panel click	
			else if (settings.role == 'master'){
						jQuerypanel.click(function(){
							jQuerytarget.slideToggle(settings.speed);//make the master element sliding					
							for (var i = 0; i < jQuerytargArray.length; i++){						
								jQuerytargArray[i].slideToggle();//the dep element toogle on master click
							}	
							//preserve the class to the dependent panel element for graphic display
							for (var i = 0; i < jQuerypanArray.length; i++){
								if(	jQuerypanArray[i].hasClass('panel-down')){
										jQuerypanArray[i].addClass('panel-up').removeClass('panel-down');
									}
									else{
								jQuerypanArray[i].addClass('panel-down').removeClass('panel-up');
								}
							}	
						});
				}
				else
				{
				
				//if Ajax with opened starting status fire the calling onLoad
				if(settings.remote == 'true' && settings.status != 'closed'){
					var page = jQuerypanel.children().filter('a').eq(0).attr('href');
					jQuerytarget.load(page);
				}
				//if role is dep add element to a repository Array	
				if (settings.role == 'dep'){		
					panelAssets(jQuerypanel,jQuerytarget);
				}
			
				//bind the click required event				
				jQuerypanel.click(function(){
						//if is ajax check if content has allready been loaded
						if(settings.remote == 'true'){
							var txt = jQuerytarget.text();
								if(txt.length == '0'){
									var jQuerya = jQuerypanel.children().filter('a').eq(0);
									var exText = jQuerya.text();
									jQuerya.text('Loading....');
										var page = jQuerypanel.children().filter('a').attr('href');
											jQuerytarget.load(page, function(){
												jQuerya.text(exText);
											});	
								}
						}
				jQuerytarget.slideToggle(settings.speed);
				jQuerypanel.toggleClass('panel-down');
				jQuerypanel.toggleClass('panel-up');
				return false;
			});
		   }	
		});				
	//repository Array for depending element		
	function panelAssets(elem,elem1){
		if (jQuerypanArray == undefined && jQuerytargArray == undefined){
				jQuerypanArray = [];
				jQuerypanArray[0] = elem;
				jQuerytargArray = [];
				jQuerytargArray[0] = elem1;				
			}
			else{
				jQuerypanArray[jQuerypanArray.length] = elem;
				jQuerytargArray[jQuerytargArray.length] = elem1;
		}
	}		
	//repository Array for depending element		
	function panelAcc(elem){
		if (jQuerypanAcc == undefined){
				jQuerypanAcc = [];
				jQuerypanAcc[0] = elem;
			}
			else{
				jQuerypanAcc[jQuerypanAcc.length] = elem;
		}
	}		
}