var speed = 250;
var limit = 12;
var offset = 0;

$(document).ready(function(){				

	_preload("images/nav_about_on.gif,images/nav_babies_on.gif,images/nav_blog_on.gif,images/nav_contact_on.gif,images/nav_engagements_on.gif,images/nav_home_on.gif,images/nav_packages_on.gif,images/nav_portraits_on.gif,images/nav_sample_engagement_on.gif,images/nav_sample_wedding_on.gif,images/nav_thank_you_notes_on.gif,images/nav_weddings_on.gif", false);

	//quit if there are no galleries
	if(!$('#list').length)
		return;

	var i = 0;
	var list = $('#list').html();
	var arr = list.split(',');

	if($('#thumbs').length){

		// complex gallery
		_thumbs_pagination(arr);

		_gallery_preload(arr, offset, limit, true);

		//var t1 = function() { _gallery_preload(arr, offset, limit, false);        }; // these images
		//var t2 = function() { _gallery_preload(arr, offset + limit, limit, true); }; // preload next page of thumbs
		//setTimeout(t1, 1000);
		//setTimeout(t2, 1600);
	}
	else{
		// simple gallery preload
		$('#prev').hide();
		_preload(list, false); 
	}


	// simple gallery - next image
	$('#next').click(function(){

		$('#feature').fadeOut(speed, function() {

			i++;
			if(i > arr.length - 1)
				i = arr.length - 1;
			
			if(i == arr.length - 1)
				$('#next').hide();
			
			$('#prev').show();
			$('#feature').attr("src", arr[i]);
			$('#feature').fadeIn(speed);	

		});

		return false;

	});


	// simple gallery - previous image
	$('#prev').click(function(){

		$('#feature').fadeOut(speed, function() {

			i--;
			if(i < 0)
				i = 0;
			
			if(i == 0)
				$('#prev').hide();

			$('#next').show();
			$('#feature').attr("src", arr[i]);
			$('#feature').fadeIn(speed);	
		
		});
		return false;

	});

	// pagination
	$('#page_next').click(function(){

		offset = offset + limit;
		_thumbs_pagination(arr);
		_swap(arr[offset]);

		_gallery_preload(arr, offset, limit, true);
		// preload only seems to allow a certain about of images so minimize # of images loaded
		//var t1 = function() { _gallery_preload(arr, offset, limit, false);        }; // these images
		//var t2 = function() { _gallery_preload(arr, offset + limit, limit, true); }; // preload next page of thumbs
		//setTimeout(t1, 1000);
		//setTimeout(t2, 1600);

		return false;

	});

	// pagination
	$('#page_prev').click(function(){

		offset = offset - limit;
		if(offset < 0)
			offset = 0;
		_thumbs_pagination(arr);
		_swap(arr[offset]);

		return false;

	});



});


function _gallery_preload(arr, start, cnt, is_thumb){

	var i, img, list = "";

	for(i = start; i < arr.length && cnt > 0; i++){
		img = arr[i];
		if(is_thumb)
			img = img.replace(/\/([^\/]+$)/, '/thumbs/$1');
		list = list + img + ",";
		cnt--;
	}

	if(list.length > 0)
		list = list.substr(0, list.length - 1);

	_preload(list, false);
	return list;

}

function _swap(src){
	
	// using onclick functions so i don't have to figure out how to rebind events after loading html
	$('#feature').fadeOut(speed, function() {

		$('#feature').attr("src", src.replace(/\/thumbs\//, '/')); // remove thumb path

		$('#feature').load(function() {
			$('#feature').fadeIn(speed);
		});		
		
	});

	return false;

}


function _thumbs_pagination(arr){

	var i = 0;
	var j = 0;
	var html = "";

	// quit if we're not on a thumbs page
	if(!$('#thumbs').length)
		return; 

	// hide previous link
	if(offset < limit)
		$("#page_prev").hide();
	else
		$("#page_prev").show();

	// hide next link
	if(offset + limit > arr.length)
		$("#page_next").hide();
	else
		$("#page_next").show();

	for(i = offset; i < arr.length; i++){

		j++;
		if(j > limit)
			break;

		// add thumb path
		html = html + "<img src='" + arr[i].replace(/\/([^\/]+$)/, '/thumbs/$1') + "' onclick='return _swap(this.src);' onmouseover='return _swap(this.src);' width='69' height='69' />";
	}

	$('#thumbs').html(html);

}



var i_preload = 0;
var arr_preload = new Array(); 
function _preload(list, reverse){

	if(!list)
		return;

	if(list.length <= 0)
		return;

	var i, arr;
	arr = list.split(',');

	// images can be loaded in reverse as an optimization. 
	// some galleries may have the last images near the navigation, in this case you want the last images loaded first.
	if(reverse){
		for(i = arr.length - 1; i >= 0; i--){
			arr_preload[i_preload] = new Image();
			arr_preload[i_preload].src = arr[i];
			i_preload++;
		}
	}
	else{
		for(i = 0; i < arr.length; i++){
			arr_preload[i_preload] = new Image();
			arr_preload[i_preload].src = arr[i];
			i_preload++;
		}
	}
}


// generic client side validation 
function _validate(list, forms_index){

	if(!forms_index){
		forms_index = 0;
	}

	var arr = list.split("|");

	// display instructions on how to use this function
	if(arr.length == 0 || arr.length % 2 != 0){
		alert("programmer error: _validate()'s 1st argument; a string of items to validate in the following paired format: 'field_name1|title1|field_name2|title2'");
		return false;
	}

	var i = 0;
	var j = 0;
	var title = "";
	var field_name = "";
	var field_value = "";
	var obj = null;
	var msg = "";
	var focused = false;
	var focusable = true;
	var regexp = '';
	var error_bg_color = "#FFAAAA"; 

	for(i = 0; i < arr.length; i++){

		focusable = true;
		field_name = arr[i];
		field_value = "";
		i++;
		title = arr[i];
		obj = null;
		
		obj = eval("document.forms[" + forms_index + "].elements['" + field_name + "']");

		if(!obj){
			alert("programmer error: _validate() cannot find input named " + field_name + "\nAlso, check forms_index: " + forms_index);
			return false;
		}

		// get value from radio, select or text
		if(obj.type == "select-one"){
			field_value = obj.options[obj.selectedIndex].value;  // select boxes
		}
		else if(obj.length > 1){
			focusable = false;
			for(j = 0; j < obj.length; j++){
				if(obj[j].checked){
					field_value = obj[j].value; 
				}
			}
		}
		else{	
			field_value = obj.value; // text inputs 
		}

		// error messages
		if(field_value.length == 0){
			msg = msg + title + " is required\n";
			if(obj.style){
				obj.style.backgroundColor = error_bg_color; 
			}
			if(!focused && focusable){
				obj.focus();
			}
		}
		else if(field_name.match(/e-?mail/i)){
			if(!field_value.match(/^([a-z0-9_\.\-])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+$/i)){
				msg = msg + title + " appears to be invalid\n";
				if(obj.style){
					obj.style.backgroundColor = error_bg_color; 
				}
				if(!focused && focusable){
					obj.focus();
				}
			}
		}

		// flag so we only set focus once
		if(msg.length > 0)
			focused = true;
	}

	if(msg.length == 0){
		return true;
	}

	alert(msg);
	return false;

}

