var clickableTable = new Class({
	
	initialize: function(table_class, with_checker) {
		this.with_checker = with_checker;
		$$('table.' + table_class + ' tbody tr').each(function(el,i) {  
			//do click  
			el.addEvent('click',function() {
				if(this.with_checker) {
					var checker = el.getChildren().getLast().getChildren();
				}
				if(el.hasClass('highlight')) {  
					el.removeClass('highlight');
					if(this.with_checker)checker.removeProperty('checked'); 
				} else {  
					el.addClass('highlight');
					if(this.with_checker)checker.setProperty('checked','checked'); 
				}  
			}.bind(this));	
		}.bind(this));  
	}
});




/* Simple Slide & Ajax-Request */
var SlideAjaxReq= new Class({

	 initialize: function(btn, element, with_ajax, with_toogle) {
		this.var_request = false;
		this.btn = $(btn);
		this.btnid = btn;
		this.el = $(element);
		this.elid = element;
		
		if(this.el && this.btn) {
		
			if(with_ajax) this.with_ajax = true;
			if(with_toogle) {
				this.slider = new Fx.Slide(this.el,{
					onStart: function() {
						this.el.getParent().getParent().getParent().setStyle('height','auto');
					}.bind(this),
					onFinish: function() {
						this.el.getParent().setStyle('height','auto');
					}
				});
				this.slider.hide();
				this.with_toogle = with_toogle;
			 }
			 
			this.btn.addEvent('click', function(event) { 
				event.stop()
				if((this.with_ajax && this.var_request == true) || this.with_toogle == true && this.with_ajax == false) {
					this.slider.toggle();
					return;	
				}
				if(this.with_ajax) {
				// AJAX
					var req = new Request.HTML({
						method: 'get',  
						url: this.btn.get('href'),
						onRequest: this.onRequest(),
						onComplete: this.onComplete(),
						onSuccess: function(html) {
							this.el.set('text', '');
							this.el.adopt(html);
						/* SLIDE */
							if(this.with_toogle) {
								this.var_request = true;
								this.slider.slideIn();
							 }
						}.bind(this)
					}).send();
				} else {
			// SLIDE
					this.slider.toggle();
				}
			}.bind(this));
		}
	},
	 
	onRequest: function() {
		this.btn.addClass('icn_loader');
	},
	 
	onComplete: function() {
		this.btn.removeClass('icn_loader');
	},
	 	 
	onFailure: function() {
	}

});
	 

/* FORM VALIDATION & REPLACEMENT */
var formVaRe = new Class({

    Implements: [Options],

    options: {
			validate: {use: true, showErrors : 1, submitByAjax: false },
			replacement: {use: true, imageDir: '/img/icons/', useSmall: true, inputs: ['checkbox']},
			switcher: {use: false, mc: '', switch_el: 'mooswitch', label: 'outside'}
    },
    
    initialize: function(element, options) {
		
		if(options) this.setOptions(options);
		
        this.el = $(element);
		this.elid = element;
		
		if(this.el) {
			if(this.options.validate.use) this.validate();
			if(this.options.replacement.use) this.replacement();
			if(this.options.switcher.use) this.switcher();
		}
	},
	
	validate: function() {
		new FormCheck(this.elid , {
			submitByAjax: this.options.validate.submitByAjax,
			tipsClass : 'tt',
			fieldErrorClass: 'error',
			display : {
				showErrors : this.options.validate.showErrors,
				closeTipsButton:0,
				errorsLocation:1,
				addClassErrorToField:1,
				indicateErrors:1,
				fixPngForIe :0	
			}
		});
	},
	
	replacement: function() {
		var MooForm = new mooForms(this.elid, {
			imageDir: this.options.replacement.imageDir,
			useSmall: this.options.replacement.useSmall,
			inputs: this.options.replacement.inputs
		});		
	},
	
	switcher: function() {
		if(this.options.switcher.mc != '') {
			if ($(this.options.switcher.mc)) {
				new mooSwitch(this.options.switcher.switch_el, {label: this.options.switcher.label});
			}
		} else {
			new mooSwitch(this.options.switcher.switch_el, {label: this.options.switcher.label});
		}
	}

});

function showHideRow(el, slider) {
	$(el).set('slide', {duration: 'long'});
	$(el).slide(slider);
	if(slider == 'in') {
		//$(el).getChildren('.show').setStyle('display','none');
		$('show_' + el).setStyle('display','none');
		$('hide_' + el).setStyle('display','inline');
	}else {
		$('show_' + el).setStyle('display','inline');
		$('hide_' + el).setStyle('display','none');
	}
}

