$.fn.vAlign = function(){
	return this.each(function(i){
		var ah = $(this).height();
		var ph = $(this).parent().height();
		var mh = (ph - ah) / 2;
		$(this).children('img').css('margin-top', mh);
	});
};

$.fn.input = function() {
	return this.focus(function() {
		if( this.value == this.defaultValue ) {
			$(this).val("");
			$(this).addClass("focus");
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
			$(this).removeClass("focus");
		}
	});
};

$.fn.digits = function(){ 
    return this.each(function(){ 
        $(this).text( $(this).text().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,") ); 
    })
};

$(document).ready(function(){
	$('body').addClass('has-js');
	
	$(window).scroll(function(){
		var pixelsScrolled = $(window).scrollTop();
		$('#social-links').css('top', 24 - pixelsScrolled);
		$('#sales-callout, #cart-tools').css('top', 30 - pixelsScrolled);
	});
	
	$('#cmnt-form input, #cmnt-form textarea, .order input, .expires input').input();
	
	$('#checkout-form input').focus(function(){$(this).prev('label').addClass('active');});
	
	$('#checkout-form input').change(function(){$(this).prev('label').addClass('active');});
	
	$('#checkout-form input').blur(function(){
		var value = $(this).val();
		
		if(value == ''){$(this).prev('label').removeClass();}
	});
	
	$('#detail-thumbs a').click(function(e){
		e.preventDefault();
		var thumbClicked = $(this).parent().index() + 1;
		$('#detail-pics li.active').removeClass().css('display','none');
		$("#detail-pics li:nth-child("+thumbClicked+")").fadeIn().addClass('active');
	});
	

	jQuery.validator.addMethod('defaultInvalid', function(value, element) {
		return value != element.defaultValue;
	}, '');

	$("#comment_form").validate({
		rules: {
			name: 'defaultInvalid',
			email: {email: true},
			comment: 'defaultInvalid',
			captcha: 'defaultInvalid'
		},
		onkeyup: false,
		invalidHandler: function(e, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				var message = errors == 1
					? 'You missed 1 field. It has been highlighted below'
					: 'You missed ' + errors + ' fields.  They have been highlighted below';
				$("span#error-msg").html(message);
				$("span#error-msg").fadeIn('fast').css('display','block');
			} else {$("span#error-msg").hide();}
		}
	});
	
	$("#add-to-cart").validate({
		rules: {
			quantity: 'defaultInvalid'
		},
		onkeyup: false,
		invalidHandler: function(e, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				var message = errors == 1
					? 'You missed 1 field. It has been highlighted above'
					: 'You missed ' + errors + ' fields.  They have been highlighted above';
				$("span#order-err").html(message);
				$("span#order-err").fadeIn('fast').css('display','block');
			} else {$("span#order-err").hide();}
		}
	});
	
	$("#checkout-form").validate({
		rules: {
			expiration_year: {digits: true, minlength: 4},
			expiration_month: {digits: true, minlength: 2},
			zip: {digits: true},
			shipping_zip: {digits: true},
			email_address: {email: true},
			shipping_email: {email: true}
		},
		onkeyup: false,
		invalidHandler: function(e, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				var message = errors == 1
					? 'You missed 1 field. It has been highlighted above'
					: 'You missed ' + errors + ' fields.  They have been highlighted above';
				$("span#order-err").html(message);
				$("span#order-err").fadeIn('fast').css('display','block');
			} else {$("span#order-err").hide();}
		}
	});
	
