// JavaScript for opening external links in XHTML document
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.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 MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

/* SwapDivs : Show & Hide Expand/Collapse */

function HideContent(d) {
document.getElementById(d).style.display = "none";
}
function ShowContent(d) {
document.getElementById(d).style.display = "block";
}
function ReverseContentDisplay(d) {
if(document.getElementById(d).style.display == "none") { document.getElementById(d).style.display = "block"; }
else { document.getElementById(d).style.display = "none"; }
}

function loadHomeFunctions(){
	var flashvars = {};
	flashvars.flashLinkURL = "";
	var params = {};
	params.wmode = "transparent";
	var attributes = {};
	attributes.id = "flashGalleryId";
	swfobject.embedSWF("/connections/flash/Connections_09_Hero.swf", "flashSplash", "580", "315", "9.0.28.0", "/flash/expressInstall.swf", flashvars, params, attributes);
}

function selectReplacement(obj) {
  obj.className += ' replaced';
  var ul = document.createElement('ul');
  ul.className = 'selectReplacement';
  var opts = obj.options;
  for (var i=0; i<opts.length; i++) {
	var selectedOpt;
	if (opts[i].selected) {
	  selectedOpt = i;
	  break;
	} else {
	  selectedOpt = 0;
	}
  }
  for (var i=0; i<opts.length; i++) {
	var li = document.createElement('li');
	var txt = document.createTextNode(opts[i].text);
	li.appendChild(txt);
	li.selIndex = opts[i].index;
	li.selectID = obj.id;
	li.URLvalue = opts[i].value;
	li.origText = opts[0].innerHTML;
	li.onclick = function() {
	  selectMe(this);
	}
	if (window.attachEvent) {
	  li.onmouseover = function() {
		this.className += ' hover';
	  }
	  li.onmouseout = function() {
		this.className = 
		  this.className.replace(new RegExp(" hover\\b"), '');
	  }
	}
	if (i == selectedOpt) {
	  li.className = 'selected';
	  li.onclick = function() {
		  this.innerHTML += " - Click to Close Menu";
		closeSet();
		this.parentNode.className += ' selectOpen';
		this.onclick = function() {
		  selectMe(this);
		  this.innerHTML = li.origText;
		}
	  }
	}
	ul.appendChild(li);
  }
  sfHover_dropDown(ul);
  obj.parentNode.insertBefore(ul,obj);
}

function selectMe(obj) {

  var lis = obj.parentNode.getElementsByTagName('li');
  for (var i=0; i<lis.length; i++) {
	if (lis[i] != obj) {
	  lis[i].className='';
	  lis[i].onclick = function() {
		selectMe(this);
	  }
	} else {
	  setVal(obj.selectID, obj.selIndex, obj.URLvalue);
	  obj.className='selected';
	  obj.parentNode.className = 
		obj.parentNode.className.replace(new RegExp(" selectOpen\\b"), '');
	  obj.onclick = function() {
		 closeSet();
		  this.innerHTML += " - Click to Close Menu";
		  
		obj.parentNode.className += ' selectOpen';
		this.onclick = function() {
		  selectMe(this);
		  this.innerHTML = obj.origText;
		}
	  }
	}
  }
}

function closeSet(){
		 var sS = document.getElementById("homePageDropDowns").getElementsByTagName('ul');
		  for (var i=0; i<sS.length; i++) {
			//selectReplacement(s[i]);
				//alert(sS[i].className)
			//document.getElementById("homePageDropDowns").getElementsByTagName('DIV')[i].style.zIndex = 100 - i*2;
			if(sS[i].className == "selectReplacement selectOpen"){
				sS[i].getElementsByTagName("li")[0].className='selected';
				  sS[i].className = sS[i].className.replace(new RegExp(" selectOpen\\b"), '');
				  sS[i].getElementsByTagName("li")[0].innerHTML = sS[i].getElementsByTagName("li")[0].origText;
				  sS[i].getElementsByTagName("li")[0].onclick = function() {
					closeSet();
					//this.innerHTML = this.origText;
		  			this.innerHTML += " - Click to Close Menu";
					this.parentNode.className += ' selectOpen';
					this.onclick = function() {
					  selectMe(this);
					  this.innerHTML = this.origText;
					}
				  }
			}
		  }	
}

function setVal(objID, selIndex, URLvalue) {
  var obj = document.getElementById(objID);
  obj.selectedIndex = selIndex;
  if(selIndex!=0){
  window.location.href=URLvalue;
  }
}

