var blocks					= new Array();
var currentBlock 			= new Array();
var blockItems				= new Array(); 
var blockExtendedItems		= new Array();
var currentExtendedPageObj	= new Array(); 
var currentTab				= new Array();
var currentPageObj			= new Array();
var tabs					= new Array();


function initBlock(name){
	blocks.push(name);
	var id = getId(name);
	
	
	currentBlock[id] = 0;
	var obj = document.getElementById(name);
	if (obj == null) {
		return;
	}
	var thisBlocks = null;
	var subtext = null;
	var pagination = null;
	var hasHeader = false;
	
	// get all html-elements
	var el = "";
	
	var elements = new Array();
	if (obj.childNodes.length == 0) {
	return;
	}
	for(var n = 0; n < obj.childNodes.length; n++){
		if(obj.childNodes[n].nodeType == 1){
			//alert(obj.childNodes[n].className);
			if(obj.childNodes[n].className == "header")
			{
				hasHeader = true;
			}
			if(obj.childNodes[n].className == "blocks")
			{
				thisBlocks = obj.childNodes[n];
			}
			if(obj.childNodes[n].className == "subtext")
			{
				subtext = obj.childNodes[n];
			}
			if((obj.childNodes[n].className == "pagination") || (obj.childNodes[n].className == "pagination noheader"))
			{
				pagination = obj.childNodes[n];
			}
		}
	}
	
	// set blocks
	var cid= 0;
	if (thisBlocks == null) {
		return;
	}
	
	if (thisBlocks != null) {
		for(var i = 0; i < thisBlocks.childNodes.length; i++)
		{
			if(thisBlocks.childNodes[i].nodeType == 1){
				thisBlocks.childNodes[i].id = "b"+id+"c"+cid;
				thisBlocks.childNodes[i].className = "hide";
				cid++;
			}
		}
	}
	
	if (document.getElementById("b"+id+"c0") == null){
		return;
	}
	
	blockItems[id] = cid; 
	document.getElementById("b"+id+"c0").className = "show";
	
	
	// set pagination
	if(pagination != null)
	{
		if(blockItems[id] > 1){
			pagination.style.display = "block";
			if(subtext != null){
				subtext.className = "subtext";
			}
		}
		else{
			if(subtext != null){
				subtext.className = "subtext subtext_nopadding";
			}
		}
		
		if(hasHeader == false){
			pagination.className += " noheader";
			if(subtext != null){
				subtext.className += " noheader";
			}
		}
		
		
		var links = pagination.getElementsByTagName("a");
		for(var r=0; r < links.length; r++){
			if(links[r].className == 'previous'){
				links[r].href = 'javascript: changeBlock('+id+',-1);';
			}
			if(links[r].className == 'next'){
				links[r].href = 'javascript: changeBlock('+id+',1);';
			}
		}
		
		var spans = pagination.getElementsByTagName("span");
		currentPageObj[id] = spans[1];
		spans[3].innerHTML = blockItems[id];
	}
	
	
	
}




function changeBlock(id,page){

	var name = blocks[id];

	
	document.getElementById(name).blur();
	
	if(blockItems[id] > 1){
		current = currentBlock[id];
		next = current + page;
		
		if(next >= 0 && next < blockItems[id]){
			currentPageObj[id].innerHTML = next+1;
			
			document.getElementById("b"+id+"c"+current).className = "hide";
			document.getElementById("b"+id+"c"+next).className = "show";
			
			currentBlock[id] = next;
		}
	}
}

/* --------------------------------------------------------------------------------------------------- 
	Extended Blocks	
*/
var blocktabs 	= new Array();	// holds the references to all tabs within a block
var tabitems	= new Array();	// holds the amount of items within the tabs of a block
var paginations = new Array(); 	// holds the references to the pagination within a block
var currtab		= new Array();	// holds the id of the current tab within a block
var currpage	= new Array();	// holds the pagenumber of the current tab within a block

