/*
All content contained in this script was created by RapidWeaver with plugins downloaded from www.yourhead.com
*/

function YHCarousel (mover, align, thumbs, content, height, leftButton, rightButton, width, pages, thumbSpeed, contentSpeed, asyncIDs, asyncSRCs) {
	this.elements = content;
	this.height = height;
	this.position = 0;
	this.width = width;
	this.pages = pages;
	this.align = align;
	this.thumbSpeed = thumbSpeed;
	this.contentSpeed = contentSpeed;
	this.asyncIDs = asyncIDs;
	this.asyncSRCs = asyncSRCs;
	this.loaded = 0;
	
	this.carousel = new yhglFx.Style($(mover), 'marginTop', {duration:this.thumbSpeed});
	
	this.rightHeight = $(rightButton).scrollHeight;
	$(rightButton).addEvent('mouseover', function(){
		$(rightButton+'_idle').style.display='none';
		$(rightButton+'_hover').style.display='block';
		$(rightButton+'_down').style.display='none';
	});
	$(rightButton).addEvent('mouseout', function(){
		$(rightButton+'_idle').style.display='block';
		$(rightButton+'_hover').style.display='none';
		$(rightButton+'_down').style.display='none';
	});
	$(rightButton).addEvent('mousedown', function(){
		$(rightButton+'_idle').style.display='none';
		$(rightButton+'_hover').style.display='none';
		$(rightButton+'_down').style.display='block';
	});
	$(rightButton).addEvent('mouseup', function(){
		this.moveRight();
		$(rightButton+'_idle').style.display='none';
		$(rightButton+'_down').style.display='none';
		$(rightButton+'_hover').style.display='block';
	}.bind(this));
	
	this.leftHeight = $(leftButton).scrollHeight;
	$(leftButton).addEvent('mouseover', function(){
		$(leftButton+'_idle').style.display='none';
		$(leftButton+'_hover').style.display='block';
		$(leftButton+'_down').style.display='none';
	});
	$(leftButton).addEvent('mouseout', function(){
		$(leftButton+'_idle').style.display='block';
		$(leftButton+'_hover').style.display='none';
		$(leftButton+'_down').style.display='none';
	});
	$(leftButton).addEvent('mousedown', function(){
		$(leftButton+'_idle').style.display='none';
		$(leftButton+'_hover').style.display='none';
		$(leftButton+'_down').style.display='block';
	});
	$(leftButton).addEvent('mouseup', function(){
		this.moveLeft();
		$(leftButton+'_idle').style.display='none';
		$(leftButton+'_down').style.display='none';
		$(leftButton+'_hover').style.display='block';
	}.bind(this));
	
	$(rightButton+'_idle').style.display='block';
	$(rightButton+'_hover').style.display='none';
	$(rightButton+'_down').style.display='none';
	
	$(leftButton+'_idle').style.display='block';
	$(leftButton+'_hover').style.display='none';
	$(leftButton+'_down').style.display='none';
	
	
	var hideH;
	this.elements.each(function(el, i){
		if (i) {
			var hideO = new yhglFx.Style($(el), 'opacity', {duration:0});
			hideO.custom (0, 0);
			$(el).style.display = 'none';
		}
	}, this);
	
	var showH = new yhglFx.Style($(align), 'height', {duration:0});
	showH.custom (0, $(this.elements[0]).offsetWidth);
	
	i=0;
	thumbs.each(function(thumb, i) {
		if (this.asyncSRCs[i] == '') {
			$(thumb).addEvent('click', function(){this.showThisHideOpen(i)}.bind(this));
		} else {
			$(thumb).style.opacity = 0.25;
		}
	}, this);

}


YHCarousel.prototype.loadLate = function () {
	var i = this.loaded;
	this.loaded ++;
	var thumb = thumbs[i];
	var t = this;
	if (i < thumbs.length) {
		if (this.asyncSRCs[i] == '') {
		  t.loadLate();
		} else {
			var aID = this.asyncIDs[i];
			var aSRC = this.asyncSRCs[i];
			new Asset.image (aSRC, {
				onload: function(){
					$(aID).src = aSRC;
					var fadeinO    = new yhglFx.Style($(thumb), 'opacity', {duration:200});
					fadeinO.start (1.0);
					$(thumb).addEvent('click', function(){t.showThisHideOpen(i)}.bind(this));
                    t.loadLate();
                }
			});
		}
	}
}


YHCarousel.prototype.moveLeft = function () {
	if (this.position == 0) {
		return;
	}
	oldM = this.position * this.height * -1;
	this.position = this.position - 1;
	newM = this.position * this.height * -1;
	this.carousel.custom(oldM, newM);
}


YHCarousel.prototype.moveRight = function () {
	if (this.position >= this.pages-1) {
		return;
	}
	oldM = this.position * this.height * -1;
	this.position = this.position + 1;
	newM = this.position * this.height * -1;
	this.carousel.custom(oldM, newM);
}


YHCarousel.prototype.showThisHideOpen = function(iToShow) {
	if (iToShow != this.previousClick){
		this.previousClick = iToShow;
		var objObjs = {};
		var madeInactive = false;
		var elToShow = '';
		var elToHide = '';
		var showWidth = this.width;
        var ov;
        var newWidth;
		
		this.elements.each(function(el, i){
			if (i != iToShow) {
				if ($(el).offsetWidth > 0) {
					elToHide = $(this.elements[i]);
				}
			} else {
				elToShow = $(this.elements[i]);
			}
		}, this);
		if (elToHide == '') {
			if (iToShow == 0)
				return;
			else
				elToHide = $(this.elements[0]);
		}
		
		var showO = new yhglFx.Style(elToShow, 'opacity', {duration:this.contentSpeed, onComplete: function(){
		}});
		
		var changeH = new yhglFx.Style($(this.align), 'width', {duration:this.contentSpeed, onComplete: function(){
			showO.custom(0, 1);
			elToShow.style.overflow = ov;
		}});
		
		var hideO = new yhglFx.Style(elToHide, 'opacity', {duration:this.contentSpeed, onComplete: function(){
			ov = elToShow.style.overflow;
			elToShow.style.overflow = 'hidden';
			elToShow.style.display = 'block';
			newWidth = (showWidth) ? showWidth : elToShow.scrollHeight;
			elToShow.style.display = 'none';
			elToShow.style.overflow = ov;
			changeH.custom(elToHide.offsetWidth, newWidth);
			elToHide.style.display = 'none';
			elToShow.style.display = 'block';
		}});
		
		hideO.custom (1, 0);
	}
}












