$(document).ready(function() {
	$.fn.cycle.updateActivePagerLink = function(pager, currSlideIndex) {
		$(pager).find('li').removeClass('currentli')
			.filter('li:eq('+currSlideIndex+')').addClass('currentli');
	};
	
	$('#fpimage').cycle({
		timeout: 3000,
		pause: 1,
		next: '#next',
		prev: '#prev'
	});
	
	$(".scrollingimage").mouseenter(function() {
		var currentid = checkCurrentStatus();
		updateProperty(currentid);
		$(".prev").fadeIn("fast");
		$(".next").fadeIn("fast");
		$(".infobox").fadeIn("fast");
	}).mouseleave(function() {
		$(".prev").fadeOut();
		$(".next").fadeOut();
		$(".infobox").fadeOut();
		$(".infobox").html("");
	});
	
	$(".prev").click(function() {
		var currentid = checkCurrentStatus();
		var top = $("#fpimage a").size();
		var goIndex = getPrevSlide(currentid, top);
		updateProperty((goIndex));
	});
	
	$(".next").click(function() {
		var currentid = checkCurrentStatus();
		var top = $("#fpimage a").size();
		var goIndex = getNextSlide(currentid, top);
		updateProperty((goIndex));
	});
});

function getPrevSlide(num, top) {
	var prevnum = (num-1);
	if (prevnum < 1) {
		return top;
	} else {
		return prevnum;
	}
}

function getNextSlide(num, top) {
	var nextnum = (num+1);
	if (nextnum > top) {
		return 1;
	} else {
		return nextnum;
	}
}

function updateProperty(id) {
	var address = $('#fpimage a:nth-child('+id+')').metadata({type: 'attr', name: 'data'}).address;
	var price = $('#fpimage a:nth-child('+id+')').metadata({type: 'attr', name: 'data'}).price;
	var href = $('#fpimage a:nth-child('+id+')').attr("href");
	$(".infobox").parent().attr("href", href);
	$(".infobox").html("<p><strong>Address: </strong>"+address+"</p><p><strong>Price:</strong> "+price+"</p>");
}

function getCurIndex (whichz) {
	imglen = $("#fpimage a").size();
	i=0;

	while (i < imglen) {
		var nchild = (i+1);
		var currimg = parseInt($("#fpimage a:nth-child("+nchild+")").css("z-index"));
		if (currimg == whichz) {
			return (i+1);
		} else {
			i++;
		}
	}
}

function checkCurrentStatus() {
	// count number of images
	imglen = $("#fpimage a").size();
	i=0;
	var zin = new Array();
	while (i < imglen) {
		var nchild = (i+1);
		zin[i] = parseInt($("#fpimage a:nth-child("+nchild+")").css("z-index"));
		i++;
	}
	var max_zindex = Math.max.apply(null, zin);
	var output = getCurIndex(max_zindex);
	return output;
}

pica =  new Image(46,46); pica.src="images/recthp_chadsblog_dn.jpg";
picb =  new Image(46,46); picb.src="images/recthp_mapsearch_dn.jpg";
picc =  new Image(46,46); picc.src="images/recthp_myproperties_dn.jpg";
pic0 =  new Image(46,46); pic0.src="images/button_next.png";
pic1 =  new Image(46,46); pic1.src="images/button_prev.png";
pic2 =  new Image(46,46); pic2.src="images/button_prev_dn.png";
pic3 =  new Image(46,46); pic3.src="images/button_next_dn.png";
pic4 =  new Image(66,17); pic3.src="images/icon_facebook_dn.png";
pic5 =  new Image(46,46); pic3.src="images/icon_twitter_dn.png";
