//oh, pardon the messing coding, i leraned  Jquery part-way through creating the site!

screenW = 640, screenH = 480;
if (parseInt(navigator.appVersion)>3) {
 screenW = screen.availWidth;
 screenH = screen.availHeight;
} else if (navigator.appName == "Netscape" 
    && parseInt(navigator.appVersion)==3
    && navigator.javaEnabled() ) {
 var jToolkit = java.awt.Toolkit.getDefaultToolkit();
 var jScreenSize = jToolkit.getScreenSize();
 screenW = jScreenSize.width;
 screenH = jScreenSize.height;
}


$('a').hover(function() {

$(this).addClass($('mo_bold'));

});

//ZOOM FUNCTIONS


global_zoom_count = 0;
post_global_site_width = 0;
post_global_margin_width = 0;

g_frames = 25;
g_frame_animate_time = calc_anim_time(1);

function calc_anim_time(seconds) {

var ms = seconds * 1000;

return ms / g_frames;

}

function zoom_handle() {
	
	if(global_zoom_count % 2 == 0) {
		zoom_in();
	} else {
		zoom_out();
	}
	
	global_zoom_count++;
}

if ( window.addEventListener ) {
        var kkeys = [], konami = "90,79,79,77";
        window.addEventListener("keydown", function(e){
                kkeys.push( e.keyCode );
                if ( kkeys.toString().indexOf( konami ) >= 0 ) {
                        zoom_handle();
                        kkeys = new Array();
                        }
        }, true);
}

function zoom_in() {

	width_of_page = parseFloat(document.getElementById('projects').style.width);
	left_margin = parseFloat(document.getElementById('projects').style.marginLeft);

	step_margin = calculate_step(left_margin, 0, g_frames);
	step_width = calculate_step(width_of_page, $(document).width(), g_frames);

	for(var i = 1; i <= g_frames; i++) {

	//step width up
	setTimeout( "document.getElementById('projects').style.width = parseFloat(document.getElementById('projects').style.width) + " + step_width + ";", i * g_frame_animate_time);
	setTimeout( "document.getElementById('main').style.width = parseFloat(document.getElementById('main').style.width) + " + step_width + ";", i * g_frame_animate_time);

	//step margin down
	setTimeout( "document.getElementById('projects').style.marginLeft = parseFloat(document.getElementById('projects').style.marginLeft) + " + step_margin + ";", i * g_frame_animate_time);
	setTimeout( "document.getElementById('main').style.marginLeft = parseFloat(document.getElementById('main').style.marginLeft) + " + step_margin + ";", i * g_frame_animate_time);
	//ds_left has same math as margin
	//setTimeout( "document.getElementById('ds_left').style.width = parseFloat(document.getElementById('projects').style.marginLeft) + " + step_margin + ";", i * g_frame_animate_time);
	
	}
	
	setTimeout("post_global_site_width = document.getElementById('main').style.width", 1000);
	
	}//end test

function zoom_out() {

	width_of_page = parseFloat(document.getElementById('projects').style.width);

	//left_margin = parseFloat(document.getElementById('projects').style.marginLeft);
	left_margin = 0;

	step_margin = calculate_step(parseFloat(document.getElementById('projects').style.marginLeft), global_margin_width, g_frames);

	step_width = calculate_step(parseFloat(document.getElementById('projects').style.width), global_site_width, g_frames);

	for(var i = 1; i <= g_frames; i++) {

		//step width down
		setTimeout( "document.getElementById('projects').style.width = parseFloat(document.getElementById('projects').style.width) + " + step_width + ";", i * g_frame_animate_time);
		setTimeout( "document.getElementById('main').style.width = parseFloat(document.getElementById('main').style.width) + " + step_width + ";", i * g_frame_animate_time);

		//step margin up
		setTimeout( "document.getElementById('projects').style.marginLeft = parseFloat(document.getElementById('projects').style.marginLeft) + " + step_margin + ";", i * g_frame_animate_time);
		setTimeout( "document.getElementById('main').style.marginLeft = parseFloat(document.getElementById('main').style.marginLeft) + " + step_margin + ";", i * g_frame_animate_time);
		
	}
	
	
	setTimeout( "document.getElementById('ds_left').style.width = " + global_margin_width + ";", g_frame_animate_time * g_frames);
	
	
	}//end test1
function calculate_step(from, to, by) {
	return (to - from) / by;
}


