////////////////////////////////////////////////////////////////////
// FILE:         funkyslide.php
//------------------------------------------------------------------
// PACKAGE:      mod_funkyslide
// NAME:         FunkySlide!
// DESCRIPTION:  FunkySlide! ...a multi-functional slider for Joomla!
// VERSION:      1.0.7
// CREATED:      March 2008
// MODIFIED:     May 2008
//------------------------------------------------------------------
// AUTHOR:       NoNumber! (Peter van Westen)
// E-MAIL:       peter@nonumber.nl
// WEBSITE:      http://www.nonumber.nl
//------------------------------------------------------------------
// COPYRIGHT:    (C) 2008-2010 - NoNumber! - All Rights Reserved
// LICENSE:      http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
////////////////////////////////////////////////////////////////////

var FS_ = new Array();

function FS_initialize() {
	$$('div.funkyslide_container').each(function(FS_container,i) {
		if ( FS_[i] == undefined ) {
			FS_[i] = {};
			FS_[i].id = FS_container.id.substring(FS_container.id.lastIndexOf('_')+1);
			FS_[i].el = FS_elements(i);
			FS_[i].options =  FS_options(i);
			FS_[i].effect = new Fx.FS_Slide(FS_[i].el.content_container.id,{duration:FS_[i].options.slide_speed});

			FS_applyStyles(i);

			if ( FS_[i].options.enable_cookie ) {
				var cookieState = Cookie.get(FS_[i].el.container.id);
				if ( cookieState == 'closed' ) {
					FS_go_slide(i,'closed');
					FS_state(i,'closed');
				} else if ( cookieState == 'opened' ) {
					FS_go_slide(i,'opened');
					FS_state(i,'opened');
				} else { // initial state
					FS_go_slide(i,FS_[i].options.initial_state);
					FS_state(i,FS_[i].options.initial_state);
				}
			} else {
				FS_go_slide(i,FS_[i].options.initial_state);
				FS_state(i,FS_[i].options.initial_state);
				
			}

			FS_[i].el.handle.addEvent('click',function(){
				FS_jump_fix(i, 0);
				FS_toggle(i);
			});
			if ( FS_[i].options.change_on_complete ) {
				FS_[i].effect.addEvent('onComplete', function(){
					FS_toggle_state(i);
				});
			}
			FS_[i].effect.addEvent('onComplete', function(){
				FS_jump_fix(i, 1);
			});
		}
	});
}

function FS_elements(i) {
	id = FS_[i].id;
	el = {};
	el.container				= $('funkyslide_container_'+id);
	el.main						= $('funkyslide_'+id);
	el.state					= $('funkyslide_state_'+id);
	el.content_container		= $('funkyslide_content_container_'+id);
	el.handle					= $('funkyslide_handle_'+id);
	el.handle_text				= $('funkyslide_handle_text_'+id);
	el.handle_text_container	= $('funkyslide_handle_text_container_'+id);
	return el;
}

function FS_options(i) {
	id = FS_[i].id;
	options = {};
	for ( option in funkyslide_options[id] ) {
		options[option] = funkyslide_options[id][option].replace(/&lt;/g,'<').replace(/&gt;/g,'>');
		if ( !isNaN(options[option]) )  options[option] = options[option].toFloat();
	}
	return options;
}

