// constructor for ad data struct
function ad(src, alt, href)
{
 this.src = src;
 this.alt = alt;
 this.href = href;
 // height, width?
}

function open_ad(href)
{
  var window_name = "HMG_ad";
  window.open(href,window_name)
}

var ads = new Array(
  // new ad (src, alt, href)
	new ad("ads/portlet/BA_HighAltClimbingEquip.gif","High Altitude Climbing Equipment - www.bradleyalpinist.com","http://www.bradleyalpinist.com"),
    new ad("ads/portlet/BA_ColdOne.gif","Up for a tall cold one? Gear for High Altitude (Valandre, DMM, Wild Things, Simond) - Bradley Alpinist","http://www.bradleyalpinist.com"),
    new ad("ads/portlet/BA_Simond.gif","Where Alpinists find Simond - www.bradleyalpinist.com","http://www.bradleyalpinist.com"),
	new ad("ads/portlet/BA_EquipFor.gif","Equipment for: Alpinists, Mountaineers, Andinistas, Mountain Guides - www.bradleyalpinist.com","http://www.bradleyalpinist.com"),
	new ad("ads/portlet/BA_Valandre.gif","Where Alpinists Find Valandre - www.bradleyalpinist.com","http://www.bradleyalpinist.com"),
	new ad("ads/portlet/BA_DMM.gif","High Altitude Climbing Equipment","http://www.bradleyalpinist.com"),
	new ad("ads/portlet/BA_WildThings.gif","Wild Things ... offered by www.bradleyalpinist.com","http://www.bradleyalpinist.com"),
	new ad("ads/portlet/BA_BarstoolFashion.gif","Barstool Fashion for Alpinists","http://www.bradleyalpinist.com")
);

var ads_path = "ads/portlet";
var curr_ad = 0;
var fade_speed = 3000;
var ad_switch_interval = 7000;
var ads_container_id = "p_ads_container";


// event handler for when DOM is ready
$(document).ready(
function() 
{
	do_it();
}
);

function do_it()
{
	generate_ads();
	do_timer();
}

function generate_ads()
{
	// iterate through each ad spec and generate ad
	$(ads).each( function(i) {
		$("#"+ads_container_id).append(
			"<div id='pAd_" + i + "' class='ad'>" +
			"<a href='"+ this.href + "' + onclick='open_ad(\"" + this.href + "\"); return false;'>" +
			"<img src='" + this.src + "' alt='" + this.alt + 
      "'/>" + "</a></div>"
			); 
		} );
	// hide generated ads, so we can reveal them with effect
	$("#"+ads_container_id + " .ad").each( 
		function(i) { $(this).hide(); } 
		);
}

function do_timer()
{
	var ad_switch_timer = setTimeout("switch_ads()",ad_switch_interval);
}

function switch_ads()
{
	// if we are at the last ad, start again from the beginning
	if(curr_ad >= ads.length - 1)
	{
		next_ad = 0;
	}
	else
	{
		next_ad = curr_ad + 1;
	}


	$("#pAd_" + next_ad).fadeIn(fade_speed);
	$("#pAd_" + curr_ad).fadeOut(fade_speed);

	
	curr_ad = next_ad;
	
	// set timer for the next one
	do_timer();
}

