﻿(function($) {

var PathEvaluator = function(path) {
	this.path = path;
}

$.extend(PathEvaluator.prototype, {
	evaluate: function(object) {
		try {
			return eval("object." + this.path);
		} catch (e) {
			return '';
		}
	}
});

var Template = function(template, pattern) {
	var source = template == null ? '' : String(template);
	this.items = [];
	while (source.length > 0) {
		if (match = source.match(pattern || Template.Pattern)) {
			if (match.index)
				this.items.push(source.slice(0, match.index));
			this.items.push(new PathEvaluator(match[1]));
			source = source.slice(match.index + match[0].length);
		} else
			this.items.push(source), source = '';
	}
};

$.extend(Template.prototype, {
	evaluate: function(object) {
		result = '';
		for(var index = 0, length = this.items.length; index < length; index++) {
			var item = this.items[index];
			result += typeof item == "string" ? item : item.evaluate(object);
		}
		return result;
	}
});

Template.Pattern = /#\{(.*?)\}/;

$.extend({
	template: function(template, pattern) {
		return new Template(template, pattern);
	}
});

$.widget("ui.ajaxIndicator", {
	_init: function() {
		var offset = this.element.offset();
		this._loader = $('<div class="ai" />')
			.addClass(this.options.className)
			.prependTo(document.body)
			.css({
				left: Math.ceil(offset.left + (this.element.width() - 20) / 2) + "px",
				top: Math.ceil(offset.top + (this.element.height() - 20) / 2) + "px",
				zIndex: 999
			});
	},

	destroy: function() {
		$.widget.prototype.destroy.apply(this);
		if (this._loader) {
			this._loader.remove();
			this._loader = null;
		}
	}
});

$.ui.ajaxIndicator.defaults = {
	className: 'icon-loader'
}

$.fn.disabled = function(value) {
	if (value == undefined)
		return this.is('@disabled');
	else
		return this.each(function() {
			if (value)
				$(this).attr('disabled', 'disabled');
			else
				$(this).removeAttr('disabled');
		});
}

})(jQuery);

function formatNumber(value, digits) {
	if (digits > 0)
		if (value != 0) {
			var s = new String(Math.round(value * Number("1e" + digits)));
			if (Math.floor(value) == 0) {
				s = "0" + s;
			}
			var l = s.length;
			return digits > 1 ? s.substring(0, l - digits) + "." + s.substring(l - digits, l) : s;
		} 
		else
			return "0." + new String(Number("1e" + digits)).substr(1, digits);
	else
		return new String(Math.round(value));
}

function printOrder(id) {
	window.open(getPrintOrderUrl(id), 'PrintOrder', 'toolbar=0,scrollbars=1,menubar=1,resizable=1,width=700,height=500');
}

function printRma(id) {
	window.open(getPrintRmaUrl(id), 'PrintRma', 'toolbar=0,scrollbars=1,menubar=1,resizable=1,width=700,height=500');
}

function trackOrder(type, trackNumber) {
	var theForm;
	var theTrackNumber;
	if ((type = type.toLowerCase()) == "airborne") {
		theForm = document.forms["formAirborneTracking"];
		theTrackNumber = theForm["txtTrackNbrs"];
	} else if (type == "ups") {
		theForm = document.forms["formUPSTracking"];
		theTrackNumber = theForm["tracknum"];
	} else if (type == "fedex") {
		theForm = document.forms["formFedexTracking"];
		theTrackNumber = theForm["tracknumbers"];
	} else if (type == "dhl") {
		theForm = document.forms["formDHLTracking"];
		theTrackNumber = theForm["txtTrackNbrs"];
	} else if (type == "us postal service") {
		theForm = document.forms["formUSPSTracking"];
		theTrackNumber = theForm["strOrigTrackNum"];
	} else if (type == "customco") {
		theForm = document.forms["formCustomCoTracking"];
		theTrackNumber = theForm["pronum"];
	} else
		return;
	theTrackNumber.value = trackNumber;
	theForm.submit();
}