function FS_applyStyles(i) {
	id = FS_[i].id;

	// turn visibility of slider container off til all sizes are set
	// (to prevent the user seeing strange stuff happening)
	FS_[i].el.container.setStyle('visibility', 'hidden');

	// turn display of slider container on
	FS_[i].el.container.setStyle('display', 'block');

	// float settings
	if ( FS_[i].options.enable_float == 1 ) {
		FS_[i].el.container.setStyle('position', 'absolute');
		FS_[i].el.main.setStyle('position', 'absolute');
		FS_[i].el.main.setStyle('width', FS_[i].options.width);
		if ( FS_[i].options.float_hor == 'center' ) {
			FS_[i].el.main.setStyle('left', '50%');
			if ( FS_[i].options.width.indexOf('%') != -1 ) {
				FS_[i].el.main.setStyle('margin-left', '-'+(FS_[i].options.width.replace('%','')/2)+'%');
			} else {
				FS_[i].el.main.setStyle('margin-left', '-'+(FS_[i].options.width.replace('px','')/2)+'px');
			}
		} else {
			FS_[i].el.main.setStyle(FS_[i].options.float_hor, FS_[i].options.float_hor_px);
		}
		FS_[i].el.main.setStyle(FS_[i].options.float_vert, FS_[i].options.float_vert_px);
		FS_[i].el.container.setStyle('width', FS_[i].el.container.parentNode.offsetWidth);
	} else {
		FS_[i].el.container.setStyle('width', FS_[i].options.width);
	}

	// makes the content appear on the same level as the handle, in case of overlapping.
	// doesn't work for IE6
	FS_[i].el.content_container.parentNode.setStyle('position', 'relative');
	FS_[i].el.content_container.parentNode.setStyle('z-index', FS_[i].options.z_index+id);

	if ( FS_[i].options.show_handle ) {
		// makes the handle clickable even if it is overlapped by another handle bar.
		// doesn't work for IE
		FS_[i].el.handle.setStyle('position', 'relative');
		FS_[i].el.handle.setStyle('z-index', FS_[i].options.z_index+id);
		
		if (FS_[i].options.dir == 'horizontal') {
			// handle padding
			FS_[i].el.handle_text.setStyle('padding-left', '0px').setStyle('padding-right', '0px');
			// handle width;
			FS_[i].el.handle.setStyle('width', FS_[i].options.handle_thickness);
			FS_[i].el.content_container.setStyle('width', FS_[i].el.main.offsetWidth - FS_[i].el.handle.offsetWidth);
			// handle height
			FS_[i].el.handle.setStyle('height', FS_[i].options.handle_length);
			arrows_height = (FS_[i].el.handle.childNodes[0].offsetHeight + FS_[i].el.handle.childNodes[2].offsetHeight);
			FS_[i].el.handle_text.setStyle('height', (FS_[i].el.handle.offsetHeight - arrows_height) + 'px');
			FS_[i].el.handle.setStyle('height', (FS_[i].el.handle_text.offsetHeight + arrows_height) + 'px');
			// handle positioning
			FS_[i].el.handle.setStyle('margin-top', FS_[i].options.pos_vert_abs);
			// text center alignment
			FS_centerHandleText(i);
		} else {
			// handle padding
			FS_[i].el.handle_text.setStyle('padding-top', '0px').setStyle('padding-bottom', '0px');
			// handle height
			FS_[i].el.handle.setStyle('height', FS_[i].options.handle_thickness);
			// handle width
			FS_[i].el.handle.setStyle('width', FS_[i].options.handle_length);
			arrows_width = (FS_[i].el.handle.childNodes[0].offsetWidth + FS_[i].el.handle.childNodes[2].offsetWidth);
			FS_[i].el.handle_text.setStyle('width', (FS_[i].el.handle.offsetWidth - arrows_width) + 'px');
			FS_[i].el.handle.setStyle('width', (FS_[i].el.handle_text.offsetWidth + arrows_width) + 'px');
			// handle positioning
			switch(FS_[i].options.pos_hor_rel) {
				case 'absolute':
					FS_[i].el.handle.setStyle('margin-left', FS_[i].options.pos_hor_abs);
					break;
				case 'left':
				case 'right':
					FS_[i].el.handle.setStyle('float', FS_[i].options.pos_hor_rel);
					break;
				default:
					FS_[i].el.handle.setStyle('margin', 'auto auto');
					break;
			}
		}
		FS_fixHandleSize(i);
	}
	
	// turn visibility of slider container on
	FS_[i].el.container.setStyle('visibility', 'visible');
}

