/*	ALL Functions for AU's Banners
*	1. COOKIE CHECK FOR PREV TOP AD 
*		Checks a cookie to know which ad had been served to the user
*		at the TOP of the HOME page and serve a different one. 
*		If no cookie found, it creates one and pushes the first ad in the function.
*		
*   2. RANDOM TOP BANNER
*		Randomly displays banners by identifying a random number and then matching a case to it.
* 
* 	3. TOP BANNER, BOTTOM BANNER, and LEFT BANNER DEPENDENCE
*		This ensures that a specific sponsor will not been seen more than once on any page.
*
*	4. LEFT BANNER ROTATION
*		This banner is above the C&A banner and at the time of implementation was intended to randomly
*		load either the Rally ad or the SolutionsIQ Scrum Cert banner.
*/

// 1. COOKIE CHECK FOR PREV TOP AD 

/*	function changeBanner() {
		var cookieVal = getCookie("adCookie");
		if (cookieVal == "0") {
			//document.getElementById("bottom1").innerHTML = "";
			document.getElementById("top1").innerHTML = "<a href='http://www.sqe.com/go?AUG23B'><img src='images/agile-728x90.gif' width='728' height='90' alt='' /></a>";
			setCookie("adCookie", 1);
		} else if (cookieVal == "1") {
			//document.getElementById("bottom1").innerHTML = "";
			document.getElementById("top1").innerHTML = "<a href='http://www.sqe.com/agiledevpractices/'><img src='images/ADP08_728x90.gif' width='728' height='90' alt='' /></a>";
			setCookie("adCookie", 2);			
		} else {
			document.getElementById("top1").innerHTML = "<a href='http://www.solutionsiq.com' target='_blank'><img src='images/siq_au_ad20080122.gif' width='728' height='90' alt='Advertisement: SolutionsIQ' /></a>";
			setCookie("adCookie", 0);
		}
	}
*/
	
// 2. RANDOM TOP BANNER

/*	function changeBanner() {
		var i = Math.floor(Math.random()*2);
		switch(i) {
		 case 0:
			document.getElementById("top1").innerHTML = "<a href='http://www.sqe.com/go?AUG23B'><img src='images/agile-728x90.gif' width='728' height='90' alt='' /></a>";
			break;
		 case 1: 
			document.getElementById("top1").innerHTML = "<a href='http://www.rallydev.com/turbulent/auemails.html '><img src='images/turbulent_au_banner02.gif' width='728' height='90' alt='' /></a>";
			break;
		}
	}
*/	
	