function openPopupWindow(path, winid, width, height) {
	window.open(path, winid, 'width=' + width + ',height=' + height + ',resizable=1,scrollbars=1,toolbar=0').focus();
}

function formatSize(size) {
	var value = Math.abs(size);
	if (value < 1024)
		return size + 'B';
	if (value < 1048576)
		return Math.round(size / 102.4, 1) / 10 + 'KB';
	if (value < 1073741824)
		return Math.round(size / 104857.6, 1) / 10 + 'MB';
	return Math.round(size / 107374182.4, 1) / 10 + 'GB';
}

$.widget('ui.progress', {
	_items: {},

	reset: function () {
		this._items = {};
		this.element.hide();
	},

	queue: function (file) {
		this._items[file.id] = { size: file.size, complete: 0, processed: false };
		this._refresh();
		for (var index in this._items)
			return false;
		return true;
	},

	progress: function (file, complete) {
		this._items[file.id].complete = complete;
		this._refresh();
	},

	processed: function (file) {
		this._items[file.id].processed = true;
		this._refresh();
	},

	_init: function () {
		this.reset();
	},

	_refresh: function () {
		var size = 0, complete = 0, total = 0, processed = 0;
		for (var id in this._items) {
			var item = this._items[id];
			size += item.size;
			complete += item.complete;
			total++;
			if (item.processed)
				processed++;
		}
		if (total == processed)
			this.reset();
		else {
			this.options.progress({
				percent: Math.ceil(complete / size * 100) + '%',
				complete: complete,
				size: size,
				total: total,
				processed: processed
			});
			this.element.show();
		}
	}
});

$.ui.progress.defaults = {
	progress: function (metrics) { }
}

function getForgotPasswordUrl() { return "/Account/ForgotPassword.aspx"; }
function getOrderStatusExplanationUrl(id) { return "/Account/OrderStatusExplanation.aspx?id=" + escape(id); }
function getOrderStatusHistoryUrl(id) { return "/Account/OrderStatusHistory.aspx?id=" + escape(id); }
function getPrintOrderUrl(id) { return "/Account/PrintOrder.aspx?id=" + escape(id); }
function getPrintRmaUrl(id) { return "/Account/PrintRma.aspx?id=" + escape(id); }
function getWishListShareUrl() { return "/Account/WishListShare.aspx"; }
function getCartHelpUrl() { return "/Cart/CartHelp.aspx"; }
function getCookiesHelpUrl() { return "/Cart/CookiesHelp.aspx"; }
function getStockStatusUrl() { return "/Cart/StockStatus.aspx"; }
function getSecurityCodeUrl() { return "/Checkout/SecurityCode.aspx"; }
function getRecyclingFeeUrl() { return "/Checkout/RecyclingFee.aspx"; }
function getTaxExemptionUrl() { return "/Checkout/TaxExemption.aspx"; }
function getExpressHelpUrl() { return "/Checkout/ExpressHelp.aspx"; }
function getNotifierUrl(id) { return "/Products/Notifier.aspx?id=" + escape(id); }
function getWriteReviewUrl(id) { return "/Products/WriteReview.aspx?id=" + escape(id); }
function getRebateUrl(id) { return "/Products/Rebate.aspx?id=" + escape(id); }
function getShippingCalcUrl(id) { return "/Products/ShippingCalc.aspx?id=" + escape(id); }
function getRecommendUrl(id) { return "/Products/Recommend.aspx?id=" + escape(id); }
function getImageGalleryUrl(id, index) { return "/Products/ImageGallery.aspx?id=" + escape(id) + "&index=" + escape(index); }
function getCantBuyUrl() { return "/Products/CantBuy.aspx"; }
function getConditionsUrl(id) { return "/Products/Conditions.aspx?id=" + escape(id); }
function getFreeShippingUrl() { return "/Products/FreeShipping.aspx"; }
function getPriceInfoUrl() { return "/Products/PriceInfo.aspx"; }
function getProductsCompareUrl(id) { return "/Products/ProductsCompare.aspx?id=" + escape(id); }
function getResellerRatingsSurveyUrl() { return "http://www.resellerratings.com/survey_popup.pl?seller_id=14568"; }
