/*
Title: Javascript Common 
Author: Designkitchen, Inc. 
*/

// toggles the share drawer in the footer of the page
function toggleShareFooter(e) {
	if(document.getElementById("sharebox").style.display=="none") {
		e.className="sharedon";
		document.getElementById("sharebox").style.display="";
	}
	else {
		e.className="share";
		document.getElementById("sharebox").style.display="none";
	}
}

// suckerfish (used to allow anchor tags w/o href to have :hover states in Internet Explorer)
sfHover = function() {
	var sfEls = document.getElementById("carousel").getElementsByTagName("A");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);



// Slide module w/ finite functionality
// super sliding action

Event.observe(window, 'load', function(e) {
	if ($$('#carousel #products .product').length > 0) {
		var width = 0;
		var slides = $$('#carousel #products .product');
		var wrapper = $('products');
		for (var i=0; i<slides.length; i++) {
			width += slides[i].offsetWidth;
		}
		wrapper.style.width = width+'px';
		wrapper.style.position = 'relative';
	}
});

function jumpTo(page) {
	var containerArea = 506, //originally 640
		container = $('products'), 
		containerOffSet = $('products').positionedOffset(),
		current = Math.floor(-containerOffSet[0]/containerArea),
		distance = ((current - page) * containerArea);
	if (distance != 0) {
		new Effect.Move(container, {
			queue: { position: 'end', scope: 'carousel', limit: 1 },
			x: distance,
			mode: 'relative',
			afterFinish: function() { updateIndicator(page) }
		});
	}
}

function viewMoreGo() {

	var containerArea = 506, //originally 640
		container = $('products'), 
		containerWidth = $('products').getWidth(),
		containerOffSet = $('products').positionedOffset();
		
	if(-containerOffSet[0] < containerWidth - containerArea) {
		new Effect.Move(container, { 
			queue: { position: 'end', scope: 'carousel', limit: 1 },
			x: -containerArea, 
			mode: 'relative',
			afterFinish: function() { updateIndicator((-containerOffSet[0]+containerArea)/containerArea) }
		});
	}
	else if (containerWidth <= containerArea ) {
		// may want to do something special when 3 or less photos are present
	}
	else {
		new Effect.Move(container, { 
			queue: { position: 'end', scope: 'carousel', limit: 1 },
			duration: 1,
			x: -containerOffSet[0] , 
			mode: 'relative',
			afterFinish: function() { updateIndicator(0) }
		});
	}
}

function viewLessGo() {

	var containerArea = 506, //originally 640
		container = $('products'), 
		containerWidth = $('products').getWidth(),
		containerOffSet = $('products').positionedOffset();
		
	if(containerOffSet[0] == 0) {
		// at starting position
	}
	else if (containerWidth == containerArea ) {
		// may want to do something special when 3 or less photos are present
	}
	else {
		new Effect.Move(container, { 
			queue: { position: 'end', scope: 'carousel', limit: 1 },
			duration: 1,
			x: containerArea, 
			mode: 'relative',
			afterFinish: function() { updateIndicator(-(containerOffSet[0]+containerArea)/containerArea) }
		});
	}
}

function updateIndicator(page) {
	$$('#indicator a').invoke('removeClassName', 'current');
	$$('#indicator a')[Math.ceil(page)].addClassName('current');
}

Event.observe(window, 'load', function(e) {
	if ($$('#carousel #products .product').length > 0) {
		slides = $$('#products .product');
		el = $('indicator');
		pages = Math.ceil(slides.length/2); //originally length/3
		str = '';
		for ( i = 0; i < pages; i++ ) {
			str += '<a onclick="jumpTo(' + i + ');"></a>';
		}
		el.insert(str);
		$$('#indicator a')[0].addClassName('current');
	}
});