function item(id){
	return document.getElementById(id)
}

function make_bookmark(){
	var host=document.location.host
	var url='http://'+host+'/'

	try{
		if(window.sidebar)
			window.sidebar.addPanel(this.title, url, '')
		else if(document.all)
		window.external.AddFavorite(url, this.title)
	}
	catch (ex) {alert(ex.message)}
	return false
}

function popImg(src){
	var w=380,h=350,left=(screen.availWidth-w)/2,top=(screen.availHeight-h)/2
	var wnd=window.open("",'blistWind','toolbar=0,location=0,directories=0,menubar=0,resizable,scrollbars=0,width='+w+',height='+h+',top='+top+',left='+left)
	wnd.document.write('<title>Click it to close</title><style>html,body,table{margin:0px;padding:0px;height:100%;width:100%;}</style><body name="body"><table><tr><th><img src="'+src+'"></table><script>document.body.onclick=function(){close()}</script></body>')
	wnd.focus()
	return false
}

var async_obj=null

/*  Behaviour v1.1 by Ben Nolan, June 2005. http://ripcord.co.nz/behaviour/ */
document.Behaviour=new Object({
list: new Array,

register: function(sheet){ document.Behaviour.list.push(sheet) },

start: function(){ document.Behaviour.addLoadEvent(function(){ document.Behaviour.apply() }) },

apply: function(){
  for (h=0;sheet=document.Behaviour.list[h];h++)
    for (selector in sheet){
      var list = document.Behaviour.getElementsBySelector(selector)
      if (list)
    for (i=0;element=list[i];i++)
      sheet[selector](element)
    }
},

addLoadEvent:function(func){
  var oldonload = window.onload
  if (typeof window.onload != 'function')
    window.onload = func
  else window.onload = function() { oldonload(); func() }
}
})

document.Behaviour.start()

/*   The following code is Copyright (C) Simon Willison 2004.*/

function getAllChildren(e){
	return e.all?e.all:e.getElementsByTagName('*')
}

document.Behaviour.getElementsBySelector = function(selector){
	if(!document.getElementsByTagName){
		return new Array();
	}
	var tokens = selector.split(' ');
	var currentContext = new Array(document);
	for(var i=0; i<tokens.length; i++){
		token = tokens[i].replace(/^\s+/,'').replace(/\s+$/,'');;
		if(token.indexOf('#') > -1){
			var bits = token.split('#');
			var tagName = bits[0];
			var id = bits[1];
			var element = document.getElementById(id);
			if(tagName && element.nodeName.toLowerCase() != tagName){
				return new Array();
			}
			currentContext = new Array(element);
			continue;
		}
		if(token.indexOf('.') > -1){
			var bits = token.split('.');
			var tagName = bits[0];
			var className = bits[1];
			if(!tagName){
				tagName = '*';
			}
			var found = new Array;
			var foundCount = 0;
			for(var h = 0; h < currentContext.length; h++){
				var elements;
				if(tagName == '*'){
					elements = getAllChildren(currentContext[h]);
				}else{
					elements = currentContext[h].getElementsByTagName(tagName);
				}
				for(var j = 0; j < elements.length; j++){
					found[foundCount++] = elements[j];
				}
			}
			currentContext = new Array;
			var currentContextIndex = 0;
			for(var k = 0; k < found.length; k++){
				if(found[k].className && found[k].className.match(new RegExp('\\b'+className+'\\b'))){
					currentContext[currentContextIndex++] = found[k];
				}
			}
			continue;
		}
		if(token.match(/^(\w*)\[(\w+)([=~\|\^\$\*]?)=?"?([^\]"]*)"?\]$/)){//"
			var tagName = RegExp.$1;
			var attrName = RegExp.$2;
			var attrOperator = RegExp.$3;
			var attrValue = RegExp.$4;
			if(!tagName){
				tagName = '*';
			}
			var found = new Array;
			var foundCount = 0;
			for(var h = 0; h < currentContext.length; h++){
				var elements;
				if(tagName == '*'){
					elements = getAllChildren(currentContext[h]);
				}else{
					elements = currentContext[h].getElementsByTagName(tagName);
				}
				for(var j = 0; j < elements.length; j++){
					found[foundCount++] = elements[j];
				}
			}
			currentContext = new Array;
			var currentContextIndex = 0;
			var checkFunction;
			switch (attrOperator){
				case '=':
				checkFunction = function(e){ return (e.getAttribute(attrName) == attrValue); };
				break;
				case '~':
				checkFunction = function(e){ return (e.getAttribute(attrName).match(new RegExp('\\b'+attrValue+'\\b'))); };
				break;
				case '|':
				checkFunction = function(e){ return (e.getAttribute(attrName).match(new RegExp('^'+attrValue+'-?'))); };
				break;
				case '^':
				checkFunction = function(e){ return (e.getAttribute(attrName).indexOf(attrValue) == 0); };
				break;
				case '$':
				checkFunction = function(e){ return (e.getAttribute(attrName).lastIndexOf(attrValue) == e.getAttribute(attrName).length - attrValue.length); };
				break;
				case '*':
				checkFunction = function(e){ return (e.getAttribute(attrName).indexOf(attrValue) > -1); };
				break;
				default :
				checkFunction = function(e){ return e.getAttribute(attrName); };
			}
			currentContext = new Array;
			var currentContextIndex = 0;
			for(var k = 0; k < found.length; k++){
				if(checkFunction(found[k])){
					currentContext[currentContextIndex++] = found[k];
				}
			}
			continue;
		}

		if(!currentContext[0]){
			return;
		}

		tagName = token;
		var found = new Array;
		var foundCount = 0;
		for(var h = 0; h < currentContext.length; h++){
			var elements = currentContext[h].getElementsByTagName(tagName);
			for(var j = 0; j < elements.length; j++){
				found[foundCount++] = elements[j];
			}
		}
		currentContext = found;
	}
	return currentContext;
}