function initExtendedBlock(name){
	blocks.push(name);
	var id = getId(name);


	var _block 		= document.getElementById(name);
	var _tabs		= new Array();
	var _tabtitles	= new Array();
	
	_block.className = "extendedblock";
	
	tabitems[id] = new Array();
	
	for(var n=0; n<_block.childNodes.length; n++){
		if(_block.childNodes[n].nodeType == 1){
			// register tabs
			if(_block.childNodes[n].className == "tab"){
				_tabs.push(_block.childNodes[n]);
				_tabtitles.push(_block.childNodes[n].title);
				
				_block.childNodes[n].id = "b"+id+"_t"+(_tabs.length-1);
				_block.childNodes[n].className = "hide";
				// set the id's of the contentblocks
				setContentIds(_block.childNodes[n],_tabs.length-1,id);
			}
			// register pagination
			if(_block.childNodes[n].className == "pagination"){
				paginations[id] = _block.childNodes[n];
				_block.childNodes[n].id = "pag"+id;
				
				var links = _block.childNodes[n].getElementsByTagName("a");
				for(var r=0; r < links.length; r++){
					if(links[r].className == 'previous'){
						links[r].href = 'javascript: changeExtendedBlock('+id+',-1);';
					}
					if(links[r].className == 'next'){
						links[r].href = 'javascript: changeExtendedBlock('+id+',1);';
					}
				}
			}
			if(_block.childNodes[n].className == "subtext"){
				_block.childNodes[n].id= "subtext"+id;
			}
		}
	}
	blocktabs[id] = _tabs;
	displayTabs(_tabtitles,id);
	setTab(id,0);
}
function changeExtendedBlock(id,nav){
	var _total 	= tabitems[id][currtab[id]];
	var _curr	= currpage[id];
	var _new	= _curr + nav;
	var _pag	= document.getElementById("pag"+id);
	
	_pag.blur();
	if(_new != 0 && _new <= _total){
	
		// set pagination
		var spans = _pag.getElementsByTagName("span");
		spans[1].innerHTML = _new;
		
		// change contentblocks
		hideContentBlock(id,currtab[id],currpage[id]-1);
		showContentBlock(id,currtab[id],_new-1);
		
		// set newpage
		currpage[id] = _new;
	}
}
function setTab(id,item){
	var li = document.getElementById("tabs"+id).getElementsByTagName("li");
	for(l=0; l<li.length; l++){
		// reset tabtitles
		li[l].getElementsByTagName("a")[0].className = "";
	}
	// set new tabtitle
	li[item].blur();
	li[item].getElementsByTagName("a")[0].className = "active";
	
	// hide al contentblocks in new tab
	var cb = document.getElementById("b"+id+"_t"+item).childNodes;
	for(var n=0; n < cb.length; n++){
		if(cb[n].nodeType == 1 && cb[n].className == "blocks")
		{
			thisBlock = cb[n];
		}
	}
	thisBlock = thisBlock.childNodes;
	
	for(var c = 0; c < thisBlock.length; c++){
		if(thisBlock[c].nodeType == 1){
			thisBlock[c].className = "hide";
		}
	}
	// hide old tab
	if(currtab[id] != undefined){
		document.getElementById("b"+id+"_t"+currtab[id]).className = "hide";
	}
	// show new tab
	document.getElementById("b"+id+"_t"+item).className = "show";
	currtab[id] = item;
	
	// change pagination
	setPagination(id,item);
	
	// display contentblock
	showContentBlock(id,item,0);
	
}
function showContentBlock(id,item,content){
	document.getElementById("b"+id+"_t"+item+"_c"+content).className = "show";
}
function hideContentBlock(id,item,content){
	document.getElementById("b"+id+"_t"+item+"_c"+content).className = "hide";
}


function displayTabs(titles,id){
	var _tab_begin 		= '<div class="tabs"><ul id="tabs'+id+'">';
	var _tab_end		= '</ul></div>';
	var _tab_content	= '';
	
	for(var n=0; n<titles.length; n++){
		if(n==0){
			_tab_content += '<li class="tab1"><a href="javascript: setTab('+id+','+n+')"><span>'+titles[n]+'</span></a></li>';
		}
		else{
			_tab_content += '<li><a href="javascript: setTab('+id+','+n+')"><span>'+titles[n]+'</span></a></li>';
		}
	}
	document.getElementById(blocks[id]).innerHTML += _tab_begin + _tab_content + _tab_end ;
}
function setContentIds(inner,tab,id){
	
	for(var n=0; n<inner.childNodes.length; n++){
		if(inner.childNodes[n].nodeType == 1 && inner.childNodes[n].className == "blocks")
		{
			thisBlock = inner.childNodes[n];
		}
	}
	
	
	var nr = 0;

	for(var n=0; n<thisBlock.childNodes.length; n++){
		if(thisBlock.childNodes[n].nodeType == 1 && thisBlock.childNodes[n].className == "contentblock"){
			thisBlock.childNodes[n].id = "b"+id+"_t"+tab+ "_c"+nr;
			nr++;
		}
	}
	tabitems[id][tab] = nr;
}
function setPagination(id,tab){
	// set pagination
	var total = tabitems[id][tab];
	if(total > 1){
		document.getElementById("pag"+id).style.display = "block";
		
		var spans = document.getElementById("pag"+id).getElementsByTagName("span");
		spans[1].innerHTML = "1";
		spans[3].innerHTML = total;
		currpage[id] = 1;
		
		// set subtext
		if(document.getElementById("subtext"+id)){
			document.getElementById("subtext"+id).className = "subtext";
		}
	}
	else{
		document.getElementById("pag"+id).style.display = "none";
		
		// set subtext
		if(document.getElementById("subtext"+id)){
			document.getElementById("subtext"+id).className = "subtext subtext_nopadding";
		}
	}
}
function getId(obj){
	for( var i=0; i<blocks.length; i++){
		if(blocks[i]==obj){
			return i;
		}
	}
}


