/* origincrafts.com */

(function($) {

	/* CENTER HORIZONTAL NAV */

	$.fn.centerHorizontalNav = function () {
		var s = [	'paddingLeft', 'paddingRight', 'marginLeft', 'marginRight',
					'borderLeftWidth', 'borderRightWidth'];
		// each UL nav
		return this.each(function(){
			var jUl = $(this);
			var ulWidth = jUl.width();
			var liWidth = 0;
			// remove left and right margins for first and last LIs respectively
			var jLis = jUl.find('> li');
			var count = jLis.size();
			jLis.eq(0).css('margin-left','0');
			jLis.eq(count - 1).css('margin-right','0');
			// each LI
			jLis.each(function(){
				var x;
				var jLi = $(this);
				liWidth = liWidth + jLi.width();
				for (var k=0;k<s.length;k++) {
					x = parseInt(jLi.css(s[k]));
					if (!isNaN(x)) { liWidth = liWidth + x; }
				}
			});
			var x = ulWidth - liWidth;
			if (x > 0) {
				//$(this).find('li').eq(0).css('margin-left', parseInt(x / 2) + 'px');
				//$(this).css('padding-left', parseInt(x / 2) + 'px');
				var p = parseInt(jUl.css('padding-left'));
				p = isNaN(p) ? 0 : p;
				p = parseInt((x / 2) + p);
				$(this).css(	{	'padding-left'	: p + 'px',
									'width'			: 'auto'});
			}
		});
	};

	/* DEFAULT INPUT TEXT */
	
	$.fn.awpDefaultInputText = function () {
		return this.each(function(){
			$(this)
				.data('colorActive', $(this).css('color'))				// save focus color
				.blur(function(){										// do blur
					var val = $(this).val();
					if (!(typeof val === 'string' && val.length > 1)) {	// set default if not value
						$(this).val($(this).metadata().text);			// set blur text
					}
					$(this).css('color', $(this).metadata().color)		// set blur color
				})
				.focus(function(){										// do focus
					$(this).css('color', $(this).data('colorActive'));	// set focus color
					if ($(this).val() == $(this).metadata().text) {
						$(this).val('');
					}
				})
				.trigger('blur');										// init
		});
	};

})(jQuery);

function awpUpdatePrice () {

	var	price		= $(".addtocart-form .item-baseprice").val(),
		quantity	= $(".addtocart-form .quantity-input").val(),
		selectElems, selectValue, selectPriceStart, selectPriceEnd, selectPrice;

	/*if ((typeof quantity == 'undefined') || (quantity == '') || (quantity.indexOf(' ') >= 0)) {
		quantity = 0;
	}*/

	quantity = quantity * 1;
	quantity = isNaN(quantity) ? 0 : quantity;
	
	selectElems = $(".order-option-select").get();
	for (var i=0;i<selectElems.length;i++) {
		if (selectElems[i].selectedIndex != -1) {
			selectValue = selectElems[i].options[selectElems[i].selectedIndex].value;
			selectPriceStart = selectValue.indexOf("(+");
			if ((selectPriceStart != -1) && (selectValue.indexOf("(+0") == -1)) {
				selectPriceEnd = selectValue.indexOf(")", selectPriceStart);
				if (selectPriceEnd != -1) {
					selectPrice = selectValue.substring(selectPriceStart + 2, selectPriceEnd);
					price = (price * 1) + (selectPrice * 1);
				}
			}
		}
	}

	price = price * quantity;
	$("#total-price .optionvalue")
		.hide()
		.text(awpFormatCurrency(price))
		.show(300);
	//$("#total-price .optionvalue").text(awpFormatCurrency(price));
	/*$("#total-price .optionvalue")
		.hide(250, function(){
			$(this)
				.text(awpFormatCurrency(price))
				.show(250);
		});*/
		

}

function awpFormatCurrency (priceAmount) {
	var price;
	if (isNaN(priceAmount)) {return '';}
	price = new Number(priceAmount);
	return '$' + price.toFixed(2);
}

$(document).ready(function() {

	if ($(".addtocart-form .item-baseprice").size() > 0) {
		$(".addtocart-form .quantity-input")
			.change(awpUpdatePrice)
			.bind("keyup", awpUpdatePrice);
		$(".addtocart-form .order-option-select")
			.change(awpUpdatePrice);
		// init
		$(".addtocart-form .quantity-input")
			.trigger('keyup');			
	} else {	
		$("#total-price").hide();
	}
		
	// CENTER HORIZONTAL NAV UL
	$('.top-nav').centerHorizontalNav();
	$('#footer-nav').centerHorizontalNav();
	
	// DEFAULT INPUT TEXT
	//$(me.settings.defaultInputTextSel).awpDefaultInputText();
	
	// FLY MENU
	
	$('.top-nav').flymenu({
		'hideDelay': 20,
		'switchDelay': 20,
		'showDelay': 50
	});

	$('.imageviewer').imageviewer({
		'transitionTime'	: 400,
		'closeText'			: 'close',
		'overlayCss'		: {'backgroundColor':'#000'},
		'containerVertical'	: 'top',
		'topMargin'			: 20
	})

	$.awpPaging({ /* v5 */
		'onDone'					: null,
		'itemsPerPage'				: 12,
		'numPageControls'			: 6,
		'progressTimeout'			: 400,
		'viewallLimit'				: false,	// 0 : always confirm
												// > 0 : confirm only when num items exceeds value
												// else no confirm
		'dataDef'					: [
			// id
			{	'sel'		: '.vwitem',
				'kind'		: 'value-attr'},
			// name
			{	'sel'		: '.name-link',
				'kind'		: 'html',
				'sort'		: 'alpha',
				'name'		: 'name'},
			// description
			{	'sel'		: '.detail',
				'kind'		: 'html'},
			// price
			{	'sel'		: '.prices',
				'kind'		: 'html'},
			// url
			{	'sel'		: '.name-link, .image-link',
				'kind'		: 'href-attr'},
			// image url
			{	'sel'		: '.image img',
				'kind'		: 'src-attr'},
			// code
			{	'sel'		: '.code .value',
				'kind'		: 'text'},
			// availability message
			{	'sel'		: '.availability',
				'kind'		: 'html'},
			// out-of-stock message
			{	'sel'		: '.outofstock',
				'kind'		: 'html'},
			// price sort
			{	'sel'		: '',
				'kind'		: 'none',
				'sort'		: 'numeric',
				'name'		: 'price'}
		]
	});

	$('#contact-us-form').validate({
		'rules': {
			'FirstName'				: {	'required'		: true,
										'maxlength'		: 99},
			'LastName'				: {	'required'		: true,
										'maxlength'		: 99},
			'InquiryText'			: {	'required'		: true,
										'maxlength'		: 1000},
			'email'					: {	'required'		: true,
										'email'			: true}
		},
		'messages': {
			'FirstName'				: 'Please enter the first name',
			'LastName'				: 'Please enter the last name',
			'InquiryText'			: 'Please enter the message or question',
			'email'					: 'Please enter a valid email address'
		}
	});
	
	$('#email-form form').validate({
		'rules': {
			'email22'				: {	'required'		: true,
										'email'			: true}
		},
		'messages': {
			'email22'				: 'Please enter a valid email address'
		}
	});

});