function setForm() {
	//alert(navigator.appName)
	if(navigator.appVersion.substring(75,81) == "Safari"){
		return;
	}
  var s = document.getElementById("homePageDropDowns").getElementsByTagName('select');
  for (var i=0; i<s.length; i++) {
	selectReplacement(s[i]);
	document.getElementById("homePageDropDowns").getElementsByTagName('DIV')[i].style.zIndex = 100 - i*2;
  }
}

function swapGraphic(num, ImgLink, Img, ImgAlt, ImgThumb, ImgNewWindow){
	document.getElementById("headerGraphicLinkControl").href = ImgLink;
	document.getElementById("headerGraphicLinkControl").title = ImgAlt;
	if(ImgNewWindow){
		document.getElementById("headerGraphicLinkControl").target = "_blank";
	}
	else{
		document.getElementById("headerGraphicLinkControl").target = "_self";
	}
	
	document.getElementById("headerGraphicControl").src = "/images/"+Img;
	document.getElementById("headerGraphicControl").alt = ImgAlt;

	document.getElementById("arrowContainer").getElementsByTagName("span")[0].id = "imgArrow"+num;

}

function homePageImg(){
	
	//values for home page image swapping
	var myImgLink = ['http://www.payrollpal.com/Resources/Webinars/SocialForward.html', 'http://www.payrollpal.com/Resources/Whitepapers/ListGrowth.html', 'http://www.connections09.com'];
	var myImg = ['home-headerImg01.jpg', 'home-headerImg02.jpg', 'home-headerImg03.jpg'];
	var myImgAlt = ['ExactTarget - Forrester Webinar: Social Forward', 'ExactTarget List Growth Whitepaper', 'Connections 2009'];
	var myImgThumb = ['selectImg01.jpg', 'selectImg02.jpg', 'selectImg03.jpg'];
	var myOpenNewWindow = [true, false, true]
	
	
	var initialSetting=Math.floor(Math.random()*3)
	//alert(initialSetting+" "+myImgLink[initialSetting]+" "+myImg[initialSetting]+" "+myImgThumb[initialSetting])
	
	
	//<ul id="navHomeSelector"><li id="ImgLink1"><a href="#" title="" class="select1"><span class="invisible">Select Img 1</span></a></li><li id="ImgLink2"><a href="#" title="" class="select2"><span class="invisible">Select Img 2</span></a></li><li id="ImgLink3"><a href="#" title="" class="select3"><span class="invisible">Select Img 3</span></a></li></ul>
	var imgThumbLink01 = document.createElement("a");
	imgThumbLink01.setAttribute("href", "#");
	imgThumbLink01.setAttribute("class", "select1");
	//imgThumbLink01.innerHTML = "appear?";
	imgThumbLink01.onclick = function(){
		swapGraphic(0, myImgLink[0], myImg[0], myImgAlt[0], myImgThumb[0], myOpenNewWindow[0]);
		return false;
	}
	var imgThumbLinkLI01 = document.createElement("li");
	imgThumbLinkLI01.setAttribute("id", "ImgLink1");
	imgThumbLinkLI01.appendChild(imgThumbLink01);	

	var imgThumbLink02 = document.createElement("a");
	imgThumbLink02.setAttribute("href", "#");
	imgThumbLink02.setAttribute("class", "select2");
	//imgThumbLink02.innerHTML = "appear?";
	imgThumbLink02.onclick = function(){
		swapGraphic(1, myImgLink[1], myImg[1], myImgAlt[1], myImgThumb[1], myOpenNewWindow[1]);
		return false;
	}
	var imgThumbLinkLI02 = document.createElement("li");
	imgThumbLinkLI02.setAttribute("id", "ImgLink2");
	imgThumbLinkLI02.appendChild(imgThumbLink02);	

	var imgThumbLink03 = document.createElement("a");
	imgThumbLink03.setAttribute("href", "#");
	imgThumbLink03.setAttribute("class", "select3");
	//imgThumbLink03.innerHTML = "appear?";
	imgThumbLink03.onclick = function(){
		swapGraphic(2, myImgLink[2], myImg[2], myImgAlt[2], myImgThumb[2], myOpenNewWindow[2]);
		return false;
	}
	var imgThumbLinkLI03 = document.createElement("li");
	imgThumbLinkLI03.setAttribute("id", "ImgLink3");
	imgThumbLinkLI03.appendChild(imgThumbLink03);	

	var imgThumbLinkUL = document.createElement("ul");
	imgThumbLinkUL.setAttribute("id", "navHomeSelector");
	imgThumbLinkUL.appendChild(imgThumbLinkLI01);	
	imgThumbLinkUL.appendChild(imgThumbLinkLI02);	
	imgThumbLinkUL.appendChild(imgThumbLinkLI03);	

	document.getElementById("headerGraphic").appendChild(imgThumbLinkUL);


	//<span class="imgCover"></span>
	var coverImgs = document.createElement("span");
	coverImgs.setAttribute("class", "imgCover");
	coverImgs.setAttribute("id", "imgCover");
	//coverImgs.innerHTML = "appear?";
	document.getElementById("headerGraphic").appendChild(coverImgs);
	
	
	//<span id="arrowControl" class="imgArrow arrow0"></span>
	var arrowContainer = document.createElement("span");
	arrowContainer.setAttribute("id", "arrowContainer");
	var arrowDisplay = document.createElement("span");
	arrowDisplay.setAttribute("id", "imgArrow"+initialSetting);
	//arrowDisplay.innerHTML = "appear?";
	arrowContainer.appendChild(arrowDisplay)
	document.getElementById("headerGraphic").appendChild(arrowContainer);
	
	
	//<ul id="imgDisplay"><li id="ImgThumb1" class="select1"><img src="/images/selectImg01.jpg" alt="Image 1" width="59" height="57" /></li><li id="ImgThumb2" class="select2"><img src="/images/selectImg02.jpg" alt="Image 2" width="59" height="57" /></li><li id="ImgThumb3" class="select3"><img src="/images/selectImg03.jpg" alt="Image 3" width="59" height="57" /></li></ul>
	var imgThumb01 = document.createElement("img");
	imgThumb01.setAttribute("src", "/images/selectImg01.jpg");
	var imgThumbLI01 = document.createElement("li");
	imgThumbLI01.setAttribute("id", "ImgThumb1");
	imgThumbLI01.setAttribute("class", "select1");
	imgThumbLI01.appendChild(imgThumb01);	

	var imgThumb02 = document.createElement("img");
	imgThumb02.setAttribute("src", "/images/selectImg02.jpg");
	var imgThumbLI02 = document.createElement("li");
	imgThumbLI02.setAttribute("id", "ImgThumb2");
	imgThumbLI02.setAttribute("class", "select2");
	imgThumbLI02.appendChild(imgThumb02);	

	var imgThumb03 = document.createElement("img");
	imgThumb03.setAttribute("src", "/images/selectImg03.jpg");
	var imgThumbLI03 = document.createElement("li");
	imgThumbLI03.setAttribute("id", "ImgThumb3");
	imgThumbLI03.setAttribute("class", "select3");
	imgThumbLI03.appendChild(imgThumb03);	


	var imgThumbUL = document.createElement("ul");
	imgThumbUL.setAttribute("id", "imgDisplay");
	imgThumbUL.appendChild(imgThumbLI01);	
	imgThumbUL.appendChild(imgThumbLI02);	
	imgThumbUL.appendChild(imgThumbLI03);	


	document.getElementById("headerGraphic").appendChild(imgThumbUL);
	
	
	
	
	//<a id="headerGraphicLinkControl" href="#" title=""><img id="headerGraphicControl" src="/images/home-headerImgTEMP.jpg" width="617" height="200" alt="TEMP IMG" /></a>
	var linkElement = document.createElement("a");
	linkElement.setAttribute("id", "headerGraphicLinkControl");
	linkElement.setAttribute("href", myImgLink[initialSetting]);
	linkElement.setAttribute("title",  myImgAlt[initialSetting]);
	if(myOpenNewWindow[initialSetting]){
		linkElement.setAttribute("target",  "_blank");
	}
	else{
		linkElement.setAttribute("target",  "_self");
	}
	var mainImgElement = document.createElement("img");
	mainImgElement.setAttribute("id", "headerGraphicControl");
	mainImgElement.setAttribute("width", "617");
	mainImgElement.setAttribute("height", "200");
	mainImgElement.setAttribute("src", "/images/"+myImg[initialSetting]);
	mainImgElement.setAttribute("alt", myImgAlt[initialSetting]);
	
	linkElement.appendChild(mainImgElement);	
	document.getElementById("headerGraphic").appendChild(linkElement);
	
	//document.getElementById("arrowControl").className = "imgArrow arrow"+initialSetting;
	//document.getElementById("headerGraphicLinkControl").href = myImgLink[initialSetting];
	//document.getElementById("headerGraphicControl").src = "/images/"+myImg[initialSetting];
	
	
	
}
	
