/*
 * * * * * * * * * * * * * * * * * * * * * * * * * * * COMMON
 */

var getBase = function () {
	bases = document.getElementsByTagName('base');
	if (bases[0] && bases[0].href) {
		base = bases[0].href;
	}
	return base;
}

/*
 * * * * * * * * * * * * * * * * * * * * * * * * * * * PROFILE TABS
 */

var hashProfileTabs = location.hash;

profileTabsInit = function (e) {
	domProfileTabs = $('profile-tabs')
	if (!domProfileTabs) {
		return;
	}
	$$('a.tab-anchor').each(
			function (a) {
				var divContent = a.up('div');
				divContent.insertBefore(a,divContent.childNodes[0]);
				
				/*a.style.position='fixed';a.style.top='0';a.style.left='0';*/
			}
		);
	
	$$('#profile-tabs li a').each(function (a) {a.observe('click',profileTabsClick);});
	$$('div.tab').each(function(a) { a.hide(); });
	
	if(document.location.hash) {
		done = profileTabsShow(document.location.hash.substring(1));
		if (!done) {
			profileTabsShow('tab1');
		}
	} else {
		profileTabsShow('tab1');
	}
	
	new PeriodicalExecuter(function() {
		if (location.hash != hashProfileTabs) {
			profileTabsShow(location.hash.substring(1));
			hashProfileTabs = location.hash;
		}
	}, 0.1);
	
	$$('span.reference').each(function(a){a.hide();});
	$$('ul.references').each(function(a){a.hide();});
	
	$$('a.reflink').each(function(a) {
			a.observe('click',
				function(ev) {
					ev.stop();
					elRef = ev.findElement('sup').next('span');
					elRef.style.left='auto';
					elRef.toggle();
				}
			);
		}
	);
	
	scroll(0,0);
}
	
profileTabsClick = function (e) {
	domAnchor = Event.findElement(e,'A');
	if(!domAnchor) {
		return;
	}
	
	newActiveTabID = domAnchor.href.substring(domAnchor.href.indexOf('#')+1);
	profileTabsShow(newActiveTabID);
	return true;
}

profileTabsShow = function (tabID) {
	if (!tabID) {
		tabID='tab1';
	}
	newActiveTab = $(tabID+'-content');
	if (!newActiveTab) {
		return false;
	}
	$$('div.tab').each(function (a) { a.hide(); });
	newActiveTab.show();
	
	$$('#profile-tabs li').each(function (aListItem) {aListItem.className='';});
	$(tabID+'-link').parentNode.className='active';
	
	supplierPage = $('supply');
	if (supplierPage) {
		anchorMark = supplierPage.action.indexOf('?');
		if (anchorMark==-1) {
			anchorMark = supplierPage.action.length;
		}
		supplierPage.action=supplierPage.action.substring(0,anchorMark)+'?tab='+tabID;
	}
	
	profileTabsAddHistory(tabID);
	return true;
}

profileTabsAddHistory = function (location) {
	if(document.location.hash) {
		document.location.hash.replace('#'+location);
	}
}

document.observe('dom:loaded',profileTabsInit);

/*
 * * * * * * * * * * * * * * * * * * * * * * * * * * * A-Z
 */

var hashAz = location.hash;

azInit = function (e) {
	oPickers = $$('ul.alphabet-picker li a');
	if (!oPickers || oPickers.length==0) {
		return;
	}
	
	oPickers.each(function (a) {a.observe('click',azPickerClick);});
	azHide();
	$$('a.az-anchor').each(function (a) {a.style.position='fixed';a.style.top='0';a.style.left='0';});

	if(document.location.hash) {
		done = azShow(document.location.hash.substring(1));
		if (!done) {
			azShow('ag');
		}
	} else {
		azShow('ag');
	}
	
	setInterval(function() {
		if (location.hash != hashAz) {
			if (location.hash.substring(1)) {
				azShow(location.hash.substring(1));
			} else {
				azShow('ag');
			}
			hashAz = location.hash;
		}
	}, 100);
}

azHide = function () {
	$$('div.alphabet-separator').each(function (a) {a.hide();});
}

azShow = function (target) {
	azHide();
	$$('div.alphabet-'+target).each(function(a) { a.show();});
	$$('ul.alphabet-picker li a').each(function (a) {if (a.href.substring(a.href.indexOf('#')+1)==target){Element.addClassName(a,'active');} else {Element.removeClassName(a,'active');}});
	return true;
}

