//Carry out tasks when page is loaded
$(document).ready(function(){
   
   	//setting interval for description to show depending on active image in staff page
	imageDescriptionTimer = setInterval('showImageDescription()', 500);
	
	//Homepage slideshow interval
	setInterval( "slideSwitch()", 5000 );
	 
	 
	
	//using jQuery tools date module
	$(".date").dateinput({
		format: 'dddd dd, mmmm yyyy',	// the format displayed for the user
		selectors: false,             	// whether month/year dropdowns are shown
		min: -1,                    	// min selectable day (100 days backwards)
		max: 60,                    	// max selectable day (100 days onwards)
		offset: [0, 0],            		// tweak the position of the calendar
		speed: 'medium',               	// calendar reveal speed
		firstDay: 1                  	// which day starts a week. 0 = sunday, 1 = monday etc..					 
	
	});
	
	
	
	$('#showStoryForm').click(function(){
		$('#petStoriesForm').toggleClass('hide', 'hide');					   
	})
	
	
	
	//use more awesome jQuery tools for form validation
	//$("#appointment-form").validator();
	
	
	//initialize validator with the new effect
	$("#appointment-form").validator({
	   effect: 'wall', 
	   container: '#errors',
	   
	   // do not validate inputs when they are edited
	   errorInputEvent: null
	   
	// custom form submission logic  
	}).submit(function(e)  { 
	   
	   // when data is valid 
	   if (!e.isDefaultPrevented()) {
		  
		  // tell user that everything is OK
		  $("#errors").html("<h2>Everything's OK. Sending form...</h2>");
		  
		  // prevent the form data being submitted to the server
		  //e.preventDefault();
	   } 
	   
	});	
	
	
	//COMMENTED CODE BELOW DIDN'T SOLVE PROBLEM SO ISN'T NEEDED ANYMORE
	/*
	var pathname = window.location.pathname;
	if( pathname == '/meet-staff' ){
		ajax_cf = new ContentFlow('ajax_cf');
		window.setTimeout(getPictures, 3000, 'staff');
	}
	*/
	
	
 });//closing document ready function


/**
GENERAL FUNCTIONS TO USE IN DOCUMENT.READY ABOVE
**/


function showImageDescription(){
	
	$('.imageDescription').each(function(index){
		$(this).removeClass('show').addClass('hide');
		//console.log( $(this).text() );
	})
	
	var activeImageTitle = $('.active .content').attr('title');
	
	//stop timer if there it isn't needed
	if( activeImageTitle == undefined ){
		clearInterval(imageDescriptionTimer);
	}
	
	var idToUse = activeImageTitle.split('.');
	//console.log( $('#description_'+ idToUse[0] ).text() );
	
	$('#description_'+idToUse[0]).removeClass('hide').addClass('show');
	
	//$('#imageDescriptions div#'+activeImageTitle).removeClass('hide').addClass('show');
	
} 



function slideSwitch() {
    var $active = $('#slideshow IMG.active');

    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');

    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}



function addPictures(t){
	var ic = document.getElementById('itemcontainer');
	var is = ic.getElementsByTagName('img');
	for (var i=0; i< is.length; i++) {
		ajax_cf.addItem(is[i], 'last');
	}
}
function getPictures(imageSet) {	
        var loadUrl = '/ajax/getImages';
				
		$.post(
            loadUrl,  
            {imageSetToUse: imageSet},  
            function(responseText){  
                $("#itemcontainer").html(responseText);
				addPictures();
            },  
            "html"
        );  
	
}



$.tools.validator.addEffect("wall", function(errors, event) {
		// get the message wall
		var wall = $(this.getConf().container).fadeIn();
		
		// remove all existing messages
		wall.find("p").remove();
		
		// add new ones
		$.each(errors, function(index, error) {
			wall.append(
				"<p><strong>" +error.input.attr("name")+ "</strong> " +error.messages[0]+ "</p>"
			);		
		});
	
	// the effect does nothing when all inputs are valid	
	}, function(inputs)  {
		
});
	




