var currentNum = 1;
var currentColor = '';

var animOptions = {
	duration: 0.3
};

var inputValueCache = new Array();
var activateInputs = function() {
	$$('input[type=text]').each(function(input) {
		inputValueCache[input.id] = input.value;
		
		Event.observe(input, 'focus', function() {
			if(inputValueCache[input.id] == input.value) {
				input.value = '';
				input.addClassName('active');
				new Effect.Morph(input, { style: {
					borderColor: '#fec006'
				}, duration: 0.3});
			}


		});
		
		Event.observe(input, 'blur', function() {
			if(input.value == '') {
				input.value = inputValueCache[input.id];
				input.removeClassName('active');
				
				new Effect.Morph(input, { style: {
					borderColor: '#333333'
				}, duration: 0.3});	
			}
			
		})
	});
	
}

new Event.observe(window, 'load', activateInputs);

var moveToSpot = function(num) {
	var width = $('image-1').getWidth();
	// console.log(width);
	var xPos = (num-1)*-width;
	//var duration = Math.abs(num-currentNum)*0.50;
	var duration = 0.3;
	new Effect.Move('carousel', {x: xPos, mode: 'absolute', duration: duration, transition: Effect.Transitions.EaseFromTo, queue: 'end' });
	if($('nav-'+num))
		$('nav-'+num).addClassName('active');	
	if($('color-'+num))
		$('color-'+num).addClassName('active');
	currentNum = num;
	$('nav-'+currentNum).classNames().each(function(className) {
		if(className.indexOf('c-') != -1) {
			currentColor = className;
			throw $break;
		}
	});
	
	$$('#productGallery .nav ol li').each(hideShowElements);
}


var prepareCarousel = function(item, index) {
	new Event.observe(item, 'click', function(evt) {
		var num = item.id.split('-')[1];

		if($('nav-'+currentNum))
			$('nav-'+currentNum).removeClassName('active');
		if($('color-'+num)) {
			if($('color-'+currentNum))
				$('color-'+currentNum).removeClassName('active');			
		}
		
		
		
		moveToSpot(num);
	});		
}

var featuredProductCarousel = function() {
	if($$('body.view')) {
		$$('#productGallery ol li').each(prepareCarousel);
		$$('#colorsNav .swatch').each(prepareCarousel);
		if($('nav-1') && $('color-1')) {
			$('nav-1').addClassName('active');
			$('color-1').addClassName('active');
		}
		
		$$('.news #productGallery .image img').each(function(image) {
			var topOffset = Math.floor((image.getHeight()/2) - (380/2)) * -1;
			
			image.setStyle({
				top: topOffset+'px'
			});
			
		});

		if($('nav-'+currentNum)) {
			$('nav-'+currentNum).classNames().each(function(className) {
				if(className.indexOf('c-') != -1) {
					currentColor = className;
					throw $break;
				}
			});			
		}


	    $$('#productGallery .nav ol li').each(hideShowElements);		
	}

}

var hideShowElements = function(item) {
	if(item.hasClassName('color')) {
		if(item.hasClassName(currentColor)) {
			item.show();
		} else {
			item.hide();
		}
	}
}

var timeout = null;
var enableColorTooltips = function() {
	$$('#colorsNav .swatch').each(function(color) {
		var toolTip = color.firstDescendant();
		
		// console.log(toolTip.getWidth());
		
		var leftOffset = Math.floor((toolTip.getWidth()/2) - (color.getWidth()/2)) * -1;
		
		// console.log(leftOffset);		
		toolTip.setStyle({
			left: leftOffset+'px'
		});
		
		
		color.observe('mouseover', function() {
			clearTimeout(timeout);
			timeout = setTimeout(function() {
				new Effect.Appear(toolTip, animOptions);
				}, 500);
			// toolTip.show();
		});
		color.observe('mouseout', function() {
			clearTimeout(timeout);
			new Effect.Fade(toolTip, animOptions);
			// toolTip.hide();
		});
	});
	
	$$('#colorFilter .swatch').each(function(color) {
		var toolTip = color.firstDescendant();
		
		var leftOffset = Math.floor((toolTip.getWidth()/2) - (color.getWidth()/2)) * -1;
		
		// console.log(leftOffset);		
		toolTip.setStyle({
			left: leftOffset+'px'
		});
		
		
		color.observe('mouseover', function() {
			clearTimeout(timeout);
			timeout = setTimeout(function() {
				new Effect.Appear(toolTip, animOptions);
				}, 500);
			// toolTip.show();
		});
		color.observe('mouseout', function() {
			clearTimeout(timeout);
			new Effect.Fade(toolTip, animOptions);
			// toolTip.hide();
		});
	});
}

new Event.observe(window, 'load', enableColorTooltips);
new Event.observe(window, 'load', featuredProductCarousel);

if (!("console" in window) || !("firebug" in console))
{
    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
    "group", "groupEnd", "groupCollapsed", "time", "timeEnd", "count", "trace", "profile", "profileEnd", "version"];

    window.console = {};
    for (var i = 0; i < names.length; ++i)
        window.console[names[i]] = function() {}
}