//global variables
var ih; // image height
var iw; // image width
var mh = 600; // min height
var mw = 810; // min width
var wh; // window height
var ww; // window width
var rd = 0; //resize duration initial (0 ms)
var rdg = 100; //resize duration grow (ms)
var rds = 400; //resize duration shrink (ms)
var snap = 0.06; // intolerance for skinny black bars (0-1)
var ch; //current image height
var cw; //current image width
var hw; //header width
var t = 0; //number of images in the gallery
var s; //thumbnail width
var m; //thumbnail left margin (all but first image
var n //number of thumbnails in set
var f = 1;//image number of the first image being shown
var selected_image_number = 0;
var mx; //mouse x
var my; //mouse y

$(document).ready(function(){
						   
	if ( ($.browser.msie && $.browser.version==7) ) {				   
		//fix ie7 z index issues
		var zIndexNumber = 1000;
		$('div').each(function() {
			$(this).css('zIndex', zIndexNumber);
			zIndexNumber -= 10;
		});
	}

	if (page_type=='gallery') {
		//gallery pages

		$(document).mousemove(function(e){
			mx = e.pageX;
			my = e.pageY;
			show_caption(mx, my);
		});
		$(function() { $(this).bind("contextmenu", function(e) {
			e.preventDefault(); });
		});

		$('div#jk_gallery_img span, div#jk_gallery_img img').css( {position:'absolute'} );
		m=parseInt($('div#jk_gallery_thmbs img:last-child').css('margin-left')); //margin
		s=$('div#jk_gallery_thmbs img:first-child').width(); //side length of square thumbnail
		t = $('div#jk_gallery_thmbs img').size(); //total number of thumbnails
		$('div#jk_gallery_thmbs_window').css({'overflow':'hidden'});
		$('div#jk_gallery_thmbs').width( t*(s+m) - m ); //total number of thumbnails
		$('div#jk_gallery_thmbs_window').click( function() {} ); //makes iphone work
		$('div#jk_gallery_thmbs_window').scrollLeft(0);
		
		$('div#jk_gallery_img *').hide();
		$('div#jk_gallery_img img:first-child').show();

		$('img#jk_next').click(function() {		
			if (selected_image_number < $('div#jk_gallery_img img').size() )
			$('div#jk_gallery_thmbs img').eq(selected_image_number+1).click();
		})
		$('img#jk_prev').click(function() {		
			if (selected_image_number>0)
			$('div#jk_gallery_thmbs img').eq(selected_image_number-1).click();
		})
		$('img#jk_next_set').click(function() {
			f = (f+2*n>t?t-n+1:f+n);
			show_hide_set_arrows();
			$('div#jk_gallery_thmbs_window').stop().animate( {scrollLeft:(f-1)*(s+m)} );
		});
		$('img#jk_prev_set').click( function() {
			f = (f<n?1:f-n);
			if (f<1) f=1;
			show_hide_set_arrows();
			$('div#jk_gallery_thmbs_window').stop().animate( {scrollLeft:(f-1)*(s+m)} );
		});
		show_hide_set_arrows = function() {
			if (f<=1) $('img#jk_prev_set').stop().animate({opacity:0}).css({cursor:'default'});
			else $('img#jk_prev_set').stop().animate({opacity:1}).css({cursor:'pointer'});
			if (f+n>t) $('img#jk_next_set').stop().animate({opacity:0}).css({cursor:'default'});
			else $('img#jk_next_set').stop().animate({opacity:1}).css({cursor:'pointer'});
		}
		show_hide_set_arrows(); //initial
		
		$('div#jk_gallery_thmbs img').click( function() {
			clear_all_captions();
			var selectedimage = this;
			var previous_selected_image_number = selected_image_number;
			$(selectedimage).parent().find('img').each( function(i,image){
				if (selectedimage==image) {
					selected_image_number = i;
					var selectedBigImage = $('div#jk_gallery_img img').eq(i);
					if (i != previous_selected_image_number) {
						$('div#jk_gallery_img img').stop().fadeOut('fast');
						selectedBigImage.stop().fadeIn('slow', function(){
							//after the animation, reset everything
							selectedBigImage.show().css('opacity',1);
							selectedBigImage.siblings().stop().css({display:'none',opacity:1});
							show_caption(mx, my);
						});
					}
				}
			})
			if (selected_image_number == 0 ) $('img#jk_prev').stop().animate({opacity:0}).css({cursor:'default'});
			else $('img#jk_prev').stop().animate({opacity:1}).css({cursor:'pointer'});
			if (selected_image_number == $('div#jk_gallery_img img').size()-1 ) $('img#jk_next').stop().animate({opacity:0}).css({cursor:'default'});
			else $('img#jk_next').stop().animate({opacity:1}).css({cursor:'pointer'});
			//show_caption(mx, my);
		});
		$('div#jk_gallery_thmbs img:first-child').click();
		
		$(document).keydown(function(e){
			// see http://ejohn.org/blog/keypress-in-safari-31/ on why keypress is bad
			c = e.which;
			//188 is the comma and the less than sign. 37 is the left arrow.
			//190 is the period and the gt sign. 39 is the right arrow.
			if (c == 188 || c == 37) $('#jk_prev').hover().click();
			if (c == 190 || c == 39) $('#jk_next').hover().click();
		});
	} else {
		//not a gallery page
		ih = $('#jk_background img').height();
		iw = $('#jk_background img').width();
	}
	
	//resize window
	$(window).resize();
	
});