var fn_submit = function(el){
	el.onclick=function(){ document.forms[0].submit();return false}
}

var common_rules={

'#edit_meta a': function(el){
	el.onclick=function(){
		window.open(this.href,'edit','toolbar=0,width=512,height=128');
		return false
	}
},
'a.edit': function(el){
	el.onclick=function(){
		window.open(this.href,'edit','toolbar=0');
		return false
	}
},
'#qsearch a': function(el){
	el.onclick=function(){
		document.location.href=document.location.protocol+'//'+document.location.host+'/?search='+item('search').value
		return false
	}
},
'#qsearch input': function(el){
	el.onclick=function(){
		if(this.value==this.defaultValue)
			this.value=""
	}
	el.onblur=function(){
		if(this.value=="")
			this.value=this.defaultValue
	}
},
'form tfoot a': fn_submit,
'form tfoot button': fn_submit,
'#order .checkout': function(el){
	el.onclick=function(){
		try{ document.checkout.submit(); }
		catch(ex){};
		return false;
	}
},
'#order .update': function(el){
	el.onclick=function(){
		try{ document.cart.submit(); }
		catch(ex){};
		return false
	}
},
'#order .continue': function(el){
	el.onclick=function(){
		document.location.pathname='/';
		return false
	}
},
'#order img.photo': function(el){
	el.onclick=function(){
		return popImg('/_img/pills/blisters/'+el.getAttribute('alt')+'.jpg')
	}
	el.title="Click to view pills in blister"
},
'#order #bonus_id': function(el){
	el.onclick=function(){
		document.cart.update.click();
	}
},
'.select select[name="shipping_method_id"]': function(el){
	el.onchange=function(){
		document.cart.update.click();
	}
},
'a.bookmark': function(el){
	el.onclick=make_bookmark
}
};

function popItUp(u,w,h){
	var l=(screen.availWidth - w) / 2, t=(screen.availHeight - h) / 2
		, op='toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,copyhistory=no,width='+w+',height='+h+',left='+l+',top='+t
	window.open(u, '_blank', op)
}

function showCertificate(){
	popItUp('/redir.php?id=certificates_url',500,350)
}

function showSpecialOffer(){
	popItUp('/redir.php?id=special_offer_url',620,620)
}

document.Behaviour.register(common_rules)