// 3. TOP BANNER, BOTTOM BANNER, and LEFT BANNER DEPENDENCE
	
	function changeBanner() {
		var i = Math.floor(Math.random()*4);
		var leftad = Math.floor(Math.random()*2);
		var pagelocation = location.href ;
		var homePage = pagelocation.indexOf("index.jsp") > -1 || pagelocation.indexOf(".jsp") < 0;
		var coursesPage = pagelocation.indexOf("courses.jsp") > -1;	
		var eventsPage = pagelocation.indexOf("events.jsp") > -1;	

	 	var rallyHomeAd = "<a href='http://www.rallydev.com/agile_products/lifecycle_management/' target='_blank'  onclick='javascript: pageTracker._trackPageview('/outgoing/hrallydev.com');'><img src='http://agileu.org/images/rally_ad_au.gif' width='125' height='125' border='1' alt='Advertisement: Rally Software' /></a>";  
	
//		Revert to this after the Mar 2 2010 webcast is done		
//		var rallyCoursesAd = "<a href='http://www.rallydev.com/?src=aubanner1' target='_blank'  onclick='javascript: pageTracker._trackPageview('/outgoing/crallydev.com');'><img src='http://agileu.org/images/rally_ad_au.gif' width='125' height='125' alt='Advertisement: Rally Software' /></a>";
		var rallyCoursesAd = "<a href='http://w.on24.com/r.htm?e=184855&s=1&k=CAAAF75152A3887D161A33A9424A86FD&partnerref=au1' target='_blank' ><img src='images/WebcastFlow200x200_mar2.jpg' width='125' height='125' alt='Advertisement: Rally Webcast' /></a>";
		switch(leftad) {
			case 0:
		 		document.getElementById("left-ad").innerHTML = " ";
			break;
			case 1:
		 		if (homePage) {
					document.getElementById("left-ad").innerHTML = rallyHomeAd; 
				} else if (coursesPage) {
					document.getElementById("left-ad").innerHTML = rallyCoursesAd; 
				} else if (eventsPage) {
					document.getElementById("left-ad").innerHTML = rallyCoursesAd; 
				}
			break;
		}	
		
		switch(i) {
		 case 0:
		 	topBannerHtml = "<a href='http://alistair.cockburn.us/Agile+University'><img src='images/ad_top_cockburn_associates_09april.jpg' width='728' height='90' alt='Advertisement: Cockburn and Associates'/></a>";
			bottomBannerHtml = "<a href='http://www.lithespeed.com' target='_blank' onClick='javascript: pageTracker._trackPageview('/outgoing/hlithespeed.com');'><img src='images/ls_au_ad.gif' width='468' height='60' alt='Advertisement: Lithespeed' /></a>";
			
			break;
			
		 case 1: 
			topBannerHtml = "<a href='http://www.sqetraining.com/SpecialPromotions.aspx?promo=AUG23'><img src='images/ad_09_sqe_spring_banner.jpg' width='728' height='90' alt='Advertisement: SQE Training' /></a>";
			bottomBannerHtml = "<a href='http://alistair.cockburn.us/Agile+University' target='_blank' ><img src='images/ad_bottom_cockburn_associates.jpg' width='468' height='60' alt='Advertisement: Cockburn and Associates' id='ad-cockburn'/></a>";
			
			break;
			
			case 2:
		 	topBannerHtml = "<a href='http://agileu.org/course_details.jsp?courseid=333&schid=656'><img src='images/ad_top_agile_cmmi_09.jpg' width='728' height='90' alt='Advertisement: Agile CMMI An Uncommon Marriage'/></a>";
			bottomBannerHtml = "<a href='http://alistair.cockburn.us/Agile+University' target='_blank' ><img src='images/ad_bottom_cockburn_associates.jpg' width='468' height='60' alt='Advertisement: Cockburn and Associates' id='ad-cockburn'/></a>";
			
			break;
			
			case 3:
		 	topBannerHtml = "<a href='http://www.agileuniversity.org/course_details.jsp?courseid=367&schid=701'><img src='images/ad_top_becoming_agile.jpg' width='728' height='90' alt='Advertisement: Becoming Agile in an Imperfect World April 2009'/></a>";
			bottomBannerHtml = "<a href='http://alistair.cockburn.us/Agile+University' target='_blank' ><img src='images/ad_bottom_cockburn_associates.jpg' width='468' height='60' alt='Advertisement: Cockburn and Associates' id='ad-cockburn'/></a>";
			
			break;
		}
		 if (document.getElementById("top1")) {
			document.getElementById("top1").innerHTML = topBannerHtml; 
		 }
		  if (document.getElementById("bottom1")) {
			document.getElementById("bottom1").innerHTML = bottomBannerHtml; 
		 }
		 // inserts an ad for all pages except home and courses page 
		  if (!document.getElementById("top1") && !document.getElementById("bottom1") && document.getElementById("left1")) {
				document.getElementById("left1").innerHTML = "<a href='http://alistair.cockburn.us/Agile+University'><img src='images/ad_side_cockburn_associates_09feb.jpg' width='125' height='82' alt='Logo: Cockburn and Associates' border='0'/></a>";
		 } 
		 // controls the left ad on the courses page to show the cockburn ad if the bottom cockburn ad did not load
		  if (!document.getElementById("top1") && !document.getElementById("ad-cockburn") && document.getElementById("left1")) {
				 document.getElementById("left1").innerHTML = "<a href='http://alistair.cockburn.us/Agile+University'><img src='images/ad_side_cockburn_associates_09feb.jpg' width='125' height='82' alt='Advertisement: Cockburn and Associates' border='0' /></a>";
		 }
	}
	
