(function(){
	function MK(ls){this.els=[];for(var i=0;i<ls.length;i++){var l=ls[i];if(typeof l=='string'){l=$i(l)}this.els.push(l)}return this}MK.prototype={push:function(a){for(var j=0;j<a.length;j++){this.els.push(a[j])}return this},all:function(f){for(var i=0,xn=this.els.length;i<xn;++i){f.call(this,this.els[i])}return this},on:function(t,f){var lsn=function(el){if(window.addEventListener){el.addEventListener(t,f,false)}else if(window.attachEvent){el.attachEvent('on'+t,function(){f.call(el,window.event)})}};this.all(function(el){lsn(el)});return this},style:function(p,v){this.all(function(el){var u=v;if(p=='opacity'){el.style['filter']='alpha(opacity='+u+')';u=u/100}el.style[p]=u});return this},css:function(o){var that=this;this.all(function(el){for(var p in o){that.style(p,o[p])}});return this},anim:function(f,b,e,t,c){var that=this;for(var p in b){if(p!='opacity'||p!='zIndex'){e[p]=(e[p]-b[p])}}var nd=new Date().getTime()+t;var _a=window.setInterval(function(){var k=(t-(nd-new Date().getTime()));if(t<=k){window.clearInterval(_a);k=t}that.all(function(el){for(var p in b){if(p=='scrollLeft'||p=='scrollTop'){el[p]=Math.floor(f(k,b[p],e[p],t))}else{var prt=(p=='zIndex'||p=='opacity')?'':'px';that.style(p,Math.floor(f(k,b[p],e[p],t))+prt)}}});if(t==k&&c){c()}},10);return this}};window.$=function(){return new MK(arguments)};$i=function(o){if(typeof o=='string'){return document.getElementById(o)}return o};$e=function(i,e){var d=e?$i(i):document;return d.getElementsByTagName(e)};$c=function(c,e){var d=e?$i(e):document;if(!document.getElementsByClassName){var r=[];c=new RegExp('\\b'+c+'\\b');d=d.getElementsByTagName('*');for(var i=0;i<d.length;i++){if(c.test(d[i].className)){r.push(d[i])}}return r}return d.getElementsByClassName(c)};xhr=function(u,p,s,f){var X=window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject('Microsoft.XMLHTTP');if(p==''){X.open('GET',u,true)}else{X.open('POST',u,true);X.setRequestHeader('Content-type','application/x-www-form-urlencoded')}X.onreadystatechange=function(){if(X.readyState==4){var r=X.responseText;if(X.status==200){if(s){s(r,X)}}else{if(f){f(r,X)}}}};X.send(p)};load=function(s,c){var f=document.createElement('script');if(c){f.onload=function(){c()};f.onreadystatechange=function(){if(this.readyState=='complete'){c()}}}f.src=s;$e(document,'head')[0].appendChild(f)};offSet=function(o){var l=0;var t=0;if(o.offsetParent){do{l+=o.offsetLeft;t+=o.offsetTop}while(o=o.offsetParent);return[l,t]}};preventDefault=function(e){if(!e)e=window.event;e.cancelBubble=true;e.returnValue=false;if(e.preventDefault){e.preventDefault()}};if(!Array.indexOf){Array.prototype.indexOf=function(o,i){for(var j=this.length,i=i<0?i+j<0?0:i+j:i||0;i<j&&this[i]!==o;i++);return j<=i?-1:i}}ease={cubic:function(t,b,c,d){if((t/=d/2)<1){return(c/2*t*t*t+b)*1}return(c/2*((t-=2)*t*t+2)+b)*1},bounce:function(t,b,c,d){if((t/=d)<(1/2.75)){return c*(7.5625*t*t)+b}else if(t<(2/2.75)){return c*(7.5625*(t-=(1.5/2.75))*t+.75)+b}else if(t<(2.5/2.75)){return c*(7.5625*(t-=(2.25/2.75))*t+.9375)+b}else{return c*(7.5625*(t-=(2.625/2.75))*t+.984375)+b}}};MKAvail=true;
	var tT;
	var cT;
	var eMode = false;	
		
	if($i('find')){
		$('find').on('submit', function(e){
			preventDefault(e);
			$('q').css({backgroundImage:'url(/media/img/l.gif)'});
			$i('q').disabled = true;
			xhr('/find/?t=1&q='+ $i('q').value, '', function(t){
				$i('latest').innerHTML = t;
				$i('ttl').innerHTML = $e('latest','li').length + ' matches for \''+$i('ttl').innerHTML+'\'';
				$('q').css({backgroundImage:'none'});
				$i('q').disabled = false;
			});
			return false;
		});
	}
	
	if($i('FillTags')){
		cT = 'FillTags';
		$().push($e('FillTags', 'a')).all(function(el){
			$(el).on('click', function(e){
				if(eMode){return;}
				preventDefault(e);								
				var nV = this.innerHTML;
				var cV = $i('tg').value.split(' ');	
				if(cV.indexOf(nV) > -1){
					cV.splice(cV.indexOf(nV), 1);
					this.className = this.className.replace('pkd','');
				} else {
					cV.push(nV);
					this.className = this.className + ' pkd';
				}
				$i('tg').value = cV.join(' ').replace(/^\s\s*/, '').replace(/\s\s*$/, '');
			});
			el.setAttribute('href','#');
			var pV = $i('tg').value.split(' ');	
			var mV = el.innerHTML;
			for(j = 0; j < pV.length; j++){
				if(pV[j] == mV){
					el.className = el.className + ' pkd';
				}
			}
		});
		$('FillTags').on('click', function(e){
			if(!eMode){return;}
			close();
		});
	}

	if($i('tags')){
		cT = 'tags';
		$().push($e('tags','a')).all(function(el){
			el.href = '#' + el.innerHTML;
			$(el).on('click', function(e){
//				if(eMode){return;}
				if(this.id == 'sall' || eMode){return;}
				preventDefault(e);
				if(navigator.userAgent.indexOf('Safari') > -1){
					var ts = $i('bd');
				} else {
					var ts = $i('bd').parentNode;
				}
				$(ts).anim(ease.cubic, {scrollTop: ts.scrollTop}, {scrollTop: 0}, 400);
				xhr('/tag/?t='+ escape(el.innerHTML), '', function(t){
					$i('latest').innerHTML = t;
					$i('ttl').innerHTML = $e('latest','li').length + ' matches for \''+ $i('ttl').innerHTML+'\'';
				});
			})
		});		
		$('tags').on('click', function(e){
			if(!eMode){return;}
			close();
		});
	}	
			
	if($i('editBar')){	
		var close = function(){
			eMode = false;
			$($e(cT,'ul')[0]).anim(ease.cubic, {opacity:30}, {opacity:100}, 500);
			$('editBar').anim(ease.cubic, {opacity:100}, {opacity:0}, 300, function(){
				$('editBar').css({display:'none'});
				$i('sTg').innerHTML = 'Edit';						
				$i('eTg').contentEditable = 'false';
			});
		};
	
		$('editBar').on('click', function(e){
			if (!e) e = window.event;
			var s = (typeof e.target != 'undefined') ? e.target : e.srcElement;
			if(s.tagName.toLowerCase() == 'b'){
			preventDefault(e);
			
			switch(s.innerHTML){
				case 'Edit':
					$i('eTg').contentEditable = 'true';
					$i('eTg').focus();
					$i('sTg').innerHTML = 'Save';
					break;
						
				case 'Delete':
					var del = confirm('Are you sure you want to delete your \''+ $e('editBar','a')[0].innerHTML +'\' tag?');
					if(del){
						xhr('/tag/?HRN_remove', 't='+ escape(tT.innerHTML));
						tT.parentNode.parentNode.removeChild(tT.parentNode);
						close();
					}
					break;
						
				case 'Close':
					close();
					break;
						
				case 'Save':
					if(document.all){
  						nV = $i('eTg').innerText;
					} else {
						nV = $i('eTg').textContent;
					}
					var oldTg = tT.innerHTML;
					tT.href = '/tag/?'+ nV;
					tT.innerHTML = nV;
					xhr('/tag/?HRN_rename', 't='+ escape(oldTg) +'&nt='+ escape(nV));
					close();		
					break;
			}		
		}});		

		$().push($e(cT, 'a')).on('contextmenu', function(e){
			preventDefault(e);
			tT = this;
			eMode = true;
			$($e(cT,'ul')[0]).anim(ease.cubic, {opacity:100}, {opacity:30}, 500);
			$('editBar').all(function(el){
				var tA = $e('editBar','a')[0];
				tA.className = tT.className;
				tA.innerHTML = tT.innerHTML;
				$i('vws').innerHTML = tT.getAttribute('title');
			}).css({display:'block',opacity:0,left:offSet(this)[0]+'px',top:(offSet(this)[1]+9)+'px'}).anim(ease.cubic, {opacity:0}, {opacity:100}, 400);
		});
	}

	
	if($i('latest')){ 
		$('latest').on('mouseover', function(e){
			if (!e) e = window.event;
			var s = (typeof e.target != 'undefined') ? e.target : e.srcElement;
			if(s.tagName.toUpperCase() == 'A'){
	
				if($i('HRDops')){
					$i('HRDops').innerHTML = '';
					$i('HRDops').id = '';
				}
					
				var spn = $e(s.parentNode, 'span');
				if(spn.length > 0){
					var vws = '';
					var spn = spn[0];

					if(spn.getAttribute('title') && spn.getAttribute('title').length > 0){
						vws =  ((parseInt(spn.getAttribute('title'), 10) > 1) ? spn.getAttribute('title') +' views : ' : '');
					}

					spn.innerHTML = vws + '<b title=\'Edit this bookmark\'>Edit</b> : <b title=\'View a saved version of this bookmark\'>Cached</b> : <b title=\'Remove this bookmark\'>Delete</b>';
					spn.id = 'HRDops';
				}
			}
		}).on('click', function(e){
			if (!e) e = window.event;
			var s = (typeof e.target != 'undefined') ? e.target : e.srcElement;
			if(s.tagName.toLowerCase() == 'b'){
				preventDefault(e);
				var tarUri = $e(s.parentNode.parentNode.parentNode,'a')[0].href;
				switch(s.innerHTML){
					case 'Edit':
						document.location = '/add/?u='+ escape(tarUri) +'&e=1';
						break;
						
					case 'Cached':
						document.location = '/cached/?'+ escape(tarUri);
						break;
						
					case 'Delete':
						var del = confirm('Are you sure you want to delete this bookmark?');
						if(del){
							var tDv = s.parentNode.parentNode.parentNode.parentNode;
							tDv.removeChild(s.parentNode.parentNode.parentNode);
							xhr('/delete/?u='+ escape(tarUri), '');
						}
						break;
				}		
			}		
		});
	}
		
	$().push($c('button')).on('click', function(){
		var but = this;
		$(but).css({opacity: 50});
		setTimeout(function(){
			$(but).css({opacity: 100});
		}, 100);
	});
})();