var theGal = {

	thePics : {
		"babies" : [
			"Babies-001.jpg",
			"Babies-004.jpg",
			"Babies-005.jpg",
			"Babies-007.jpg",
			"Babies-008.jpg",
			"Babies-009.jpg",
			"Babies-010.jpg",
			"Babies-011.jpg",
			"Babies-012.jpg",
			"Babies-013.jpg",
			"Babies-014.jpg",
			"Babies-015.jpg",
			"Babies-016.jpg",
			"Babies-017.jpg",
			"Babies-018.jpg",
			"Babies-019.jpg",
			"Babies-020.jpg",
			"Babies-021.jpg"
		],
		
		"children" : [
			"Children-002.jpg",
			"Children-003.jpg",
			"Children-004.jpg",
			"Children-005.jpg",
			"Children-007.jpg",
			"Children-010.jpg",
			"Children-011.jpg",
			"Children-012.jpg",
			"Children-013.jpg",
			"Children-015.jpg",
			"Children-016.jpg",
			"Children-017.jpg",
			"Children-020.jpg",
			"Children-021.jpg",
			"Children-022.jpg",
			"Children-024.jpg",
			"Children-026.jpg",
			"Children-027.jpg",
			"Children-029.jpg",
			"Children-030.jpg",
			"Children-031.jpg",
			"Children-034.jpg",
			"Children-035.jpg",
			"Children-037.jpg",
			"Children-038.jpg",
			"Children-039.jpg"		
		],

		"families" : [
			"Family-002.jpg",
			"Family-004.jpg",
			"Family-007.jpg",
			"Family-008.jpg",
			"Family-009.jpg",
			"Family-012.jpg",
			"Family-013.jpg",
			"Family-014.jpg",
			"Family-015.jpg",
			"Family-018.jpg",
			"Family-019.jpg",
			"Family-020.jpg",
			"Family-022.jpg",
			"Family-023.jpg",
			"Family-024.jpg",
			"Family-027.jpg",
			"Family-029.jpg",
			"Family-031.jpg",
			"Family-032.jpg",
			"Family-033.jpg",
			"Family-034.jpg",
			"Family-035.jpg",
			"Family-036.jpg",
			"Family-039.jpg",
			"Family-040.jpg",
			"Family-041.jpg",
			"Family-048.jpg",
			"Family-049.jpg",
			"Family-051.jpg",
			"Family-052.jpg",
			"Family-053.jpg",
			"Family-054.jpg",
			"Family-057.jpg",
			"Family-059.jpg",
			"Family-060.jpg",
			"Family-063.jpg",
			"Family-064.jpg",
			"Family-065.jpg",
			"Family-068.jpg",
			"Family-069.jpg",
			"Family-070.jpg",
			"Family-072.jpg",
			"Family-073.jpg",
			"Family-074.jpg",
			"Family-075.jpg",
			"Family-076.jpg",
			"Family-077.jpg",
			"Family-078.jpg"
 		],

		"portraits" : [
			"Senior-Portrait-001.jpg",
			"Senior-Portrait-002.jpg",
			"Senior-Portrait-003.jpg",
			"Senior-Portrait-005.jpg",
			"Senior-Portrait-006.jpg",
			"Senior-Portrait-007.jpg",
			"Senior-Portrait-008.jpg",
			"Senior-Portrait-009.jpg"
		],

		"weddings" : [
			"Wedding-001.jpg",
			"Wedding-002.jpg",
			"Wedding-003.jpg",
			"Wedding-004.jpg",
			"Wedding-005.jpg",
			"Wedding-006.jpg",
			"Wedding-007.jpg",
		]
	},

	speed : '3500',
	timer : '',
	gallery : 'weddings',
	counter : 0,
	preloaded : [],

	showPic : function(gallery,index) {
		theGal.gallery = gallery;
		if((theGal.counter >= theGal.thePics[theGal.gallery].length) || (index >= theGal.thePics[theGal.gallery].length)) { theGal.counter = 0; }
		else if((theGal.counter < 0) || (index < 0)) { theGal.counter = theGal.thePics[theGal.gallery].length-1; }
		else {
			theGal.counter = index;
			theGal.preload(gallery,4); 
			}
		$('<img>').attr('src','/images/'+theGal.thePics[theGal.gallery][theGal.counter]).insertAfter($('#slideshow img:first')).hide().fadeIn('slow');
		$('#slideshow img:first').css('position','absolute').fadeOut('slow',function(){$(this).remove();});
		$('#counter').text(theGal.thePics[theGal.gallery][theGal.counter].slice(0,theGal.thePics[theGal.gallery][theGal.counter].length-4));
	},

	start : function(gallery,delay) {
		theGal.preload(gallery,4);
		if (!delay ) delay = theGal.speed;
		theGal.gallery = gallery;
		theGal.timer = setTimeout(function(){theGal.play();},delay);
	},

	preload : function(gallery,amount) {
		for(n=1; n<=amount; n++){
			if(theGal.thePics[gallery][theGal.counter+n] && !theGal.preloaded[theGal.thePics[gallery][theGal.counter+n]]){
				theGal.preloaded[theGal.thePics[gallery][theGal.counter+n]] = $('<img>').attr('src','/images/'+theGal.thePics[gallery][theGal.counter+n]);
			}
		}
	},

	play : function() {
		if(!theGal.isBusy()){
			theGal.counter++;
			theGal.showPic(theGal.gallery,theGal.counter);
			clearTimeout(theGal.timer);
			theGal.timer = setTimeout(function(){ theGal.play(theGal.gallery); },theGal.speed);
		}
	},

	pause : function() {
		clearTimeout(theGal.timer);
	},
	
	isBusy : function() {
		return $('#slideshow img').length>1 ? true : false;
	},

	goTo : function(index) {
		$('#slideshow img').stop();
		theGal.pause();
		if((theGal.counter != index) || ((theGal.counter == index) && theGal.isBusy())){
			if(theGal.isBusy()) $('#slideshow img:first').remove();
			theGal.showPic(theGal.gallery,index);
		}
	},

	next : function() {
		theGal.goTo(theGal.counter+1);
	},

	previous : function() {
		theGal.goTo(theGal.counter-1);
	},
	
	events : function() {
		$('#controls a').each(function(){
			switch(this.innerHTML){
				case 'Pause' : $(this).click(function(){ theGal.pause(); return false; }); break;
				case 'Play' : $(this).click(function(){ theGal.play(); return false; }); break;
				case '\u2039 Previous' : $(this).click(function(){ theGal.previous(); return false; }); break;
				case 'Next \u203A' : $(this).click(function(){ theGal.next(); return false; }); break;
			}
		});
	},

	mycarousel_itemLoadCallback : function (carousel, state){
		for (var i = carousel.first; i <= carousel.last; i++) {
			if (carousel.has(i)) continue;
			if (i > theGal.thePics[theGal.gallery].length) break;
			carousel.add(i, theGal.mycarousel_getItemHTML(theGal.thePics[theGal.gallery][i-1],i));
		}
	},
	mycarousel_getItemHTML : function (item,theindex){
		item = '/images/'+item;
		theindex--;
		return $('<a href="'+item+'"></a>').html('<img src="'+item+'" alt="'+item+'" width="84" height="57" />').bind('click',{theindex:theindex},function(event){theGal.goTo(event.data.theindex);return false;});
	},
	makeCarousel : function() {
		if($('#mycarousel').length) $('#mycarousel').html('');
		$('#mycarousel').jcarousel({
			scroll : 5,
			visible : 5,
			size: theGal.thePics[theGal.gallery].length,
			itemLoadCallback: {onBeforeAnimation: theGal.mycarousel_itemLoadCallback}
		});
	},
	
	init : function() {
		theGal.events();
		if($('#slideshow').length){
			$(document).ready(function() {
				if($('#slideshow.home').length) theGal.start('children',theGal.speed);
				if($('#slideshow.babies').length) { theGal.start('babies',theGal.speed); theGal.makeCarousel(); }
				if($('#slideshow.children').length) { theGal.start('children',theGal.speed); theGal.makeCarousel(); }
				if($('#slideshow.families').length) { theGal.start('families',theGal.speed); theGal.makeCarousel(); }
				if($('#slideshow.portraits').length) { theGal.start('portraits',theGal.speed); theGal.makeCarousel(); }
				if($('#slideshow.weddings').length) { theGal.start('weddings',theGal.speed); theGal.makeCarousel(); }
			}); 
			$('#controls').show();
		}
	}
}


var theAjax = {

	sendcontact: function() {
		var url = '/process.php';
		var params = $('#contactform').serialize();
		$.ajax({
			type : "POST",
			url : url,
			data: params,
			success : function(data, status){
				if(!$('#status').length) $('#contactform').before('<div id="status"></div>'); 
				$('#status').html(data);
				if (data.indexOf('Oops') != -1) { $('#status').addClass('error'); }
				else { $('#contactform').hide(); $('#status').removeClass('error').removeAttr('id'); };
				$('#masthead').focus();
				},
			error : function(){ $("#contactform").before('<div id="status" class="error"><p>Sorry there an error occured, try again.</p></div>'); }
		});
	},
	
	init : function() {
		if( $('#contactform') ) $('#contactform').submit( function(){ theAjax.sendcontact(); return false; });
		$('.submit').each(function(){
			var parentform = $(this).parents('form');
			var anchor = $('<a>'+this.value+'</a>').attr('href','#null').click(function(){ parentform.submit();  return false; });
			$(this).replaceWith(anchor);
		});
	}
}


$(document).ready(theGal.init);
$(document).ready(theAjax.init);
