function archivesList(id,numCols){
	var h = document.getElementById(id);
	var ls = h.getElementsByTagName('li');
	var n=0;
	var c= Math.ceil(ls.length / numCols);
	var s='';
	for (var i=0; i < ls.length; i++){
		if ((i % c) == 0) {
			if (n!=0) s+='</ul></li>';
			n++;
			s+='<li class="column column-'+n+'"><ul>';
		}
		s+='<li>'+ls[i].innerHTML+'</li>';
	}
	s+='</li>';
	h.innerHTML = s;
}

function archiveYears(id){
	var h=document.getElementById(id);
	var its=h.getElementsByTagName('li');
	var arc=[];
	var reCount = /\((\d*)\)/
	var yearRe = /(.*) (\d{4})/;
	var a=null;
	var m=null;
	var c=null;
	for (var i=0; i<its.length;i++){
		a = its[i].getElementsByTagName('a');
		m = a[0].innerHTML.match(yearRe);
		c = its[i].innerHTML.match(reCount);
		if ((m.length >2) && (c.length>1))
			arc[arc.length]={href:a[0].href,month:m[1],year:m[2],count:c[1]};
	}
	
	var s='';
	var lastYear = -1;
	for (var i=0; i < arc.length; i++){
		if (arc[i].year != lastYear){
			if (lastYear > 0)
				s+='</ol></div>';
			lastYear = arc[i].year;
			s+='<div class="singleYear"><h5>'+arc[i].year+'</h5><ol>';
		}
		s+= '<li><a href="'+arc[i].href+'" title="' +arc[i].month+' '+arc[i].year+ ' archive">' +arc[i].month+'</a> <em>(' +arc[i].count+ ')</em></li>';
	}
	s+='</ol></div>';
	h.innerHTML = s;
}

function wireSideSearch(){
	var h = document.getElementById('sidebarSearchForm');
	if (!h) return;
	h.onsubmit = function(){
		var t = document.getElementById('sidebarSearchText');
		if (t.value.length > 2) return true;
		alert('Please enter a longer search term. Minimum length is 3 characters');
		t.focus();
		return false;
	};
}

function wireSearch(){
	var h = document.getElementById('commentForm');
	if (!h) return;
	h.onsubmit = function(){
		var s = '';
		var m = [{id:'author',msg:'name'},{id:'email',msg:'email address'},{id:'comment',msg:'message text'}];
		for (var i=0; i < m.length; i++){
			var t = document.getElementById(m[i].id);
			if (t.value.length < 3) s+= ' * '+m[i].id +"\n";
		}
		if (s.length == 0) return true;
		alert("You've omitted one of the required fields. Please complete:\n"+s);
		//t.focus();
		return false;
	};
}


/*
	
	Original:  Mike McGrath (mike_mcgrath@lineone.net)
	This script and many more are available free online at http://www.ibold.net
	
	Updated: Buz Carter

*/
strToNbrEncode = function(){
}
strToNbrEncode.prototype = {
	errMsg: "Enter Text!",
	
	toNumber: function( inString ){
		var strIn = '';
		var nbrOut = "";
		if(inString == ""){
			alert(errMsg);
			return "";
		} 
		else {
			strIn = escape(inString);
			for(i = 0; i < strIn.length; i++){
				nbrOut += strIn.charCodeAt(i) - 23;
			}
			return nbrOut;
		}
	},
	
	toString: function(inNumber){
		var nbrIn;
		var nbrOut = "";
		var strOut = ""; 
		if(inNumber == ""){
			alert(errMsg)
			return "";
		} 
		else {
			nbrOut = inNumber;
			for(i = 0; i < nbrOut.length; i += 2) {
				nbrIn = parseInt(nbrOut.substr(i,[2])) + 23;
				nbrIn = unescape('%' + nbrIn.toString(16));
				strOut += nbrIn;
			}
			return unescape(strOut);
		}
	}
}