// 4. FUNCTION TO ROTATE TROUGH ADS 
/*	  The ad banner images are defined in arrays for each location. For example, "topAds" array contains images that go on the top of the home page. "topAdLinks" array stores the urls that the ads link to. 4 sets of arrays are defined for the 4 banner ad locations. 
*/

	var topAds = new Array ("images/rallyrevup728x90_2.jpg", "images/rallyrevup728x90_2.jpg", "images/rallyrevup728x90_2.jpg", "images/rallyrevup728x90_2.jpg", "images/rallyrevup728x90_2.jpg");

	var topAdLinks = new Array ("http://www.rallydev.com/events/webinars/rally_rev_up/", "http://www.rallydev.com/events/webinars/rally_rev_up/", "http://www.rallydev.com/events/webinars/rally_rev_up/" , "http://www.rallydev.com/events/webinars/rally_rev_up/", "http://www.rallydev.com/events/webinars/rally_rev_up/");

//Left ads are inline for slot 1, 2 and 3 - rally, Events and C&A. The middle should be rotated once the events are past.

//	var leftAds = new Array("images/rally_ad_au.gif", "images/bsc_2010-125x125.jpg", "images/adpw_125_125_web_banner.gif", "images/rally_ad_au.gif", "images/rally_ad_au.gif");

//	var leftAdLinks = new Array ("http://www.rallydev.com/?src=aubanner1", "http://www.sqe.com/BetterSoftwareConf/", "http://www.sqe.com/adpwest/", "http://www.rallydev.com/?src=aubanner1", "http://www.rallydev.com/?src=aubanner1");

//	var leftAds1 = new Array("images/r2-Rally-030_Webinar1-125x125.jpg", "images/r2-Rally-030_Webinar2-125x125.jpg", "images/r2-Rally-030_Webinar1-125x125.jpg", "images/r2-Rally-030_Webinar2-125x125.jpg", "images/r2-Rally-030_Webinar1-125x125.jpg");

//	var leftAd1Links = new Array ("http://event.on24.com/r.htm?e=219644&s=1&k=51880B1224C2D0483241866CF71A3875&partnerref=AU1", "http://event.on24.com/r.htm?e=214169&s=1&k=72C0DBD7C87439E80AC086ED9F05B7A4&partnerref=AU1", "http://event.on24.com/r.htm?e=219644&s=1&k=51880B1224C2D0483241866CF71A3875&partnerref=AU1", "http://event.on24.com/r.htm?e=214169&s=1&k=72C0DBD7C87439E80AC086ED9F05B7A4&partnerref=AU1", "http://event.on24.com/r.htm?e=219644&s=1&k=51880B1224C2D0483241866CF71A3875&partnerref=AU1");

//	var leftAds2 = new Array("images/blank.gif", "images/ad_side_cockburn_associates_09feb.jpg", "images/ad_side_cockburn_associates_09feb.jpg", "images/blank.gif", "images/blank.gif");
	
