/************
 * Author:    	Jim Harvey
 * File name: 	TabControl.js
 * 
 * Purpose:		Javascript for the product page
 ***********/

var tabArray = new Array();

/*********
 * Function name: addTab
 * Inputs:	id - the id of the div tag that contains the tab contents
 *			selectedImageName - the image that is to be displayed when the tab is selected
 *			inactiveImageName - the image that is to be displayed when the tab is not selected
 * Outputs: None
 **********/
function addTab(id, text)
{
	var tab = new Object();
	tab.id 					= id;
	tab.text				= text;
	tab.currentClass		= 'ti_inactive';
	
	tabArray.push(tab);
}


/*********
 * Function name: selectTab
 * Inputs:	tabId - the id of the selected tab
 *			tabHolder - the id of the div that holds the selected tab
 *
 * Outputs: None
 **********/
function selectTab(tabId, tabHolder)
{ 
	for(var x=0; x<tabArray.length; x++)
	{
		if(tabId == tabArray[x].id)
		{
			tabArray[x].currentClass = 'ti_active';
			document.getElementById(tabArray[x].id).style.display ="block";
		} else
		{
			tabArray[x].currentClass = 'ti_inactive';
			document.getElementById(tabArray[x].id).style.display = "none";
		}
	}
	
	document.getElementById(tabHolder).innerHTML = writeTabs(tabHolder);
}

/*********
 * Function name: writeTabs
 * Inputs:	tabDiv - the id of the div that holds the tabs
 *
 * Outputs: html for the tabs
 **********/
function writeTabs(tabDiv)
{
	var html = "";
	for(var x=0; x<tabArray.length; x++)
	{
		html = html + '<a href="#tab' + x + '" class="' + tabArray[x].currentClass + '" onclick="selectTab(\'' + tabArray[x].id +  '\', \'' + tabDiv + '\'); return false;" >';
		html = html + '<span>' + tabArray[x].text+ '</span>';
		html = html + '</a>';
	}
	return html;
}


/*********
 * Function name: promoteImage
 * Inputs:	largeId - the id of the large product image
 *			newSrc - the source of the new large product image
 *
 * Outputs: none
 **********/
function promoteImage(thumbElement, largeId, newSrc)
{
	document.getElementById(largeId).src = newSrc;
	var x = document.getElementsByTagName('img');
    //alert(x.length);
    for (var i=0;i<x.length;i++) {
      if (x[i].getAttribute('rel') == 'thumbnail') {
        x[i].className="thumbnail_inactive pointer";
      }
    }
    thumbElement.className="thumbnail_active pointer";
}

/*********
 * Function name: convertToLarge
 * Inputs:	imgSrc - the source of the small product image
 *
 * Outputs: the src of the large product image
 **********/
function convertToLarge(imgSrc)
{
	var sz = /_S\./;
	var dir = /_small/;
	imgSrc = imgSrc.replace(sz, "_L.");
	return imgSrc.replace(dir, "_large");
}

/*********
 * Function name: preloadImages
 * Inputs:	args - the source of the large product images to preload
 *
 * Outputs: none
 **********/
function preloadImages() 
{
  var d=document; 
  
  if(d.images)
  { 
  	if(!d.MM_p) 
  		d.MM_p=new Array();
    
    var i,j=d.MM_p.length,a=arguments; 
    	
	for(i=0; i<a.length; i++)
		if (a[i].indexOf("#")!=0)
		{ 
			d.MM_p[j]=new Image; 
			d.MM_p[j++].src=a[i];
		}
  }
}

/*********
 * Function name: setVisible
 * Inputs:	obj - the object to set visible on
 *
 * Outputs: none
 **********/
function setVisible(obj)
{
	obj = document.getElementById(obj);
	obj.style.visibility = (obj.style.visibility == 'visible') ? 'hidden' : 'visible';
	obj.style.display    = (obj.style.display    == 'block')   ? 'none'   : 'block';
	
	return false;
}

function hideDiv(obj)
{
	obj = document.getElementById(obj);
	obj.style.visibility = 'hidden';
	obj.style.display    = 'none';
}


function showDiv(obj)
{
	obj = document.getElementById(obj);
	obj.style.visibility = 'visible';
	obj.style.display    = 'block';
}

/*********
 * Function name: positionDiv
 * Inputs:	wrapperId - the id of the object who's location is used to position the div
 *			divId - the id of the div to be positioned
 *
 * Outputs: none
 **********/
function positionDiv(wrapperId, divId)
{
	
	var divObj = document.getElementById(divId);
	var wrapperObj = document.getElementById(wrapperId);
	
	divObj.style.top = findPosY(wrapperObj) + 'px';
	divObj.style.left = findPosX(wrapperObj) + 'px';
	
	return false;
}

/*********
 * Function name: findPosX
 * Inputs:	obj - the object who's horizontal position is to be found
 *
 * Outputs: horizontal position
 **********/
function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

/*********
 * Function name: findPosY
 * Inputs:	obj - the object who's verticle position is to be found
 *
 * Outputs: verticle position
 **********/
  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }
  
/*********
 * Function name: setIframeSource
 * Inputs:	iframeId - the id of the iframe who's source is to be set
 *			source - the source of the iframe
 *
 * Outputs: false
 **********/
  function setIframeSource(iframeId, source)
  {
  	document.getElementById(iframeId).src = source;
  	
  	return false;
  }

/*********
 * Function name: hideSelectElements, showSelectElements
 * Inputs:	none
 *
 * Outputs: none
 **********/
  function hideSelectElements()
  {
	//<!--[if lte IE 6]>
	//<![endif]-->
	
	var x = document.getElementsByTagName('select');
	//alert(x.length);
	for (var i=0;i<x.length;i++)
	{
			x[i].style.visibility = 'hidden';
	}
  }

  function showSelectElements()
  {
	var x = document.getElementsByTagName('select');
	for (var i=0;i<x.length;i++)
	{
			x[i].style.visibility = 'visible';
	}
  }


/*********
 * Function name: toggleDetailsLink
 * Inputs:	
 *			
 *
 * Outputs: 
 **********/
  function toggleDetailsLink(obj, linktxt1, linktxt2)
  {
     (obj.innerHTML == linktxt1) ? obj.innerHTML = linktxt2 : obj.innerHTML = linktxt1;
     setVisible('bulleted_product_copy');
     return false;
  }