function FS_fixHandleSize(i) {
	// sets the handle size to the minimum size to fit both the opened and closed text in.
	// if the handle has a size larger than the minimum required size, nothing is changed.
	if ( FS_[i].options.show_text ) {
		handle_text = FS_[i].el.handle_text_container.innerHTML
		for ( j=1; j<=2;j++ ) {
			temp_text = (j == 1)?FS_[i].options.handle_text_opened:FS_[i].options.handle_text_closed;
			FS_[i].el.handle_text_container.innerHTML = temp_text;
			if (FS_[i].options.dir == 'horizontal') {
				// handle height
				arrows_height = (FS_[i].el.handle.childNodes[0].offsetHeight + FS_[i].el.handle.childNodes[2].offsetHeight);
				if ( (FS_[i].el.handle_text.offsetHeight + arrows_height) >  FS_[i].el.handle.offsetHeight ) {
					FS_[i].el.handle_text.setStyle('height', FS_[i].el.handle_text.offsetHeight + 'px');
					FS_[i].el.handle.setStyle('height', (FS_[i].el.handle_text.offsetHeight + arrows_height) + 'px');
				}
			} else {
				// handle width
				arrows_width = (FS_[i].el.handle.childNodes[0].offsetWidth + FS_[i].el.handle.childNodes[2].offsetWidth);
				if ( (FS_[i].el.handle_text.offsetWidth + arrows_width) >  FS_[i].el.handle.offsetWidth ) {
					FS_[i].el.handle_text.setStyle('width', FS_[i].el.handle_text.offsetWidth + 'px');
					FS_[i].el.handle.setStyle('width', (FS_[i].el.handle_text.offsetWidth + arrows_width) + 'px');
				}
			}
		}
		FS_[i].el.handle_text_container.innerHTML = handle_text;
	}
};

function FS_centerHandleText(i) {
	FS_[i].el.handle_text.setStyle('position', 'relative');
	FS_[i].el.handle_text_container.setStyle('position', 'absolute');
	FS_[i].el.handle_text_container.setStyle('left', '50%').setStyle('margin-left', '-' + (FS_[i].el.handle_text_container.offsetWidth/2) + 'px');
	FS_[i].el.handle_text_container.setStyle('top', '50%').setStyle('margin-top', '-' + (FS_[i].el.handle_text_container.offsetHeight/2) + 'px');
};

function FS_toggle(i) {
	if ( FS_[i].el.state.className == 'funkyslide_opened' ) {
		FS_go_slide(i,'close');
	} else {
		if ( FS_[i].options.close_all ) FS_close_all();
		FS_go_slide(i,'open');
	}
};

function FS_close_all() {
	for ( var i=0; i<FS_.length; i++ ) {
		if ( FS_[i].el.state.className == 'funkyslide_opened' && !FS_[i].options.overrule_close_all  ) {
			FS_go_slide(i,'close');
		}
	}
};

function FS_toggle_state(i) {
	if ( FS_[i].el.state.className == 'funkyslide_opened' ) {
		FS_state(i,'close');
	} else { // state.className == 'funkyslide_closed'
		FS_state(i,'open');
	}
};

function FS_jump_fix(i, hide) {
	// hide/show the content block to prevent the handle from 'jumping' in IE
	if ( FS_[i].el.state.className == 'funkyslide_closed' ) {
		display = (hide)?'none':'block';
		FS_[i].el.content_container.setStyle('display',display);
	}
};