/* --------------------------------------------------------------------------------------------------- 
	SetVisual	
*/
function setVisual(obj,url)
{	
	document.getElementById(obj).style.backgroundImage = "url("+url+")";
}

/* --------------------------------------------------------------------------------------------------- 
	SetOffices	
*/
var officeInfo = new Array();
var officeInfoHeight = new Array();
var currentRow = null;
var currentOffice = null;

function setOffices(obj)
{
	obj = document.getElementById(obj);
	
	var _rows = 0;
		
	// get Rows
	
	for(var i=0; i< obj.childNodes.length; i++)
	{
		if(obj.childNodes[i].nodeType == 1){		
			var row 	= obj.childNodes[i];
			var item 	= 0;
			
			row.id = "row"+_rows;		
			
			officeInfo[_rows] = new Array();
			officeInfoHeight[_rows] = new Array();
		
			for(var n=0; n< row.childNodes.length; n++)
			{
				if(row.childNodes[n].nodeType == 1){	
					var block = row.childNodes[n];
					
					
					for(var r=0; r< block.childNodes.length; r++)
					{
						if(block.childNodes[r].nodeType == 1){		

							if(block.childNodes[r].tagName == "H4")
							{
								var thisid = "h"+_rows+"_"+item;
								block.childNodes[r].id = thisid;
								
								var anchors = block.childNodes[r].getElementsByTagName("a");
								anchors[0].href = "javascript: showInfo("+_rows+","+item+")";								
							}
						
							if(block.childNodes[r].className == "info")
							{
								if(document.all){
									officeInfoHeight[_rows][item] = block.childNodes[r].clientHeight;
								}
								else{
									officeInfoHeight[_rows][item] = block.childNodes[r].clientHeight -10;
								}
								officeInfo[_rows][item] = block.childNodes[r].innerHTML;
								block.removeChild(block.childNodes[r]);
							}
						}
					}
					item++;
				}
			}
			
			
			row.innerHTML += "<div class='adressblock' id='adressblock"+_rows+"'></div>";
			
			_rows++;
		}
	}
}

function showInfo(row, item)
{
	var obj = "h"+row+"_"+item;

	if(currentRow != null && currentRow != row)
	{
		hideInfo(currentRow);
	}
	
	if(currentOffice != null && currentOffice != obj)
	{
		blurOffice(currentOffice);
	}
	
	document.getElementById("row"+row).blur();
	document.getElementById("adressblock"+row).innerHTML = "<div class='close' onClick='hideInfo("+row+")'><span>X</span></div>" + officeInfo[row][item];
	document.getElementById("adressblock"+row).style.display = "block";
	
	$("#adressblock"+row).animate({height: officeInfoHeight[row][item]+"px", marginBottom: "10px"}, 500);

    document.getElementById(obj).style.background = "url('/static/getronicsglobalpresentation/images/address_selected.gif') bottom center no-repeat";
	document.getElementById(obj).className = "active";

	
	
	currentRow = row;
	currentOffice = obj;
}
function hideInfo(row)
{
	$("#adressblock"+row).animate({height: "0px", marginBottom: "0px"}, 500);

	if(currentOffice != null){
		document.getElementById(currentOffice).style.background = "none";
		document.getElementById(currentOffice).className = "";
		currentOffice = null;
	}
	
}
function blurOffice(obj)
{
	document.getElementById(obj).style.background = "none";
	document.getElementById(obj).className = "";
}

