/** ########################################## 
 * 	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(){
				heightArray.push($(this).find(o.panelSel).innerHeight());
			});
			
			boxes.each(function(){
				
				$(this).parent().css({'overflow': 'hidden'});
			});

			boxes.hide();
			tabs.removeClass(o.activeTabName);
			jele.find(o.activeTabClass).find(o.panelSel).show();
			 
			
			buttons[o.toggleOn](
				function(){
					
					var id = $(this).parent().attr('id');
					id = getIdFromString(id);
					id = parseInt(id)-o.beginAt;
					
					var height = heightArray[id];
					
					var currentTab = $(this).parent();
					
					if(currentTab.hasClass(o.activeTabName)){
						//currentTab.find(o.panelSel).parent().slideUp();
						currentTab.find(o.panelSel).parent().animate({height:0});
						currentTab.removeClass(o.activeTabName);
					} else {
						jele.find(o.activeTabClass).removeClass(o.activeTabName);
						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;});
			//buttons.click(function(){$(this).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;		
		fx.syncEnd = Math.floor(fx.syncEnd);
		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;
}