function SetMenu(id) {
	var menuitem = $('.nav li #' + id);
	if (menuitem)
		menuitem.parent().addClass('selected');
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

var pricePromo = function(props) {
	this.startDate = null;
	this.endDate = null;
	this.departments = null;
	this.expr = null;
	this.priceOff = 0.0;
	this.percentOff = 0.0;
	this.description = null;
	this.note = null;
	
	this.getStartDate = function() {
		return this.getDate(this.startDate);
	}
	this.getEndDate = function() {
		return this.getDate(this.endDate);
	}
	this.getDate = function(date) {
		return date == null ? null : new Date(date);
	}
	this.init = function(props) {
		var promo = this;
		for (var prop in props)
			promo[prop] = props[prop];
	}

	if (props)
		this.init(props);
}
var pricePromos = new Array();
function ApplyPricePromos() {
	this._dept = null;
	this.GetDepartment = function() {
		if (!this._dept) {
			var url = window.location.href;
			var regex = new RegExp('https?://([^/]+)/([^/]+)?/?.*');
			var match = regex.exec(url);
			if (match.length < 3 || match[2] === undefined)
				this._dept = 'home';
			else
				this._dept = match[2].toLowerCase();
		}
		return this._dept;
	}
	this.ApplyPromo = function(promo) {
		var priceElement = $('.itemprice');
		var dept = this.GetDepartment();
		var deptok = true;
		if (promo.departments && promo.departments.length > 0) {
			deptok = false;
			for (var i = 0; i < promo.departments.length; i++)
				if (promo.departments[i] == dept) {
					deptok = true;
					break;
				}
		}
		if (deptok && priceElement.length > 0)
			priceElement.each(function() {
				var el = $(this);
				var price = parseFloat(el.text().replace('$', ''));
				if (price && (price > 0.0) && (promo.priceOff || promo.percentOff)) {
					el.css('text-decoration', 'line-through');
					el.css('color', '#cc0000');
					var newPrice = promo.priceOff > 0.0 ? (price - promo.priceOff) : (Math.round(price * (100 - promo.percentOff)) / 100);
					var snewPrice = newPrice + '';
					if (snewPrice.indexOf('.') == -1)
						snewPrice += '.';
					while (snewPrice.indexOf('.') > snewPrice.length - 3)
						snewPrice += '0';
					el.after('&nbsp;<b' + (promo.description ? (' title="' + promo.description.replace('"', '&quot;') + '"') : '') + '>$' + snewPrice +
						(promo.note ? (' (' + promo.note + ')') : '') + '</b>');
				}
			});
	}
	var now = new Date();
	for (var i = 0; i < pricePromos.length; i++) {
		var promo = pricePromos[i];
		if (!promo.startDate || (promo.getStartDate() <= now))
			if (!promo.endDate || (promo.getEndDate() > now)) {
				this.ApplyPromo.apply(this, [promo]);
				break;
			}
	}
}
$(document).ready(function() {
	var isdeptspage = $('#depts').length != 0;
	if (isdeptspage)
		$('#depts div.item.newstyle').each(function(i, e) {
			var elm = $(e);
			elm.append('<div class="new">new styles</div>');
		});
	else {
		$('div.catalogitems > div.item.newstyle').each(function(i, e) {
			var elm = $(e);
			var newfor = elm.attr('newfor');
			var txt = newfor ? ('new for ' + newfor) : 'new style';
			elm.append('<div class="new">' + txt + '</div>');
		});
		$('div.catalogitems > div.item.popularstyle:not(.newstyle)').append('<div class="popular">popular style</div>');
		$('div.catalogitems > div.item.clearance').append('<div class="clearance">clearance</div>').find('a:not(.cartlink)').attr('href', '/sale/');
		$('div.catalogitems > div.item.clearance > div.clearance').click(function(){location.href='/sale/';});
	}
});
