
jQuery(function($){

	var previous_color = false, shown_want_it_fast = false;
	
	$('.delivery-date').each(function(){datePickify(this);});
	
	if ($.browser.msie)
		$('a:has(img)').addClass('img');	
	
	function datePickify(el)
	{
		var $this = $(el),
			month = $this.find('select[name$="month]"]'),
			day = $this.find('select[name$="day]"]'),
			year = $this.find('select[name$="year]"]'),
			name = 'jqdate',
			date_format = 'yy-m-d',
			default_date = Date.parse(''+
				parseInt(year.find(':selected').val()) + '-' + 
				parseInt(month.find(':selected').val()) + '-' +
				parseInt(day.find(':selected').val())),
			value;
			
		if (isNaN(default_date) == false)
			default_date = new Date(default_date);
		else
			default_date = new Date();
			
		value = $.datepicker.formatDate(date_format, default_date);
		
		$this.wrapInner('<div class="visuallyhidden"/>');
		$this.append('<div class="jqdate"/>');
		$this.find('.jqdate').datepicker({
			dateFormat: date_format,
			defaultDate: default_date,
			/*buttonText: 'Calendar',
			showOn: 'both',
			showButtonPanel: true,*/
			onSelect: function(dateText, dp) {
				var selected_date = $(this).datepicker('getDate'),
					now = new Date(),
					show_want_it_fast_date = new Date(now.getFullYear(), now.getMonth(), now.getDate()+15);
				$this.find('option:selected').removeAttr('selected');	
				month.find('option[value='+$.datepicker.formatDate('m', selected_date)+']')[0].selected = true;
				day.find('option[value='+$.datepicker.formatDate('d', selected_date)+']')[0].selected = true;
				year.find('option[value='+$.datepicker.formatDate('yy', selected_date)+']')[0].selected = true;
				if (selected_date.getTime() < show_want_it_fast_date.getTime() && !shown_want_it_fast) {
					shown_want_it_fast = true;
					$('#want_it_fast').removeClass('visuallyhidden').dialog({
						'title': 'Rush Orders',
						'width': '500px'
					});
				}
			}
		});		
	}
	
	$('ul li:last-child').addClass('last');

	$('ul.csl li:last-child').each(function() {
		var node = $(this);
		node.html(node.html().replace(/,\s*$/, ''));
	});

	$('.what-next button.add-artwork').click(function() {
		$('.what-next .artwork').removeClass('visuallyhidden');
		$(this).addClass('visuallyhidden');
		return false;
	});
		
	if ($('input[name*="[tmp_name]"]').val()) {
		$('.what-next .artwork').removeClass('visuallyhidden');
		$('.what-next button.add-artwork').addClass('visuallyhidden');
		return false;
	}

	$('button.printable-copy').click(function(){
		window.location = $(this).parent('form').attr('action');
		return false;
	});

	$('ul.faq-qa-set a.question').toggle(
		function() {
			var set = $(this).parent().parent();
			window.location.hash = '#'+set.attr('id').replace('_set', '');
			$(this).siblings().filter('ul.answer').css('display', 'block');
		},
		function() {
			window.location.hash = '';
			$(this).siblings().filter('ul.answer').css('display', 'none');
		}
	);

	if (window.location.hash && window.location.hash.indexOf('#faq_') === 0) {
		var hash = window.location.hash;
		window.location.hash = hash+'_position';
		window.location.hash = hash;
		$(window.location.hash+'_set').find('ul.answer').css('display', 'block');
	}

	$('.product-quote ul.colors a').click(function() {
		var link = $(this);
		
		if (previous_color)	{
			previous_color.removeClass('selected');
		}
		link.find('img').addClass('selected');
		previous_color = link.find('img');

		setPrintQuoteColor(link);
		return false;
	});

	$('input#product_color').each(function(){
		$('.product-quote ul.colors a[href=#'+$(this).val()+']').click();
	});
	
	function setPrintQuoteColor(node)	{
		if (typeof node == 'string' && parseInt(node))
			node = $('.product-quote ul.colors a[href=#'+node+']');

		if (!node || !node.length)
			return;

		$('input#product_color').val(node.attr('href').replace('#', ''));
		$('.product-quote .selected-color .name').html(node.children('img').attr('alt'));
		$('.product-quote .selected-color .color').html(node.html());		
	}

//	$('.product-embroider-quote-calculator button.show-more').click(function(){
//		$('.product-embroider-quote-calculator .show-more-hidden').css('display', 'block');
//		$(this).css('display', 'none');
//		return false;
//	})
//
//	$('.product-quote-calculator button.show-more').click(function(){
//		$('.product-quote-calculator .show-more-hidden').css('display', 'block');
//		$(this).css('display', 'none');
//		return false;
//	})

	$('button.start-over').click(function() {
		window.location = $('input[name=start-over]').val();
		return false;
	});

	$('.colorbox-popup').click(function(){
		var content = $(this).find('.colorbox-popup-hidden-content');
		$.colorbox({
			html:   content.html(),
			width:  content.width(),
			height: content.height()
		});
		return false;
	});

	if (typeof PrintedShirts.alert !== 'undefined')
		$.colorbox({
			html: PrintedShirts.alert,
			width: '500px'
		});
});

