$(window).load(function(){			// wait to load images
//$(document).ready(function() {
//========================================================================================================================
//========================================================================================================================



/* classes
------------------------------------------------------------ */
var dialogs = $('.dialog');
$.each(dialogs, function(indexInArray, valueOfElement) {

	var id = $(valueOfElement).attr('id');
	switch (id) {
		case 'complete_dialog':
		case 'weeknight_dialog':
		case 'postpartum_dialog':
			$(valueOfElement).dialog({
				autoOpen: false,
				width: 500,
				height: 600,
				modal: true,
				buttons: {
					Send: function() {
						send_form($(valueOfElement).find('form'));
						$(this).dialog('close');
					},
					Cancel: function() {
						$(this).dialog('close');
						$(valueOfElement).find('form')[0].reset();
					}
				},
				close: function() {
					// alert('closing');
				}
			});
			break;
		case 'photos_slideshow_dialog':
			$(valueOfElement).dialog({
				autoOpen: false,
				width: 980,
				height: 720,
				modal: true,
				beforeClose: function() {
					$('img#slideshow_image').css('display','none');
					$('img#slideshow_image').attr('src','');
				}
			});			
			break;
		case 'coursebook_slideshow_dialog':
		case 'doulabook_slideshow_dialog':
			$(valueOfElement).dialog({
				autoOpen: false,
				width: 600,
				modal: true,
				beforeClose: function() {
					$('img#slideshow_image').css('display','none');
					$('img#slideshow_image').attr('src','');
				}
			});			
			break;
	}
});


open_dialog = function(dialog_selector) {
	$(dialog_selector).dialog('open');
}



/* faq
------------------------------------------------------------ */
$('a.faq_question').click(function(){
	var qnum = $(this).attr('id').substr(13);
	$('p#faq_answer_'+qnum).toggle();
});
$('p.faq_answer').prepend(function(index, html) {
	var qnum = $(this).attr('id').substr(11);
	return '<div class="faq_close_button"><a id="faq_close_'+qnum+'" class="faq_close_link" href="javascript:void(0);"><img src="/images/closeicon.png" /></a></div>';
});
$('a.faq_close_link').click(function() {
	var qnum = $(this).attr('id').substr(10);
	$('p#faq_answer_'+qnum).toggle();
});

$('p.faq_answer').hide();



/* send form
------------------------------------------------------------ */
send_form = function(form_selector) {
	var form_data = $(form_selector).serialize();

	$.ajax({												// http://api.jquery.com/jQuery.ajax
		type: 'POST',
		url: '/ajax/send_form_mail.php',
		data: form_data,
		dataType: 'text',									// xml, html, script, json, jsonp, text
		cache: true,
		beforeSend: function(XMLHttpRequest, settings) {	// return false to cancel request
			$('a#home_send_button').hide();
			$('img#home_ajax_loader').show();
		},
		success: function(result) {
			$('a#home_send_button').show();
			$('img#home_ajax_loader').hide();
			alert(result);
		},
		complete: function(XMLHttpRequest, textStatus) {
			$(form_selector)[0].reset();
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {			
			$('a#home_send_button').show();
			$('img#home_ajax_loader').hide();
		}
	});
}










//============================================================


expand = function (picnum) {
	$('div#slideshow_quote').html(desc_long[picnum]);
	$('div#slideshow_quote_box').css({
    	'padding-top':topmargin_long[picnum]+'px'
    });
}
next_slide = function() {
	currentpic += 1;
	// $('div#left_arrow').show();
	// if (currentpic == numpics-1) {
    //	$('div#right_arrow').hide();
	// }
	if (currentpic == numpics) {
    	currentpic = 0;
	}
	showpic(currentpic);
}
prev_slide = function() {
	currentpic -= 1;
	// $('div#right_arrow').show();
	// if (currentpic == 0) {
    //	$('div#left_arrow').hide();
	// }
	if (currentpic == -1) {
    	currentpic = numpics-1;
	}
	showpic(currentpic);
}
showpic = function(picnum) {
	if (numpics == 1) {
		$('div#right_arrow').hide();
		$('div#left_arrow').hide();
	}
	
	
	$('div#slideshow_quote_box').css({
    	'padding-top':topmargin_short[picnum]+'px'
    });
	$('div#slideshow_photo').html('<img alt="" src="'+pic[picnum].src+'"/>');
	$('div#slideshow_quote').html(desc_short[picnum]);
	$('div#slideshow_quote').css('display','block');
}


showpic(currentpic);



//============================================================

open_slideshow = function (slideshow_type, id) {
	$('div#'+slideshow_type+'_slideshow_dialog').dialog('open');
	run_slideshow(id);
}

//============================== SLIDESHOW ==============================

run_slideshow = function(first_slide_to_show){
	timer_fired = function() {
		slideshowengine.advance_slide_next();
	}

	/* slideshowengine
	------------------------------------------------------------ */
	var slideshowengine = new function() {
		this.preloaded_images = [];
		this.images_to_preload = 0;
		this.speed = 3000;
		//this.slideshow_photo_name = $('div#slideshow_photo_name');
		this.timer = '';
		this.currentslidenum = parseInt(first_slide_to_show);
		this.slideshow_image = $('img#slideshow_image');
		this.slideshow_image.parent().append('<div class="slideshow_loader"><img src="/images/ajax-loader2.gif"/></div>');
		//------------------------------------------------------------
		this.play = function() {

			//Call the gallery function to run the slideshow	
			this.timer = setInterval('timer_fired()',this.speed);

		}
		this.stop = function() {
			clearInterval(this.timer);
		}
		/* this.update_title = function() {
			//update appropriate title
			this.slideshow_photo_name.text('# '+img[this.currentslidenum][1]);
		} */
		this.show_current_slide = function() {

			// hide before resizing and reloading
			$('img#slideshow_image').css('display','none');


			//update appropriate title
			//this.update_title();			

			//update current
			$('div.slideshow_loader').show();
			var current_image = new Image;
			$(current_image).load(function() {
				$('img#slideshow_image').attr('src', current_image.src);
				$('div.slideshow_loader').hide();
			});
			current_image.src = img_path + img[this.currentslidenum];
			
			//preload images
			/*
			for (i=0; i<this.images_to_preload; i++) {
				this.preloaded_images[i] = new Image();
				this.preloaded_images[i].src = img[this.get_next_slidenum(i + 1)];
			}
			*/
			
			//resize image
			//this.resize_image();

			// show image
			$('img#slideshow_image').css('display','block');

		}
		this.get_next_slidenum = function(offset) {
			var new_slidenum = Number(this.currentslidenum) + Number(offset);
			if (new_slidenum < 0) {
				new_slidenum = img.length + (new_slidenum % img.length);		// note: possible that new_slidenum == img.length (handled in next IF statement)
			}
			if (new_slidenum > img.length - 1) {
				new_slidenum = new_slidenum % img.length;
			}
			return new_slidenum;
		}
		this.advance_slide_next = function() {			
			this.currentslidenum = this.get_next_slidenum(1);
			this.show_current_slide();
		}
		this.advance_slide_prev = function() {
			this.currentslidenum = this.get_next_slidenum(-1);
			this.show_current_slide();
		}

			
	} 		// slideshowengine

		//============================================================


		$('a#pause').click(function() {
			slideshowengine.stop();
			$('a#pause').css('display','none');
			$('a#play').css('display','inline');
		});
		$('a#play').click(function() {
			slideshowengine.play();
			$('a#play').css('display','none');
			$('a#pause').css('display','inline');
			timer_fired();
		});
		$('a#prev').click(function() {
			//if playing, reset the timer
			if ($('a#play').css('display') == 'none') {
				slideshowengine.stop();
				slideshowengine.play();
			}
			slideshowengine.advance_slide_prev();
		});
		$('a#next').click(function() {
			//if playing, reset the timer
			if ($('a#play').css('display') == 'none') {
				slideshowengine.stop();
				slideshowengine.play();
			}
			slideshowengine.advance_slide_next();
		});


	//------------------------------------------------------------
	// *** setup first slide ***
	slideshowengine.show_current_slide();


	// *** start the slideshow ***	
	//slideshowengine.play();


}







//========================================================================================================================
//========================================================================================================================
});
