/*
	PluggedIn JavaScript
	Author: Jesse Weisbeck
	Dev note: This files relies on jquery 1.4.2 at least. File is added to header in gigMain.php - the plugin's main file
*/

var students = {
	
	bandmateFieldCount: 0,
	fieldLimitFlag: false,
	bandmateLimit: 5,
	linkFlag: false,
	
	toggleStudentForms: function(){
		// hide reservation forms on initialization
	    jQuery('.gig-res-form').css("display", "none");

		var formCount = jQuery('form').size(); // forms are output by wp, so first inspect # of forms on page
		
		// set up show/hide toggle <a> links for each form under each gig listing	
		for (var i=0; i < formCount; i++) {
			jQuery('#showform_'+i+'').toggle(function(e){ 
				jQuery(this).parent().next().fadeIn();
				jQuery(this).html('Hide form');
				// reset values for bandmates
				students.bandmateFieldCount = 0;
				students.fieldLimitFlag = false;
				students.bandmateLimit = 5;
				students.linkFlag = false;
				
				e.preventDefault();
				}, function(e){ 
					jQuery(this).parent().next().fadeOut();
					jQuery(this).html('Show form');
					e.preventDefault();
			});
		}
		
	},
	
	addFirstBandmate: function(e){
		jQuery('.add-first').live('click', function(e){
		
			students.bandmateFieldCount++; //jQuery(e.target).parent().siblings('.bandmate').size();	
			
			// add bandmate fields, does this relative to "add bandmate" linked clicked because page has > 1 gig form. Need to keep it relative
			jQuery(e.target).parent().after('<p class="bandmate""><label for="bandmate">Bandmate</label><input type="text" name="bandmate' + students.bandmateFieldCount + 
			'" value=""  class="bandmate-link"> <a href="" class="add">Add another bandmate</a></p>');
			
			jQuery(e.target).replaceWith('Your bandmates');
			
			e.preventDefault();
		})
	},
	
	addBandMates: function(e){
			
		jQuery('.add').live('click', function(e){
						
				students.bandmateFieldCount++; //jQuery(e.target).parent().siblings('.bandmate').size();	
				
				//console.log(students.bandmateFieldCount);
				
				// make the last link a remove link, so user can't keep adding more bandmate fields
				if (students.bandmateFieldCount == students.bandmateLimit ) {
					jQuery(e.target).replaceWith('<a href="" class="remove">Remove</a>');
					students.fieldLimitFlag = true;
					students.bandmateLimit++;
				};
				
				// add bandmate fields, does this relative to "add bandmate" linked clicked because page has > 1 gig form. Need to keep it relative
				var el = jQuery(e.target).parent().after('<p class="bandmate""><label for="bandmate">Bandmate</label><input type="text" name="bandmate' + students.bandmateFieldCount + 
				'" value=""  class="bandmate-link"> <a href="" class="add">Add another bandmate</a></p>').fadeTo(0, 0);
				
				el.animate({opacity: 1}, 500);
				
				// if there's one input as a result of removing previous inputs, handle link status here	
				if(students.linkFlag){
					jQuery(e.target).next('span').remove();
					jQuery(e.target).replaceWith('<a href="" class="remove">Remove</a> ');
					students.linkFlag = false;
				}
				
				
				// puts a remove link adjacent to every newly added input field
				if( students.bandmateFieldCount  > 1) {
					jQuery(e.target).replaceWith('<a href="" class="remove">Remove</a>');
				} else if( students.bandmateFieldCount == 1){
					jQuery(e.target).remove();
				}
			
				e.preventDefault();
		})
	},

	removeBandMates: function(e){
		jQuery('.remove').live('click', function(e){

			students.bandmateFieldCount--;
			
			//console.log(students.bandmateFieldCount);
			
			
			// when input limit is reachd, last link becomes a 'remove bandmate' link
			// if user then removes an input we need to flip that 'remove bandmate' link back to
			// 'add another bandmate' because we're now < # of inputs allowed
			if(students.bandmateFieldCount > 1 && students.fieldLimitFlag == true){
				jQuery(e.target).parent().siblings('.bandmate').last().children('.remove').replaceWith('<a href="" class="add">Add another bandmate</a></p>');
			} 
									
			jQuery(e.target).parent().siblings('.bandmate').size() == 1 ? students.linkFlag = true : students.linkFlag = false;
			
			if(jQuery('.bandmate').size() == 1) {
				jQuery('.bandmate').css('text-decoration', 'line-through').animate({opacity: 0}, 500, function(){
					jQuery(this).remove(); 
					jQuery('.bandmate-first-link').each(
						function(i){
							jQuery(this).html('<a href="" class="add-first">Add a bandmate</a> (leave blank if no bandmates)');
						});
				})
			} else {
				
				jQuery(e.target).parent().css('text-decoration', 'line-through').animate({opacity: 0}, 500, function(){
					jQuery(this).remove();
					// dynamically reset 'name' attribute of bandmate inputs to make sure they're always named lowest to highest, 1-n.
					jQuery('.bandmate-link').each(
						function(i){
							jQuery(this).attr('name', 'bandmate'+(i+1)+'');
						});
				});
			}
					
			students.linkFlag ? jQuery('.add').replaceWith('<a href="" class="add">Add another bandmate</a> <span> &nbsp; | &nbsp; <a href="" class="remove">Remove</a></span> ') : null;
			
			e.preventDefault();
			
		});
	}
	
} // end students obj literal

var slideshow = {
	loop: null,
	numImages: 0,
	count: 0,
	interval: 7, // animation delay in seconds
	
	init: function(){
		slideshow.numImages = $('.home_slideshow').children().size();		
		slideshow.loop = setInterval('slideshow.cycleImages()', (slideshow.interval*1000));
	},
	
	cycleImages: function(){
		slideshow.count ++;
		//console.log(slideshow.numImages);
		//console.log(slideshow.count);
		
		if (slideshow.count < slideshow.numImages) {
			var curr = $('img.active');
			var next = curr.next().css({opacity: 0.0});
			
			curr.removeClass('active').addClass('last-active');
			next.removeClass('last-active').addClass('active').animate({opacity: 1.0}, 1000);

		} else {
			// clear all class names before restarting the cycle
			$('.last-active').removeClass('last-active');
			$('.active').removeClass('active');

			var firstImg = $('.home_slideshow').children().first().css({opacity: 0.0});
			firstImg.removeClass('last-active').addClass('active').animate({opacity: 1.0}, 1000);
				
			slideshow.count = 0;
			clearInterval(slideshow.loop);
			slideshow.init();
		}

	}
}


jQuery(document).ready(function() {
	// gig scheduler functions
	students.toggleStudentForms();
	students.addFirstBandmate();
	students.addBandMates();
	students.removeBandMates();
	
	//slideshow
	slideshow.init();
	
});