//caption functions
clear_all_captions = function() {
	$('div#jk_gallery_img span').hide();
}
show_caption = function(x, y) {
	var position=$("div#jk_gallery_img img").eq(selected_image_number).offset()
	var left = position.left
	var top = position.top
	var right = left + $("div#jk_gallery_img img").eq(selected_image_number).width()
	var bottom = top + $("div#jk_gallery_img img").eq(selected_image_number).height()
	if (x > left && x < right && y > top && y < bottom) {
		$('div#jk_gallery_img span').eq(selected_image_number).css({display:'inline',opacity:0.7});
	} else {
		$('div#jk_gallery_img span').eq(selected_image_number).hide();
	}	
}

$(window).resize(function() {
	if (page_type=='gallery') resize_gallery();
	else resize_background();
});

adjust_window_and_header = function() {
	//called by both non gallery and gallery pages
	if ($(window).height() > wh || $(window).width() > ww) rd = rdg;
	wh = $(window).height();
	ww = $(window).width();
	if ( !($.browser.msie && $.browser.version==7) ) {		
		if (wh < mh) { wh = mh; $('body').css('overflow-y','auto'); } else { if (page_type != 'blog') $('body').css('overflow-y','hidden'); }
		if (ww < mw) { ww = mw; $('body').css('overflow-x','auto'); } else { $('body').css('overflow-x','hidden'); }
	} else {
		if (wh < mh) wh = mh;
		if (ww < mw) ww = mw;
	}
	hw = (ww/wh > 1.35 ? wh*1.35-200 : ww-200);
}