function h_view_image(thumb) {

//the IE backup-plan
document.getElementById('shade_layer').style.opacity = .5;

	//loading Q
	document.getElementById('nav_status').innerHTML = '<span style="text-decoration:blink;color:#56c9db;">Loading...</span>';

	var lg_img = shortenSrc(thumb);
	
	//set title of image layer to image location (lg_img)
	//document.getElementById('il_img_name').innerHTML = lg_img;
	
	
	var prc_src_height = $(window).height();
	
	img_layer_top_margin = (prc_src_height *.05) / 2;
	
	document.getElementById('image_layer').style.height = prc_src_height * .95;
	document.getElementById('image_layer').style.marginTop = document.body.scrollTop + img_layer_top_margin;
	
	
	$('div#shade_layer').removeClass('hide');
	$('div#image_layer').show('slow', function() {
	
		document.getElementById('il_image_container').style.height = parseFloat(document.getElementById('image_layer').offsetHeight) - parseFloat(document.getElementById('il_top').offsetHeight);

	
	$('body').addClass('control_overflow');
	
	
	document.getElementById('shade_layer').style.marginTop = document.body.scrollTop;
	
	
	
	//size up image layer
	//document.getElementById('il_image').style.height = document.getElementById('image_layer').offsetHeight - document.getElementById('il_top').offsetHeight;
	
	//set image
	document.il_image.src = lg_img;
	
		$(window).scroll( function() {
			document.getElementById('shade_layer').style.marginTop = document.body.scrollTop;
			document.getElementById('image_layer').style.marginTop = document.body.scrollTop + img_layer_top_margin;
		});
	});
}//h_view_imag


function shortenSrc(thumb) {//may contain thumb or not, expects FULL src location

//RESOLVE IMAGE LOCATION
	var str = thumb.src;
	var arr = str.split("/");
	
	//array elements 3, 4 and 5 provide location\
	if (arr[5].indexOf('thumb_') == 0 ){
	var lg_img = '/' + arr[3] + '/' + arr[4] + '/' + arr[5].substring(6);
	} else {
	var lg_img = '/' + arr[3] + '/' + arr[4] + '/' + arr[5];
	}
	
	return lg_img;
}

$( function() {/*JQUERY*/
	$('td.h_nav').hover( function() {
	
		$(this).css('background-color', '#DDDDDD');
		
		}, function() {
	
		$(this).css('background-color', '#FFFFFF');
	
	});
	
	
	$('div#shade_layer').click(function() {
	h_close_image();
	});	
	
	
	$('img').load(function() {
		fix_ie_junk();
	});
	

	//new img.clickable function
	$('img.clickable').click(function() {
		h_view_image(this);
	});
	
	//image layer NEXT button
	$('td#il_next').click(function() {
		
		//whats the current image
		var current = shortenSrc(document.getElementById('il_image'));
		
		//loading Q
		document.getElementById('nav_status').innerHTML = '<span style="text-decoration:blink;color:#56c9db;">Loading...</span>';
		
		//collect clickable items into array, filter out 'thumb_'
		var img_arr = new Array();
		img_arr = document.getElementsByClassName('clickable');
		
		var match = '';
		
		//find current index
		for(var i = 0; i < img_arr.length; i++){
			if(shortenSrc(img_arr[i]) == current)
			match = i;
		}
		
		//remove old img
		document.il_image.src = '';
		
		if ((match + 1) >= img_arr.length) {document.il_image.src = shortenSrc(img_arr[0]);	} else {document.il_image.src = shortenSrc(img_arr[match + 1]);}
		
	});
	
$('td#il_back').click(function() {
		
		//whats the current image
		var current = shortenSrc(document.getElementById('il_image'));
		
		//loading Q
		document.getElementById('nav_status').innerHTML = '<span style="text-decoration:blink;color:#56c9db;">Loading...</span>';
		
		//collect clickable items into array, filter out 'thumb_'
		var img_arr = new Array();
		img_arr = document.getElementsByClassName('clickable');
		
		var match = '';
		
		//find current index
		for(var i = 0; i < img_arr.length; i++){
			if(shortenSrc(img_arr[i]) == current)
			match = i;
		}
		
		//find prev image
		if ((match - 1) < 0) {//if last image was selected
			//load src to last img
			document.il_image.src = shortenSrc(img_arr[img_arr.length - 1]);
			
		} else {
			//otherwise, load as prev src in array
			document.il_image.src = shortenSrc(img_arr[match - 1]);
		}
		
	});
	
$('td#il_x').click(function() {
	h_close_image();
});

$('img#il_image').load(function() {

document.getElementById('nav_status').innerHTML = '';

});

$('div#shade_layer').click( function() {
h_close_image();
});
	
	$('img.hrule:first').css('margin-top', '0px');
	
});/*END JQUERY*/

function h_close_image() {


	$('div#image_layer').hide();
	
	$('div#shade_layer').addClass('hide');
	//$('div#image_layer').addClass('hide');
	
	$('body').removeClass('control_overflow');
	
	document.il_image.src = '';
	
}


//once document has finished loaded, attempt to preload larger images
var arr_img = document.getElementsByClassName('clickable');
var arr_load = new Array(200);

for(var i = 0; i < arr_img.length; i++)
{
arr_load[i] = new Image();
arr_load[i].src = shortenSrc(arr_img[i]);
}