function FS_go_slide(i,state) {	
	if ( state == 'closed' ) {
		FS_[i].effect.FS_hide(i);
		FS_jump_fix(i, 1);
	} else if ( state == 'opened' ) {
		FS_[i].el.content_container.effect('opacity',{duration:0}).start(FS_[i].options.opacity,FS_[i].options.opacity); // Needed for IE6
		FS_[i].effect.FS_show(i);
	} if ( state == 'close' ) {
		FS_[i].effect.FS_slideOut(i);
		if ( FS_[i].options.enable_fade ) {
			FS_[i].el.content_container.effect('opacity',{duration:FS_[i].options.slide_speed}).start(FS_[i].options.opacity,0); // fade out
		} else {
			FS_[i].el.content_container.effect('opacity',{duration:0}).start(FS_[i].options.opacity,FS_[i].options.opacity); // Needed for IE6
		}
	} else if ( state == 'open' ) {
		if ( FS_[i].options.enable_fade ) {
			FS_[i].el.content_container.effect('opacity',{duration:FS_[i].options.slide_speed}).start(0,FS_[i].options.opacity); // fade in
		} else {
			FS_[i].el.content_container.effect('opacity',{duration:0}).start(FS_[i].options.opacity,FS_[i].options.opacity); // Needed for IE6
		}
		FS_[i].effect.FS_slideIn(i);
	}
	if ( !FS_[i].options.change_on_complete ) FS_toggle_state(i);
};
function FS_state(i,state) {
	if ( state == 'closed' || state == 'close' ) {
		FS_[i].el.state.className = 'funkyslide_closed';
		if ( FS_[i].options.enable_cookie ) Cookie.set(FS_[i].el.container.id,'closed',{duration: 1});
		if ( FS_[i].options.show_text ) {
			FS_[i].el.handle_text_container.innerHTML = FS_[i].options.handle_text_closed;
			FS_centerHandleText(i);
		}
	} else { // state == 'open' || state == 'opened'
		FS_[i].el.state.className = 'funkyslide_opened';
		if ( FS_[i].options.enable_cookie ) Cookie.set(FS_[i].el.container.id,'opened',{duration: 1});
		if ( FS_[i].options.show_text ) {
			FS_[i].el.handle_text_container.innerHTML = FS_[i].options.handle_text_opened;
			FS_centerHandleText(i);
		}
	}
};

Fx.FS_Slide = Fx.Slide.extend({
	FS_style_slide: function(i){
		switch(FS_[i].options.direction){
			case 'ttb':
				this.margin = 'margin-top';
				break;
			case 'btt':
				this.margin = 'margin-bottom';
				break;
			case 'ltr':
				this.margin = 'margin-left';
				break;
			case 'rtl':
				this.margin = 'margin-right';
				break;
		};
	},
	FS_style_ease: function(i){
		switch(FS_[i].options.direction){
			case 'ttb':
				this.margin = 'margin-top';
				break;
			case 'btt':
				this.margin = 'margin-top';
				break;
			case 'ltr':
				this.margin = 'margin-left';
				break;
			case 'rtl':
				this.margin = 'margin-left';
				break;
		};
		this.startMargin = this.startMargin/2;
		this.endMargin = this.endMargin/2;
	},
	FS_style_peal: function(i){
		switch(FS_[i].options.direction){
			case 'ttb':
				this.margin = 'margin-bottom';
				break;
			case 'btt':
				this.margin = 'margin-top';
				break;
			case 'ltr':
				this.margin = 'margin-right';
				break;
			case 'rtl':
				this.margin = 'margin-left';
				break;
		};
	},
	FS_slideIn: function(i){
		this[FS_[i].options.dir]();
		this.startMargin = this.element.getStyle(this.margin).toInt();
		this.startSize = this.wrapper.getStyle(this.layout).toInt();
		this.endMargin = 0;
		this.endSize = this.offset;
		this['FS_style_'+FS_[i].options.style](i);
		return this.start([this.startMargin, this.startSize], [this.endMargin, this.endSize]);
	},
	FS_slideOut: function(i){
		this[FS_[i].options.dir]();
		this.startMargin = this.element.getStyle(this.margin).toInt();
		this.startSize = this.wrapper.getStyle(this.layout).toInt();
		this.endMargin = -this.offset;
		this.endSize = 0;
		this['FS_style_'+FS_[i].options.style](i);
		return this.start([this.startMargin, this.startSize], [this.endMargin, this.endSize]);
	},
	FS_show: function(i){
		this[FS_[i].options.dir]();
		this['FS_style_'+FS_[i].options.style](i);
		this.open = true;
		return this.set([0, this.offset]);
	},
	FS_hide: function(i){
		this[FS_[i].options.dir]();
		this['FS_style_'+FS_[i].options.style](i);
		this.open = false;
		return this.set([-this.offset, 0]);
	}
});