azPickerClick = function (e) {
	linkPicker = Event.element(e);
	target = linkPicker.href.substring(linkPicker.href.indexOf('#')+1);
	azShow(target);
}

document.observe('dom:loaded',azInit);

/*
 * * * * * * * * * * * * * * * * * * * * * * * * * * * RESEARCH ALT
 */

researchAltInit = function (e) {
	imgs = $$('#news div.message img,#news-item div.message img');
	for (i=0,j=imgs.length;i<j;i++) {
		var holder = imgs[i].parentNode;
		var sibling = imgs[i].nextSibling;
		
		imgs[i].style.width='';
		imgs[i].style.height='';
		
		iWrapper = document.createElement('div');
		iWrapper.setAttribute('style',imgs[i].getAttribute('style'));
		iWrapper.appendChild(imgs[i]);
		iWrapper.className = 'photo';
		
		if (typeof imgs[i].style.cssFloat!='undefined') {
			iWrapper.style.cssFloat = imgs[i].style.cssFloat;
			imgs[i].style.cssFloat='none';
		} else if (typeof imgs[i].style.styleFloat!='undefined') {
			iWrapper.style.styleFloat = imgs[i].style.styleFloat;
			imgs[i].style.styleFloat = 'none';
		}
		
		iLegend = document.createElement('span');
		iLegend.className = 'legend';
		iLegendText = document.createTextNode(imgs[i].getAttribute('alt'));
		iLegend.appendChild(iLegendText);
		
		iWrapper.appendChild(iLegend);
		
		if (sibling) {
			holder.insertBefore(iWrapper,sibling);
		} else {
			holder.appendChild(iWrapper);
		}
		
	}
}

document.observe('dom:loaded',researchAltInit);

/*
 * * * * * * * * * * * * * * * * * * * * * * * * * * * EXTERNAL LINKS
 */

externalLinksInit = function (e) {
	base = getBase();

	areas = document.getElementsByTagName('a');	
	for (i=0,j=areas.length;i<j;i++) {
		dest = areas[i].getAttribute('href');
		if (dest) {
			// new window for external websites
			if (dest.substring(0,7)=='http://' && dest.substring(0,base.length)!=base) {
				Event.stopObserving(areas[i]);
				Event.observe(areas[i],'click',externalLinksClick);
			}
		}
	}
}

externalLinksClick = function (e) {
	Event.stop(e);
	
	anchor = Event.findElement(e,'A');
	oWin = window.open(anchor.getAttribute('href'),'_blank');
	if (oWin && oWin.focus) oWin.focus();
};

Event.observe(window, 'load',externalLinksInit);

/*
 * * * * * * * * * * * * * * * * * * * * * * * * * * * FLEX PAPER
 */

flexPaperInit = function (e) {
	oFlexPaper = $('paper');
	
	if (!oFlexPaper) {
		return;
	}
	
	var swfVersionStr = "10.0.0";
	var xiSwfUrlStr = "playerProductInstall.swf";

	var flashvars = { 
	      SwfFile : oFlexPaper.getAttribute('src'),
		  Scale : 1.0, 
		  ZoomTransition : "easeOut",
		  ZoomTime : 0.5,
		  ZoomInterval : 0.1,
		  FitPageOnLoad : false,
		  FitWidthOnLoad : true,
		  PrintEnabled : true,
		  FullScreenAsMaxWindow : false,
		  ProgressiveLoading : true,
		  
		  PrintToolsVisible : true,
		  ViewModeToolsVisible : true,
		  ZoomToolsVisible : true,
		  FullScreenVisible : true,
		  NavToolsVisible : true,
		  CursorToolsVisible : true,
		  SearchToolsVisible : true,
		  
		  localeChain: "en_US"
	};
		  
	 var params = {
		
	    }
	params.quality = "high";
	params.bgcolor = "#ffffff";
	params.allowscriptaccess = "sameDomain";
	params.allowfullscreen = "true";
	var attributes = {};
	attributes.id = "FlexPaperViewer";
	attributes.name = "FlexPaperViewer";
	swfobject.embedSWF(
	    "js/FlexPaperViewer.swf", "paper", 
	    "660", "500",
	    swfVersionStr, xiSwfUrlStr, 
	    flashvars, params, attributes);
}

document.observe('dom:loaded',flexPaperInit);
