//nospam for email
IE_fix = navigator.userAgent.indexOf('MSIE') > 0 ? true : false;

function noSpam(user,domain) {
	locationstring = "mailto:" + user + "@" + domain;
	window.location = locationstring;
} function noSpamPrint(user,domain) {
	document.write(user + "@" + domain);
}

//Keith Claunch Photography image scaling functions
function scaleThumbs() {
	//resize thumbs
	var doc = document.viewport.getDimensions()
	var doc_proportions = (doc.height-90) / doc.width;
	if (doc_proportions > 0.66) {
		$('thumbs').getElementsBySelector('img').each(function(thumb) {
			thumb.width = ($('thumbs').getWidth()/Grid_cols)-16;
			thumb.height = thumb.width*0.6666;
		});
		$('thumbs').setStyle({ paddingLeft: '0px', paddingRight: '0px' });
	} else {
		$('thumbs').getElementsBySelector('img').each(function(thumb) {
			thumb.height = ((doc.height-100)/Grid_rows)-15;
			thumb.width = thumb.height*1.5;
			THUMB_WIDTH = thumb.width+16;
		});
		var scaled_box_width = THUMB_WIDTH * Grid_cols;
		var extra_horiz_space = (doc.width - scaled_box_width - 30)/2;
		$('thumbs').setStyle({ paddingLeft: extra_horiz_space+'px', paddingRight: extra_horiz_space+'px'});
	}
}

function scaleViewer() {
	//resize viewer photo (large)
	var doc = document.viewport.getDimensions()
	var doc_proportions = (doc.height-90) / doc.width;
	if (doc_proportions > 0.66) {
		$('photo').width = $('viewer').getWidth()-20;
		$('photo').height = $('photo').width*0.6666;
		$('viewer').setStyle({ paddingLeft: '0px', paddingRight: '0px' });
	} else {
		$('photo').height = doc.height-100 -20;
		$('photo').width = $('photo').height*1.5;
		
		var extra_horiz_space = (doc.width - $('photo').width -50)/2;
		var padding_px = extra_horiz_space+'px';
		$('viewer').setStyle({ paddingLeft: padding_px, paddingRight: padding_px });		
	}
}

function scaleText () {
	//resize text box (contact, about, etc.)
	var doc = document.viewport.getDimensions()
	var doc_proportions = (doc.height-90) / doc.width;
	if (doc_proportions > 0.66) {
		var t = { }; var i = { };
		var t_width = doc.width-20 -20;
		var t_height = t_width*0.6666;
		t.width = (t_width)+'px';
		t.height = i.height = (t_height)+'px';
		i.width = (t_width/2)+'px';
		
		t.paddingLeft = t.paddingRight = '0px';
		$('text').setStyle(t); $('text_image').setStyle(i);
	} else {
		var t = { }; var i = { };
		var t_height = doc.height-100 -20;
		var t_width = t_height*1.5;
		var extra_horiz_space = (doc.width - t_width -50)/2;
		var padding_px = extra_horiz_space+'px';
		
		t.height = i.height = (t_height)+'px';
		t.width = (t_width)+'px';
		i.width = (t_width/2)+'px';
		
		t.paddingLeft = t.paddingRight = padding_px;
		$('text').setStyle(t); $('text_image').setStyle(i);	
	}
}

function scaleDoc() {
	if ($('thumbs') && $('thumbs').visible()) {
		scaleThumbs();
		$('image_space').setStyle({ height: $('thumbs').getHeight()+'px' });
	}
	
	if ($('viewer') && $('viewer').visible()) {
		scaleViewer();
		$('image_space').setStyle({ height: ($('viewer').getHeight()+14)+'px' });
	}
	
	if ($('text') && $('text').visible()) {
		scaleText();
		$('image_space').setStyle({ height: ($('text').getHeight()+14)+'px' });
	}
}