function externalLinks() {
 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") == "external")
     anchor.target = "_blank";
 }
}


sfHover_dropDown = function(menu) {
	//alert("DD")
	var sfEls = menu.getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
			//alert(this.className)
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}

// JavaScript for suckerfish drop-downs <http://www.htmldog.com/articles/suckerfish/>
sfHover = function() {
	var sfEls = document.getElementById("navMain").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}

function nextHomeLink(){
	//var maxNumber = document.getElementById("ctl00_cpMain_txtMax").value;
	//var randomnumber=Math.floor(Math.random()*maxNumber);
	//document.getElementById("ctl00_cpMain_txtNext").value = randomnumber;
	if(timerOn){
	callahah('/SiteResources/casestudynext.aspx','caseStudies', 'ctl00_cpMain_txtNext', 'ctl00_cpMain_txtMax', 'next');
	setTimeout("alertTimerClickHandler()",0);
	return false;
	}
}

function callahah(url, target, nindex, mindex, nextprev) {
            var nextindex = document.getElementById(nindex).value;
            var maxindex = document.getElementById(mindex).value;
            --maxindex;
            var nextcheck = nextindex;
            ++nextcheck;
            var prevcheck = nextindex;
            --prevcheck;
			
            if (nextprev == "next")
            {
                if (nextcheck > maxindex)
                    document.getElementById(nindex).value = 0;
                else
                    document.getElementById(nindex).value = nextcheck;
            }
            else
            {
                if (prevcheck <= 0){
                    document.getElementById(nindex).value = maxindex;
					nextindex = maxindex;
				}
                else {
                    document.getElementById(nindex).value = prevcheck;
					nextindex = prevcheck - 1;
					/*if(nextindex < 0){
						nextindex = maxindex;
					}*/
				}
            }
			//alert(nextindex)
            url = url + '?index=' + nextindex;
            ahah(url, target);
            
        }
            
        function ahah(url,target) {
            document.getElementById(target).innerHTML = '';
            if (window.XMLHttpRequest) {
                req = new XMLHttpRequest();
                req.onreadystatechange = function() {ahahDone(target);};
                req.open("GET", url, true);
                req.send(null);
            } else if (window.ActiveXObject) {
                req = new ActiveXObject("Microsoft.XMLHTTP");
                if (req) {
                    req.onreadystatechange = function() {ahahDone(target);};
                    req.open("GET", url, true);
                    req.send();
                }
            }
        } 

        function ahahDone(target) {
           // only if req is "loaded"
           if (req.readyState == 4) {
               // only if "OK"
               if (req.status == 200 || req.status == 304) {
                   results = req.responseText;
                   document.getElementById(target).innerHTML = results;
               } else {
                   document.getElementById(target).innerHTML="ahah error:\n" +
                       req.statusText;
               }
           }
        }

