/**
 * imagine orange
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to a commercial license 
 * Licensed for use from www.surplus.de
 *
 *
 * @category    ioHelper
 * @package     ImagineOrange 
 * @copyright   Copyright (c) 2011 imagine orange, Folker Schellenberg (http://www.imagine-orange.com)
 * @license     commercial
 *
 * 
 */
var ioHelperClass = Class.create({
	
	ioInitCarousel: function(id) {

		thatCarousel = new UI.Carousel(id);
		
		thatCarousel.observe('scroll:ended', function(event) {
			els = $$('#'+id+'CntlStatus ul li');
			els.each(function(el,i) {
						if (el.hasClassName('active')){
							el.removeClassName('active');
						}
						
					});
			if (els){
				els[thatCarousel.currentIndex()].addClassName('active');
			}
	 	   //alert("Carousel with id " + event.memo.carousel.id + " has just been scrolled" +  thatCarousel.currentIndex());
	 	 
	 	});
	   
		thatCarousel.observe('previousButton: enabled', function(event) {
	 	  alert("prev Btn");
	 	});
	   
	   thatCarousel.observe('nextButton: enabled', function(event) {
	   	  alert("next Btn");
	   	});
	},

	ioInitPageScroller: function() {
                   
    	  $('top').observe('click', function(event) {
    		  				new Effect.ScrollTo('topAnchor', { duration: 0.9 });
    		  				//anchor.scrollTo();
    		  				//Effect.ScrollTo('topAnchor');
    		  				Event.stop(event);
    	    				});
    	  
	},

	ioInitToggler: function(toggler, cnt) {
		//console.log('toggler found');
		toggler.insert({
			  top: new Element('span', {className: 'ioToggler' + (cnt ? '' : ' active')})
			});
		
		// handle each descendant element and search fpr ioToggler and ioTogglerContent
		toggler.descendants().each(function(el,i){
			
			if (el.hasClassName('ioToggler')){
				// set toggler id
				el.id = 'iot-' + cnt;
				
				// watch click event
				el.observe('click', function(event) {
					
					if (this.hasClassName('active')){
						this.removeClassName('active');
						$(this.id +'C').hide();
					} else {
						this.addClassName('active');
						$(this.id +'C').show();
					}
				})
			}
			if (el.hasClassName('ioTogglerContent')){
				// set content id
				el.id = 'iot-' + cnt + 'C';
				//set initial visibility
				if ($('iot-' + cnt).hasClassName('active')){
					el.show();
				} else {
					el.hide();
				}
			}
			
		});
	
 
	},

	ioPageInit: function() {
		
		this.ioInitPageScroller();
		var self = this;
		// search for carousels and init them
		$$('.ioCarousel').each(function(s,i) {
					self.ioInitCarousel(s.id);
		});
		
		// search for toggle boxes and init them
		$$('.ioToggleBox').each(function(el,i) {
					self.ioInitToggler(el,i);
		});
		
		var loginForm = $('login-form');
		
		if (loginForm != null){
			loginForm.observe('keypress', function(event){
				if ( event.keyCode == Event.KEY_RETURN  || event.which == Event.KEY_RETURN ) {
					document.loginForm.submit();
					Event.stop(event);
				}
			
			});
		}
			
	}
});

// initialize
Event.observe(window, "load", function(){
								var ioHelper = new ioHelperClass(); 
								ioHelper.ioPageInit();
								});
