$("dt.gallery-icon a").each(function(){
	$(this).attr('rel','superbox\[gallery\]');
	$(this).attr('class','gallery-slink');
});

var currentmax = 2000;

$(window).resize(function(){
	if($('div#superbox').hasClass('image')){
		var w = ((document.body.clientWidth - 40) > currentmax) ? currentmax : document.body.clientWidth - 40;
		document.getElementById("superbox").style.width = w+"px";
		document.getElementById("superbox-innerbox").style.width = "100%";
	}
});

$("a#pano").live("click", function(){
	currentmax = this.className.split('w-')[1].split(' ')[0];
});

$("a#supersize").live("click", function(){
	currentmax = this.className.split('w-')[1].split(' ')[0];
});

$("a#editclink").live("click", function(){
	document.getElementById("captioneditor").style.display = "block";
	return false;
});

$("a#capcancel").live("click", function(){
	document.getElementById("captioneditor").style.display = "none";
});

$("a.hijax,div.fullview li.ln a,a#back2thumbs").live("click", function(){
	$('div#thephotocontent').fadeOut();

	var path = $(this).attr('href');
	var oldhtml = $('div#thephotocontent').html();
	var oldloco = document.location.href.replace(/.*:\/\/[^\/]+/,'');

	if(!hijaxHistory[oldloco]){
		hijaxHistory[oldloco] = oldhtml;
	}
	
	$.get(
		path,'',function(data){
			data = $(data).find('div#photocanvas').html();
			$(document).find('div#photocanvas').html(data);

			$('div#thephotocontent').fadeIn();

			if(window.history.pushState){
				hijaxHistory[path] = data;
				window.history.pushState('', '', path);
			}
		},
		"html"
	);
	return false;
});

// popstate watches back/fwd button clicks and responds as needed
if(window.history.pushState){
	window.onpopstate = function(event) {
		(function($) {
			var newstate = window.location.href.replace(/.*:\/\/[^\/]+/,'');
			if(hijaxHistory[newstate]){
				$('div#photocanvas').html(hijaxHistory[newstate]);
			}
		})(jQuery);
	}
}

console.log('v9');

var hijaxHistory = [];

$("a.gallerynav").live("click", function(){

	var img = $(this).attr('href').split('.jpg')[0];

	var ogal = $('div#'+$(this).parent().attr('class').split('cgallery-')[1]);
		
	var olnk = ogal.find('a[href="'+$(this).attr('href')+'"]');

	var cap = (olnk.parent().parent().find('dd.gallery-caption').text()) ? '<div id="photocaption"><div><p>'+olnk.parent().parent().find('dd.gallery-caption').text()+'</p></div></div>' : "";
	var tpic = olnk.parent().parent().parent().find('a').length;
	var cpic;

	olnk.parent().parent().parent().find('a').each(function(i){
		if ($(this).attr('href') == olnk.attr('href')){
			cpic = i + 1;
		}
	});
	
	var ppic = cpic - 1;					
	var npic = cpic + 1;					
	var plink = '';
	var nlink = '';

	if (ppic > 0){
		plink = '<a href="'+olnk.parent().parent().parent().find("a:eq("+(ppic - 1)+")").attr('href')+'" class="gallerynav" id="previmg">prev</a>'; 
	}

	if (npic <= tpic){
		nlink = '<a href="'+olnk.parent().parent().parent().find("a:eq("+(npic - 1)+")").attr('href')+'" class="gallerynav" id="nextimg">prev</a>'; 
	}

	$(document).find('div#superbox-innerbox').html('\
	<div id="photocanvas" class="wpgallery">\
	<div class="photoinfo"><div class="photonum"><span>'+cpic+'/'+tpic+'</span></div></div>\
	<div id="thephoto"><div id="thephotoInner"><div id="thephotoborder"><div id="thephotoloader"><div id="thephotocontent" class="'+$(this).parent().attr('class')+'">\
	'+plink+'\
	'+nlink+'\
	<img style="margin-top:0px" src="'+img+'-869x587.jpg">\
	'+cap+'\
	</div></div></div></div></div>\
	</div>\
	');

	return false;
});



$(document).ready(function(){
	$.superbox();
});

