﻿/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Common Functions
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
function Insert(menu, option) {
	var x = document.createElement('option');
	if (option.value) {
		x.value = option.value;
		x.text = option.text;
	}
	else {
		x.value = option;
		x.text = option;
	}
	try { menu.add(x, null); } // standards compliant
	catch (ex) { menu.add(x); } // IE only
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
function setCookie(c_name, value, expiredays) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = c_name + "=" + escape(value) +
      ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
function getCookie(c_name) {
	if (document.cookie.length > 0) {
		c_start = document.cookie.indexOf(c_name + "=");
		if (c_start != -1) {
			c_start = c_start + c_name.length + 1;
			c_end = document.cookie.indexOf(";", c_start);
			if (c_end == -1) c_end = document.cookie.length;
			return unescape(document.cookie.substring(c_start, c_end));
		}
	}
	return "";
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
function AddOnload(myfunc) {
	if (window.addEventListener)
		window.addEventListener('load', myfunc, false);
	else if (window.attachEvent)
		window.attachEvent('onload', myfunc);
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
function GetImageUrl(root, id, type, name) {
	return root + 'a' + Math.floor((id / 1000)) + '000/type' + type + '/' + id + '_' + name;
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
function GetMonth(month) {
	switch (month) {
		case 1: return 'Jan'; break;
		case 2: return 'Feb'; break;
		case 3: return 'Mar'; break;
		case 4: return 'Apr'; break;
		case 5: return 'May'; break;
		case 6: return 'Jun'; break;
		case 7: return 'Jul'; break;
		case 8: return 'Aug'; break;
		case 9: return 'Sep'; break;
		case 10: return 'Oct'; break;
		case 11: return 'Nov'; break;
		case 12: return 'Dec'; break;
	}
	return '';
}
//var _key = '';
//var _swf = new Array();
//document.write = function(txt) { _swf[_key] = txt; }
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
function Ad(args) {
	this.adId = args.id;
	this.monthTwoDigit = args.mt;
	this.yearTwoDigit = String(args.yr).substring(2, 4);
	this.month = GetMonth(args.mt);
	this.year = args.yr;
	//this.fiveStarRating = args.rt;
	this.commentCount = args.cc;
	this.isNew = args.nw;
	this.aspectRatio = args.ar;
	this.imagePreview = args.ip;
	this.attributes = args.ty;
	//this.unrated = 'http://i.americanapparel.net/storefront/ratings/images/en/rating_notrated.gif';
	//this.unrated = 'http://i.americanapparel.net/storefront/ratings/images/rating_00stars.gif';  
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	this.renderFile = function (url, w) {
		var i = url.indexOf('.swf');
		var h = Math.round(w / this.aspectRatio);
		return (i == url.length - 4
    ? "<div style='postion:relative;z-index:-10' >"
    + '<object classid="clsid:D27CDB6E-AE6D-11CF-96B8-444553540000" id="obj' + this.adId + '" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" border="0" width="' + w + '" height="' + h + '">'
    + '<param name="movie" value="' + url + '">'
    + '<param name="quality" value="High">'
    + '<param name="wmode" value="transparent">'
    + '<embed height="' + h + '" width="' + w + '" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" quality="high" src="' + url + '" wmode="transparent"></embed></object>'
    + '</div>' + '<img alt="" style="width:' + w + 'px; height:' + h + 'px;float:z-index:10; left;position:relative;top:-' + h + 'px;" src="http://www.americanapparel.net/storefront/images/overlays/clear.gif" />'
    + (this.isNew ? '<img alt="" style="float:left;position:relative;top:-' + (h * 2) + 'px;" src="http://i.americanapparel.net/storefront/images/overlays/en/slashNew.gif" />' : '')
    : '<img class="thumb" src="' + url + '" />'
    + (this.isNew ? '<img alt="" style="float:left;position:relative;top:-' + (h + 2) + 'px;" src="http://i.americanapparel.net/storefront/images/overlays/en/slashNew.gif" />' : '')
    );
	}
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	this.render = function (root, query, type, width, rowHeight, isFirstCell, isLastCell, isFirstRow) {

		var lnk = 'ad.aspx?i=' + this.adId + (query ? query : '');
		var url = GetImageUrl(root, this.adId, 1, this.imagePreview);
		url = url.replace("dev", "www");

		var type_ = '';
		if (this.attributes) {
			switch (this.attributes) {
				case 9:
					type_ = '<img src="http://i.americanapparel.net/images/icons/advert_04.gif" />';
					break;
				case 10:
					type_ = '<img src="http://i.americanapparel.net/images/icons/advert_01.gif" />';
					break;
				case 11:
					type_ = '<img src="http://i.americanapparel.net/images/icons/advert_03.gif" />';
					break;
			}
			//for (var i = 0; i < this.attributes.length; i++) {
			//    types += type['' + this.attributes[i]];
			//}
		}

		var html = "";
		if (isFirstCell) {
			html += '<div style="width: 558px; clear: both;';

			if (!isFirstRow) {
				html += "padding-top: 30px;";
			}

			html += '">';
		}

		html += '<div style="float: left;">'
           + '<div style="float: left;height:' + (rowHeight + 6) + 'px;" ><a onclick="javascript:return _page.saveState()" href="' + lnk + '">'
           + this.renderFile(url, width)
           + '</a></div>'
           + '           <div style="padding-top: 10px; width: 135px;clear:both;">'
           + '              <div style="float: left; width: 45px;">'
           + this.monthTwoDigit + '/' + this.yearTwoDigit
           + '                 </div>'
           + '                 <div style="float: left; width: 40px; margin-top: 2px;">'
           + type_
           + '                 </div>'
           + '                 <div>'
           + '                     <div style="float: left; padding-right: 3px;">'
           + '                         <a class="rateLink" onclick="return _page.saveState()" href="' + lnk + '" >' + this.commentCount + '</a></div>'
           + '                    <div style="float:left;padding-top: 2px;">'
           + '                         <img src="http://i.americanapparel.net/images/icons/advert_02.gif"></div>'
           + '                 </div>'
           + '             </div>'
           + '</div>'

		if (isLastCell) {
			html += '</div>';
		}

		return html;
	}
	/*

	return ''
	+ '<div class="adbox"' + (isRowStart ? 'style="margin-right:1px;margin-left:11px;"' : '') + '>'
	+ '  <div class="inner" style="height:' + (rowHeight + 6) + 'px;" ><a onclick="javascript:return _page.saveState()" href="' + lnk + '">'
	+ this.renderFile(url, width)
	+ '  </a></div>'
	+ '  <div class="inner">'
	+ '    <div class="top">'
	+ '      <div class="adDate">' + this.month + '. ' + this.year + '</div>'
	//    + '      <div class="adAttributes">' + types + '</div>'
	+ '    </div>'
	+ '    <div class="bot">'
	+ '      <img class="rateBubb" src="http://i.americanapparel.net/storefront/ratings/images/bubble_white.gif" />'
	+ '      <a class="rateLink" onclick="return _page.saveState()" href="' + lnk + '" >' + this.commentCount + '</a>'
	+ '    </div>'
	+ '  </div>'
	+ '</div>';
	}
  
	*/
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Index Page
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
function Pagination(itemsPerPage) {
	this.ITEMS_PER_PAGE = (itemsPerPage ? itemsPerPage : 52);
	this.pageIndex = 0;
	this.totalItems = 0;
	this.totalPages = 0;

	this.fix =
  { className: { live: "livelink navlinks"
               , dead: "deadlink navlinks"
  }
  , prev: { src: { live: "http://i.americanapparel.net/storefront/images/arrows/triangle_left_True.gif"
                 , dead: "http://i.americanapparel.net/storefront/images/arrows/triangle_left_False.gif"
  }
          , onclick: { live: '_page.prev();'
                      , dead: ''
          }
  }
  , next: { src: { live: "http://i.americanapparel.net/storefront/images/arrows/triangle_right_True.gif"
                 , dead: "http://i.americanapparel.net/storefront/images/arrows/triangle_right_False.gif"
  }
          , onclick: { live: '_page.next();'
                      , dead: ''
          }
  }

  };

	this.navi = [
  { stat: "divHeadPage"
  , prev: { div: "divHeadPrev", img: "imgHeadPrev" }
  , next: { div: "divHeadNext", img: "imgHeadNext" }
  },
  { stat: "divFootPage"
  , prev: { div: "divFootPrev", img: "imgFootPrev" }
  , next: { div: "divFootNext", img: "imgFootNext" }
  }
  ];

	this.current = { prev: { className: '', src: '' }
                 , next: { className: '', src: ''}
	};
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	this.init = function (itemCount) {
		this.pageIndex = 0;
		this.totalItems = itemCount;
		this.totalPages = Math.ceil(this.totalItems / this.ITEMS_PER_PAGE);

		if (this.totalItems > 1) {
			for (var i = 0; i < this.navi.length; i++) {
				document.getElementById(this.navi[i].stat).style.display = 'block';
				document.getElementById(this.navi[i].prev.div).style.display = '';
				document.getElementById(this.navi[i].next.div).style.display = '';
				document.getElementById(this.navi[i].prev.img).style.display = '';
				document.getElementById(this.navi[i].next.img).style.display = '';
			}
		}

	}
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	this.step = function (delta) {
		if (this.totalPages > 0) {
			this.pageIndex += delta;
			this.pageIndex = (this.pageIndex < 1 ? 0
                        : (this.pageIndex >= this.totalPages - 1
                          ? this.totalPages - 1
                          : this.pageIndex));
		}
		else {
			this.pageIndex = 0;
		}

		if (this.pageIndex > 0) {
			this.current.prev.className = this.fix.className.live;
			this.current.prev.src = this.fix.prev.src.live;
		}
		else {
			this.current.prev.className = this.fix.className.dead;
			this.current.prev.src = this.fix.prev.src.dead;
		}

		if (this.pageIndex >= this.totalPages - 1) {
			this.current.next.className = this.fix.className.dead;
			this.current.next.src = this.fix.next.src.dead;
		}
		else {
			this.current.next.className = this.fix.className.live;
			this.current.next.src = this.fix.next.src.live;
		}

		var pg = this.pageMinMax();
		var msg = (pg.min + 1) + ' - ' + pg.max + ' of ' + this.totalItems;
		for (var i = 0; i < this.navi.length; i++) {
			document.getElementById(this.navi[i].stat).innerHTML = msg;
			document.getElementById(this.navi[i].prev.div).className = this.current.prev.className;
			document.getElementById(this.navi[i].next.div).className = this.current.next.className;
			document.getElementById(this.navi[i].prev.img).src = this.current.prev.src;
			document.getElementById(this.navi[i].next.img).src = this.current.next.src;
		}
	}
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	this.pageMinMax = function () {
		var i = (this.pageIndex * this.ITEMS_PER_PAGE);
		var max = i + this.ITEMS_PER_PAGE;
		max = (max > this.totalItems ? this.totalItems : max);
		return { min: i, max: max };
	}
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
}

function PageMatrix() {
	this.renderMap;
	this.nav = new Pagination();
	this.data = new Array();
	this.sort = new Array();
	this.filt = new Array();
	this.attr = new Array();
	this.year = new Array();
	this.pick = new Array();
	this.rootImageUrl = '';
	this.IMAGE_WIDTH = 106;
	this.initSearchValue = 'Style#/Style Name/Keyword';
	this.searchColor = { on: '#333333', off: '#c6c3c0' };
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	this.getMap = function () {
		var i, n = 0
		var combo = new Array();

		// Combine filters
		for (i = 0; i < this.sort[0].length; i++) combo[i] = 0;
		if (this.pick) {
			for (var f in this.pick) {
				if (f.charAt(0) == 'a' && this.pick[f]) {
					n++;
					for (i = 0; i < this.filt[f].length; i++) {
						if (this.filt[f][i]) combo[i] = 1;
					}
				}
			}
		}
		// Show All Attributes if No Attributes selected
		if (n < 1) for (i = 0; i < combo.length; i++) combo[i] = 1;

		if (this.pick) {
			var type = parseInt(this.pick['t']);
			if (type > 0) {
				var mask = this.filt['t' + type];
				if (mask.length == combo.length) {
					for (i = 0; i < combo.length; i++) {
						if (mask[i] == 0) combo[i] = 0;
					}
				}
			}
		}

		// Make map from combo filter and sorting array
		var m = 0;
		var map = new Array();
		var sort = this.sort[(this.pick['o'] ? this.pick['o'] : 0)];
		if (sort) {
			for (i = 0; i < sort.length; i++) {
				if (combo[sort[i]]) map[m++] = sort[i];
			}
		}
		return map;
	}
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	this.renderPage = function () {

		var url = parent.document.URL;
		var i = url.indexOf('?s=');
		if (i < 0) i = url.indexOf('?y=');
		var query = (i < 0 ? '' : '&' + url.substring(i + 1, url.length));

		var data = this.data;
		var map = this.renderMap;

		var html = '';
		var tempAR, cell = 0;
		var bestAR, maxJ = 0;
		var ad, rowHeight;

		var pg = this.nav.pageMinMax();
		i = pg.min;

		while (i < pg.max) {
			if (cell == 0) {
				// Determine best height
				bestAR = 10000;
				maxJ = (i + 3 < pg.max ? i + 3 : pg.max);
				for (var j = i; j < maxJ; j++) {
					tempAR = data[map[j]].aspectRatio;
					if (tempAR < bestAR) bestAR = tempAR;
				}
				rowHeight = Math.round(this.IMAGE_WIDTH / bestAR);
			}
			ad = data[map[i]];
			html += ad.render(this.rootImageUrl, query, this.attr, this.IMAGE_WIDTH, rowHeight, (cell == 0), (cell == 3), (i == 0));

			if (++cell > 3) {
				cell = 0;
			}

			i++;
		}
		document.getElementById('divAdsShowResult').innerHTML = html;

		scrollTo(0, 0);
	}
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	this.render = function () {
		var i;
		document.getElementById('divAdsShowResult').style.display = 'none';
		document.getElementById('divAdsNoneFilter').style.display = 'none';
		document.getElementById('divAdsNoneSearch').style.display = 'none';

		var displayElem = 'divAdsShowResult';
		if (this.data && this.data.length > 0) {
			this.renderMap = this.getMap(); //alert(map);
			this.nav.init(this.renderMap.length);
			if (this.nav.totalItems > 0) {

				// Get the Referrer AdID
				var referrer = document.referrer;
				var referrerAdID = 0;
				if (referrer) {
					var token = 'ad.aspx';
					i = referrer.indexOf(token);
					if (i > -1) {
						referrer = referrer.substr(i + token.length);
						token = '?i=';
						i = referrer.indexOf(token);
						if (i < 0) {
							token = '&i=';
							i = referrer.indexOf(token);
						}
						if (i > -1) {
							referrer = referrer.substr(i + token.length);
							token = '&';
							i = referrer.indexOf(token);
							if (i > -1) {
								referrer = referrer.substring(0, i);
							}
							referrerAdID = parseInt(referrer);
							referrerAdID = (referrerAdID ? referrerAdID : 0);
						}
					}
				}

				// Collect AdIDs for cookie and find referrerAdID (if any)
				var referrerAdIndex = 0;
				var referrerFound = false;
				var adId;
				var adids = [];
				var map = this.renderMap;
				for (var i = 0; i < map.length; i++) {
					adId = this.data[map[i]].adId;
					adids[adids.length] = adId;
					if (!referrerFound && referrerAdID == adId) {
						referrerAdIndex = i;
						referrerFound = true;
					}
				}
				setCookie('aaAdIds', adids.toString(), 1);

				this.nav.step(referrerAdIndex ? Math.floor(referrerAdIndex / this.nav.ITEMS_PER_PAGE) : 0);
				this.renderPage();
			}
			else {
				displayElem = 'divAdsNoneFilter';
			}
		}
		else {
			var i = parent.document.URL.indexOf('?s=');
			var query = (i < 0 ? '' : parent.document.URL.substring(i + 3, parent.document.URL.length));

			displayElem = 'divAdsNoneSearch';
			if (query) document.getElementById("spnQuery").innerHTML = unescape(query.replace(/\+/g, ' '));
		}
		document.getElementById(displayElem).style.display = 'block';
	}
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	this.next = function () { this.nav.step(+1); this.renderPage(); }
	this.prev = function () { this.nav.step(-1); this.renderPage(); }
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	this.saveState = function () {
		var f, v = '';
		for (f in this.pick) {
			if (f != 's') {
				v += '|' + f + '|' + (this.pick[f] ? this.pick[f] : '');
			}
		}
		f = 's';
		v += '|' + f + '|' + (this.pick[f] ? this.pick[f] : '');
		v = v.substring(1);
		//alert(v);
		setCookie('aaAdState', v, 1);
		return true;
	}
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	this.loadState = function (query) {
		var v, i, e, k;

		for (k in this.filt) {
			if (k.charAt(0) == 'a') {
				this.pick[k] = 0;
			}
		}
		this.pick['t'] = 0;
		this.pick['o'] = 0;
		this.pick['s'] = query;

		var v = getCookie('aaAdState');
		if (v) {
			i = v.indexOf('|s|');
			var storedQuery = '';
			if (i > -1) {
				storedQuery = v.substring(i + 3);
				v = v.substring(0, i);
			}
			if (query == storedQuery) {
				var token = v.split('|');
				for (i = 0; i < token.length; i += 2) {
					e = parseInt(token[i + 1]);
					if (e) {
						this.pick[token[i]] = e;
					}
				}
				this.pick['s'] = storedQuery;
				stateLoaded = true;
			}
			else {
				setCookie('aaAdState', '', 1);
			}
		}

		if (this.pick['o']) {
			this.selectOrderOption(false, this.pick['o']);
		}
		if (this.pick['t']) {
			this.setSelected('selType', this.pick['t']);
		}
		if (this.pick['y']) {
			this.setSelected('selArchive', this.pick['y']);
		}

		for (k in this.filt) {
			if (k.charAt(0) == 'a') {
				e = document.getElementById('cbxAdAttribute_' + k);
				if (e) e.checked = (this.pick[k] == 0 ? false : true);
			}
		}
	}  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	this.setSelected = function (menuid, value) {
		var e = document.getElementById(menuid);
		if (e) {
			var v = e.options;
			for (i = 0; i < v.length; i++) {
				if (v[i].value == '' + value) {
					e.selectedIndex = i;
					break;
				}
			}
		}
	}
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	this.filterAttributes = function (attribute, checked) {
		this.pick[attribute] = (checked ? 1 : 0);
		this.render();
	}
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	this.filterType = function (menu) {
		this.pick['t'] = menu.options[menu.selectedIndex].value;
		this.render();
	}
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	this.filterReset = function () {
		for (var f in this.pick) {
			if (f.charAt(0) == 'a') {
				this.pick[f] = 0;
				document.getElementById('cbxAdAttribute_' + f).checked = false;
			}
		}
		this.pick['t'] = 0;
		document.getElementById('selType').selectedIndex = 0;
		this.render();
	}
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	this.selectOrderOption = function (e, option) {
		if (!e) e = document.getElementById('divSort' + option);
		this.pick['o'] = option; /*
    for (var i = 0; i < 3; i++) {
      document.getElementById('divSort' + i).className = 'off'
    }
    if (e) e.className = 'on';*/
	}
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	this.adSort = function (e, option) {
		this.selectOrderOption(e, option)
		this.render();
	}
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	this.searchFocus = function (e) {
		if (e.value == this.initSearchValue) {
			e.value = '';
			e.style.color = this.searchColor.on;
		}
	}
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	this.searchBlur = function (e) {
		if (e.value.length < 1) {
			e.value = this.initSearchValue;
			e.style.color = this.searchColor.off;
		}
	}
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	this.searchCheck = function () {
		var e = document.getElementById('tb_query');
		return (e && e.value.length > 0 && e.value != this.initSearchValue);
	}
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	this.changeArchiveYear = function (menu) {
		if (menu) {
			var year = parseInt(menu.value);
			if (year) {
				this.pick['y'] = year;
				var url = parent.document.URL;
				i = url.indexOf('?s=');
				if (i < 0) i = url.indexOf('?y=');
				if (i < 0) i = url.indexOf('?');
				if (i < 0) i = url.length;

				_page.saveState();
				window.location = url.substr(0, i) + (year < 0 ? '' : '?y=' + menu.value);
			}
		}
	}
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	this.changeArchive = function (menu, value) {
		if (!menu) {
			menu = document.getElementById('selArchive');
		}
		if (menu) {
			var i, e;
			for (i = 0; i < menu.options.length; i++) {
				e = document.getElementById('divArchive' + menu.options[i].value);
				if (e) e.style.display = 'none';
			} /*
      if (!value) {
        value = menu.options[menu.selectedIndex].value;
      }
      if (value) {
        e = document.getElementById('divArchive' + value);
        if (e) e.style.display = 'block';
      }*/
		}
	}
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	this.load = function () {
		var v, i;

		var url = parent.document.URL;
		i = url.indexOf('?s=');
		var query = (i < 0 ? '' : url.substring(i + 3, url.length));
		this.loadState(query);
		this.render();

		// Load Archive Menu
		var menu = document.getElementById('selArchive');
		if (menu) {
			for (i = 0; i < this.year.length; i++) {
				Insert(menu, { text: 'Archive ' + this.year[i], value: this.year[i] });
			}
			var year = '';
			// Hi-lite Ad Archives if index.aspx?s=MM/dd/yyyy
			if (query && query.length == 10) {
				var e = document.getElementById('month' + query.replace(/\//g, '_'));
				if (e) {
					e.style.color = '#333333';
					year = query.substring(6);
				}
			}
			else {
				i = url.indexOf('?y=');
				query = (i < 0 ? '' : url.substring(i + 3, url.length));
				if (query && query.length == 4 && parseInt(query)) {
					year = query;
				}
			}

			if (year) {
				this.changeArchive(menu, year);
				for (i = 0; i < menu.options.length; i++) {
					if (menu.options[i].value == year) {
						menu.selectedIndex = i;
						break;
					}
				}
			}
		}
	}
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* Ad Page
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
var _rate = true;
function RateClick() { _rate = !_rate; }
function RateHover(e, event, rate) {
	if (_rate) {
		if (rate == 1) {
			var x = event.offsetX ? (event.offsetX) : event.pageX - e.offsetLeft;
			rate = (x < 5 ? 0 : 1);
		}
		for (var i = 0; i < rate; i++) document.getElementById('imgRateStar' + i).src = 'http://i.americanapparel.net/storefront/ratings/images/largestar_full.gif';
		for (var i = rate; i < 5; i++) document.getElementById('imgRateStar' + i).src = 'http://i.americanapparel.net/storefront/ratings/images/largestar_empty.gif';
		document.getElementById(GetID('HF_Rate')).value = rate.toString();
	}
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
function UserPreview(show, user, l) {
	var rate = document.getElementById(GetID('HF_Rate')).value;
	document.getElementById('imCommentRate').src = 'http://i.americanapparel.net/storefront/ratings/images/rating_' + (rate ? rate : '0') + '0stars.gif'
	return Preview(show, user, l);
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
function PageLoadAd() {
	var adid = parseInt(getQueryStringValue('i'));
	if (adid) {

		var url = parent.document.URL;
		var i = url.indexOf('&s=');
		if (i < 0) i = url.indexOf('&y=');
		var query = (i < 0 ? '' : url.substring(i + 1, url.length));

		eval("_list = [" + getCookie('aaAdIds') + "]");
		document.getElementById("divContent").style.display = '';
		document.getElementById("ankIndexPg").href = 'index.aspx' + (query ? '?' + query : '');

		var ePrev = document.getElementById("ankPrevAd");
		var eNext = document.getElementById("ankNextAd");
		var n = _list.length;
		if (_list && n > 1) {
			for (i = 0; i < n; i++) if (_list[i] == adid) break;
			ePrev.href = 'ad.aspx?i=' + _list[(i > 0 ? i : n) - 1] + (query ? '&' + query : '');
			eNext.href = 'ad.aspx?i=' + _list[(i < n - 1 ? i + 1 : 0)] + (query ? '&' + query : '');
		}
		else {
			ePrev.style.display = 'none';
			eNext.style.display = 'none';
		}
	}
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
function Disable(id) {
	var e = document.getElementById(id);
	if (e) {
		e.disabled = true;
		e.style.cursor = 'default';
		e.style.color = '#989898';
	}
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
function ButtonHandler() {
	// After Style Button Click
	var e = document.getElementById(GetID('HF_Index'));
	if (e) {
		var i = parseInt(e.value);
		if (i > -1) {
			e = document.getElementById(GetID('HF_Style'));
			if (e.value) {
				var url = 'http://www.americanapparel.com/' + e.value + '.html';
				e = document.getElementById(GetID('HF_Color'));
				if (e && e.value) {
					url += '?c=' + escape(e.value);
				}
				window.open(url);
			}
			else {
				var prefix = GetID('StylePrefix') + i + '_';
				Disable(prefix + 'ibtStyle');
				Disable(prefix + 'lbtStyle');
				e = document.getElementById(prefix + 'pnlStyle');
				if (e) {
					e.style.display = '';
				}
			}
		}
	}
	// After Comment Post Click
	e = document.getElementById(GetID('HF_Rate'));
	if (e) {
		var rate = parseInt(e.value);
		if (rate && rate < 0) {
			document.getElementById('pnCommentDone').style.display = '';
			document.getElementById('pnCommentEdit').style.display = 'none';
			document.getElementById('pnCommentPrev').style.display = 'none';
			e.value = '';
		}
	}
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
