var slides, playLink, stopLink;

$(document).ready(function() {
	slides = $("#Slides");
	if(slides.length == 0)
		return;

	playLink = $("#PlayLink");
	stopLink = $("#StopLink");

	var opts = {
		timeout: 3000,
		prev: "#PrevLink",
		next: "#NextLink",
		prevNextClick: pauseCycle
	}
	
	var linkOpts = {
		pager: "#LinkContainer ul",
		pagerAnchorBuilder: buildPagerLink,
		pagerEvent: 'mouseover',
		pauseOnPagerHover: true,
		allowPagerClickBubble: true
	}
	
	// create/setup links only if container is present
	if($("#LinkContainer").length > 0)
		$.extend(opts, linkOpts);

	slides.cycle(opts);

	playLink.click(resumeCycle);
	stopLink.click(pauseCycle);
});

function buildPagerLink(idx, slide) {
	var link = $(slide);
  	return "<li><a target='_blank' href='" + link.attr("href") + "'>" + link.attr("href").replace("http://", "") + "</a></li>"; 
}

function pauseCycle() {
	slides.cycle("pause");
	stopLink.hide();
	playLink.show();
}

function resumeCycle() {
	slides.cycle("resume");
	playLink.hide();
	stopLink.show();
}
