var AD_REFRESH_TIME_MS = 420000;

cio={

	navRollover:function() {
		var center = this.id;
		var centerDiv = document.getElementById(center);
		
		if (!centerDiv)
      return;

		var attr = centerDiv.getAttribute("cio:globalTab-selected");
    if (attr && attr == "true")
      return;
    
		var left = center + "left";
		var leftDiv = document.getElementById(left);
		var right = center + "right";
		var rightDiv = document.getElementById(right);

    leftDiv.className = (leftDiv.className == 'solutionLeftImg') ? 'solutionLeftImgOn' : 'solutionLeftImg';
		centerDiv.className = (centerDiv.className == 'solutionCenter') ? 'solutionCenterImg' : 'solutionCenter';

    var rClassName = rightDiv.className;
    if (rClassName == "solutionRightEmpty" || rClassName == "solutionRightEmptyImgOn")
    {
      rightDiv.className = (rightDiv.className == 'solutionRightEmpty') ? 'solutionRightEmptyImgOn' : 'solutionRightEmpty';
    }
    else if (rClassName == "solutionRightImg" || rClassName == "solutionRightImgOn")
    {
      rightDiv.className = (rightDiv.className == 'solutionRightImg')   ? 'solutionRightImgOn' : 'solutionRightImg';
    }
	},


	// this script aligns bottoms of 2 right content divs on Detail pages
	// doing it programatically since IE doesn't support height:100%
	alignDivBottoms:function() {
		if(!document.getElementById("detailBody")) { return };
		var rtDiv = document.getElementById("rightColumnContent");
		var rtDivHt = (rtDiv.offsetHeight-15);  // subtract 15px margin
		
		var wMod = document.getElementById("detailModule");
		var wModHt = wMod.offsetHeight;
		
		var wBod = document.getElementById("detailBody");
		var wBodHt = wBod.offsetHeight;
		var offset = wModHt-wBodHt;

		if(rtDivHt > wBodHt) {
			wBod.style.height = (rtDivHt-offset) + "px";
		}

	},
	
	navAttach: function() {
		if(!document.getElementById("solutions")) { return };
		var solDiv = document.getElementById("solutions");
		var child = solDiv.childNodes;
			for(y=0; y<child.length; y++) {
				if(child[y].nodeName == 'DIV') var solutionDiv = child[y];
			}
		if(solutionDiv.childNodes) {
			for(x=0; x<solutionDiv.childNodes.length; x++) {
				if(solutionDiv.childNodes[x].nodeName == 'DIV') {
					var solution = solutionDiv.childNodes[x];
					if(solution.className.indexOf("Center") > -1) {
						solution.onmouseover = cio.navRollover;
						solution.onmouseout = cio.navRollover;
					}
				}
			}			
		}
	},

  loadTabs: function() {
    var tabGroups = {};
    document.getElementsByAttribute("cio:tab", false, 'div').each( function(el) { 
      var attrValue = el.getAttribute("cio:tab");
      if (tabGroups[attrValue])
        tabGroups[attrValue].push(el.id);
      else
      {
        tabGroups[attrValue] = [el.id];
        tabGroups[attrValue].selectedClassPrefix = el.getAttribute("cio:tab-selectedClassPrefix");
      }
    });

    $H(tabGroups).values().each( function(tabGroup) {
      var tabHandler = new TabHandler(tabGroup, {}, [new TabCurvedEdgeObserver({selectedClassPrefix: tabGroup.selectedClassPrefix})]);
    });
  },

	addEvent: function(elm, evType, fn, useCapture){
		if (elm.addEventListener){
			elm.addEventListener(evType, fn, useCapture);
			return true;
		} else if (elm.attachEvent) {
			var r = elm.attachEvent('on' + evType, fn);
			return r;
		} else {
			elm['on' + evType] = fn;
		}
	}
	

}

cio.addEvent(window, 'load', cio.navAttach, false);
cio.addEvent(window, 'load', cio.alignDivBottoms, false);
cio.addEvent(window, 'load', cio.loadTabs, false);



document.getElementsByAttribute = function(attribute, parentElement, tagName) {
	//alert("attributes:" + attribute + " tagname: " + tagName + "parentElement: " + parentElement);
	var children;
	if (parentElement) {
		children = ($(parentElement)).getElementsByTagName(tagName || '*');
	} else {
	 	document.body.getElementsByTagName(tagName || '*');
	}
	
  	return $A(children).inject([], function(elements, child) {
    if (child.getAttribute(attribute))
    	elements.push(Element.extend(child));
    return elements;
  	});
}

