
$(document).ready(function() {

	
	// hide viewer lay over
	//$('#viewer').fadeOut(0);
	
	$('iframe').load(function() {
		
		updateViewer();
		
	});
	
	
	
	// create new object for each carousel div
	$('div').filter("#carousel").each(function () {
		// create new caroussel for this div
		var nc=new NuCaroussel($(this));
		
	});
	
});



function NuCaroussel(_div)
{
	
	this.mydiv=_div;
	this.timer; 
	this.mouseX=0;
	this.margin=144;
	this.offsetEase=0;
	this.carouselWidth=this.mydiv.width();
	this.slidesWidth=this.mydiv.find('#slides').width();
	this.thumbWidth=this.mydiv.find('#thumbnail').width();
	//document.write(this.thumbWidth);
	var _this=this; 
	
	
	// create ease loop
	this.timer = setInterval(function(){ _this.loopin(_this);}, 30); 
	
	// scroll thumbs on mouse move
	this.mydiv.mousemove( function(event){ _this.mouseMove(_this,event);})
	
	// slide effect enter
	this.mydiv.find("#slide").mouseenter(function(event) {
		$(this).fadeTo(200,.9);
		$(this).find('#caption').slideUp('fast');
	});
	
	// slide effect leave
	this.mydiv.find("#slide").mouseleave(function(event) {
		$(this).fadeTo(200,1);
		$(this).find('#caption').slideDown('fast');
	});
	
	// slide effect click
	this.mydiv.find("#slide").click(function(event) {
		// goto ahref
		var ahref=$(this).attr('clickref');
		var mwidth=$(this).attr('mediaWidth');
		var mheight=$(this).attr('mediaHeight');
	
		openViewer(ahref,mwidth,mheight);
	
	});

}


// updatimng loop
NuCaroussel.prototype.loopin = function(_this)
{
	
	var percent=(_this.mouseX-(_this.margin))/(_this.carouselWidth-_this.margin*2);
	if(percent<0){percent=0;}
	if(percent>1){percent=1;}
	var range=_this.slidesWidth-_this.carouselWidth;
	var offset=-range*percent;
	
	_this.offsetEase+=(offset-_this.offsetEase)*.125
	
	if(_this.offsetEase>0){_this.offsetEase=0;}
	
	_this.mydiv.find('#slides').css( { "left": _this.offsetEase } );
	
}

//NuCaroussel.prototype.mouseEnter = function(_this,_event)
//{
//	_this.mouseX=_this.mydiv.offset().left;
//}
NuCaroussel.prototype.mouseMove = function(_this,_event)
{
	//document.write(_event.pageX);
	_this.mouseX=_event.pageX-_this.mydiv.offset().left;
}

//NuCaroussel.prototype.slideEnter = function(_event)
//{
	
	//var slide=_event.currentTarget;
	//document.write(slide)
	//$(this).fadeTo(200,.9);
	//slide.slideUp('fast');
	//slide.find('#caption').slideUp('fast');
//}