resize_background = function() {
	//for non gallery pages
	var fill;
	adjust_window_and_header();
	fill = ( Math.abs(ww/wh - iw/ih)<snap ? true : false);
	if ( (ww/wh > iw/ih && !fill) || (ww/wh <= iw/ih && fill) ) {
		cw = wh*iw/ih;
		ch = wh;
	} else {
		cw = ww;
		ch = ww*ih/iw;
	}
	$('#jk_header').stop().animate( {width:hw}, rd, 'linear', reset_header_overflow);
	$('#jk_background img').stop().animate( {width:cw, height:ch}, rd );
	//for contact pages
	if (page_type=='contact') {
		//can be used for other pages with an absolutely positioned foreground in the future
		$('#jk_foreground').css( {width:cw} )
		//for the contact form
		$('#jk_contact_form').stop().animate({
				left:473*cw/1080,
				top:164*ch/800-89,
				width:450*cw/1080,
				height:255*ch/800
			},rd,'linear',function(){ $('#jk_contact_form').css( {overflowY:'auto'} ); }
		)
		$('#jk_contact_form textarea').stop().animate({
				height:100- (255-255*ch/800)
			},rd,'linear',function(){ $('#jk_contact_form textarea').css( {overflowY:'auto'} ); }
		)
	}
	rd = rds;
	if (page_type=='blog') {
		$('#jk_foreground').css( {width:cw } )
		
		$('#jk_blog img').each( function(i,image){
			//$(image).removeAttr('width');
			//$(image).removeAttr('height');
			bih = $(image).height(); //blog image height
			biw = $(image).width();
			var cwt = cw-200; //current width trimmed
			var f=false; //scaling factor
			if ( biw > 50) f = 0.75; //for scaling anything larger than 50 wide
			//if ( biw > cwt) f = 1;
			//else if ( biw > 50) f = 1; //for scaling anything larger than 50 wide
			//else if ( biw > cwt*0.75) f = 0.75;
			//else if ( biw > cwt*0.50) f = 0.50;
			//else if ( biw > cwt*0.25) f = 0.25;
			if (f) {
				$(image).width(cwt*f);
				$(image).height( (bih/biw)*cwt*f );
			}
		})
		$('body').css( {'overflow-y':'visible'} )
	}
}

resize_gallery = function() {
	//define variables
	var fh; // frame height for image
	var fw; // frame width for images
	var sm; // span margin left for caption
	var tw; // thumbnail width
	
	//adjust the window and the header
	adjust_window_and_header();
	
	//adjust css for, header, content, frame, thumbnails
	fw = hw;
	fh = wh-75-14-23-10-55-33;
	if ( fw/fh > 1.5) fh = fw/1.5;
	tw = (fw-100-100);		
	if ( (tw+m)%(s+m)>0) {
		tw = tw - (tw+m)%(s+m);
	}
	n = Math.floor((tw+m)/(m+s))
	if (f+n>t) f = t-n+1;
	
	$('#jk_header,#jk_gallery_content').stop().animate( {width:hw}, rd, 'linear', reset_header_overflow);
	$('#jk_gallery_thmbs_window').stop().animate( {width:tw,scrollLeft:(f-1)*(s+m) }, rd, 'linear' );
	
	$('#jk_gallery_img').stop().animate( {height:fh}, rd ); //replaced ch with frame height
	
	//adjust css for images and caption in the frame
	$('div#jk_gallery_img img').each( function(i,image){
		ih = $(image).height();
		iw = $(image).width();
		if ( fw/fh > iw/ih ) {
			cw = fh*iw/ih;
			ch = fh;
		} else {
			cw = fw;
			ch = fw*ih/iw;
		}
		sm = (fw-cw)/2;
		if ( $(image).is(':visible')) {
			$('#jk_gallery_img img:visible').stop().animate( {width:cw, height:ch}, rd );
			$('#jk_gallery_img span').eq(i).stop().animate( {'margin-left':sm, 'margin-top':(fh-ch)/2, 'max-width':cw-100}, rd );	
		} else {
			$(image).css( {width:cw, height:ch} );
			$('#jk_gallery_img span').eq(i).css( {'margin-left':sm, 'margin-top':(fh-ch)/2, 'max-width':cw-100} );
		}
		$(image).css( {'left':(fw-cw)/2} );			
		$(image).css( {'top':(fh-ch)/2} );			
	})
	rd = rds;
	show_hide_set_arrows();
}

//jquery changes the overflow on animate. this fixes the problem.
reset_header_overflow = function() { $('#jk_header').css( {overflow:'visible'} ); }
