
// darren-c.co.uk v3 JavaScript/jQuery

var navtab=0,menuan=false,menuon=false,menuto,sscur=0,ssint;

$(document).ready(function()
{
	$('#photos a:has(img)').click(function(e){ e.preventDefault(); viewphoto($(this).parent()); });
	$('#vphoto.hide p.n a:eq(1)').click(function(e){ e.preventDefault(); backtogallery(); });
	$('#header a[href="/choose"]').click(function(e){ e.preventDefault(); $(this).blur(); showmenu(); });
	$('#menu.hide').hover(function(){ clearTimeout(menuto); },function(){ menuto=setTimeout('showmenu()',1000); });
	$('#contact p.e .i').keyup(function(){ $(this).unbind('keyup').next().slideUp(500,function(){ $(this).parent().removeClass('e'); }); });
	$('#ng div').hover(function(){ clearInterval(ssint); },function(){ ssint=setInterval('slideshow(sscur+1,1000)',5000); });
	$('#ng li').mouseover(function(){ clearInterval(ssint); slideshow($(this).index(),0); });
	$('#ng ul').mouseout(function(){ clearInterval(ssint); ssint=setInterval('slideshow(sscur+1,1000)',5000); });
	if($('#ng').length>0){ slideshow(0,0); ssint=setInterval('slideshow(sscur+1,1000)',5000); }
	$('#vphoto img').load(function(){ if($(this).attr('id')!='v0') viewcount('photo'); });
	viewcount('page');
	menusortlinks();
});

function viewphoto(sel)
{
	var photo=$(sel).find('img');
	var prev=$(sel).prev(),next=$(sel).next();
	if(!prev.length) prev=$(sel).siblings().last();
	if(!next.length) next=$(sel).siblings().first();
	var exif=$(photo).attr('title').split(' - ');

	$('#vphoto h3').html($(photo).attr('alt'));
	$('#vphoto em').html($(sel).find('em').html());
	$('#vphoto img').attr('alt',$(photo).attr('alt')).attr('id','v'+$(sel).attr('id').substring(1))
		.attr('src',$(photo).attr('src').replace('ps','pl'));
	$('#vphoto p.n a:eq(0)').unbind('click').click(function(e){ e.preventDefault(); viewphoto(prev); })
		.attr('title',$(prev).find('img').attr('alt'));
	$('#vphoto p.n a:eq(2)').unbind('click').click(function(e){ e.preventDefault(); viewphoto(next); })
		.attr('title',$(next).find('img').attr('alt'));
	$('#vphoto p:last').html('<strong>Taken:</strong> '+exif[0]+' (Local Time) - <strong>Exposure:</strong>\n'+exif[1]+
		' - <strong>Aperture:</strong> '+exif[2]+' - <strong>ISO Speed:</strong> '+exif[3].replace('ISO ','')+
		' -\n<a href="'+$(sel).find('a').attr('href')+'" class="dl">Direct Link</a>');

	if(window.addthis)
	{
		window.addthis.update('share','url','http://'+location.hostname+$(sel).find('a').attr('href'));
		window.addthis.update('share','title',$(photo).attr('alt')+' - '+document.title);
	}

	if($('#vphoto:visible').length>0) return;
	$('#photos').hide();
	$('#vphoto').show();
}

function backtogallery()
{
	if(window.addthis)
	{
		window.addthis.update('share','url',location.href);
		window.addthis.update('share','title',document.title);
	}

	$('#vphoto').hide();
	$('#photos').show();
	$('#vphoto img').attr('id','v0').attr('src','/img/pl00000.jpg');
}

function showmenu()
{
	if(menuan) return;
	menuan=true;
	menuon=!menuon;
	clearTimeout(menuto);
	if(menuon)
	{
		$('#menu').slideDown(500,'bounce',function(){ menuan=false; });
		navtab=$('#header li').index($('#header li.cp'));
		if(navtab>-1) $('#header li').eq(navtab).removeClass('cp');
		$('#header li').eq(1).addClass('cp');
	}
	else
	{
		$('#menu').slideUp(500,function(){ menuan=false; });
		$('#header li').eq(1).removeClass('cp');
		if(navtab>-1) $('#header li').eq(navtab).addClass('cp');
	}
}

function slideshow(index,duration)
{
	$('#ng div a').stop();
	$('#ng li').eq(sscur).removeClass('cur');
	sscur=index>=$('#ng li').length?0:index;
	$('#ng div a').eq(sscur).siblings().fadeTo(duration,0,function(){ $(this).hide(); });
	$('#ng div a').eq(sscur).show().fadeTo(duration,1);
	$('#ng li').eq(sscur).addClass('cur');
}

function viewcount(type)
{
	var count={ type:type,id:-1,key:0 };
	if(type=='page'&&$('#galintro h2').length==1)
	{
		count.id=$('#galintro h2').attr('id').substring(1);
	}
	if(type=='photo'&&$('#vphoto img').length==1)
	{
		count.id=$('#vphoto img').attr('src').substring(7,12);
		count.key=$('#vphoto img').attr('id').substring(1);
	}
	$.post('/count',count);
}

function menusortlinks()
{
	var curord=$('#menu br').attr('id').charAt(3);
	var ordlinks='',ords=new Array('Title','Date Taken','Date Added','Popularity');
	for(var i in ords) ordlinks+=' | <'+(i==curord?'strong':'a href="#" id="mo'+i+'"')+'>'+ords[i]+'</'+(i==curord?'strong':'a')+'>';
	$('#menu br').before('<p>Sort by: '+ordlinks.substring(3)+'</p>\n');
	$('#menu p a').click(function(e){ e.preventDefault(); menusort($(this).attr('id').charAt(2)); });
}

function menusort(order)
{
	$('#menu p').html('Please Wait...');
	$.post('/menu',{ order:order,current:location.pathname },function(data){ $('#menu').html(data); menusortlinks(); });
}

jQuery.extend(jQuery.easing,
{
	bounce: function(x,t,b,c,d)
	{
		if((t/=d)<(1/2.75)) return c*(7.5625*t*t)+b;
		if(t<(2/2.75)) return c*(7.5625*(t-=(1.5/2.75))*t+.75)+b;
		if(t<(2.5/2.75)) return c*(7.5625*(t-=(2.25/2.75))*t+.9375)+b;
		return c*(7.5625*(t-=(2.625/2.75))*t+.984375)+b;
	}
});

