/* BENWILT SCRIPTS */

jQuery.noConflict();

var global_valfield = '';

function setFocusDelayed() {
        document.getElementById(global_valfield).select();
        document.getElementById(global_valfield).focus();
}

function setFocus(field) {
  // save valfield in global variable
  global_valfield = field;
  setTimeout( 'setFocusDelayed()', 100 );
}



var main_image_original_width = 1700;
var main_image_original_height = 1139;
var pic_width = main_image_original_width;
var pic_height = main_image_original_height;

var more_info_showing = false;

var nav_panels = new Array("#bottom_div");


// navigation hiding/showing
function hide_nav() {
	for(i = 0; i < nav_panels.length; i++) {
		if (jQuery(nav_panels[i])) {
			jQuery(nav_panels[i]).fadeOut("slow");
		}
	}
	if (more_info_showing) {
			jQuery("#panel_3").fadeOut("slow");
	}
}

function show_nav() {
	for(i = 0; i < nav_panels.length; i++) {
		if (jQuery(nav_panels[i])) {
			jQuery(nav_panels[i]).fadeIn("slow");
		}
	}
	if (more_info_showing) {
			jQuery("#panel_3").fadeIn("slow");
	}
}

 
// emulate 'position: fixed;' for IE6
function move_box() {
	var main_image_element = document.getElementById('image_holder');
    main_image_element.style.top = (document.documentElement.scrollTop + 0) + 'px';
}

function show_more_info() {
	jQuery("#panel_3").css("display", "block");
	more_info_showing = true;
}

function hide_more_info() {
	jQuery("#panel_3").css("display", "none");
	more_info_showing = false;
}

function preload_project_images() {
	for(var i = 0; i < images.length; i++) {
		MM_preloadImages('/i/project_images/' + images[i] + '.jpg');
	}
}

function show_previous_image() {
	if (current_image_index > 0) {
		current_image_index--;
		change_image();
//		jQuery()[0].src = '/i/project_images/' + images[current_image_index];
	} else {
		current_image_index = (images.length - 1);
		change_image();
	}
}

function show_next_image() {
	if (current_image_index < (images.length - 1)) {
		current_image_index++;
		change_image();
//		jQuery()[0].src = '/i/project_images/' + images[current_image_index];
	} else {
		current_image_index = 0;
		change_image();
	}
}


function change_image() {
	var image_source = '/i/project_images/' + images[current_image_index] + '.jpg';
	jQuery("#main_image")[0].src = image_source;
	update_link();
}

function update_link() {
	jQuery("#current_image")[0].innerHTML = (current_image_index + 1);
}

function resize_image() {
	var win_width = jQuery(window).width();
//	alert(win_width);

	var win_height = jQuery(window).height();
//	alert(win_height);

	current_width = jQuery('#main_image').width();
	if (current_width > 1) {
		pic_width = current_width;
	} else {
		pic_width = pic_width;
	}
	
	current_height = jQuery('#main_image').height();
	if (current_height > 1) {
		pic_height = current_height;
	} else {
		pic_height = pic_height;
	}
			
	// make image fit in window height
	var new_height = win_height;
	var new_width = (win_height / pic_height) * pic_width;
	
//	alert(new_width);

	if (new_width > win_width) {
		new_width = win_width;
		new_height = (win_width / pic_width) * pic_height;		
	}
	
	if (main_image_original_width < new_width || main_image_original_height < new_height) {
		new_width= main_image_original_width;
		new_height = main_image_original_height;
	}

	jQuery('#main_image').width(new_width);
	jQuery('#main_image').height(new_height);

	
	jQuery('#image_holder').css({visibility:"visible", display:"block"});
	
	
	
}


function resize_image_with_cropping() {
	var win_width = jQuery(window).width();
	var win_height = jQuery(window).height();
	var win_aspect_ratio = win_width / win_height;

	current_width = jQuery('#main_image').width();
	if (current_width > 1) {
		pic_width = current_width;
	}	

	current_height = jQuery('#main_image').height();
	if (current_height > 1) {
		pic_height = current_height;
	}
	
	pic_aspect_ratio = pic_width / pic_height;
	
	if (win_aspect_ratio > pic_aspect_ratio) {
		// scale pic to fit window width
		var new_height = (win_width / pic_width) * pic_height;
		var new_width = win_width;
	} else {
		// scale pic to fit window height		
		var new_height = win_height;
		var new_width = (win_height / pic_height) * pic_width;
	}
	
	// center image
	new_top = 0 - ((new_height - win_height) / 2);
	new_left =  0 - ((new_width - win_width) / 2);
//	alert('new_top: ' + new_top + 'new_left: ' + new_left);
	jQuery('#main_image').css({top: new_top, left: new_left});

	jQuery('#main_image').css({height: new_height, width: new_width});
	jQuery('#image_holder').css({height: win_height, width: win_width});

	
	jQuery('#image_holder').css({visibility:"visible", display:"inline"});
	
	
}


function resize_image_non_linear() {
	var win_width = jQuery(window).width();
//	alert(win_width);

	var win_height = jQuery(window).height();
//	alert(win_height);

	current_width = jQuery('#main_image').width();
	if (current_width > 1) {
		pic_width = current_width;
	} else {
		pic_width = pic_width;
	}
	
	current_height = jQuery('#main_image').height();
	if (current_height > 1) {
		pic_height = current_height;
	} else {
		pic_height = pic_height;
	}
			
	// make image fit in window height
	var new_height = win_height;
	var new_width = win_width;
	
//	alert(new_width);

	jQuery('#main_image').width(new_width);
	jQuery('#main_image').height(new_height);

	
	jQuery('#image_holder').css({visibility:"visible", display:"block"});
	
	
	
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