function loadImage(img_id) {
	var options = Object.extend({ displayAfterLoad: true }, arguments[1] || { });
	
	if (!Image_details.get(img_id).get('loaded')) {
		if (options.displayAfterLoad) {
			//prog bar show+start
			$('load_status_container').show();
			progress_scale = new Effect.Scale('load_status',1000,{scaleY: false, duration: 12, queue: {scope: 'loadqueue'}, afterFinish: function() {$('load_status').width = "60px";} });
		}
		
		//load in image to cache
		var img_url = Image_details.get(img_id).get('url');
		var myimg = new Image();
		Event.observe(myimg,'load',function() {
			Image_details.get(img_id).set('loaded',true);
			
			if (options.displayAfterLoad) {
				displayImage(img_id);
				
				//prog bar hide+reset
				progress_scale.cancel();
				new Effect.Fade('load_status_container', { duration: 0.25, queue: {scope: 'loadqueue', position: 'end'}, 
					afterFinish: function() { $('load_status').style.width = '60px'; }
				});
			}
		});
		myimg.src = img_url;
	}
}

function displayImage(img_id) {
	updatePageUrlVariable();
	
	if ($('thumbs').visible()) new Effect.Fade('thumbs',{duration: 0.25, queue: 'end'});
	new Effect.Fade('viewer',{duration: 0.25, queue: 'end',
		afterFinish: function() {
			//pull image from cache to page
			if (Image_details.get(img_id) != null) {
				if (Image_details.get(img_id).get('loaded')) { 
					
					$('photo').src = $('base_element').href+Image_details.get(img_id).get('url');
					//alert($('photo').src);
					$('inspect_txt').innerHTML = Image_details.get(img_id).get('title');
					
					var this_rank = Image_details.get(img_id).get('rank');
					var next_img_rank = (Image_details.values().find(function(e) {return e.get('rank') == this_rank+1;}) ? this_rank+1 : 1);
					var prev_img_rank = (Image_details.values().find(function(e) {return e.get('rank') == this_rank-1;}) ? this_rank-1 : Image_details.values().max(function(e) {return e.get('rank');}));
					
					var next_img_id = Image_details.find(function(val,id) { return val[1].get('rank') == next_img_rank } )[0];
					var prev_img_id = Image_details.find(function(val,id) { return val[1].get('rank') == prev_img_rank } )[0];
					
					var next_img_url = $('next_link').pathname+'#'+next_img_id;
					var prev_img_url = $('prev_link').pathname+'#'+prev_img_id;
					
					//the following hostname, http, etc. is needed only for IE - otherwise could just use next_img_url var
					if (IE_fix) {
						$('next_link').href = $('photo_link').href = 'http://'+window.location.hostname+'/'+next_img_url;
						//'http://'+window.location.hostname+'/'+next_img_url;
						$('prev_link').href = 'http://'+window.location.hostname+'/'+prev_img_url;
					} else {
						$('next_link').href = $('photo_link').href = next_img_url;
						// = next_img_url;
						$('prev_link').href = prev_img_url;
					}
					
					
					new Effect.Appear('viewer',{duration: 0.75, queue: 'end', afterUpdate: scaleViewer, 
						afterFinish: function() {
							if (!$('inspector').visible()) {
								if (IE_fix) $('inspector').show();
								else new Effect.SlideIn('inspector', {duration: 0.5});
							}
						}
					});
					
					//preload next image
					loadImage(next_img_id,{ displayAfterLoad: false });
					
				} else loadImage(img_id);
			} else displayThumbs();
		}
	});
	return false;
}

function displayThumbs() {
	updatePageUrlVariable();
	
	if ($('viewer').visible()) new Effect.Fade('viewer',{duration: 0.25, queue: 'end'});
	if (!$('thumbs').visible()) new Effect.Appear('thumbs',{duration: 0.75, delay: 0.5, queue: 'end', afterUpdate: scaleThumbs});
	$('inspector').hide();
}

function loadPageFromUrl() {
	if (window.location.hash != '') displayImage(window.location.hash.sub('#',''));
	else displayThumbs();
	
	scaleDoc();
	updatePageUrlVariable();
}
function updatePageUrlVariable() { Current_url = window.location.hash; }