/**
 * CustomrSite Javascript Behaviour
 */
jQuery.centrik_form_comments = {
	toggle : function() {
		$('form.wordpress_post_comment').each(
			function() {
				$(this).children('fieldset:eq(0)').children('legend:eq(0)').click(
					function() {
						this.lastChild.innerHTML = (this.lastChild.innerHTML == 'yes' ? 'no' : 'yes');
						//the form element fieldsets to act on
						$(this.form).children('fieldset:gt(0)').each(
							function() {
								if(this.className != 'helpers') {
									if(this.style.display == 'none' || this.offsetHeight == 0) {
										$(this).show();
									} else {
										$(this).hide();
									}
								}
							}
						);
					}
				);
			}
		);
	}
}

//slideshow handling
jQuery.centrik_slideshow = {
	slideshow : '.ck_s_slideshow',//container for the complete show
	selector : '.ck_s_slides',//container for the slides
	slide : '.ck_s_slide',//selector for an individual slide
	set : null,
	animationOut : 'slideUp',
	speedOut : 260,
	animationIn : 'slideDown',
	speedIn : 220,
	handle : function() {
		var _self = this;
		//all slides in the slideshow
		this.set = $(this.slideshow).children(this.selector).children(this.slide);
		this.buttons = $(this.slideshow).children('.ck_s_buttons').children();
		if(this.total() > 1) {
			this.buttons.each(
				function(b) {
					$(this).click( function() {
						if($(this).hasClass('ck_s_button_previous')) {
							_self.prevslide();
						} else if($(this).hasClass('ck_s_button_next')) {
							_self.nextslide();
						} else if($(this).hasClass('ck_s_button_last')) {
							_self.lastslide();
						} else if($(this).hasClass('ck_s_button_first')) {
							_self.firstslide();
						}
					});
				}
			);
		}
	},
	total : function() {
		return this.set.length;
	},
	//get the active slide in the set
	getactive : function() {
		return $(this.set).siblings('.ck_s_active');
	},
	getfirst : function() {
		return $(this.set)[0];
	},
	getlast : function() {
		return $(this.set)[this.set.length - 1];
	},
	isfirst : function(e) {
		return $(e).hasClass('ck_s_first');
	},
	islast : function(e) {
		var is = $(e).hasClass('ck_s_last');
		this.togglebutton('last', is);
		return is;
	},
	togglebutton : function(type, last) {
		var s = '.ck_s_button_' + type;
		if(last) {
			$(s).css('visibility','hidden');
		} else {
			$(s).css('visibility','visible');
		}
		
	},
	lastslide : function() {
		var _self = this;
		$(this.getactive()).fadeOut(
			_self.speedOut,
			function() {
				_self.togglebutton('last','hidden');
				$(this).removeClass('ck_s_active');
				var n = _self.getlast();
				$(n).fadeIn(_self.speedIn);
				$(n).addClass('ck_s_active');
			}
		);
	},
	firstslide : function() {
		var _self = this;
		$(this.getactive()).fadeOut(
			_self.speedOut,
			function() {
				$(this).removeClass('ck_s_active');
				var n = _self.getfirst();
				$(n).fadeIn(_self.speedIn);
				$(n).addClass('ck_s_active');
				_self.islast(n);
			}
		);
	},
	nextslide : function() {
		var _self = this;
		$(this.getactive()).fadeOut(
			_self.speedOut,
			function() {
				$(this).removeClass('ck_s_active');
				//if last, first slide, if not, next matching slide
				var n = (_self.islast(this) ? _self.getfirst() : $(this).nextAll(_self.slide).get(0));
				$(n).fadeIn(_self.speedIn);
				$(n).addClass('ck_s_active');
				_self.islast(n);
			}
		);
	},
	prevslide : function() {
		var _self = this;
		$(this.getactive()).fadeOut(
			_self.speedOut,
			function() {
				$(this).removeClass('ck_s_active');
				//if first, last slide, if not, prev matching slide
				var n = (_self.isfirst(this) ? _self.getlast() : $(this).prevAll(_self.slide).get(0));
				$(n).fadeIn(_self.speedIn);
				$(n).addClass('ck_s_active');
				_self.islast(n);
			}
		);
	}
};