var CMenuItemsCtrl = Class.create( {
		activeElement:null,
		root:null,
		debug:null,
		size:50,
		initialize:function( root_element, nsize ) {
			this.root = $(root_element);
			this.activeElement = null;
			this.size = nsize;
			if ( this.root )
				this.setActivity( );
		},
		setActivity: function( ) {
			var els = this.root.select( '.menuItemList' );
			
			function overAction( event ) {
				var element = Event.element(event).up(0);
				
				
				if ( !element.hasClassName('menuItemList') ) {
					var anc = element.ancestors();
					var parent = null;
	
					for (var i=0;i<anc.length;++i) {
						if (anc[i].hasClassName('menuItemList')) {
													parent = anc[i];
													break;
												}
					}
				} else {
					parent = element;
				}
				if (!parent) {
					//alert('no parent');
					return false;
				}
				
				parent.animate.run = 1;
				parent.ctrl.eSlideDown( parent.animate );
			}
			
			function outAction( event ) {
				var element = Event.element(event).up(0);
				
				
				if ( !element.hasClassName('menuItemList') ) {
					var anc = element.ancestors();
					var parent = null;
	
					for (var i=0;i<anc.length;++i) {
						if (anc[i].hasClassName('menuItemList')) {
													parent = anc[i];
													break;
												}
					}
				} else {
					parent = element;
				}
				if (!parent) {
					//alert('no parent');
					return false;
				}
				parent.animate.run = 1;
				parent.ctrl.eSlideUp( parent.animate );
			}
			
			for (var i=0; i<els.length;++i) {
				els[i].observe('mouseover', overAction );
				els[i].observe('mouseout', outAction );
				els[i].timerID = -1;
				els[i].uniq_id = i;
				els[i].ctrl = this;
				els[i].motion = false;
				els[i].effect = null;
				els[i].mode = 0;
				els[i].position = 0;
				els[i].animate = els[i].select('div.parentItems').first();
				els[i].animate.ctrl = this;
				els[i].animate.size = this.size;
				els[i].animate.run = 1;
			}
		},
		eSlideDown: function( obj ) {
			if (obj.effect_type == undefined) {
				obj.effect_type = 'slidedown';
			}
			
			if (obj.effect > -1 && obj.effect_type != 'slidedown') {
				obj.effect = -1;
			}
			
			obj.effect_type = 'slidedown';
		
			if (obj.position == undefined) {
				obj.position = 0;
			}
			
			if (obj.position > 1) obj.position = 1;
			if (obj.position < 1 && obj.run == 1) {
				obj.position = 1;
				obj.up(0).select('div.itemTitle').first().setStyle('text-decoration: underline');
			} else {
				obj.run = 0;
				obj.effect = -1;
			}
			
			var size = (obj.position>1?1:obj.position)*obj.size;
			obj.setStyle( 'height:'+size+'px');
		},	
		eSlideUp: function( obj ) {
			if (obj.effect_type == undefined) {
				obj.effect_type = 'slideup';
			}
		
			if (obj.effect > -1 && obj.effect_type != 'slideup') {
				obj.effect = -1;
			}
			
			obj.effect_type = 'slideup';
		
			if (obj.position == undefined) {
				obj.position = 1;
			}
			
			if (obj.position < 0) obj.position = 0;
			if (obj.position > 0 && obj.run == 1) {
				obj.position = 0;
				obj.up(0).select('div.itemTitle').first().setStyle('text-decoration: none');
			} else {
				obj.run = 0;
				obj.effect = -1;
			}
			
			var size = (obj.position<0?0:obj.position)*obj.size;
			obj.setStyle( 'height:'+size+'px');
		}
} );


var CTabItemAjax = null;
var CTabItem = Class.create( {
		el:null,
		ctrl:null,
		url:'',
		backImageTimer:0,
		initialize:function( tab_element, ctabctrl ) {
			this.el = tab_element;
			this.ctrl = ctabctrl;
			this.el.ctrl = ctabctrl;
			this.el.itemctrl = this;
			
			this.backImageTimer = 0;
			
			var alink = this.el.select('a');
			this.url = alink.first().readAttribute('href');
			
			this.url = this.url.gsub('#', '');
			
			this.setActions();
		},
		setActions: function() {
			
			function onclick( event ) {
				var element = Event.element(event);
				if ( !element.hasClassName('tabMenuItem') ) {
					var anc = element.ancestors();
					var parent = null;
					for (var i=0;i<anc.length;++i) {
						if (anc[i].hasClassName('tabMenuItem')) {
													parent = anc[i];
													break;
												}
					}
				} else {
					parent = element;
				}
				
				this.itemctrl.sendAjax();

				this.ctrl.setActiveTab( parent );
				element.blur();
			}
			
			this.el.observe('click', onclick );
		},
		resultAjax:function( transport ) {
			CTabItemAjax.container.fade( { 
					duration:0.5, 
					from:1, 
					to:0.45, 
					afterFinish:function() {
						//CTabItemAjax.ctrl.black.style.height = CTabItemAjax.ctrl.root.getHeight()+'px';
						CTabItemAjax.container.update( transport.responseText );
						CTabItemAjax.container.fade( {
							duration:0.5, 
							from:0.45, 
							to:1,
							afterFinish:function() {
								CTabItemAjax.ctrl.removeAjaxProgress();
								delete CTabItemAjax;
								CTabItemAjax = null;
							}
						} );
					} 
				} );
			
		},
		sendAjax: function() {
			if (CTabItemAjax == null) { 
				CTabItemAjax = { container:this.ctrl.container, ctrl:this.ctrl };
				this.ctrl.ajaxProgress();
				var ajax = new Ajax.Request(this.url, {
				  method: 'get',
				  onSuccess: this.resultAjax
				});
			}
		},
		returnImage: function( el ) {
			if ( this.ctrl.activeTab == this.el.identify() ) {
				var cel = this.ctrl.container;
				var img1_source = cel.select('img.hidden').first();
				var img1_dest = cel.select('img.mainImg').first();
				img1_dest.src = img1_source.src;
				
				var dest_text = cel.select('span').first();
				dest_text.update( img1_source.readAttribute('alt') );
			}
		}
} );