var alertTimerId = 0;
var timerOn = true;

function alertTimerClickHandler(timeOn)
{
  if ( timerOn )
  {
    // Start the timer
    //document.getElementById("alertTimerButton").value = "Click me to stop the timer!";
    alertTimerId = setTimeout ( "nextHomeLink()", 5000 );
  }
  else
  {
    //document.getElementById("alertTimerButton").value = "Click me and wait!";
    clearTimeout ( alertTimerId );
  }
}
// JavaScript for loading above functions into the page
var PageInitializer = {
    start : function(){
        PageInitializer.addLoadEvent(function(){
            
            //MM_preloadImages('/images/connections09/nav_registration_over.jpg','/images/connections09/nav_agenda_over.jpg','/images/connections09/nav_speakers_over.jpg','/images/connections09/nav_contact_over.jpg','/images/connections09/nav_register_over.jpg')
            //Add Functions Here
			externalLinks();
			if(document.getElementById("navMain")){
				sfHover();
			}
			
			if(document.getElementsByTagName("BODY")[0].className == "homePage"){
				var flashvars = {};
				flashvars.urlLink = "http://www.payrollpal.com/Templates/CaseStudiesList.aspx?id=380&LangType=1033";
				var params = {};
				params.wmode = "transparent";
				var attributes = {};
				attributes.id = "flash-home";
				swfobject.embedSWF("/flash/EXAC-homeBanner.swf", "flash-home", "999", "78", "8.0.0", "/flash/expressInstall.swf", flashvars, params, attributes);
				//document.getElementById("caseStudies").style.display="none";
				setForm();
				homePageImg();
				/*var maxNumber = document.getElementById("ctl00_cpMain_txtMax").value;
				var randomnumber=Math.floor(Math.random()*maxNumber);
				document.getElementById("ctl00_cpMain_txtNext").value = randomnumber;
				callahah('/SiteResources/casestudynext.aspx','caseStudies', 'ctl00_cpMain_txtNext', 'ctl00_cpMain_txtMax', 'next');
				document.getElementById("caseStudies").style.display="block";
				setTimeout("alertTimerClickHandler()",0);*/
			}
			
			if(document.getElementById("flashSplash")){
				loadHomeFunctions();
			}

        });
    },
    addLoadEvent:function(f){var w=window;var o=w.onload;if(typeof w.onload!='function'){w.onload=f;}else{w.onload=function(){o();f();}}}
}
PageInitializer.start();
