function DynInput( idVal ) {
	var el = $(idVal);
	
	function clearIn() {
		if (el.value == el.defaultValue) {
			el.value = '';
			el.removeClassName('isDefault');
		}
	}
	
	function resetIn() {
		if (!el.value) {
			el.value = el.defaultValue;
			el.addClassName('isDefault');
		}
	}
	
	if (el) {
		el.observe('click', clearIn);
		el.observe('blur', resetIn);
		resetIn();
	}
}

(new DynInput('searchIn'));
(new DynInput('mailingIn'));

function DropDown( idVal ) {
	var el = $(idVal);
	
	function jumpToPage() {
		if (el.selectedIndex && el.getValue()) {
			window.location.href = el.getValue();
		}
	}
	
	if (el) {
		el.selectedIndex = 0;
		el.observe('change', jumpToPage);
	}
}

(new DropDown('shopByDD'));

function stripLast( str, chr ) {
	if ((typeof str === 'string') && (typeof chr === 'string')) {
		var lastChar = str.length - chr.length;
		if (str.substr(lastChar) === chr) {
			str = str.substr(0, lastChar);
		}
		lastChar = null;
	}
	return str;
}

function genQueryStr( qVal, lVal ) {
	if (!lVal) {
		lVal = window.location.href;
	}
	if (qVal) {
		var qStart = lVal.indexOf('?'), pQuery = '';
		if (qStart !== -1) {
			pQuery = lVal.substr(qStart + 1);
			if (pQuery && (pQuery.charAt(pQuery.length - 1) !== '&')) {
				lVal += '&';
			}
		} else {
			lVal += '?';
		}
		qStart = pQuery = null;
		var qType = typeof qVal;
		if (qType == 'string') {
			lVal += encodeURI(qVal);
		} else if (qType == 'object') {
			for (var i in qVal) {
				lVal += encodeURIComponent(i) + '=' + encodeURIComponent(qVal[i]) + '&';
			}
			lVal = stripLast(lVal, '&');
		}
		qType = null;
		lVal = stripLast(lVal, '?');
	}
	return lVal;
}

function setQuery( qVal, lVal ) {
	var wLoc = window.location;
	if (!lVal) {
		lVal = wLoc.href;
		var qStart = lVal.indexOf('?');
		if (qStart !== -1) {
			lVal = lVal.substr(0, qStart);
		}
	}
	if (lVal = genQueryStr(qVal, lVal)) {
		return wLoc.href = lVal;
	}
	return '';
}

function linkAsPopup( idVal, opts ) {
	var el = $(idVal);
	
	function doPop( event ) {
		window.open(genQueryStr({
			url: (window.location.href),
			name: el.title
		}, el.href), idVal, opts);
		event.stop();
	}
	
	if (el) {
		el.observe('click', doPop);
	}
}

(new linkAsPopup('sendToFriendLink', 'width=640px, height=400px'));



