$(document).ready(function () {
	var dev = $('#dev').length > 0 ? true : false;
	var activeSlide = 1;
	var slides = 4;
	$('div#home_content_side > .side_object').click(function() {
		$(document).stopTime();
		slide_id = $(this).attr('data-slide-id');
		switchSlide(slide_id);
	});
	$('div#home_content_side > .side_object:first-child').addClass('active');
	if($('div#home_content_side > .side_object:first-child > div.long_description').html() != '') {
		$('div#home_content_main > div#active_lifestyle > div.long_description').show();
		$('div#home_content_main > div#active_lifestyle > div.long_description').html($('div#home_content_side > .side_object:first-child > div.long_description').html()).attr('data-slide-id',1);
	} else {
		$('div#home_content_main > div#active_lifestyle > div.long_description').hide();
	}
	$('div#home_content_main > div#active_lifestyle > div.large_image').html($('div#home_content_side > .side_object:first-child > div.large_image').html());
	$('#features .feature a').click(function(){ _gaq.push(['_trackEvent', 'Features', 'Feature', $(this).attr('href')]); });
	$('#home_content_main a').click(function(){ _gaq.push(['_trackEvent', 'Slides', 'Slide (Main)', $(this).attr('href')]); });
	$('#home_content_side a').click(function(){ _gaq.push(['_trackEvent', 'Slides', 'Slide (Side)', $(this).attr('href')]); });
	timerSwitchSlides = function() {
		switchSlide(((activeSlide+1) > slides ? 1 : activeSlide+1));
	}
	if (typeof $(document).everyTime !== 'undefined') {
		$(document).everyTime('10s',function() {
			timerSwitchSlides();
		});
	}
	switchSlide = function (slide_id) {
		activeSlide = slide_id;
		slide = $("#home_content_side .side_object[data-slide-id="+slide_id+"]");
		if (!slide.hasClass('active')) {
			if(slide.children('div.long_description').html() != '') {
				$('div#home_content_main > div#active_lifestyle > div.long_description').show();
				$('div#home_content_main > div#active_lifestyle > div.long_description').html(slide.children('div.long_description').html()).attr('data-slide-id',slide.attr('data-slide-id'));
			} else {
				$('div#home_content_main > div#active_lifestyle > div.long_description').hide();
			}
			$('div#home_content_main > div#active_lifestyle > div.large_image').html(slide.children('div.large_image').html());
			$('div#home_content_side > .side_object').removeClass('active');
			slide.addClass('active');
		} else {
			if(typeof slide.attr('data-action-url') !== 'undefined' && slide.attr('data-action-url') != false) { window.location = slide.attr('data-action-url'); }
		}
	}
	$('#NavNew ul li ul').parent().addClass('open');
	if (dev === false) {
		$('.product_swatch_switch').each(function() {
			$(this).click(function(){
				$('#ProductImage-img').attr('src','/images/products/400/' + $(this).attr('fullsku') + '.jpg');
				$('#ZoomLink a').attr('href','/images/products/800/' + $(this).attr('fullsku') + '.jpg')
			});
		});
		if($('.colorset').has('.product_swatch_switch').length) {
			$('#colorsNote').show();
		}
		$('.Page-product').each(function () {
			if (typeof jQuery.param.fragment === 'undefined') {
				return;
			}
			var fragment = jQuery.param.fragment();
			if(fragment != '') {
				$('.colorset-default').each(function(){ $(this).hide().removeClass('colorset-default'); });
				$('a.colorlink').removeClass('colorlink-active');
				$('.colorset#'+fragment).show().addClass('colorset-default');
				$('a.colorlink[colorset=' + fragment + ']').addClass('colorlink-active');
			}
			$('a.colorlink').click(function(e){
				e.preventDefault();
				$('.colorset-default').each(function(){ $(this).hide().removeClass('colorset-default'); })
				$('.colorset#'+$(this).attr('colorset')).show().addClass('colorset-default');
				$('a.colorlink').removeClass('colorlink-active');
				$(this).addClass('colorlink-active');
				jQuery.bbq.pushState('#'+$(this).attr('colorset'));
			});
		});

	}
	$("#ZoomLink > a").fancybox({
		cyclic: true
	});
	$('#MainMenu ul li.child-menu').hover(function() {
		$(this).children('ul').show();
		$(this).addClass('open');
	}, function() {
		$(this).children('ul').hide();
		$(this).removeClass('open');
	});
	$('#MainMenu ul li.columns_item ul li').unbind('mouseenter mouseleave');
	$('#MainMenu ul.columns').each(function() {
		var columns = 7;
		var i=1;
		$(this).append('<div class="columnar"></div>');
		for(i=1;i<=columns;i++) {
			$('#MainMenu ul.columns div.columnar').append('<ul class="menu_col_' + i + '"></ul>');
			$('#MainMenu ul.columns li.col'+i).clone().appendTo($('.menu_col_'+i));
		}
	});
	$('#CartContentsLink').hover(function(ev) {
		ev.preventDefault();
		elementContainer = $('#CartContentsDropDown');
		element = $('#CartContentsDropDown .contents');
		if(!$('#CartContentsDropDown').hasClass('open')) {
			refreshCartDropDown(element);
			elementContainer.addClass('open').show();
		} else {
			elementContainer.removeClass('open').hide();
		}
	},function(){
		elementContainer = $('#CartContentsDropDown');
		elementContainer.removeClass('open').hide();
	});
	function refreshCartDropDown(element) {
		$.getJSON('/catalog/shopping_cart.json.php',function(data){
			var cart_items = [];
			$.each(data.products, function(key,val){
				var product_options = [];
				$.each(val.options,function(option_key,option){
					product_options.push('<div class="product_option">' + option.products_options_name + ': '+ option.products_options_values_name + '</div>');
					if(option.products_options_name == "color") {
						product_options.push('<div class="product_option_image"><img src="' + val.materials_swatch_path + '/'+ option.products_options_values_name + '.png"></div>');
					}
				});
				cart_items.push('<tr><td class="table_product"><div class="product_name">' + val.name + '</div>' +
					product_options.join('') +
					'</td>' +
					'<td class="table_quantity">' + val.quantity + '</tr>');
			});
			element.html($('<table/>',{'class':'cartItemsTable','html':cart_items.join('')}));
		});
	}

	$('#CartContents input[type="text"]').live('change', function () {
		$(this).addClass('changed');
	});
	
});


