var fade_time = 1000;
var picid;
var picarray = [];
var numpics;
var img_max_width = 500;
var img_max_height = 375;

function fadepic()
{
	//console.log(picid);
	picarray[picid].fadeOut(fade_time);
	if(++picid == numpics)
		picid = 0;

	//fix for IE8
	if($.browser.msie && $.browser.version == '8.0')
	{
		$('#main_photo').css('width', '499px');//.css('height','375px');
		picarray[picid].fadeIn(fade_time, function()
		{
			$('#main_photo').css('width', '500px');//.css('height','375px');
		});
	}
	else
		picarray[picid].fadeIn(fade_time);

}

function getpicinfo(obj)
{
	img_w = obj.width();
	img_h = obj.height();

	var offset_y;
	if(img_h > img_max_height)
	{
		offset_y = Math.ceil((img_max_height - img_h) / 2) + 'px';
		obj.css('top',offset_y);
	}
	else if(img_h < img_max_height)
	{
		offset_y = Math.floor((img_max_height - img_h) / 2) + 'px';
		obj.css('top',offset_y);
	}
	
	var offset_x;
	if(img_w > img_max_width)
	{
		offset_x = Math.ceil((img_max_width - img_w) / 2) + 'px';
		obj.css('left',offset_x);
	}
	else if(img_w < img_max_width)
	{
		offset_x = Math.floor((img_max_width - img_w) / 2) + 'px';
		obj.css('left',offset_x);
	}
}

$(document).ready(function()
{
	// Get the IDs for each pic
	$('.piccycle').each(function()
	{
		picarray.push($(this));
		getpicinfo($(this));
	});
	numpics = picarray.length;

	picid=0;
	if(numpics > 1)
	{
		setInterval('fadepic()', 5000);
	}

});
