/*******************************************
Version: d2
Created: 29/10/2008
Comments: eric@bright-interactive.co.uk
********************************************/

DOMAssistant.DOMReady(function() {
  makeTabsNonBreaking();
  resizeTabs();
  window.addEvent("resize", resizeTabs);
//  addClickEvents();
});

function makeTabsNonBreaking() {
  $("div#tabs ol li a").each(function() {
    var linkText = this.innerHTML;
    var nonBreakingText = linkText.replace(/\s/g, '&nbsp;');
	var nonBreakingText = nonBreakingText.replace('-', '&#8209;');
    this.innerHTML = nonBreakingText;
  });
  
  
}

function addClickEvents() {
  var selected_item = $('#selected a');
  var current_href = selected_item.first().getAttribute('href');

  hideAllContentSections(current_href);
    	
  $("div#tabs li a").addEvent("click", function(evt) {
  		imgEl = resetTabs();
  		var href = $(this).getAttribute('href');
  		DOMAssistant.preventDefault(evt);
    	hideAllContentSections(href);
    	listitem = $(this).parentNode;
    	$(listitem).id = 'selected';
    	if (imgEl != false) {
	    	$(listitem).addContent(imgEl);
	    	centreHighlightArrow($(imgEl));    	
	    }

  });
}
function resetTabs() {
	var imgEl = $('#selected img').first();
	$$('selected').id = '';
	if ($(imgEl)) {
		imgEl.remove();
		return imgEl;
	}
	else {
		return false;
	}
}



function hideAllContentSections(href) {


	parts = href.split('#');
    id = parts[1];
	$("div.content_section").each(function() {
		$(this).setStyle({
			'position': 'absolute', 
			'left': '-9999em',
			'height': '0',
			'display': 'none'
		});
	});
	$$(id).setStyle({
		'position': 'relative', 
		'left': '0',
		'height': 'auto',
		'display': 'block'
	});
}

function resizeTabs() {
  var linksWidth = 0; var linksCount = 0; var rowCount = 1;
  var tabsContainerWidth = ($$("tabs_area").offsetWidth) - 2;
  
  /*
  var toprowcount = 0;
  var topTabs = 4;
  var tabsLen = 0;
  $("div#tabs ol li.toprow a").each(function() {
	toprowcount += 1;
	var liNode = $(this).parentNode;
	tabsLen += $(liNode).offsetWidth;
	if (toprowcount == topTabs) {
		var licolor = $(this).getStyle('background-color');
		$(this).setStyle({background: licolor});;
		
		// IE seems to have problems rounding the widths of elements so will have to do it manually!!! Grrr
		if (navigator.userAgent.toLowerCase().indexOf("msie") != -1) {
			if (tabsLen < tabsContainerWidth) {
				var diff = tabsContainerWidth - tabsLen;
				$(liNode).setStyle({width: (($(liNode).offsetWidth) + diff) + 'px'});
			}
			if (tabsLen > tabsContainerWidth) {
				var diff = tabsLen - tabsContainerWidth;
				$(liNode).setStyle({width: (($(liNode).offsetWidth) - diff) + 'px'});
			}
		}
	}
	
	  
  });
  */
  var topTabCount = 0;
  var tabWidthCount = 0;
  $('div#tabs ol li.toprow').each(function () {
	 topTabCount++;
	 tabWidthCount += $(this).offsetWidth;
	 if (topTabCount == $('div#tabs ol li.toprow').length) {
		 var diff = tabsContainerWidth - tabWidthCount - 1;
		 $(this).setStyle({'margin-right': diff + 'px', 'display': 'inline'});
	 }
  });


  $("div#tabs ol li:not([class=toprow]) a").each(function() {
    this.className = "";
    $(this).setStyle({width: 'auto', padding: '6px 20px', 'border-right': 'none'});
    
    linksCount = linksCount + 1;
    linksWidth = linksWidth + this.offsetWidth;

    if (linksWidth > (tabsContainerWidth)) {
      linksWidth = 0;
      rowCount = rowCount + 1;
      linksWidth = this.offsetWidth;
    }

    $(this).addClass('row'+rowCount);
        
 });
 
 for (var rows=1; rows<=rowCount; rows++) {
 
    var totalWidth = 0;
    
    $('a.row'+rows).each(function() {
      totalWidth = totalWidth + this.offsetWidth;
    });
    
    var numLinks = $('a.row'+rows).length;
    var diff = ((tabsContainerWidth) - totalWidth) - numLinks;
    var amountToAdd = diff/numLinks;
    amountToAdd = Math.round(amountToAdd);
    var totalWidths = 0; var rowLinks = 1;
    
    $('a.row'+rows).each(function() {
    
      var newWidth = this.offsetWidth + amountToAdd;
      totalWidths = totalWidths + newWidth;
      if (rowLinks == numLinks) {
      
        thediff = tabsContainerWidth - totalWidths - numLinks;
	if (navigator.userAgent.toLowerCase().indexOf("msie") == -1) {
		thediff = thediff - 0.5; // for left border in non ie...
	}
        newWidth = newWidth + thediff;  
      }

      if (rows == rowCount) {
        $(this).removeClass('border'); 
      }
      
      if (newWidth > ($$('tabs_area').offsetWidth)-2) {
  		$(this).setStyle({width: 'auto', padding: '6px 20px', height: '1.625em'});
  	  } else {
	      $(this).setStyle({width: newWidth + 'px', padding: '6px 0', height: '1.625em'});
	  }
      rowLinks = rowLinks + 1;
      
    });
   
  }

  if ($$('current_tab')) {
	  // poss need to remove arrow and add it to content div
	  // pos absolute top, left: offset from left hand side
	  // only if on last row, so need to check if rowcount if last digit of class rowX is same as rowcount
	  // do a split on 'row' to get 2nd part
	
	  centreHighlightArrow($$('current_tab'), rowCount);
	
  }  
}

