// TAB MODULE
function TabModule (ObjectName,DivID,RequestURL) {	
	// PRIVATE VERIABLES
	var TabObject = document.getElementById(DivID);	
	var MaxWidth = TabObject.offsetWidth;
	var URL = RequestURL;
	var NavObject = (TabObject.childNodes[0].nodeName != '#text') ? TabObject.childNodes[0].childNodes : TabObject.childNodes[1].childNodes;	
	for(a = 0; a < TabObject.childNodes.length; a++) {
		if (TabObject.childNodes[a].className == 'tabloader') {
			var Loader = TabObject.childNodes[a];	
		}
		if (TabObject.childNodes[a].className == 'tabcontent') {
			var Content = TabObject.childNodes[a];	
		}
	}
	var Name = ObjectName;
	var TabSection = new Array();
	var ShowAllTabs = false;	
	var TotalWidth = 0;
	
	// PUBLIC	
	this.Active = 0;
	this.ActiveSection = 0;	
	this.ButtonActive = null;
	this.ButtonActiveSection = null;	
	this.MinWidth = 0;
	this.FirstElement = 1;	
	this.LastElement = 1;
	this.InAnimation = false;
	
	// SCAN NAV
	var CountLI = 0;
	var CountSection = 0;
	var SectionClassID = '';
	for(i=0; i < NavObject.length; i++) {
		if (NavObject[i].nodeName == 'DIV') {
			
			if (SectionClassID == '') {
				SectionClassID = NavObject[i].className.split(' ')[0];
				TabSection[CountSection] = new Array();
			} else if (SectionClassID != NavObject[i].className.split(' ')[0]) {
				SectionClassID = NavObject[i].className.split(' ')[0];				
				CountSection++;
				TabSection[CountSection] = new Array();
				CountLI = 0;
			} else {
				CountLI++;
			}	
			
			if (NavObject[i].className.match(/active/)) {
				this.Active = CountLI;
				this.ActiveSection = CountSection;
				this.ButtonActive = CountLI;
				this.ButtonActiveSection = CountSection;	
				TabSection[CountSection][CountLI] = new Array (i,NavObject[i].offsetWidth+1);	
				NavObject[i].onclick = new Function ("document."+Name+".LoadTab("+CountSection+","+CountLI+")");
				NavObject[i].setAttribute('id',Name+'_'+CountSection+'_'+CountLI);	
				TotalWidth += NavObject[i].offsetWidth+17;
			}	
			
			else if (NavObject[i].className.match(/sub/)) {
				TabSection[CountSection][CountLI] = new Array (i,NavObject[i].offsetWidth+18);		
				NavObject[i].onclick = new Function ("document."+Name+".Previous("+CountSection+")");	
				NavObject[i].setAttribute('id',Name+'_'+CountSection+'_'+CountLI);
				NavObject[i].style.display = "none";	
			}				
			
			else if (NavObject[i].className.match(/add/)) {
				TabSection[CountSection][CountLI] = new Array (i,NavObject[i].offsetWidth+18);				
				NavObject[i].onclick = new Function ("document."+Name+".Next("+CountSection+")");	
				NavObject[i].setAttribute('id',Name+'_'+CountSection+'_'+CountLI);	
				NavObject[i].style.display = "none";
			}	
			
			else {
				TabSection[CountSection][CountLI] = new Array (i,NavObject[i].offsetWidth+17);
				NavObject[i].onclick = new Function ("document."+Name+".LoadTab("+CountSection+","+CountLI+")");
				NavObject[i].setAttribute('id',Name+'_'+CountSection+'_'+CountLI);
				TotalWidth += NavObject[i].offsetWidth+17;
			}	
			
	
		}
	}
	
	// CHECK SHOW TABS	
	if (MaxWidth > TotalWidth) {
		ShowAllTabs = true;
	}
	
	// DEC 14 HACK
	ShowAllTabs = false;
	
	// SET GROUPS/SET VISIBLE
	for (a = 0; a < TabSection.length; a++) {
		
		var SliderHolder = document.createElement('div');
			SliderHolder.setAttribute('id',Name+'_Holder_'+a);
			SliderHolder.style.overflow = "hidden";
			SliderHolder.style.width = "0px";
			SliderHolder.style.display = "block";
		
		var Slider = document.createElement('div');
			Slider.setAttribute('id',Name+'_Silder_'+a);			
	
		var SectionWidth = 0;	
		for (b = 1; b < TabSection[a].length-1; b++) {
			Slider.appendChild(document.getElementById(Name+'_'+a+'_'+b));	
			SectionWidth += TabSection[a][b][1]+4;
		}
		
		Slider.style.width = SectionWidth+"px";
		SliderHolder.appendChild(Slider);		
		
		document.getElementById(Name+'_0_0').parentNode.insertBefore(SliderHolder,document.getElementById(Name+'_'+a+'_'+(TabSection[a].length-1))); 		
		
		if (this.ActiveSection == a) {
			if(TabSection[a].length > 3 && !ShowAllTabs) {	
				document.getElementById(Name+'_'+a+'_'+(TabSection[a].length-1)).style.display = "block";
				this.MinWidth += TabSection[a][(TabSection[a].length-1)][1];	
			}		
		} else {		
			if(TabSection[a].length > 3 && !ShowAllTabs) {	
				document.getElementById(Name+'_'+a+'_'+(TabSection[a].length-1)).style.display = "block";
				this.MinWidth += TabSection[a][(TabSection[a].length-1)][1];
			}			
			if (ShowAllTabs) {
				var SectionWidth = 0;
				for (b = 1; b < TabSection[a].length-1; b++) {
				SectionWidth += TabSection[a][b][1];	
				}
				document.getElementById(Name+'_Holder_'+a).style.width = SectionWidth+"px";
			} else {			
				document.getElementById(Name+'_Holder_'+a).style.width = TabSection[a][1][1]+"px";
				this.MinWidth += TabSection[a][1][1];			
			}
		}		
	}		
		
	// DISPLAY ACTIVE GROUP	
	var ActiveWidth = 0;
	this.FirstElement = 1;
	for (a = 1; a < TabSection[this.ActiveSection].length-1; a++) {	
		if ((this.MinWidth+ActiveWidth+TabSection[this.ActiveSection][a][1]) < MaxWidth) {
			ActiveWidth += TabSection[this.ActiveSection][a][1];	
			this.LastElement = a+1;
		} else {
		 break;
		}
	}	
	document.getElementById(Name+'_Holder_'+this.ActiveSection).style.width	= ActiveWidth+"px"; 
	
	if (this.LastElement == TabSection[this.ActiveSection].length-1) {				
		document.getElementById(Name+'_'+this.ActiveSection+'_'+(TabSection[this.ActiveSection].length-1)).style.display = "none";		
		this.MinWidth -= TabSection[this.ActiveSection][TabSection[this.ActiveSection].length-1][1];			
	}	
	TabObject.style.visibility = "visible";		
	
	// LOAD 
	this.LoadTab = function (Selection,ID) { 
	
		// OPEN TAB SECTION
		if (Selection != this.ActiveSection && !ShowAllTabs) {
			this.Next(Selection);		
		}
		
		// SET BUTTONS
		if (this.ButtonActive != null) {
			document.getElementById(Name+'_'+this.ButtonActiveSection+'_'+this.ButtonActive).className = document.getElementById(Name+'_'+this.ButtonActiveSection+'_'+this.ButtonActive).className.split(' ')[0];
		}
		document.getElementById(Name+'_'+Selection+'_'+ID).className = document.getElementById(Name+'_'+Selection+'_'+ID).className+' active';
		this.ButtonActive = ID;
		this.ButtonActiveSection = Selection
			
		// PRE LOADER
		Content.style.visibility = "hidden";
		Loader.style.left = ((Content.offsetWidth-16)/2)+"px";
		Loader.style.top = ((Content.offsetHeight-16)/2)+50+"px";
		Loader.innerHTML = '<img src="/images/tabs-loader.gif" width="16" height="16" />';	
		
		// CONTENT
		var ContentPost = new HTTPRequest ();
		ContentPost.URL = URL;
		ContentPost.Parameters = "section="+Selection+"&id="+ID;	
		ContentPost.Completed = function(Response) {
				
				Loader.innerHTML = '';
				Loader.style.left = "0px";
				Loader.style.top = "0px";				
				Content.innerHTML = Response;	
				Content.style.visibility = "visible";
				
				var x = Content.getElementsByTagName("script");   
				for(var i=0;i<x.length;i++)  { 
					eval(x[i].text);  
				}  
				
			};	
		ContentPost.Error = function (Response) {
				Loader.innerHTML = '';
				Loader.style.left = "0px";
				Loader.style.top = "0px";				
				Content.innerHTML = '';
			};	
		ContentPost.POST();				
			
	}
	
	this.LoadTab(this.ActiveSection,this.Active);
	
	// NEXT
	this.Next = function (Selection) {
		if (!this.InAnimation) {		
			if (this.ActiveSection != Selection) {
			
				// CURRENT SELECTION
				this.Animation(this.ActiveSection,TabSection[this.ActiveSection][1][1],0);
				
				// PREVIOUS
				if (document.getElementById(Name+'_'+this.ActiveSection+'_'+0).offsetWidth != 0) {
					this.AnimationWidth(document.getElementById(Name+'_'+this.ActiveSection+'_'+0),0);
					this.MinWidth -= TabSection[this.ActiveSection][0][1];	
				}
				
				// NEXT
				if(TabSection[this.ActiveSection].length > 3 && document.getElementById(Name+'_'+this.ActiveSection+'_'+(TabSection[this.ActiveSection].length-1)).offsetWidth == 0) {	
					this.AnimationWidth(document.getElementById(Name+'_'+this.ActiveSection+'_'+(TabSection[this.ActiveSection].length-1)),26);
					this.MinWidth += TabSection[this.ActiveSection][(TabSection[this.ActiveSection].length-1)][1];
				}	

				// ADD FIRST ITEM
				this.MinWidth += TabSection[this.ActiveSection][1][1];	
				
								this.ActiveSection = Selection;
				
								// NEW SELECTION				
								this.MinWidth -= TabSection[this.ActiveSection][1][1];		
								
								var ActiveWidth = 0;
								this.FirstElement = 1;
				for (a = 1; a < TabSection[Selection].length-1; a++) {	
					if ((this.MinWidth+ActiveWidth+TabSection[Selection][a][1]) <= MaxWidth) {
						ActiveWidth += TabSection[Selection][a][1];	
						this.LastElement = a+1;
					} else {
					 break;
					}
				}

				
				if (this.LastElement == TabSection[Selection].length-1 && TabSection[Selection].length > 3) {
					this.AnimationWidth(document.getElementById(Name+'_'+Selection+'_'+(TabSection[Selection].length-1)),0);				
					this.MinWidth -= TabSection[Selection][TabSection[Selection].length-1][1];			
				}
				
				this.InAnimation = true;
				this.Animation(Selection,ActiveWidth,0);
				
			
			} else {			
				if (document.getElementById(Name+'_'+Selection+'_'+0).style.display == "none" || document.getElementById(Name+'_'+Selection+'_'+0).offsetWidth == 0) {
					document.getElementById(Name+'_'+Selection+'_'+0).style.width = "0px";	
					document.getElementById(Name+'_'+Selection+'_'+0).style.display = "block";	
					this.AnimationWidth(document.getElementById(Name+'_'+Selection+'_'+0),26);
					this.MinWidth += TabSection[Selection][0][1];			
				}
				
				var SlideX = document.getElementById(Name+'_'+Selection+'_'+this.LastElement).offsetLeft;
				
				var ActiveWidth = 0;
				this.FirstElement = this.LastElement;
				
				for (a = this.LastElement; a < TabSection[Selection].length-1; a++) {	
					if ((this.MinWidth+ActiveWidth+TabSection[Selection][a][1]) <= MaxWidth) {
						ActiveWidth += TabSection[Selection][a][1];	
						this.LastElement = a+1;
					} else {
					 break;
					}
				}

				if (this.LastElement == TabSection[Selection].length-1) {
					this.AnimationWidth(document.getElementById(Name+'_'+Selection+'_'+(TabSection[Selection].length-1)),0);				
					this.MinWidth -= TabSection[Selection][TabSection[Selection].length-1][1];			
				}	

				this.InAnimation = true;
				this.Animation(Selection,ActiveWidth,(SlideX*-1));
			} 	
		}
	}
	
	// PREVIOUS
	this.Previous = function (Selection) {
		if (!this.InAnimation) {
		
				if (document.getElementById(Name+'_'+Selection+'_'+(TabSection[Selection].length-1)).offsetWidth == 0) {
					this.AnimationWidth(document.getElementById(Name+'_'+Selection+'_'+(TabSection[Selection].length-1)),26);
					this.MinWidth += TabSection[Selection][(TabSection[Selection].length-1)][1];			
				}
		
				var ActiveWidth = 0;
				this.LastElement = this.FirstElement;
				for (a = this.FirstElement-1; a > 0; a--) {
					if ((this.MinWidth+ActiveWidth+TabSection[Selection][a][1]) < MaxWidth) {
						ActiveWidth += TabSection[Selection][a][1];
						this.FirstElement = a;
					} else {				
						break;
					}
				}	
					
				if (this.FirstElement == 1) {
					this.AnimationWidth(document.getElementById(Name+'_'+Selection+'_'+0),0);
					this.MinWidth -= TabSection[Selection][0][1];			
				}		
			
			var SlideX = document.getElementById(Name+'_'+Selection+'_'+this.FirstElement).offsetLeft;		
			this.InAnimation = true;
			this.Animation(Selection,ActiveWidth,(SlideX*-1));
			
		}		 	
	}
	
	// ANIMATION
	this.AnimationWidth = function (Obj,Width) {	
		
		var IE = /*@cc_on!@*/false;
		Obj.style.display = "block";
		
		function EaseInOut(Start,End,Current) {			
			if (Start < End) {
				var nEnd = Math.abs(Math.abs(End)-Math.abs(Start));
				var nCurrent = Math.abs(nEnd - Math.abs(Math.abs(End)-Math.abs(Current)));		
			} else if (Start > End) {
				var nEnd = Math.abs(Math.abs(Start)-Math.abs(End));
				var nCurrent = Math.abs(nEnd - Math.abs(Math.abs(Current)-Math.abs(End)));	
			}	
			var Percentage = Math.abs(Math.round((nCurrent/nEnd)*100))+1;
			if (Percentage < 50) {
				return Math.ceil(Math.pow(nEnd*(Percentage/100),.65));				
			} else {
				return (100-Percentage <= 0) ? (IE) ? 2:1 : Math.ceil(Math.pow(nEnd*((100-Percentage)/100),.65));
			}	 
		} 
		
		function ObjDiv(Obj,Start,Increment) {		
			if (Start < Width && Width > Obj.offsetWidth) {			
				Obj.style.width = (Obj.offsetWidth+Increment)+"px";
				ObjDelay = function () { ObjDiv(Obj,Start,EaseInOut(Start,Width,Obj.offsetWidth)); } 
				setTimeout(ObjDelay,40);	
			} else if (Start > Width && Width < Obj.offsetWidth) {			
				Obj.style.width = (Obj.offsetWidth-Increment)+"px";
				ObjDelay = function () { ObjDiv(Obj,Start,EaseInOut(Start,Width,Obj.offsetWidth)); } 
				setTimeout(ObjDelay,40);	
			} else if (Start != Width) {
				Obj.style.width = Width+"px";
				if (Obj.offsetWidth == 0) {
					Obj.style.display = "none";
				} 		
			}  else if (Start == Width) {
				Obj.style.width = Width+"px";
				if (Obj.offsetWidth == 0) {
					Obj.style.display = "none";
				} 	
			} 		
		}				
		
		var ObjDelay = function () { ObjDiv(Obj,Obj.offsetWidth,EaseInOut(Obj.offsetWidth,Width,Obj.offsetWidth)); } 
		setTimeout(ObjDelay,40);	
	}
	
	
	// ANIMATION
	this.Animation = function (Selection,Width,Position) {	
		
		var IE = /*@cc_on!@*/false;
		
		function EaseInOut(Start,End,Current) {			
			if (Start < End) {
				var nEnd = Math.abs(Math.abs(End)-Math.abs(Start));
				var nCurrent = Math.abs(nEnd - Math.abs(Math.abs(End)-Math.abs(Current)));		
			} else if (Start > End) {
				var nEnd = Math.abs(Math.abs(Start)-Math.abs(End));
				var nCurrent = Math.abs(nEnd - Math.abs(Math.abs(Current)-Math.abs(End)));	
			}	
			var Percentage = Math.abs(Math.round((nCurrent/nEnd)*100))+1;
			if (Percentage < 50) {
				return Math.ceil(Math.pow(nEnd*(Percentage/100),.65));				
			} else {
				return (100-Percentage <= 0) ? (IE) ? 2:1 : Math.ceil(Math.pow(nEnd*((100-Percentage)/100),.65));
			}	 
		} 
		
		function HolderDiv(Start,Increment) {		
			if (Start < Width && Width > document.getElementById(Name+'_Holder_'+Selection).offsetWidth) {			
				document.getElementById(Name+'_Holder_'+Selection).style.width = (document.getElementById(Name+'_Holder_'+Selection).offsetWidth+Increment)+"px";
				HolderDelay = function () { HolderDiv(Start,EaseInOut(Start,Width,document.getElementById(Name+'_Holder_'+Selection).offsetWidth)); } 
				setTimeout(HolderDelay,40);	
			} else if (Start > Width && Width < document.getElementById(Name+'_Holder_'+Selection).offsetWidth) {			
				document.getElementById(Name+'_Holder_'+Selection).style.width = (document.getElementById(Name+'_Holder_'+Selection).offsetWidth-Increment)+"px";
				HolderDelay = function () { HolderDiv(Start,EaseInOut(Start,Width,document.getElementById(Name+'_Holder_'+Selection).offsetWidth)); } 
				setTimeout(HolderDelay,40);	
			} else if (Start != Width) {
				document.getElementById(Name+'_Holder_'+Selection).style.width = Width+"px";	
			}  else if (Start == Width) {
				document.getElementById(Name+'_Holder_'+Selection).style.width = Width+"px";	
			}		
		}
		var HolderDelay = function () { HolderDiv(document.getElementById(Name+'_Holder_'+Selection).offsetWidth,EaseInOut(document.getElementById(Name+'_Holder_'+Selection).offsetWidth,Width,document.getElementById(Name+'_Holder_'+Selection).offsetWidth)); } 
		setTimeout(HolderDelay,40);		
		
		function SlideDiv(Start,Increment) {			
			if (Start < Position && Position > document.getElementById(Name+'_Silder_'+Selection).offsetLeft) {			
				document.getElementById(Name+'_Silder_'+Selection).style.left = (document.getElementById(Name+'_Silder_'+Selection).offsetLeft+Increment)+"px";			
				SliderDelay = function () {  SlideDiv(Start,EaseInOut(Start,Position,document.getElementById(Name+'_Silder_'+Selection).offsetLeft));  }
				setTimeout(SliderDelay,40);
			} else if (Start > Position && Position < document.getElementById(Name+'_Silder_'+Selection).offsetLeft) {			
				document.getElementById(Name+'_Silder_'+Selection).style.left = (document.getElementById(Name+'_Silder_'+Selection).offsetLeft-Increment)+"px";	
				SliderDelay = function () {  SlideDiv(Start,EaseInOut(Start,Position,document.getElementById(Name+'_Silder_'+Selection).offsetLeft));  }
				setTimeout(SliderDelay,40);
			} else if (document.getElementById(Name+'_Silder_'+Selection).offsetLeft != Position && IE) {
				document.getElementById(Name+'_Silder_'+Selection).style.left = (Position-1)+"px";	
				document[Name].InAnimation = false;
			} else if (document.getElementById(Name+'_Silder_'+Selection).offsetLeft == Position) {	
				document[Name].InAnimation = false;		
			}			
		}	
		var SliderDelay = function () {  SlideDiv(document.getElementById(Name+'_Silder_'+Selection).offsetLeft,EaseInOut(document.getElementById(Name+'_Silder_'+Selection).offsetLeft,Position,document.getElementById(Name+'_Silder_'+Selection).offsetLeft));  }
		setTimeout(SliderDelay,40);		
	}
				
}
