$(document).ready(function() {
	$('.clemSlider').clemSlider();
	
	if($('.product select').length && !$('#content').hasClass('product')){
		var cf = new ClemFilter();
	}
	
	setUpPress();
	setupShipping();	
	// setUpDD();
});
$.fn.clemSlider = function(){
	new ClemSlider(this);
}

var ClemSlider = function(el){
	var th = this;
	
	this.el = el;
	this.wrapper = $('<div/>').attr('class', 'slideWrap');
	this.rbutton = $('<div/>').attr('class', 'sliderButton rButton');
	this.lis = this.el.find('li');
	this.total = this.lis.length;
	this.iter = 0;	
	
	this.el.width(this.lis.eq(0).width() * this.total);
	this.wrapper.width(this.lis.eq(0).width());
	this.wrapper.height(this.lis.eq(0).height());
	this.el.wrap(this.wrapper);
	this.rbutton.appendTo(this.wrapper);
	
	if(this.total > 0){
		this.interval  = setInterval(function(){th.slide('right');}, 4000);
	}
}
ClemSlider.prototype.slide = function(dir){
	var th = this;
	switch(dir){
		case 'left':
		
		break;
		
		case 'right':
		
		if(th.iter < th.total-1){
			th.el.animate({marginLeft:'-=' + this.lis.eq(0).width()}, {duration:400});
			th.iter++;
		}else{
			th.reset();
		}

		break;
	}
}
ClemSlider.prototype.reset = function(){
	this.el.animate({marginLeft:0}, {duration:400});
	this.iter = 0;
}

var setUpPress = function(){
	$('.pressText').each(function(){
		var t = $(this);
		var pdfLink = t.find('a');
		if(pdfLink.attr('href') && pdfLink.attr('href').substring(pdfLink.attr('href').lastIndexOf('.') + 1) == 'pdf'){
			t.prev().append(pdfLink.parent());
		}

	});
}

var setUpDD = function(){
	var isOverNav = false;
	var ddls = $('div.dd');
	var navs = $('#menu-upper_nav li a');
	
	navs.bind('mouseenter',function(e){
		var targ = $(this);
		var title = targ.attr('title');
		navs.removeClass('over');
		targ.addClass('over');
		ddls.hide();
		$('#dd_' + title).css({left:targ.offset().left - 160}).show();
		isOverNav = true;
	});
	
	ddls.bind('mouseenter', function(){ isOverNav = true }).
		bind('mouseleave', function(){
			navs.removeClass('over');
			ddls.hide();
			isOverNav = false;
		});
	$('#bigNavWrapper').bind('mouseleave', function(){
		ddls.hide();
	});
}

var ClemFilter = function(){
	var t = this;
	this.sizes = [];
	this.colors = [];
	this.grid = $('.category.grid #content ul.products');
	this.items = this.grid.find('li.product');
	
	this.sizeFilterLinks = $('<ul class="size_filter" />');
	this.colorsFilterLinks = $('<ul class="colors_filter" />');
	
	this.items.each(function(){
		var _t = $(this);
		var variations = _t.find('option');
		
		variations.each(function(){
			var thisText = ($(this).text().replace('.', 'point'));
						
			if(!isNaN(parseInt(thisText)) || thisText.toLowerCase() == 'small' || thisText.toLowerCase() == 'extra small' || thisText.toLowerCase() == 'medium' || thisText.toLowerCase() == 'large'){
				if(t.sizes.indexOf(thisText) == -1){
					t.sizes.push(thisText);
				}
				
				thisText = 'size_' + thisText;
			}else{
				if(t.colors.indexOf(thisText) == -1){
					t.colors.push(thisText);
				}
			}
			_t.addClass(thisText);
		});
	});
	
	t.colorsFilterLinks.append('<li><a href="#all">Show All</a></li>');
	t.sizeFilterLinks.append('<li><a href="#all">Show All</a></li>');
	

	for(var c =0;c< t.colors.length; c++ ){
		t.colorsFilterLinks.append('<li><a href="#'+ t.colors[c] +'">'+ t.colors[c] +'</a></li>');
	}
	
	for(var d =0;d< t.sizes.length; d++ ){
		t.sizeFilterLinks.append('<li><a href="#'+ t.sizes[d] +'">'+ t.sizes[d].replace('point', '.') +'</a></li>');
	}
	
	$('#sideBar').append('<h3 class="catTitle">Size</h3>').append(t.sizeFilterLinks).append('<h3 class="catTitle">Colors</h3>').append(t.colorsFilterLinks);
	
	t.sizeFilterLinks.find('li a').bind('click', function(e){
		e.preventDefault();
		t.filterEm('size_' + $(this).attr('href').replace('#', ''));
	});
	
	t.colorsFilterLinks.find('li a').bind('click', function(e){
		e.preventDefault();
		t.filterEm($(this).attr('href').replace('#', ''));
	});
}


var setupShipping = function(){
	$('form#cart ul#shipping-methods input').bind('click', function(){
		$('.update-button').click();
	});
}
ClemFilter.prototype.filterEm = function(term){
	if(term === 'all' || term === 'size_all'){
		this.items.show();
	}else{
		this.items.filter('.'+ term).show().siblings().not('.'+term).hide();
	}
}




