var ignoreAllVars = false;
var ignoreGclTargets = false;
var linkExtraFeatures = [[]];
linkExtraFeatures[0] = [0,0,'#SomeInvalidTestLink#',
'title','Test'];
var extraFeatureString = "";

function generateTab(tabNames, tabURLs, sublinksNames, sublinksURLs){
	//document.write("<div class=\"ibm-container\"><h2 class=\"ibm-first\">Testing the tab script</h2><div class=\"ibm-container-body\">Testing, sorry for any inconvenience. Michael Nielsen.</div></div>");
	//alert("The automated script is currently under reconstruction.\nPlease note that the tabs may not work properly, and that you might experience several alert-boxes.\n\nSorry for the inconvenience.");
	try{
//Check parameters and throws error if wrong
if(tabNames.length!=tabURLs.length) 
	throw "Number of tab names does not match number of tab URLs";
if(sublinksNames.length!=sublinksURLs.length) 
	throw "Number of sublink names does not match number of sublink URLs";
if((sublinksNames.length!=0 && sublinksURLs.length!=0) && (sublinksNames.length!=tabNames.length || sublinksURLs.length!=tabURLs.length))
	throw "Number of sublink specification does not match number of tabs";
if(sublinksNames.length!=0 && sublinksURLs.length!=0){
	for(i=0;i<tabNames.length;i++)
	 if(sublinksNames[i].length!=sublinksURLs[i].length)
	 	throw "Number of sublink names and URLs does not match on tab number " + i;
}

//Identifies selected page by URL
  selected = 0; 
  var selectedSubLink;;
  str=location.href;
  //str="http://www-03preview.ibm.com/businesscenter/smb/dk/da/bladecenter?ca=dk_tablink";
  var pageURL;

if(ignoreAllVars){//
	//pageURL = str.replace(location.search,"");//
	pageURL = pathNoVars(str);	
}else{//

pageURL = str;//
}
if(ignoreGclTargets){
	pageURL = removeTarget(pageURL,"gcl_xmlid");
}



//
 // just for help 
 //pageURL = "http://www-03preview.ibm.com/businesscenter/smb/dk/da/amdservers?ca=dk_tablink";
  
var thisTabURL;
var thisSublinkArray = [];  

for(i=0;i<tabNames.length;i++){
if(ignoreAllVars){//
	thisTabURL = pathNoVars(tabURLs[i]);//
	if(sublinksNames.length != 0){
		for(var k=0;k<sublinksURLs[i].length;k++){//
		    thisSublinkArray[k] = pathNoVars(sublinksURLs[i][k]);//
		    
		}// 
	}
    }else{
	thisTabURL = tabURLs[i];//
	if(sublinksNames.length != 0){
		for(var k=0;k<sublinksURLs[i].length;k++){//
		    thisSublinkArray[k] = sublinksURLs[i][k];//
		    
		}//     
	}
    }//

	if(pageURL.slice(pageURL.length-thisTabURL.length)==thisTabURL){
		selected = i;
	}
	
	if(selected == 0 && pageURL.search(thisTabURL)>0){			
			selected = i;
	}
	 
	if(sublinksNames.length!=0){
		for(j=0;j<thisSublinkArray.length;j++){
			  if(pageURL.slice(pageURL.length-thisSublinkArray[j].length)==thisSublinkArray[j]){
				selected = i;
				selectedSubLink = j;
				status = "OK";
			 }

			 if(selectedSubLink==0 && status!="OK"){
				if(pageURL.search(thisSublinkArray)>0){
					selectedSubLink = j;
				}
			}
		}
	}
	thisSublinkArray=[];
  }








//Checks if script is being run on live server and adds junction part, "jct3004c", to the URLs
//  testServerIdentifier = "preview";
//  testServerProtocol = "http";
//  urlAppend = "/jct03004c";
//  testone = pageURL.match(testServerIdentifier);

 
// if(testone==null){
//  	for(i=0;i<tabURLs.length;i++){
//		testtwo = tabURLs[i].match(testServerProtocol);
//		
//		if(testtwo==null){
//			tabURLs[i] = urlAppend + tabURLs[i];		
//		}
//		
//		if(sublinksNames.length!=0){
//			for(j=0;j<sublinksURLs[i].length;j++){
//				testthree = sublinksURLs[i][j].match(testServerProtocol);
//				if(testthree==null){
//				sublinksURLs[i][j] = urlAppend + sublinksURLs[i][j];
//				}
//			}
//		}
//	}
//  }
  
//Initial table structure for tabs

  if(selected==0)
    document.write('<div class="ibm-container ibm-graphic-tabs ibm-combo-tabs"><div class="ibm-tab-section"><h2 class="ibm-access">Tab navigation</h2><ul class="ibm-tabs">')			
  else
    document.write('<div class="ibm-container ibm-graphic-tabs ibm-combo-tabs"><div class="ibm-tab-section"><h2 class="ibm-access">Tab navigation</h2><ul class="ibm-tabs">');			

  
//Loops thru tabs and outputs HTML table cells
 
  for(i=0;i<tabNames.length;i++)
  {
	
	if (i == 0){
		//prvy
		document.write("<li class='ibm-first-tab")
		
		} else if (i == tabNames.length-1){
		//posledny
		document.write("<li class='ibm-last-tab")
		
		} else {
		//ostatne
		document.write("<li class='")
		
		}  
  
		
	

	//check if there are special conditions on this link
	//linkExtraFeatures = [MainLinkID, SublinkID, URL, name, value]
	extraFeatureString = "";
	if(linkExtraFeatures.length>0){
		for(var v=0;v<linkExtraFeatures.length;v++){
			if(linkExtraFeatures[v][0] == null){linkExtraFeatures[v][0] = -1;}
			if(linkExtraFeatures[v][1] == null){linkExtraFeatures[v][1] = -1;}
			
			if(linkExtraFeatures[v].length==5){
				if(linkExtraFeatures[v][1]==-1){
					if((linkExtraFeatures[v][0]==i)|| (linkExtraFeatures[v][0]!=i && tabURLs[i]==linkExtraFeatures[v][2])){
						//Extra feature to be added
						extraFeatureString += " " + linkExtraFeatures[v][3] + "=\""+linkExtraFeatures[v][4]+"\"";
					}
				}
			}else{
				throw "Tab link extra feature num " + v + " in main tabs is out of bounds.\n offset="+(5-linkExtraFeatures[v].length)+"";
			}
		}
	}		
 	 if(i==selected){
		document.write(" ibm-highlight-tab'><a href='"+tabURLs[i]+"'");
		document.write(extraFeatureString);
		document.write(">"+tabNames[i]+"<\/a><span class='ibm-access'>- selected tab,<\/span><\/li>");
	 }else{
		document.write("'><a href='"+tabURLs[i]+"'"+extraFeatureString+">"+tabNames[i]+"<\/a><\/li>");
	 }
  
    
  }   

//End-table structure for tabs
document.write('<\/ul><\/div>');

//If sub-links exist
if(sublinksNames.length!=0 && sublinksNames[selected].length!=0){
	//Begin sub-links table structure
	document.write('<div class="ibm-tab-section ibm-text"><h2 class="ibm-access">Tab navigation</h2><ul class="ibm-tabs">');

	
	//Loops thru sublinks		
	names = sublinksNames[selected];
	urls = sublinksURLs[selected];
	for(i=0;i<names.length;i++){
		if (i == 0){
		//prvy
		document.write("<li class='ibm-first-tab ")
		
		} else if (i == names.length-1){
		//posledny
		document.write("<li class='ibm-last-tab");
		
		} else {
		//ostatne
		document.write("<li class='");
		
		}
		extraFeatureString = "";
		if(linkExtraFeatures.length>0){
			for(var v=0;v<linkExtraFeatures.length;v++){
				if(linkExtraFeatures[v].length==5){
					if(linkExtraFeatures[v][0] == null){linkExtraFeatures[v][0] = -1;}
					if(linkExtraFeatures[v][1] == null){linkExtraFeatures[v][1] = -1;}
					//alert("i=" + i + "\nURL=" + urls[i] + "\nlinkExtraFeatures["+v+"][1]=" + linkExtraFeatures[v][1] + "\nlinkExtraFeatures["+v+"][2]" + linkExtraFeatures[v][2]);
					if((linkExtraFeatures[v][1]==i)|| (linkExtraFeatures[v][1]!=i && urls[i]==linkExtraFeatures[v][2])){
						//Extra feature to be added
						extraFeatureString += " " + linkExtraFeatures[v][3] + "=\""+linkExtraFeatures[v][4]+"\"";
					}
				}else{
					throw "Tab link extra feature num " + v + " in sub tabs is out of bounds.\n offset="+(5-linkExtraFeatures[v].length)+"";
				}
			}
		}	
		
		//for test only
		//if(extraFeatureString != ""){
		//	alert("Added extras!\nurl: "+urls[i] + "\nExtras: " + extraFeatureString);
		//}
		
		if(selectedSubLink==i) {
			document.write(" ibm-highlight-tab'><a href='"+urls[i]+"'" + extraFeatureString + ">"+names[i]+"<\/a> <span class='ibm-sep'>&nbsp;<\/span><\/li>");
		} else {
			document.write("'><a href='"+urls[i]+"'" + extraFeatureString + ">"+names[i]+"<\/a><span class='ibm-sep'>&nbsp;<\/span><\/li>"); 
		}
	
	}
	    
	//End sub-links table structe
	document.write('<\/ul><\/div><\/div>');//<div class="ibm-container-body"><br \/><p><br \/><\/p><\/div>');
}else{
	//document.write('<div class="ibm-container-body"><\/div><\/div>');
	document.write('<\/div>');
}

//handling the spacing issue with firefox
document.write("<div id=\"tabMain\"></div>");
addPaddingDiv();

}
catch(error){
	alert("Error in tabs.js: "+error);
}


}
function addPaddingDiv(){
	if(navigator.userAgent.match("Firefox")) {
	   	//alert("Firefox detected: resolving spacing issue");	
		var ni = document.getElementById("tabMain");
		var newdiv = document.createElement('div');
		var divIdName = "1";//'ibm-container-body';
		var cssString = "height:20px;font-size:17px;"; 
		newdiv.setAttribute('id',divIdName);
		newdiv.setAttribute('class',"ibm-container-body");
		//newdiv.setAttribute('style',cssString);
		newdiv.innerHTML = "&nbsp;";
		ni.appendChild(newdiv);
	}
	return true;
}


function pathNoVars(path){
	var results = path.split("?");
	if(results==null){
		return "";
	}else{
		return results[0];
	}
} 
function removeTarget(urlStr,targetStr){
	var tmpSlashedSplitArr = urlStr.split("/");
	var tmpString = "";
	if(tmpSlashedSplitArr.length>0){
		for(var t=0;t<tmpSlashedSplitArr.length;t++){
			if((t==0)||((t-1>=0) && tmpSlashedSplitArr[t]!=targetStr && tmpSlashedSplitArr[t-1]!=targetStr)){
				tmpString +=tmpSlashedSplitArr[t]; 
				if(t!=(tmpSlashedSplitArr.length-1)){
					tmpString += "/";
				}
			}
					
		}
	}
	for(t=(tmpString.length-1);t>=0;t--){
		if(tmpString.charAt(t)=="/"){
			tmpString = tmpString.slice(0,t);
		}else{
			break;
		}
	}
	return tmpString;
}