$(function() {
	$.fn.hasAttr = function(attribute){
		return (typeof attribute !== 'undefined') && (attribute !== false);
	};
	
	$('body').addClass('javascript-enabled');
	
	if (screen.width <= 480) {
		$.fx.off = true;
	}
	
	$('.header-nav-item dl dt').each(function(){
		$(this).html($(this).html());
	});
	
	// navigation menus
	$('.header-nav-item dl dd').parent('dl').addClass('header-nav-dropdown').hoverIntent(function(){
		$(this).addClass('header-nav-hover').children('dd').show();
		if ($('body').hasClass('ie6')){
			$(this).children('dd').css({
				'top': $(this).offset().top-$('#header-block').offset().top+20,
				'left': $(this).offset().left-$('#header-block').offset().left-10
			});
		}
	}, function(){
		$(this).removeClass('header-nav-hover').children('dd').hide();
	});
	
	// tooltip
	$.fn.T = function(){
		return this.each(function(){
			$(this).mouseover(function(){
				$('#dss-tooltip').remove();
				$('body').append('<div id="dss-tooltip">' + $(this).attr('title') + '</div>');
				$(this).attr('title', '');
			}).mousemove(function(e){
				$('#dss-tooltip').css({
					'left': e.pageX+20,
					'top': e.pageY
				}).show();
			}).mouseout(function(){
				$(this).attr('title', $('#dss-tooltip').text());
				$('#dss-tooltip').remove();
			});
		});
	};
	
	$.fn.WP = function(){
		return this.each(function(){
			$('#payinvoice-invoices').append('<div class="x-field x-field-full"><input class="dss-btn dss-btn-primary" id="add-more" type="button" value="Add another invoice"></div><div class="x-field x-field-full"><em>Total Amount:</em> $ <input class="dss-input dss-input-text dss-input-disabled" readonly="readonly" id="payinvoice-total" type="text" value="0.00"></div>');
			$.fn.WP.settotal();
			$('input[name^="paymentAmt"]').live('blur', function(){
				$.fn.WP.settotal();
			});
			$('#add-more').click(function(){
				var _invoiceCount = parseInt($('input[name^="invoiceNum"]').length, 10) + 1;
				var _html = '';
				_html += '<table class="x-fieldsubset" border="0" cellpadding="0" cellspacing="0" width="100%">';
				_html += '<tr>';
				_html += '<th class="x-label"><label for="invoiceNum-' + _invoiceCount + '">Invoice Number:</label></th>';
				_html += '<td class="x-field"><input class="dss-input dss-input-text" id="invoiceNum-' + _invoiceCount + '" maxlength="50" name="invoiceNum[' + _invoiceCount + ']" size="30" type="text" value=""></td>';
				_html += '</tr>';
				_html += '<tr>';
				_html += '<th class="x-label"><label for="paymentAmt-' + _invoiceCount + '">Payment Amount:</label></th>';
				_html += '<td class="x-field">$ <input class="dss-input dss-input-text" id="paymentAmt-' + _invoiceCount + '" maxlength="20" name="paymentAmt[' + _invoiceCount + ']" size="20" type="text" value=""></td>';
				_html += '</tr>';
				_html += '</table>';
				$(this).parent('div').before(_html);
			});
		});
	};
	
	$.fn.WP.settotal = function(){
		var _total = 0;
		$('input[name^="paymentAmt"]').each(function(){
			if ($(this).val().length > 0) {
				_total += parseFloat($(this).val());
			}
		});
		if (isNaN(_total)) {
			_total = 'ERROR';
		} else {
			_total = _total.toFixed(2)
		}
		$('#payinvoice-total').val(_total);
	};
	
	$.fn.FCA = function(){
		return this.each(function(){
			var _isIE6 = $('body').hasClass('ie6');
			if ($('#fca').length) {
				$('a#home-link').wrap('<div id="home-link"></div>').find('img').unwrap();
				if (!$('#fca').is('span')) {
					$('div#home-link').tooltip({
						bounce: true,
						delay: 100,
						direction: 'down',
						effect: _isIE6 ? 'toggle' : 'slide',
						events: { def: 'mouseover click,mouseout' },
						offset: _isIE6 ? [0, 0] : [-10, 0],
						position: 'bottom center',
						predelay: 100,
						relative: true,
						tip: $('#fca')
					});
				}
			}
		});
	};
	
	// QO
	$.fn.QO = function(){
		return this.each(function(){
			$(':text', this).focus(function(){$.fn.QO.clear($(this));}).blur(function(){$.fn.QO.reset($(this));});
		});
	};
	$.fn.QO.clear = function($this){
		var _placeholder = $this.attr('placeholder');
		if ($this.val() == _placeholder) {
			$this.val('');
		}
		$this.addClass('quickorder-focus');
	};
	$.fn.QO.reset = function($this){
		var _placeholder = $this.attr('placeholder');
		if ($this.val() == '') {
			$this.val(_placeholder).removeClass('quickorder-focus');
		}
	};
	$('#quickorder-block').QO();
	(function($){
		var proto = $.ui.autocomplete.prototype;
		$.extend(proto, {
			_renderItem: function(ul, item){
				return $('<li/>').data('item.autocomplete', item).append($('<a></a>').append($('<img>', {
					'alt': item.value,
					'height': '50',
					'src': item.img.length ? '/pcat/productImages/' + item.img + '-1-50.jpg' : '/pcat/images/clear.gif',
					'width': '50'
				}).after($('<div/>', {
					'class': 'autocomplete-sku'
				}).append('<strong>' + item.value.substr(0, this.term.length) + '</strong>' + item.value.substr(this.term.length))).after($('<div/>', {
					'class': 'autocomplete-desc'
				}).append(item.desc)))).appendTo(ul);
			},
			_search: function(value){
				this.pending++;
				this.element.addClass('ui-autocomplete-loading');
				this.source({term: value.toLowerCase()}, this.response); // case-insensitive
			}
		});
	})(jQuery);
	$('#quickorder-sku').autocomplete({
		position: { my: 'right top', at: 'right bottom', of: $('#quickorder-submit') },
		select: function(e, ui) {
			e.preventDefault();
			$(this).val(ui.item.value);
			$('#quickorder-qty').focus();
		},
		source: '/pcat/quickorder-autocomplete.php'
	});
	
	// feedback
	$.fn.F = function(){
		return this.each(function(){
			if ($(this).hasClass('ie6')) {
				$('#footer-block').append('<div id="feedback" class="feedback-min"><h3>Feedback</h3></div>');
			} else {
				$(this).append('<div id="feedback" class="feedback-min"><h3>Feedback</h3></div>');
			}
			$('.feedback-min').live('click', function(){
				$feedback = $(this);
				if (!$('button', $feedback).length) {
					$('p', $feedback).remove();
					$feedback.append('<img alt="close" id="feedback-close" src="/themes/close.gif"><p class="dss-p dss-medium">We strive to provide you with the best shopping experience possible. Please let us know how we\'re doing. While we will be unable to respond to your comments directly, your input will help improve Delasco.com.</p><p class="dss-p dss-medium">If you have a question or need assistance, please contact Customer Service:</p><p class="dss-p dss-medium">Email: <a href="mailto:questions@delasco.com" target="_blank">questions@delasco.com</a></p><p class="dss-p dss-medium">Phone: (800) 831-6273 or (712) 323-3269</p><p class="dss-p dss-p-center"><textarea class="dss-input dss-input-textarea"></textarea></p><p class="dss-p dss-p-center dss-medium"><button class="dss-btn dss-btn-primary" type="button" value="Submit Feedback"><span>Submit Feedback</span></button></p>');
				}
				$feedback.addClass('feedback-max').removeClass('feedback-min').find('textarea').focus();
			});
			$('#feedback-close').live('click', function(){
				$('.feedback-max').addClass('feedback-min').removeClass('feedback-max').height('auto');
			});
			$('.feedback-max button').live('click', function(){
				$feedback = $('#feedback');
				$feedback.css('height', $feedback.height());
				var _message = $('textarea', $feedback).val();
				if ($.trim(_message).length > 0) {
					$feedback.addClass('feedback-loading');
					$('p', $feedback).remove();
					$.ajax({
						type: 'POST',
						url: '/pcat/xt_marketing_feedback.php',
						data: 'feedback=' + _message,
						success: function(){ $.fn.F.thanks(); },
						error: function(){ $.fn.F.thanks(); }
					});
				}
			});
		});
	};
	$.fn.F.thanks = function(){
		$('#feedback').removeClass('feedback-loading').append('<p class="dss-p dss-p-center dss-medium">Thank you for your feedback!</p>');
	};
	$('body').F().FCA();
});
