function set_photo_div_width() {
	var outdiv_width = $('#listphotos').width();
	var no_thumbs = Math.floor(outdiv_width/200);
	var indiv_width = 200 * no_thumbs;
	$('#innerlistphotos').width(indiv_width);
	//$('#innerlistphotos').width(200 * Math.floor($('#listphotos').width()/200));
}

function set_inner_index_width() {
	var total_no_teasers = 8;
	var outdiv_width = $('#indexcontent').width();
	var no_teasers = Math.floor(outdiv_width/250);
	if (no_teasers < 1) no_teasers = 1;
	var indiv_width = 250 * no_teasers;
	$('#innerindexcontent').width(indiv_width);
	
	// set the number of visible teasers
	var no_visible_teasers = Math.floor(total_no_teasers/no_teasers) * no_teasers;
	for (i = 0; i < total_no_teasers; i++)
	{
		var thisid = '#index' + i;
		if (i < no_visible_teasers)
		{
			$(thisid).css({'visibility':'visible','display':'block'});
		}
		else
		{
			$(thisid).css({'visibility':'hidden','display':'none'});
		}
	}	
}

// the two following functions are used by jQuery-ui autocomplete
function split(val) {
	return val.split( /,\s*/ );
}

function extractLast(term) {
	return split(term).pop();
}

$(window).load(function () {

	set_photo_div_width();
	
	set_inner_index_width();
	
	
	$('#openlogin').attr('href','#');
	$('#openregister').attr('href','#');
	$('#slide_show').fadeOut(0);
	$('#slide_show_photo_container').fadeOut(0);
	
	$(window).resize(function() {
		set_photo_div_width();
		set_inner_index_width();
	});

	$('html').click(function() {
		$('#register-form').css('display','none');
		$('#login-form').css('display','none');
	});
	
	$('#register-form').click(function(event) {
		event.stopPropagation();
	});
	
	$('#login-form').click(function(event) {
		event.stopPropagation();
	});
	
	$('#openlogin').click(function(event) {
		event.stopPropagation();
		$('#register-form').css('display','none');
		if ($('#login-form').css('display') == 'none')
		{
			$('#login-form').css('display','block');
			$('input[name="username"]').focus();
		}
		else
			$('#login-form').css('display','none');
	});
	
	$('#openregister').click(function(event) {
		event.stopPropagation();
		$('#login-form').css('display','none');
		if ($('#register-form').css('display') == 'none')
		{
			$('#register-form').css('display','block');
			$('input[name="first_name"]').focus();
		}
		else
			$('#register-form').css('display','none');
	});
	
	$('.closeform').click(function() {
		$('#login-form').css('display','none');
		$('#register-form').css('display','none');
	});
	
	
	
	// jquery ui autocomplete function

	$(".textareatags")
		// don't navigate away from the field on tab when selecting an item
			// don't navigate away from the field on tab when selecting an item
			.bind( "keydown", function(event) {
				if (event.keyCode === $.ui.keyCode.TAB &&
					$(this).data("autocomplete").menu.active) {
					event.preventDefault();
				}
			})
			.autocomplete({
				source: function( request, response ) {
					var thisid = this.element.attr('id');
					$.getJSON("../js/ajax.autocompletetags.php"
						     ,{ term:extractLast(request.term), thiscat: thisid }
						     ,response);
				},
				search: function() {
					// custom minLength
					var term = extractLast(this.value);
					if (term.length < 1) {
						return false;
					}
				},
				focus: function() {
					// prevent value inserted on focus
					return false;
				},
				select: function( event, ui ) {
					var terms = split( this.value );
					// remove the current input
					terms.pop();
					// add the selected item
					terms.push(ui.item.value);
					// add placeholder to get the comma-and-space at the end
					terms.push("");
					this.value = terms.join(", ");
					return false;
				}
			});
			
	// on contact form, make e-mail mandatory if copy is requested
	$("#checkcopy").change(function() {
		if ($(this).attr('checked'))
		{
			$("#emailcopy").css('visibility','visible');		
		}
		else
		{
			$("#emailcopy").css('visibility','hidden');
		}
	});
	
});