/*	var picsHeight = 1000;
	
	$('#detail-pics li').each(function(){
		var thisHeight = $(this).height();
		
		if(thisHeight < picsHeight){
			picsHeight = thisHeight;
			$('#detail-pics, #detail-pics a').height(picsHeight);
		}
	});
	
	/*$('#furniture-detail #detail-pics li img').each(function(){
		var listHeight = $('#detail-pics').height();
		var thisHeight = $(this).height();
		
		if(thisHeight > listHeight){
			var moveMe = - (thisHeight / 2)
			$(this).css('top',moveMe)
		}
	});
	
	$('#furniture-detail #detail-pics li').vAlign();*/
	
	$('#furniture-detail #detail-pics li').each(function(){
		var picHeight = $(this).children('img').height();
		
		if(picHeight < 400){
			$(this).children('a').css('backgroundPositionY',(picHeight - 35));
		}
	});
	
	$('#detail-pics li + li').css('display','none');
	
	$('#detail-pics a, #cart td div a').colorbox({
		previous: 'prev'
	});
	
	$('#shipping-btn, #tax-btn').colorbox({
		height: 400,
		iframe: true,
		width: 500
	});
	
	$('div.captcha').each(function(){
		var captchaWidth = $('div.captcha').width();
		var imgWidth = $(this).children('label').width();
		
		$(this).children('input').width((captchaWidth - imgWidth - 13))
	});
	
	$('#reps #go-btn').click(function(e){
		e.preventDefault();
		
		var area = $('select').val();
		
		if(area != '' && area != 'canada' && area != 'united-states'){
			document.location.href = '/reps/' + area;
		}
	});
	
	var area = jQuery.url.segment(1);
	
	$('#area option[value="'+area+'"]').attr('selected', 'selected');
	
	// SELECT FURNITURE OPTIONS
	$(document).click(function(){$('.options-list').hide();});
	$('.option-btn').click(function(e){	
		e.preventDefault();
		$('.options-list').hide();
		$(this).next().show();
		return false;
	});
	
	$('.options-list a').click(function(e){
		e.preventDefault();
		selected = $(this).text();
		number = $(this).parent().index() + 2;
		priceString = $(this).children('span').text();
		price = Number(priceString.replace(/[^0-9\.]+/g,""));
		$(this).parent().parent().hide();
		$(this).parent().parent().prev().text(selected).addClass('filled');
		$(this).parent().parent().parent().children('select').children('option:nth-child('+number+')').attr('selected','selected');
		if(price > 0){
			oldString = $('.order h4 strong').text();
			oldPrice = Number(oldString.replace(/[^0-9\.]+/g,""));
			newPrice = price + oldPrice;
			$('#subtotal').text('$' + newPrice);
			$("#subtotal").digits();
		}
		zoomImg = $(this).attr('data-zoom');
		newImg = $('#detail-pics a[data-zoom="' + zoomImg + '"]').length;
		if(newImg == "1"){
			$('#detail-pics .active').removeClass('active').hide();
			$('#detail-pics a[data-zoom="' + zoomImg + '"]').parent().fadeIn().addClass('active');
		}
	});
	
	$('#tax-state, #shipping-option').change(function(){$('#update-form').submit();});
	
	// FILL SHIPPING INFO
	$('#checkout-form input').each(function(){
		value = $(this).val();
		if(value != ""){$(this).prev().addClass('active');}
	});
	
	$('#same input').change(function(){
		billing_fname = $('#billing-fname').val();
		billing_lname = $('#billing-lname').val();
		billing_email = $('#billing-email').val();
		billing_phone = $('#billing-phone').val();
		billing_adr = $('#billing-adr').val();
		billing_apt = $('#billing-apt').val();
		billing_city = $('#billing-city').val();
		billing_state = $('#billing-state').val();
		billing_zip = $('#billing-zip').val();
		billing_country = $('#billing-country').val();
	
		if($(this).is(':checked')){
			if(billing_fname != ''){$('#shipping-fname').val(billing_fname).prev().addClass('active');}
			if(billing_lname != ''){$('#shipping-lname').val(billing_lname).prev().addClass('active');}
			if(billing_email != ''){$('#shipping-email').val(billing_email).prev().addClass('active');}
			if(billing_phone != ''){$('#shipping-phone').val(billing_phone).prev().addClass('active');}
			if(billing_adr != ''){$('#shipping-adr').val(billing_adr).prev().addClass('active');}
			if(billing_apt != ''){$('#shipping-apt').val(billing_apt).prev().addClass('active');}
			if(billing_city != ''){$('#shipping-city').val(billing_city).prev().addClass('active');}
			if(billing_state != ''){$('#shipping-state').val(billing_state).prev().addClass('active');}
			if(billing_zip != ''){$('#shipping-zip').val(billing_zip).prev().addClass('active');}
			if(billing_country != ''){$('#shipping-country').val(billing_country).prev().addClass('active');}
		} else {
			$('#shipping-fname').val('').prev().removeClass('active');
			$('#shipping-lname').val('').prev().removeClass('active');
			$('#shipping-email').val('').prev().removeClass('active');
			$('#shipping-phone').val('').prev().removeClass('active');
			$('#shipping-adr').val('').prev().removeClass('active');
			$('#shipping-apt').val('').prev().removeClass('active');
			$('#shipping-city').val('').prev().removeClass('active');
			$('#shipping-state').val('').prev().removeClass('active');
			$('#shipping-zip').val('').prev().removeClass('active');
			$('#shipping-country').val('').prev().removeClass('active');
		}
	});
	
	// PRINT RECEIPT
	$('#print-btn').click(function(e){
		e.preventDefault();
		window.print();
	});
	
	if ($.browser.msie) {
		$('#specs li p:last-child').addClass('last');
		
		var zIndexNumber = 10;
		$('.options > li').each(function(){
			$(this).css('z-index',zIndexNumber);
			zIndexNumber -= 1;
		});
	}

});
