/* global javascripts */

// use $ as shortcut to getElementByID, ex: $('id_name')
var $ = YAHOO.util.Dom.get;

// Simon Willison's function for adding onload events
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

// add global onload event
addLoadEvent(setExtLinks);


// function to open external links in new window
function openInNewWin() {
	var newWindow = window.open(this.getAttribute('href'), 'newWindow');
	newWindow.focus();
	return false;
}

// function to open enlarged images
function openPopWin() {
	var params = 'height=480,width=760,directories=no,location=no,menubar=no,resizable=yes,scrollbars=yes,toolbar=no,screenX=5,screenY=5,left=5,top=5';
	var newWindow = window.open(this.getAttribute('href'), 'popWindow', params);
	newWindow.focus();
	return false;
}

// function to set external links opening option
function setExtLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "ext"){
			newImg = document.createElement('img');
			newImg.setAttribute('src','/images/arrow_ext.gif');
			newImg.setAttribute('alt',' [external link]');
			newImg.setAttribute('title','external link');
			newImg.setAttribute('height','10');
			newImg.setAttribute('width','14');
			anchor.appendChild(newImg);
			anchor.onclick = openInNewWin;
       		} else if (anchor.getAttribute("href") && YAHOO.util.Dom.hasClass(anchor,'dload')){
			newImg = document.createElement('img');
			newImg.setAttribute('src','/images/icon_download.gif');
			newImg.setAttribute('alt',' [download link]');
			newImg.setAttribute('title','click to download');
			newImg.setAttribute('height','11');
			newImg.setAttribute('width','13');
			newImg.setAttribute('class','dloadImg');
			newImg.setAttribute('className','dloadImg');
			anchor.appendChild(newImg);
            // do not lauch new windows for Offer pages
			/* anchor.onclick = openInNewWin; */
            } else if (anchor.getAttribute("href") && YAHOO.util.Dom.hasClass(anchor,'dload-nwin')){
			newImg = document.createElement('img');
			newImg.setAttribute('src','/images/icon_download.gif');
			newImg.setAttribute('alt',' [download link]');
			newImg.setAttribute('title','click to download');
			newImg.setAttribute('height','11');
			newImg.setAttribute('width','13');
			newImg.setAttribute('class','dloadImg');
			newImg.setAttribute('className','dloadImg');
			anchor.appendChild(newImg);
            anchor.onclick = openInNewWin;
			} else if (anchor.getAttribute("href") && YAHOO.util.Dom.hasClass(anchor,'enlarge')){
			if (anchor.getAttribute("rel") == "link"){
				newImg = document.createElement('img');
				newImg.setAttribute('src','/images/icon_enlarge.gif');
				newImg.setAttribute('alt',' [enlarge]');
				newImg.setAttribute('title','click to enlarge');
				newImg.setAttribute('height','12');
				newImg.setAttribute('width','14');
				newImg.setAttribute('class','enlargeImg');
				newImg.setAttribute('className','enlargeImg');
				anchor.appendChild(newImg);
			}
			anchor.onclick = openPopWin;
		}
	}
}

// function to set management links opening option
function setMoreLinks() {
	if (!document.getElementsByTagName) return;
	// set up onclick handlers
	var morePs = YAHOO.util.Dom.getElementsByClassName('mlink', 'p', $('mainContent'));
	for (i=0; i < morePs.length; i++){
		if (morePs[i].childNodes[0].tagName == 'A'){
			YAHOO.util.Dom.addClass(morePs[i].childNodes[0],"up");
			morePs[i].childNodes[0].onclick = function (){
				return showHideMoreText(this);
			}
		}
	}
	// close all more text divs
	var moreTextDivs = YAHOO.util.Dom.getElementsByClassName('mtext', 'div', $('mainContent'));
	for (j=0; j < moreTextDivs.length; j++){
		YAHOO.util.Dom.addClass(moreTextDivs[j],"hide");
	}
}

function showHideMoreText(pObj){
	node = pObj;
	// find parent div
	while (node.parentNode){
		node = node.parentNode;
		if (YAHOO.util.Dom.hasClass(node, moreParentClass)){
			break;
		}
	}
	// find reference to mtext div
	moreDiv = YAHOO.util.Dom.getElementsByClassName('mtext', 'div', node)[0];
	if (YAHOO.util.Dom.hasClass(moreDiv, "hide")){
		YAHOO.util.Dom.replaceClass(moreDiv, 'hide', 'show');
		YAHOO.util.Dom.replaceClass(pObj, 'up', 'dn');
	} else {
		YAHOO.util.Dom.replaceClass(moreDiv, 'show', 'hide');
		YAHOO.util.Dom.replaceClass(pObj, 'dn', 'up');
	}
	return false;
}