(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery);

if (typeof CHUMS == "undefined" || !CHUMS) {
    /**
     * The C2 global namespace object.  If C2 is already defined, the
     * existing C2 object will not be overwritten so that defined
     * namespaces are preserved.
     * @class C2
     * @static
     */
    var CHUMS = {};
}

/**
 * Returns the namespace specified and creates it if it doesn't exist
 * <pre>
 * YAHOO.namespace("property.package");
 * YAHOO.namespace("YAHOO.property.package");
 * </pre>
 * Either of the above would create YAHOO.property, then
 * YAHOO.property.package
 *
 * Be careful when naming packages. Reserved words may work in some browsers
 * and not others. For instance, the following will fail in Safari:
 * <pre>
 * YAHOO.namespace("really.long.nested.namespace");
 * </pre>
 * This fails because "long" is a future reserved word in ECMAScript
 *
 * @method namespace
 * @static
 * @param  {String*} arguments 1-n namespaces to create 
 * @return {Object}  A reference to the last namespace object created
 * 
 * - FROM YAHOO.js YUI 2.6.0
 */
CHUMS.namespace = function() {
    var a=arguments, o=null, i, j, d;
    for (i=0; i<a.length; i=i+1) {
        d=a[i].split(".");
        o=CHUMS;

        // CHUMS is implied, so it is ignored if it is included
        for (j=(d[0] == "CHUMS") ? 1 : 0; j<d.length; j=j+1) {
            o[d[j]]=o[d[j]] || {};
            o=o[d[j]];
        }
    }

    return o;
};