window.addEvent('domready', function() {
									 
$$('.ignored').each(function(el) {
	el.slide('hide');
});								 
	
/*var thread_slider = new Fx.Slide('element').chain(function(){
    this.toogle();
});
*/

SqueezeBox.assign($$('a.boxed'), {
	parse: 'rel',
	ajaxOptions: {
		data: { 'boxed' : 'true' },
		evalScripts: true
	}
});

SqueezeBox.assign($$('a.console'), {
	size:{
		x:560,
		y:550
	},
	ajaxOptions: {
		data: { 'boxed' : 'true' },
		evalScripts: true
	},
	onOpen: function() {
//		alert("open");
		new formVaRe('email_console_form', {validate: {submitByAjax: true }, replacement: {inputs: ['checkbox','radio']	}});
	},
	onShow: function() {
//		alert("show");
		new formVaRe('email_console_form', {validate: {submitByAjax: true }, replacement: {inputs: ['checkbox','radio']	}});
	}
});

ReMooz.assign('a.remoozed', {
	'origin': 'img',
	'shadow': 'onOpenEnd', // fx is faster because shadow appears after resize animation
	'resizeFactor': 0.8, // resize to maximum 80% of screen size
	'cutOut': false, // don't hide the original
	'opacityResize': 0.4, // opaque resize
	'dragging': false, // disable dragging
	'centered': true // resize to center of the screen, not relative to the source element
});


$$('.icarousel').each(function(element) {
	var el_id = element.get('id');
	var el_frame_id = '#'+el_id+'_frame'; 
	var el_content_id = el_id+'_content'; 
	
/*	var iCarouselGallery = new iCarousel(el_content_id, {
		idPrevious: el_id+"_previous",
		idNext: el_id+"_next",
		idToggle: "undefined",
		item: {
			size: 532
		},
		animation: {
			type: "scroll",
			duration: 1000,
			amount: 1
		}
	});*/

//	alert(el_frame_id);
	
	$$(el_frame_id + ' a').each(function(el,i) {
//		alert("test");										  
		el.addEvent('click', function(event) {
			//alert("go to "+i);						  
			event.stop();
			iCarouselGallery.goTo(i);
		});
	}.bind(this));
});


var centerRoar = new Roar({
	/* hook in js */
	position: 'topRight'
});

if($$('.togglers') && $$('.elements')) {
	var openElement = Cookie.read('sideAcc');
	var sideAccordion = new Accordion($$('.togglers'), $$('.elements'), {
		show:0,
		alwaysHide: true,
		onActive: function(toggler, element){
			toggler.blur();
			toggler.addClass('active');
			var myCookie  = Cookie.write('sideAcc', toggler.get('text'), {duration: 1});	
			//Cookie.set('sideAcc', toggler.get('text'));
		},
		onBackground: function(toggler, element){
			toggler.blur(); 
			toggler.removeClass('active');
		}
	});
	
	if (openElement != '') {
        // loop over each accordion title
        $$('.togglers').each(function(element,i) {
	  if (openElement == element.get('text') && i > 0) {
	      sideAccordion.display(i);
			}
        });
	}
	
}

if($('imageMenu')) {
	var AngeboteMenu = new ImageMenu($$('#imageMenu div.li_menu'),{openWidth:250, border:0, open:0});
}

if($('anrede')) {
	new mooSwitch('anrede', {label_position:'outside'});
}

if($('mitgliedschaft')) {
	new mooSwitch('type_member', {label_position:'outside'});
	new mooSwitch('type_st', {label_position:'outside'});
	new mooSwitch('type_st_active', {label_position:'outside'});
	new mooSwitch('type_schule', {label_position:'outside'});
	new mooSwitch('type_firma', {label_position:'outside'});
}

/* Kontakt */
new formVaRe('freeform', {
	replacement: {
		useSmall: false
	}
});

/* Comments */
new formVaRe('comment_form');

/* META */
new formVaRe('req_form');
new formVaRe('register_member_form', {
	replacement: {
		useSmall: false
	}
});
new formVaRe('member_login_form');
new formVaRe('forgot_password_form');

/* Forum */

new formVaRe('sending_bulletin', {
	replacement: {
		inputs: ['checkbox','radio']
	}
});

new formVaRe('forum_form', {replacement: {inputs: ['checkbox','radio']}});
new formVaRe('submit_message', {replacement: {inputs: ['checkbox','radio']}});

new formVaRe('email_console_form', {replacement: {inputs: ['checkbox','radio']	}});

new formVaRe('advanced_search', {
	replacement: {
		inputs: ['checkbox','radio']
	}
});

new formVaRe('submit_post', {
	validate: {
		use: false
	},
	replacement: {
		inputs: ['checkbox','radio']
	}
});

new UvumiTextarea({
	selector:'textarea.limited',
	maxChar:100
});

new clickableTable('clickable', true);

new SlideAjaxReq('show_message_meta_ext', 'message_meta_ext', false, true);
new SlideAjaxReq('add_poll_btn', 'poll_mc', false, true);
new SlideAjaxReq('more_options_btn', 'more_options_mc', false, true);
new SlideAjaxReq('fast_reply_btn', 'fast_reply_mc', false, true);
new SlideAjaxReq('smiley_btn', 'smiley_mc', true, true);

$$('table.sorttable').each(function(sort) { new tableSoort(sort.id);});

if($('forum-tabs')) {
	var allTabs = $$('#forum-tabs h2');
	if(allTabs.length > 1) {
		var tabs = new SimpleTabs('forum-tabs', {
			selector: 'h2'
		});
	} else {
		$$('#forum-tabs h2').setStyle('display','none');
	}
}

if($('home-tabs')) {
	var allTabs = $$('#home-tabs .tab');
	if(allTabs.length > 1) {
		var tabs = new SimpleTabs('home-tabs', {
			selector: '.tab'
		});
	}
}

/* Too-Tips */
var greenTips = new Tips('.tt', {
	offsets: {'x': 10},
	onShow: function(tip) {
		tip.set('tween',{duration: 300});
		tip.fade('in');
	},
	onHide: function(tip) {
		tip.set('tween',{duration: 550});
		tip.fade('out');
	}
});

var siteSmoothScroll = new SmoothScroll();


// DOM-READY END
});