var current_ad = 0
var total_ads = 0;
var animation_id;

function startAnimation()
{
	//start the animation playback
	animation_id = window.setInterval("transition()", 10000); // switch after 15 seconds.
	$("#ad_toggle").addClass("pause").removeClass("play").attr({title : "Pause Ad Animation"});
}

function transition()
{
	//here's the transition effect for the current slide.
	var this_ad = current_ad;
	var next_ad = current_ad == total_ads ? 0 : current_ad + 1;
	$("#ads .ad").eq(this_ad).css("z-index", 10);
	$("#ads .ad").eq(this_ad).fadeOut("slow");
	$("#ads .ad").eq(next_ad).css("z-index", 5).show();
//	$("#ads .ad").eq(this_ad).hide();
	current_ad = next_ad;
	
}

function stopAnimation(){
	//stop the animation playback
	window.clearInterval(animation_id);
	$("#ad_toggle").addClass("play").removeClass("pause").attr({title : "Play Ad Animation"});
}

function makeSameHeight(){
	//Let's set heights to auto and then calculate the computed height.
	if ($("#sideBar").height() == $("#mainContent").height()){ return; }
	$("#sideBar, #mainContent").css("height", "auto");
	var contentSize = $("#mainContent").height();
	var sideBarSize = $("#sideBar").height();
	if (contentSize == sideBarSize) { return; }
	if (contentSize > sideBarSize) {
		$("#sideBar").css("height", contentSize + "px");
	}
	else {
		$("#mainContent").css("height", sideBarSize + "px");
	}
}

$(document).ready(function(){
	makeSameHeight();
	
	//install the interval to keep the column heights the same start with a quarter of a second (250ms)
	var _refresh = window.setInterval("makeSameHeight()", 250);
	
	$("#search_term").focus(function() {
		if (this.value == 'search our website'){
			this.value = '';
			this.style.color = '#000';
		} 
	}).blur(function(){			
		if (this.value == ''){
			this.value = 'search our website';
			this.style.color = '#666';
		}
	});

	if($.browser.mozilla){
		$("#ads").css("opacity", 0.9999);
	}
	//initialize
	var ads = $("#ads .ad");
	ads.hide();
	total_ads = ads.length - 1; //we're counting from zero.
	ads.eq(current_ad).show();
	
	startAnimation();
	
	$("#ad_toggle").toggle(stopAnimation, startAnimation).blur();

	$("#prev_ad").click(function(){
		ads.eq(current_ad).hide();
		current_ad = current_ad == 0 ? total_ads : current_ad - 1;
		ads.eq(current_ad).show();
		return false;
	});
	
	$("#next_ad").click(function(){
		ads.eq(current_ad).hide();
		current_ad = current_ad == total_ads ? 0 : current_ad + 1;
		ads.eq(current_ad).show();
		return false;
	});
	
	//ok, now let's install the animation thingie.
	// 
});
