var sites = new Array();
var oOp = null;
var op = new Class({
	txt: null,
	lft: null,
	rgt: null,
	img: null,
	by: null,
	current: 0,
	
	initialize: function(){
		this.txt = $('opText');
		if(!this.txt){return;}
		this.lft = $('opLeft');
		this.rgt = $('opRight');
		this.imgcont = $('opImgCont');
		this.img = $('opImg');
		this.by = $('opAuthor');
		this.site = $('opLink');
		this.vs = $('opLinkVS');
		
		// Get index of starting item
		sites.each(function(o,n){
			if(o&&o.id==hp_cur){this.current=n;}
		},this);

		// Make buttons visible as we can use them...
		this.rgt.setStyle('display','block');
		this.lft.setStyle('display','block');
		// Preload next/previous images
		if(this.current<sites.length-1){
			this.preImg(this.current+1);
		} else {
			this.preImg(1);
		}
		if(this.current>1){
			this.preImg(sites.length);
		}
		
		// Add button events
		this.rgt.addEvent('click',function(e){
			e.stop();
			this.chRight();
		}.bind(this));
		this.lft.addEvent('click',function(e){
			e.stop();
			this.chLeft();
		}.bind(this));
	},
	
	chLeft: function(){
		// Handle currently selected item and whether button is shown
		this.current--;
		if(this.current <= 0)
			this.current = sites.length - 1;
		this.setStuff(sites[this.current]);
	},
	
	chRight: function(){
		// Handle currently selected item and whether button is shown
		this.current++;
		if(this.current >= sites.length)
			this.current = 1;
		this.setStuff(sites[this.current]);
	},
	
	preImg: function(idx){
		if(sites[idx]){
			img = new Image();
			img.src = 'data/cache/hp_'+sites[idx].img+'.jpg';
		}
	},
	
	setStuff: function(current){
		this.img.fade('hide');
		this.img.addEvent('load',function(){
			this.img.fade(1);
			this.img.removeEvents();
		}.bind(this));
		this.img.setProperty('src', 'data/cache/hp_'+current.img+'.jpg');
		this.txt.set('html', current.text);
		this.by.set('text', current.author);
		this.site.set('text', current.domain);
		this.site.setProperty('href', current.url);
		this.vs.setProperty('href', 'portfolio/'+current.slug+'/');
		this.imgcont.setProperty('href', 'portfolio/'+current.slug+'/');
		
		this.preImg(this.current+1);
		this.preImg(this.current-1);
	}
});

window.addEvent('load', function(){
	// Homepage portfolio...
	oOp = new op();
	
	// Menu (fix for IE)
	var menuSubs = $$('#hdNav p.sub');
	var menuCons = $$('#hdNav .navc');
	menuCons.each(function(obj, n){
		obj.addEvent('mouseenter', function(e){
			if(menuSubs[n]){menuSubs[n].setStyle('display','block');}
			menuCons[n].addClass('a');
			menuCons[n].addEvent('mouseleave', function(le){
				menuCons[n].removeEvent(le);
				menuCons[n].removeClass('a');
				if(menuSubs[n]){menuSubs[n].setStyle('display','none');}
			});
		});
	});
	
	// CAPTCHA reload thing
	var captchaReload = $('reloadCaptcha');
	if(captchaReload){
		var captchaPath = $('captchaImage').get('src');
		captchaReload.addEvent('click',function(e){
			$('captchaImage').set('src',captchaPath+'?'+Math.random());
		});
	}
	
	// Funky tooltips
	if($$('.help').length>0)
		var tips = new Tips('.help',{className:'helpHover'});
	
	// File browse replace...
	var fnrDivs = $$('.fileNiceReplace');
	var fnrInput = null;
	fnrDivs.each(function(obj,n){
		obj.setStyle('display','block');		// Make replacement visible to folk that can use it
		fnrInput = obj.getParent().getChildren('input.file');
		fnrInput.setStyles({
			float: 'right',
			opacity: .01,
			width: 'auto'
		});
		fnrInput.addEvent('change',function(fe){
			fnrDivs.getChildren('.fileReplaceText')[0].set('value',fnrInput.get('value'));
		});
	});
	
	// Code highlighting: highlight all "pre" elements in a document
	$$('pre').light({
		altLines: 'hover',
		flame: 'git',
		path: 'assets/js/lighter/'
	});
});


// making this a global variable so that it is accessible for
// debugging/inspecting in Firebug
var map = null;
function get_osm_url (bounds) {
	var res = this.map.getResolution();
	var x = Math.round ((bounds.left - this.maxExtent.left) / (res * this.tileSize.w));
	var y = Math.round ((this.maxExtent.top - bounds.top) / (res * this.tileSize.h));
	var z = this.map.getZoom();
	var limit = Math.pow(2, z);
	if (y < 0 || y >= limit)
		return null;
	else
	{
		x = ((x % limit) + limit) % limit;
		var path = z + "/" + x + "/" + y + "." + this.type; 
		var url = this.url;
		if (url=="http://tah.openstreetmap.org/~ojw/Tiles/cycle.php/") {
			if (z<12) url="http://tah.openstreetmap.org/~ojw/Tiles/cycle.php/"
			else url="http://tah.openstreetmap.org/Tiles/tile/"
		}
		if (url instanceof Array)
			url = this.selectUrl(path, url);
		return url + path;
	}
}