Object.extend(Element, {
  move: function(element, x, y) {
    Element.setStyle(element, {top: y});
    Element.setStyle(element, {left: x});
  }
});

function reloadAd(iFrameId) {
	self.setTimeout(
		function () { 
			if ( $(iFrameId) ) {
				var oldSrc = $(iFrameId).src;		
				var newSrc = addRandomKeyValue(oldSrc);			
				$(iFrameId).src = "";
				$(iFrameId).src = newSrc;
			}
			reloadAd(iFrameId); 
		}, AD_REFRESH_TIME_MS);
}

function stripDARTKeyValue(url, key) {
	var idx = url.indexOf(key);
	var nextSemiColonIdx = url.indexOf(";", idx);
		
	if (idx > 0) {
		return url.substring(0, idx) + url.substring(nextSemiColonIdx+1,url.length);
	}
	else {
		return url;
	}
}

function addRandomKeyValue(url) {
	url = stripDARTKeyValue(url, "rnd");
	var idx = url.indexOf(";");
	var rnd =  parseInt(Math.random() * 1000000);
	
	return url.substring(0,idx) + ";rnd=" + rnd + url.substring(idx,url.length);
}

function submitTopicsDropdown(form, baseUrl)
{
  var taxonomyId = parseInt($F(form.taxonomyId));
  if (taxonomyId > 0)
  {
    if (baseUrl)
      location.href=baseUrl + taxonomyId;
    else
      form.submit();
  }
  else
  {
    form.taxonomyId.selectedIndex = 0;
  }
}


var pollId = -1;
var setCookie = false;

function submitPollForm(form, userVoted, isHomePage)
{
	
	var pollChoiceId = -1;
  var module = 'poll_results';
  if(isHomePage)
  	module = 'home_poll_results';

  pollId = $F('pollId');
  var url = contextPath+"/module.htm";
	if(userVoted)
	{
		setCookie = true;
		pollChoiceId = getSelectedChoice(form)
		if(pollChoiceId == undefined || pollChoiceId == -1)
	  {
	  	setCookie = false;
	  	$('voteButton').disabled = false;
	    alert("Please select an option.");
	    return;
	  }
		
	}
	var params = "moduleType=" + module + "&pollId=" + pollId + "&pollChoiceId=" + pollChoiceId+"&r=true";
	var pollCall = new Ajax.Request(
			url,
			{
			   method: 'get',
			   parameters: params,
			   onComplete: handlePollResults
			});
}

function getSelectedChoice(form)
{
  var selectedValue;
  for(i=0;i<form.pollChoice.length;i++)
  {
    if(form.pollChoice[i].checked)
      return form.pollChoice[i].value;
  }
  return -1;
}


function handlePollResults(pollRequest)
{
	
	$('pollDiv').innerHTML = pollRequest.responseText;
	if(setCookie)
		updateCookies();
}

function updateCookies()
{
	var cookieValue = xGetCookie("votedPolls");
	if(cookieValue == null)
		cookieValue = pollId;
	else
		cookieValue = cookieValue +","+pollId;
	
	xSetCookie("votedPolls", cookieValue, getCookieDate(), "");
}

function getCookieDate()
{
	var date = new Date();
	date.setTime(date.getTime()+(365*24*60*60*1000));
	return date;
}


function xGetCookie(name)
{
  var value=null, search=name+"=";
  if (document.cookie.length > 0) {
    var offset = document.cookie.indexOf(search);
    if (offset != -1) {
      offset += search.length;
      var end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      value = unescape(document.cookie.substring(offset, end));
    }
  }
  return value;

}

function xSetCookie(name, value, expire, path)
{
	document.cookie = name + "=" + escape(value) +
                    ((!expire) ? "" : ("; expires=" + expire.toGMTString())) +
                    "; path=" + ((!path) ? "/" : path);
}

function popUp(URL, width, height, resizable)
{
  day = new Date();
  id = day.getTime();
  eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=" + resizable + ",width=" + width + ",height=" + height + "');");
}

function clearDefault(el) 
{
    if (el.defaultValue==el.value) el.value = ""
}