//	var leftAd2Links = new Array("javascript:;", "http://alistair.cockburn.us/Agile+University", "http://alistair.cockburn.us/Agile+University", "javascript:;", "javascript:;");
	
	var bottomAds = new Array("images/ls_au_ad.gif", "images/ls_au_ad.gif", "images/ls_au_ad.gif", "images/ad_bottom_cockburn_associates.jpg", "images/ad_bottom_cockburn_associates.jpg");

	var bottomAdLinks = new Array ("lithespeed.jsp", "lithespeed.jsp", "lithespeed.jsp", "http://alistair.cockburn.us/Agile+University", "http://alistair.cockburn.us/Agile+University");

	function showAdBanner() {
		//Generate a random number between 0 & 4. Use this number as an array index to show the corresponding ads on all 4 location.
		var rand_no = (parseInt(Math.random() * 10)) % topAds.length;  

		//Check if the current page is the home page or the courses page.
		var pagelocation = location.href ;
		var homePage = pagelocation.indexOf("index.jsp") > -1 || pagelocation.indexOf(".jsp") < 0;
		var coursesPage = pagelocation.indexOf("courses.jsp") > -1;	
		var eventsPage = pagelocation.indexOf("events.jsp") > -1;	
		
		if (document.getElementById("top1")) {
			if (rand_no == 0) {
				rand_no = Math.floor(Math.random()*2) + 1; 
			}
			document.getElementById("top1").innerHTML = "<a href='" + topAdLinks[rand_no] + "' target='_blank'><img src='" + topAds[rand_no] + "'  width='728' height='90' /></a>";
		}
		
		// Left ad top slot is inline 
		/*if (document.getElementById("left-ad")) {
			//Check if the Rally ad is showing. Add the tracking code to the ad link
			if (rand_no == 3) { 
				if (homePage) {
					document.getElementById("left-ad").innerHTML = "<a href='" + leftAdLinks[rand_no] + "' onclick='javascript: pageTracker._trackPageview('/outgoing/hrallydev.com');><img src='" + leftAds[rand_no] + "' vspace='10' /></a>";
				} else if (coursesPage) {
					document.getElementById("left-ad").innerHTML = "<a href='" + leftAdLinks[rand_no] + "'  target='_blank' onclick='javascript: pageTracker._trackPageview('/outgoing/crallydev.com');><img src='" + leftAds[rand_no] + "' vspace='10' /></a>";
				}
			} else {
				if(leftAds[rand_no].indexOf("blank.gif") <= -1) {
					document.getElementById("left-ad").innerHTML = "<a href='" + leftAdLinks[rand_no] + "' target='_blank'><img src='" + leftAds[rand_no] + "' /></a>";
				}
			}
		} */
		
		// This is for Left Ads bottom slot */
		
		/* if (document.getElementById("left1")) {
			if(leftAds1[rand_no].indexOf("blank.gif") <= -1) {
				document.getElementById("left1").innerHTML = "<a href='" + leftAd1Links[rand_no] + "' target='_blank'><img src='" + leftAds1[rand_no] + "' width='125' vspace='10'/></a>";
			} 			
		} */
		
		/*if (document.getElementById("left2")) {
			if(leftAds2[rand_no].indexOf("blank.gif") <= -1) {
				document.getElementById("left2").innerHTML = "<a href='" + leftAd2Links[rand_no] + "' target='_blank'><img src='" + leftAds2[rand_no] + "' width='125' vspace='10'/></a>";
			} 			
		} */
		
		//Show ads only on home page and courses page
		if (homePage || coursesPage) {
			document.getElementById("side-ads").style.display = "block";
		} else {
			document.getElementById("side-ads").style.display = "none";		
		}
		
		if (eventsPage) {
			document.getElementById("side-ads").style.display = "block";
			document.getElementById("left-ad").style.display = "none";
			document.getElementById("left2").style.display = "none";
		}
		
		if(document.getElementById("bottom1")) {
			//Check if the Lithespeed ad is showing. Add the tracking code to the ad link
			if(rand_no == 1) {
				document.getElementById("bottom1").innerHTML = "<a href='" + bottomAdLinks[rand_no] + "' onClick='javascript: pageTracker._trackPageview('/outgoing/hlithespeed.com');'><img src='" + bottomAds[rand_no] + "' width='468' height='60' /></a>";
			} else {
				document.getElementById("bottom1").innerHTML = "<a href='" + bottomAdLinks[rand_no] + "' target='_blank'><img src='" + bottomAds[rand_no] + "' width='468' height='60' /></a>";
			}
		}
	}	