// ----------------------------------
flickrBadge = function(){
}
flickrBadge.prototype = {
	// Properties to change
	// -----------------------------------------------
	// setup & behavior
	numPicsToShow: 5,
	// ID's
	largePicID: 'flickrShot',
	containerId: '',
	// text
	nextLabel: 'next',
	prevLabel: 'prev',
	navClass: 'nav',
	loadingMessage: 'Loading images...',
	fullImageLink: 'View full-sized image on flickr',
	// -----------------------------------------------
	_current: 0,
	_srcLink: null,
	_largePhoto: null, // handle: div holding Large Photo 
	_photosList: null, // handle: UL holding photos
	_nextBtn: null, // handle: "Next" button
	_previousBtn: null, // handle: "Previous" button
	_photoCount: -1,
	
	init: function(){
		this._photosList = document.getElementById(this.containerId);
	},
	deAmp: function(s){
		s= s.replace(/\&amp;/g,'&');
		s= s.replace(/\&quot;/g,'"');
		return s;
	},
	display: function(obj){
		if (!this._photosList) return;

		this._photoCount = obj.items.length;
		
		var tmp = '';
		var l = null;
		var a = null;
		var img = null;
		var x = this;
		var ul = document.createElement('ul');

		for(i in obj.items){
			// some change at flickr, regex update from: /src=&quot;([^&]*)&quot;/
			var tmp2 = obj.items[i].description.match(/src="([^"]*)/);
			if (tmp2 && tmp2.length>1){
				tmp = tmp2[1];
				tmp = tmp.replace(/_m.jpg/g,'_s.jpg');
			}
			else{
				tmp='';
			}
			// console.log(i+' '+tmp+' '+obj.items[i].description);
			l = document.createElement('li');
			l.style.display = 'none';
			a = document.createElement('a');
			a.href = obj.items[i].link;
			a.title= this.deAmp(obj.items[i].title);
			a.onclick = function(){
				x.show(this);
				return false;
			};
			img = document.createElement('img');
			img.src = tmp;
			img.alt = a.title;
			
			a.appendChild(img);
			l.appendChild(a);
			ul.appendChild(l);
		}

		this._photosList.innerHTML = '';
		this._photosList.appendChild(ul);

		this.navigate(0);

	},
	navigate: function(offset){
		var items = this._photosList.getElementsByTagName('li');
		var change = this.numPicsToShow*offset;
		this._current += change; 

		for(var i=0; i < items.length; i++){
			items[i].style.display = ((i >= this._current) && (i < this._current+this.numPicsToShow)) ? 'block': 'none';
		}
		
	},
	show: function(o){
		if(this._largePhoto === null){
			this._largePhoto = document.createElement('div');
			document.body.appendChild(this._largePhoto);
			this._largePhoto.id = this.largePicID;
			this._largePhoto.className = this.largePicID;
		}
		this._largePhoto.innerHTML = '<a href="#" title="click to close" onclick="this.parentNode.style.display=\'none\';this.parentNode.parent.focus();return false" class="closeFlickrBtn">X</a>'+
			'<h4>'+o.title+'&nbsp;</h4>'+
			'<a href="#" title="click to close" onclick="this.parentNode.style.display=\'none\';this.parentNode.parent.focus();return false">'+o.innerHTML.replace(/_s.jpg/,'_m.jpg')+'</a>'+
			'<p><a href="'+o.href+'">'+this.fullImageLink+'</a></p>';
		this._largePhoto.style.display = 'block';
		var y = 0;
		if(self.pageYOffset){
			y = self.pageYOffset;
		} 
		else if (document.documentElement && document.documentElement.scrollTop){
			y = document.documentElement.scrollTop;
		} 
		else if(document.body){
			y = document.body.scrollTop;
		}
		this._largePhoto.style.top = y+'px';
		this._largePhoto.parent = o;
		this._largePhoto.getElementsByTagName('a')[0].focus();
	}
};

// Flickr API's javascript will call this global function upon loading:
function jsonFlickrFeed(json){
	var fjb = new flickrBadge;
	fjb.numPicsToShow = 18;
	fjb.containerId = 'flickrBox';
	fjb.init();
	fjb.display(json);
}

// ----------------------------------
downSize = function(){
}
downSize.prototype = {
	initialize: function(){
		var g = document.getElementsByTagName('img');
		var re = /url\((.*)\)/gi;
		for (var i=0; i < g.length; i++){
			if (g[i].className == 'downSize'){
				if (g[i].style.backgroundImage != 'undefined' && g[i].style.backgroundImage != '')
					g[i].src = g[i].style.backgroundImage.replace(re,'$1');
				else if(g[i].lowsrc) 
					g[i].src=g[i].lowsrc;
			}
		}
	},
}

function doFreshiPic(){
	//var myGallery = new InstantGallery;
}

function everyPage(){
	var e= new strToNbrEncode;
	var h = document.getElementById('footerTalkMe');
	if (h){
		var em = e.toString('7688879374769314284214272514284474142725819178791428451427278674828593881428427594997674919378914181889386748285237688861427271428467594997674919378914181889386748285237688861428442474142846');
		h.innerHTML= em;
	}

	var m = new downSize();
	m.initialize();

	wireSideSearch();
	wireSearch();

}
