var homeGallery = { 
	
	containerElement : null, 
	marqueeElement : null,
	numPages : null,
	page : 1,
	
	init : function () {
	
		homeGallery.containerElement = $('div.gallery');
		homeGallery.marqueeElement = homeGallery.containerElement.find('div.scrollContainer');
		
		var ul = homeGallery.marqueeElement.find('ul');
		var lis = ul.children('li');
		var width = (lis.width() + parseInt(lis.css('padding-left')) + parseInt(lis.css('padding-right'))) * lis.size()
		homeGallery.numPages = Math.ceil(width/homeGallery.marqueeElement.width());
		ul.width(homeGallery.numPages*homeGallery.marqueeElement.width());
		homeGallery.containerElement.find('a.next').click( homeGallery.nextPage );
		homeGallery.containerElement.find('a.prev').click( homeGallery.prevPage );
		
		//Opacity stuff : 
		lis.css('opacity',0.5);
		lis.mouseover( function() { $(this).stop().fadeTo(400, 1); } )
		   .mouseout( function() { $(this).stop().fadeTo(400,1).fadeTo(400, 0.5); } );
		
		lis.find('a').click( homeGallery.onPopup );
		
	},
	
	nextPage : function() {
		homeGallery.movePage(1);
		return false;
	},
	prevPage : function() {
		homeGallery.movePage(-1);
		return false;
	},
	movePage : function(n) {
		
		if (homeGallery.marqueeElement.children().is(':animated'))
			return false;
		if (homeGallery.page == homeGallery.numPages && n == 1)
			return ;
		if (homeGallery.page == 1 && n == -1)
			return;
			
		homeGallery.page += n;
		
		var marginRight = parseInt(homeGallery.marqueeElement.children().css('marginRight'))
						- parseInt(homeGallery.marqueeElement.css('width'))*n;
		
		homeGallery.marqueeElement.children().animate( { marginRight:marginRight }, 1000, 'swing');
	},
	
	onPopup : function() {
		
		var obj = $(this);
		if (window.open(obj.attr('href'), '', 'width=600,height=500'))
			return false;
		
	}
	
	
	
}

$( homeGallery.init );