/**
 * @author  	Michael Shamoon <michael@g-fighter.com>
 * @copyright	Copyright (c) 2009 Kalon Studios
 */
window.onload = init;
Shadowbox.loadSkin('kalon', 'http://kalonstudios.com/skin/frontend/kalon/default/shadowbox/');
Cufon.replace('.content h2');

function init() {
	var options = {
		slideshowDelay:     '3',
		overlayColor: '#ffffff',
		onFinish: function(el) { Cufon.replace($$('#shadowbox_content h2')); }
	};
	Shadowbox.init(options);
	var path = document.location.pathname;
	if (path == '/' || path == '/index.php') {
		setInterval(changeHomepageBackground, 10000);
		changeHomepageBackground();
	}
	animatePage();
	if ($$('.slide-container').length > 0) {
		initGallery();
		window.onresize = initGallery;
	}
	initEmailLinks();
}

var bkgdIndex = -1;

function changeHomepageBackground() {
	if (bkgdIndex == -1) bkgdIndex = Math.round(Math.random() * 4);
	new Effect.Fade('bg'+bkgdIndex, {duration: 2});
	bkgdIndex++;
	if(bkgdIndex > 4) bkgdIndex = 0;
	new Effect.Appear('bg'+bkgdIndex, {duration: 2});
}

function setContentBkgd(imgURL, id) {
	if ($('container').hasClassName('showgrid')) return;
	if (id == undefined) id = 'content-wrapper';
	if ($('bg0')) $('bg0').style.backgroundImage = 'none';
	$(id).style.backgroundImage = 'url('+imgURL+')';
	$(id).style.backgroundRepeat = 'no-repeat';
	$(id).style.backgroundPosition = 'center center';
}

function animatePage() {
	if ($('col-left') || $$('#content-wrapper .span-18').length > 0 || $$('#content-wrapper .span-17').length > 0 || $$('#content-wrapper .span-14').length > 0) return; // hack, do not animate 2col-cD pages or 1 column
	ieOffset = (navigator.userAgent.indexOf("MSIE") > 0) ? 171 : 0;
	new Effect.Appear('content-wrapper', { duration: 1.0, from: 0, to: 1 });
	if ($('product')) {
		$('product').style.left = "1000px";
		$('product').style.position = "relative";
		new Effect.Move('product', { x: -1000 + ieOffset, y: 0, mode: 'relative', duration: 1.5, delay: 1 });
		$('product-essential').setOpacity(0);
		new Effect.Appear('product-essential', { duration: 1.0, delay: 1.5, from: 0, to: 1, afterFinish: initGallery });
	} if ($('category')) {
		$('category').style.left = "1000px";
		$('category').style.position = "relative";
		new Effect.Move('category', { x: -1000 + ieOffset, y: 0, mode: 'relative', duration: 1.5, delay: 1 });
		$('category-content').setOpacity(0);
		new Effect.Appear('category-content', { duration: 1.0, delay: 1.5, from: 0, to: 1, afterFinish: initGallery });
	}
}

var index = 0;
var ieOffset = 0;
var scrollBarOffset = 0;
var xDelta;
var maxIndex;
var animating = false;

function initGallery() {
	scrollBarOffset = (window.outerWidth) ? window.outerWidth - document.viewport.getWidth() : 0;
	xDelta = document.viewport.getWidth() - scrollBarOffset;
	var baseIndex = index > 0 ? index : 1;
	$$('.slide-container').each( function(slide, i) { slide.style.left = ((i + 1)*xDelta - 511 + ieOffset)+'px'; slide.style.display = "block"; } );
	if (index > 0) $('content-wrapper').style.left = (-baseIndex * xDelta)+'px';
	$('gallery-nav').style.left = ((baseIndex)*xDelta + ieOffset) +'px';
	$('gallery-nav').style.display = "block";
}

function setMaxIndex(newMaxIndex) {
	maxIndex = newMaxIndex;
}

function goNext() {
	if (animating) return;
	if (index >= maxIndex) {
		goPrevious(0);
		return;
	}
	var els = [
		new Effect.Move('content-wrapper', { sync: true, x: -xDelta, y: 0, mode: 'relative' }),
		new Effect.Move('gallery-nav', { sync: true, x: (index > 0) ? xDelta : 0, y: 0, mode: 'relative' }) 
		]
	new Effect.Parallel(els, { 
		duration: 0.8,
		queue: { position: 'front', scope: 'gallery', limit: 1 },
		afterFinish: function() { 
			index++;
			animating = false;
		}
	});
	animating = true;
}

function goPrevious(goToIndex) {
	if (animating) return;
	var step = (goToIndex != null) ? index - goToIndex : 1;
	var navStep = (step > 1) ? step - 1 : step;
	var els = [
		new Effect.Move('content-wrapper', { sync: true, x: step * xDelta, y: 0, mode: 'relative' }),
		new Effect.Move('gallery-nav', { sync: true, x: (index > 1) ? navStep * -xDelta : 0, y: 0, mode: 'relative' }) 
		]
	new Effect.Parallel(els, { 
		duration: 0.8,
		queue: { position: 'front', scope: 'gallery', limit: 1 },
		afterFinish: function() {
			index -= step;
			animating = false;
			if (index == 0) $('content-wrapper').style.position = 'static';
		}
	});
	animating = true;
}

function initEmailLinks() {
	if ($('wholesale-email')) addEmailLink($('wholesale-email'), 'wholesale');
	if ($('orders-email')) addEmailLink($('orders-email'), 'orders');
	if ($('general-email')) addEmailLink($('general-email'), 'studio');
}

function addEmailLink(anchor, address) {
	var at = '@';
	var domain = 'kalonstudios.com';
	var mail = 'mail';
	var to = 'to';
	anchor.href = mail + to + ':' + address + at + domain;
	anchor.innerHTML = address + at + domain;
}