function centreHighlightArrow(currentTab, rCount) {
	if (navigator.userAgent.toLowerCase().indexOf("msie") != -1) {
		var splits = $(currentTab).parentNode.firstChild.className.split('row');
		var offB = '-4px';
		if (splits[1] == rCount) {
			offB = '-5px';
		}
		var selectedImage = $(currentTab);
		var link = selectedImage.parentNode;
		var positionLeft = (Math.round(link.offsetWidth/2))-5;
		selectedImage.setStyle({'left': positionLeft+'px', 'bottom': offB});
	}
	else
	{
		var imgEl = document.createElement('img');
		imgEl.setAttribute('src', $(currentTab).src);
		imgEl.setAttribute('alt', 'testy');
		imgEl.setAttribute('height', '4');
		imgEl.id = $(currentTab).id;
		imgEl.className = 'arrow';
		$('img.arrow').each(function () {
			$(this).remove();
		});
		//$(currentTab).parentNode.removeChild($(currentTab));
		
		var currTabLI = $$('selected');
		var currTabLink = currTabLI.firstChild
		var splitClass = currTabLink.className.split('row');
	
		if (splitClass[1] == rCount) {
			var leftOff = findPos(currTabLI);
			leftOff = leftOff + currTabLI.offsetWidth/2;
			var outerLeftOff = findPos($$('center_section'));
			$$('inner_tab_content').appendChild(imgEl);
			$$('current_tab').setStyle({'position': 'absolute', 'top': '0', 'left': (leftOff - outerLeftOff)+'px', 'height': '4px'});
		}
		else {
			currTabLI.appendChild(imgEl);
			var positionLeft = (Math.round(currTabLI.offsetWidth/2))-5;
			$(currentTab).setStyle({'left': positionLeft+'px', 'height': '4px'});
		}
	}
}

function findPos(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
		} while (obj = obj.offsetParent);
		
		return curleft;
	}
}


// only use to debug errors
DOMAssistant.DOMLoad.setErrorHandling(function (e) {
  alert(e)
}); 