var CTabController = Class.create( {
		root:null,
		tabs:[],
		container:null,
		black:null,
		activeTab:null,
		//pageNavi:{},
		menuItems:[],
		initialize:function( root_element ) {
			
			this.tabs = [];
			this.black = null;
			this.activeTab = null;
			
			this.root = $(root_element);
			this.container = this.root.select('.tabContent').first();
			
			this.menuItems = this.root.select('.tabMenu .tabMenuItem');

			//var nv = this.root.select('.pageNavigator a');
			//this.pageNavi.leftArrow = nv[0];
			//this.pageNavi.rightArrow = nv[1];
			
			//nv[0].ctrl = this;
			//nv[1].ctrl = this;
			
			//this.pageNavi.leftArrow.observe('click', this.prevTab );
			//this.pageNavi.rightArrow.observe('click', this.nextTab );
		},
		addTab:function( tab ) {
			this.tabs.push( new CTabItem(tab, this) );
			if (this.activeTab == null) {
				this.setActiveTab( tab );
			} else if (this.tabs.length == 2) {
				this.setActiveTab( this.tabs[0].el );
			}
		},
		ajaxProgress: function() {
			if ( this.root ) {
				var bl = new Element('div');
				
				bl.style.width = this.root.getWidth()+'px';
				bl.style.height = this.root.getHeight()+'px';
				bl.style.position = 'absolute';
				bl.style.zIndex = 999;
				bl.style.left = '0px';
				bl.style.top = '0px';
				bl.style.display = 'block';
				//bl.style.backgroundColor = '#FFFFFF';
				bl.setOpacity( 1 );
				
				bl.style.backgroundPosition = 'center';
				bl.style.backgroundRepeat = 'no-repeat';
				bl.style.backgroundImage = 'url(/images/ajax.gif)';
				
				this.black = bl;
				this.root.insert( {'top':bl} );
			}
		},
		removeAjaxProgress: function() {
			this.black.remove();
		},
		changeImage: function( obj ) {
			var source = obj.select('img.hidden');
			source = source.first();
			var dest = this.root.select('.tabContent .mainImg').first();
			dest.src = source.src;
			
			var dest_text = this.root.select('.tabContent span').first();
			dest_text.update( source.readAttribute('alt') );
			
			var ictrl = $( this.activeTab ).itemctrl;
			if (ictrl.backImageTimer != 0) {
				window.clearTimeout( ictrl.backImageTimer );
				ictrl.backImageTimer = 0;
			}
			ictrl.backImageTimer = window.setTimeout( "\
						var el = $('"+this.activeTab+"');\
						el.itemctrl.backImageTimer = 0;\
						el.itemctrl.returnImage();\
						", 15000 );
		},
		getTabIndex:function( tab_id ) {
			var obj = $(tab_id);
			if ( obj ) {	
				var ctrl = obj.itemctrl.ctrl;

				for (var i=0;i<ctrl.tabs.length;++i){
					if (ctrl.tabs[i].el.identify() == tab_id ) {
						
						return i;
					}
				}
			}
			
			return -1;
		},
		setActiveTab: function( tab ) {
			var ctrl = tab.itemctrl.ctrl;
			
			/* Clear back timer */
			if (tab.itemctrl.backImageTimer > 0) {
				window.clearTimeout( tab.itemctrl.backImageTimer );
				tab.itemctrl.backImageTimer = 0;
			}
			/* Select menuItems */
			var index = ctrl.getTabIndex( ctrl.activeTab );
			if ( index >= 0 && ctrl.menuItems[ index ].hasClassName('activeMenu') ) {
				ctrl.menuItems[ index ].removeClassName('activeMenu');
			}
			
			var index2 = ctrl.getTabIndex( tab.identify() );
			if ( index2 >= 0 && !ctrl.menuItems[ index2 ].hasClassName('activeMenu') ) {
				ctrl.menuItems[ index2 ].addClassName('activeMenu');
			}
			
			ctrl.activeTab = tab.identify();
			/*
			var i=0
			for(; i < ctrl.tabs.length; ++i) {
				if (ctrl.tabs[i].el.identify() == ctrl.activeTab)
					break;
			}
			
			if (i <= 0 && ctrl.pageNavi.leftArrow.hasClassName('imageLeft')) {
				ctrl.pageNavi.leftArrow.removeClassName('imageLeft');
				ctrl.pageNavi.leftArrow.addClassName('imageLeftInactive');
			} else if ( ctrl.pageNavi.leftArrow.hasClassName('imageLeftInactive') && i > 0 ) {
				ctrl.pageNavi.leftArrow.removeClassName('imageLeftInactive');
				ctrl.pageNavi.leftArrow.addClassName('imageLeft');
			}
			
			if (i >= ctrl.tabs.length-1 && ctrl.pageNavi.rightArrow.hasClassName('imageRight')) {
				ctrl.pageNavi.rightArrow.removeClassName('imageRight');
				ctrl.pageNavi.rightArrow.addClassName('imageRightInactive');
			} else if ( ctrl.pageNavi.rightArrow.hasClassName('imageRightInactive') && i < ctrl.tabs.length-1 ) {
				ctrl.pageNavi.rightArrow.removeClassName('imageRightInactive');
				ctrl.pageNavi.rightArrow.addClassName('imageRight');
			}*/
		}
} );
