document.getElementsByClassName = function(cl) {
	var retnode = [];
	var myclass = new RegExp('\\b'+cl+'\\b');
	var elem = this.getElementsByTagName('*');
	for (var i = 0; i < elem.length; i++) {
	var classes = elem[i].className;
	if (myclass.test(classes)) retnode.push(elem[i]);
}
	return retnode;
}; 

var imageArray = new Array;
var t;
var container = '.promo-type-1.double';
var time = 4000;
var page = 'alumni'
var name = 'plu_'+page+'_ads_index';
var orderName = 'plu_'+page+'_ads_order';

		
//create a new cookie
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

//read and existing cookie
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

//erase a cookie
function eraseCookie(name) {
	createCookie(name,"",-1);
}

//preload all of the Ad images to avoid rotating to an unloaded Ad
function preloadImages(){
	var root = xmlobject.getElementsByTagName('rss')[0];
	var channels = root.getElementsByTagName("channel");
	var items = channels[0].getElementsByTagName("item");
	
	
	for(i=0;i<items.length;i++){
		//set variables for image element
		var adAlt = items[i].getElementsByTagName('title')[0].firstChild.nodeValue;
		var imgSource = items[i].getElementsByTagName('enclosure')[0].getAttribute('url');
		
		//create necessary dom elements for the ad
		
		var newImg = document.createElement('img');

		newImg.src = imgSource;
		newImg.alt = adAlt;
		
		
		//place current image into an array to easily find it later
		imageArray[i] = newImg;

	}
}

//create the new Ad group
function createAd(a,b,items){
			
	//set variables for ad element
	var adLink1 = items[a].getElementsByTagName('link')[0].firstChild.nodeValue;

	
	var adLink2 = items[b].getElementsByTagName('link')[0].firstChild.nodeValue;

	//create necessary dom elements for the ad
	
	var newAd = document.createElement('div');
	var promo1 = document.createElement('div');
	var newLink1 = document.createElement('a');

	var promo2 = document.createElement('div');
	var newLink2 = document.createElement('a');

	
	$(promo1).addClass('promo');
	newLink1.href = adLink1;

	$(promo2).addClass('promo');
	newLink2.href = adLink2;

	
	
	//create the new ad object 
	$(newAd).append(
		$(promo1).append(
			$(newLink1).append(
				imageArray[a]
			)
		)
	);
	$(newAd).append(
		$(promo2).append(
			$(newLink2).append(
				imageArray[b]
			)
		)
	);
	
	return newAd;
}



//fade out two existing ads, then fade in the bottom ad with a new ad below it
rotateAd = function (){
	
	
	//set the variables for traversing the feed object
	var root = xmlobject.getElementsByTagName('rss')[0];
	var channels = root.getElementsByTagName("channel");
	var items = channels[0].getElementsByTagName("item");
	if(items.length > 2){
		//find the value of the cookie in order to know which ad index the user is on
		var index = parseFloat(readCookie(name));
		var order = readCookie(orderName).split('%2C');
		
		//if at the last ad item, set the index to 0
		if(index < (items.length-1)){
			index++;
		}else{
			index=0;
		}
		
		var a = parseFloat(order[index])-1;
		
		if(index < (items.length-1)){
			index++;
		}else{
			index=0;
		}
		
		var b = parseFloat(order[index])-1;
		
		var oldAd = $(container + ' div');
		var newAd = createAd(a,b,items);
	
		//check if the next two Ad images are done loading
		if(imageArray[a].complete && imageArray[b].complete){
			//delete old cookie
			eraseCookie(name);
			
			//set a new cookie with the new index and to expire in 2 days
			createCookie(name,index,2);
			
			
			
			//add the new ad to the page
			$(oldAd).css({'z-index':2, 'position':'relative', "display" : 'block'});
			$(newAd).appendTo(container).css({'margin-top':-$(newAd).height()-14, 'z-index':1, 'position':'relative', "display" : 'block'});
			
			
			//fade out old ads
			$(oldAd).fadeTo(600,0,function(){
				
				//remove old ads from the dom
				$(oldAd).remove();
				//fade in new ads
				$(newAd).css("margin-top",-14);
				
			});		
			t=setTimeout("rotateAd()",time);
		}else{
			t=setTimeout("rotateAd()",1000);
		}
	}
}

$(document).ready(function(){
	
	
	//Store the external RSS feed as a dom object
	if (window.XMLHttpRequest){
		// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}else{
		// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			feed=xmlhttp.responseText;
			if (window.DOMParser){
				parser=new DOMParser();
				xmlobject=parser.parseFromString(feed,"text/xml");
			}else{ // Internet Explorer
			
				xmlobject=new ActiveXObject("Microsoft.XMLDOM");
				xmlobject.async="false";
				xmlobject.loadXML(feed);
			} 
			preloadImages();
			
		}
	}
		  
	xmlhttp.open("GET","/widgets/"+page+"-ads/feed.rss.filtered.php",true);
	xmlhttp.send();
		
	//pause the ad changes during hover
	
	$(container).hover(
	
			function(){
				
				// Stop the image sequence
				clearTimeout(t);
			
			}
			,function(){
				
				// Restart sequence
				t=setTimeout("rotateAd()",4000);
			}
		
	
	);
	
	//wrap the content of the Ads to be faded out
	//in a Div so that it will be easier to fade
	//both at the same time
	$(container).css({'height':$(container).height(),'overflow':'hidden'}).html('<div style=\'margin-top:-14px;\'>'+$(container).html()+'</div>');
	
	
		
	//rotate out ads every six seconds
	//$(container).everyTime('4s', 'cycleNews', rotateAd);
	t=setTimeout("rotateAd()",4000);
});
