/** ########################################## 
 * 	TOOGLE TAB
 * ########################################## */
(function($){
	$.widget('ui.toggleTab', {
		_init: function(){
			
			var o = this.options,
				elem = this.element,
				jele = $(this.element),
				heightArray = new Array(),
				isHTMLSelected
			; 
 
			var tabs = $(o.tabSel, elem[0]);
			var boxes = $(o.panelSel, elem[0]);
			var buttons = $(o.buttonSel, elem[0]);
			
			tabs.each(function(){
				// calculate height of toggle div
				var maxHeight = $(this).find(o.panelSel).innerHeight();
				
				// handling for hospital foldable: determine max inner height
				var hospital_foldable_innertabs = $(this).find(o.panelSel).find('.content-preview'); 
				// hide all inner tabs
				hospital_foldable_innertabs.hide();
				// calculate height of div for each tab inside
				hospital_foldable_innertabs.each(
					function(i, e) {
						$(e).show();
						var curheight = $(this).parent().innerHeight();
						if(curheight > maxHeight) {
							maxHeight = curheight;
						}
						$(e).hide();
					}
				);
				// show first inner tab
				$(this).find(o.panelSel).find('.content-preview:first').show();
				
				// store max height
				heightArray.push(maxHeight);
				
			});
			
			boxes.each(function(){
				$(this).parent().css({'overflow': 'hidden'});
			});

			boxes.hide();
			
			// check if a active tab is set, if not set first tab active (=opened)
			var activeTab = jele.find(o.activeTabClass);
			if(activeTab.length < 1) {
				var firstTab = jele.find(o.tabSel+':first');
				firstTab.addClass(o.activeTabName);
			} 
				
			jele.find(o.activeTabClass).find(o.panelSel).show();
			
			
			buttons[o.toggleOn](
				function(){
					
					var currentTabId = $(this).parent().attr('id');
					var activeTabId = jele.find(o.activeTabClass).attr('id');
			
					if(currentTabId == activeTabId) {
						return;
					}
					
					var id = $(this).parent().attr('id');
					id = getIdFromString(id);
					id = parseInt(id)-o.beginAt;
					
					jele.find(o.activeTabClass).removeClass(o.activeTabName);
					var currentTab = $(this).parent();
					currentTab.addClass(o.activeTabName);
					currentTab.find(o.panelSel).parent()
					.stop(true)
					.animate({
						syncHeight: heightArray[id]
					},{
						syncElements: boxes.parent(),
						duration: o.duration, 
						complete: function(){
							/* nothing */
						}
					});
					tabs.find(o.panelSel).show();
				}
			);
			buttons.find("a").click(function(){$(this).parent().trigger("click"); return false;});
		
		},
		ui: function(){
			return {
				instance: this
			};
		}
	});
	
	$.ui.toggleTab.defaults = {
		duration: 400,
		activeTabClass: '.tab-active',
		activeTabName: 'tab-active',
		toggleOn: 'click',
		beginAt: 1
	};
	

})(jQuery); 


/** ########################################## 
 * 	syncHeight - animation step
 * ########################################## */
$.fx.step.syncHeight = function(fx){
	
	if (!fx.syncStart) {
        var o 		= fx.options,
			jElm	= $(fx.elem),
			full 	= 0
		;
		
		if(jElm.css('display') === 'none'){
			fx.elem.style.height = '0px';
			fx.elem.style.overflow = 'hidden';
			fx.elem.style.display = 'block';
			fx.start = 0;
		} else {
			fx.start = jElm.height();
		}
		fx.syncStart = [];

		fx.syncElements = $(o.syncElements)
			.map(function(i, elem){
				if(elem !== fx.elem){
					return elem;
				}
			})
			.each(function(i){
				fx.syncStart.push($(this).height());
				full += this['offsetHeight'];
			});
		
		fx['fullHeight'] = o['fullHeight'] || full;
		
		fx.syncEnd = (fx['fullHeight'] - fx.end) / fx.syncElements.length;
		if (fx.syncEnd > 0) {fx.syncEnd = 0;}
    }
    
	var synced = 0;
	fx.syncElements
		.each(function(i){
			var dim = Math.ceil(fx.pos * (fx.syncEnd - fx.syncStart[i]) + fx.syncStart[i]);
			synced += dim;
			dim = (dim <= 0) ? 0 : dim;
			this.style.height = dim + fx.unit;
		});
	
	if(fx['fullHeight'] <= fx.end){
		var tmpVal = fx['fullHeight'] - synced;
		if (tmpVal < 0){
			fx.elem.style.height = 0 + fx.unit;
		}else{
			fx.elem.style.height = fx['fullHeight'] - synced + fx.unit;
		}
	}else {
		var tmpVal = fx.end - synced;
		if (tmpVal < 0){
			fx.elem.style.height = 0 + fx.unit;
		}else{
			fx.elem.style.height = fx.end - synced + fx.unit;
		}
	}
};

/** ##############################################
 *	Helper function
 *	@param String string - completeID
 *	@return int 
 * ############################################## */
function getIdFromString(string){
	
	if(!string.match('[0-9]')){return -1;}
	
	var indexPos = string.lastIndexOf("_"); 
	var index = string.substring(indexPos+1);
	return index;
		 
	
//	var returnVal = '';
//	var c = string[string.length-1];
//	var subString = string.substring(0,string.length-1);
//	
//	if(c.match('[0-9]')){
//		if(string.length-2 > 0 && getIdFromString(subString) != -1){
//			returnVal += getIdFromString(subString);
//		}
//		returnVal += c;
//	}
//	
//
//	return returnVal;
}
