// File: loadBanners.js
//Randomizes banner ad image displays

// Start function when DOM has completely loaded 
$(document).ready(function(){ 
if($("div#BannerAd").length > 0) 	{
	var currentURL = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ":" + window.location.port : "");
	// Open the xml file
	$.get(currentURL + '/mod/custom_index/views/default/canvas/gallery/xml/ads_banner.xml',{},function(xml){ //Note must remove the mtiss outside of Dev
		$('album',xml).each(function(i) {
			imgpath = $(this).attr("lgPath");
			imgcount = $(this).attr("count") 
			//Now get a random item
			var n = parseInt(imgcount);
			var randn=Math.floor(Math.random()*n) +1;
			randn = randn + "";
			subs = 'img[id="' + randn + '"]';	
			isrc = $(this).find(subs).attr("src"); 
			ilink = $(this).find(subs).attr("link"); 
			ititle = $(this).find(subs).attr("title"); 

			//Open external links in a new window.
			if(ilink.indexOf('mtishowspace.com')== -1) ilink = ilink + '" target="_blank';

			myHTMLOutput = '<a href="' + ilink + '"><img title="'  + ititle +  '" alt="'  + ititle +  '" src="' + currentURL + '/' + imgpath + isrc + '"/> </a>';
		});

		// Update the DIV
		$("div#BannerAd").append(myHTMLOutput);
	});
}
});
 
	 
