$(document).ready(function() {

$('.slideshow').cycle({
		slideExpr: 'div',
		fx: 'fade',
		speed:  500,
	 	timeout: 10000,
//    	pauseOnPagerHover: 1,
    	pause: 1,
	 	pager:  '#nav',
		pagerAnchorBuilder: function(idx, slide) { 
	     // return selector string for existing anchor 
        	return '#nav li:eq(' + idx + ') a'; },   
        before:  onBefore,
        after:   onAfter
	});

	// set up the 'menu' clicks:
	bindThumbClick('1', 0);
	bindThumbClick('2', 1);
	bindThumbClick('3', 2);
	bindThumbClick('4', 3);
	
	// initialize to thumb1:
	initThumb('1', 0);
//	$('#btn-thumb1').trigger('init');
	swapPic();
});

function onBefore(curr, next, opts) { 
	$('#btn-thumb'+ (opts.currSlide + 1)).attr({src : '/images/home-rotator/thumb-pos-'+ (opts.currSlide + 1) + '-off.png'});
}

function onAfter(curr, next, opts) { 
	bindThumbHovers();
	$('#btn-thumb'+ (opts.currSlide + 1)).attr({src : '/images/home-rotator/thumb-pos-'+ (opts.currSlide + 1) + '-on.png'});
	$('#btn-thumb'+ (opts.currSlide + 1)).unbind('mouseenter mouseleave');

}

function bindThumbHovers() {
	// binds all the rollover/rollout events for the 'slide' show:
	$("#btn-thumb1").hover(
			function(){$(this).attr({src : '/images/home-rotator/thumb-pos-1-on.png'});}, 
			function(){$(this).attr({src : '/images/home-rotator/thumb-pos-1-off.png'});}				 
	);
	$("#btn-thumb2").hover(
			function(){$(this).attr({src : '/images/home-rotator/thumb-pos-2-on.png'});}, 
			function(){$(this).attr({src : '/images/home-rotator/thumb-pos-2-off.png'});}				 
	);
	$("#btn-thumb3").hover(
			function(){$(this).attr({src : '/images/home-rotator/thumb-pos-3-on.png'});}, 
			function(){$(this).attr({src : '/images/home-rotator/thumb-pos-3-off.png'});}				 
	);
	$("#btn-thumb4").hover(
			function(){$(this).attr({src : '/images/home-rotator/thumb-pos-4-on.png'});}, 
			function(){$(this).attr({src : '/images/home-rotator/thumb-pos-4-off.png'});}				 
	);

}

function bindThumbClick(thumb, cycleNum) {	
	// binds click event and jquery.cycle index to a target thumb graphic:
	var target = '#btn-thumb' + thumb;
	$(target).click(function() { 
		//cycle to the selected slide and pause the automated slide show	
		$('.slideshow').cycle(cycleNum);
		$('.slideshow').cycle('pause');
		//rebind all mouse events
//		bindThumbHovers();
		//turn all thumbs offs
//		allThumbsOff();			
		// set ourself to the 'on' state:
//		$(target).unbind('mouseenter mouseleave');
		$(target).attr({src : '/images/home-rotator/thumb-pos-'+thumb+'-on.png'});

		return false; 
	}); 
}	
	
function initThumb(thumb, cycleNum) {	
	// binds click event and jquery.cycle index to a target:
	var target = '#btn-thumb' + thumb;	
	$(target).init(function() { 
		$('.slideshow').cycle(cycleNum);
		bindThumbHovers();
		initAllThumbsOff();
		return false; 
	});	
}


function allThumbsOff() {
	$('#btn-thumb1').attr({src : '/images/home-rotator/thumb-pos-1-off.png'});
	$('#btn-thumb2').attr({src : '/images/home-rotator/thumb-pos-2-off.png'});
	$('#btn-thumb3').attr({src : '/images/home-rotator/thumb-pos-3-off.png'});
	$('#btn-thumb4').attr({src : '/images/home-rotator/thumb-pos-4-off.png'});
}	

function initAllThumbsOff() {
	$('#btn-thumb1').attr({src : '/images/home-rotator/thumb-pos-1-on.png'});
	$('#btn-thumb2').attr({src : '/images/home-rotator/thumb-pos-2-off.png'});
	$('#btn-thumb3').attr({src : '/images/home-rotator/thumb-pos-3-off.png'});
	$('#btn-thumb4').attr({src : '/images/home-rotator/thumb-pos-4-off.png'});
}	

//Swap the marketing widget graphics randomly
function swapPic()
{
if(document.getElementById)
            {
			rValue = rnd(2);
            var thePicture1=document.getElementById("claim-1");
            var picPath1="/images/home/reporting-a-claim-v"+rValue+".png";
            thePicture1.style.background="url("+picPath1+")";
            
            var thePicture2=document.getElementById("insurance-review-1");
            var picPath2="/images/home/members-save-more-v"+rValue+".png";
            thePicture2.style.background="url("+picPath2+")";
            }
}

function rnd(n) {return Math.floor(Math.random() * n) + 1; }



