$(document).ready(function() {

if( $("#hero-space-home").length > 0 ){
	hero_events();//click event handlers	
}
if( $("#hero-space").length > 0 ){
	$('#hero-space .wrap').nivoSlider({
		effect: 'fade', // Specify sets like: 'fold,fade,sliceDown'
		//slices: 15, // For slice animations
        //boxCols: 8, // For box animations
        //boxRows: 4, // For box animations
        animSpeed: 500, // Slide transition speed
        pauseTime: 3000, // How long each slide will show
        startSlide: 0, // Set starting Slide (0 index)
        directionNav: false, // Next & Prev navigation
        directionNavHide: true, // Only show on hover
        controlNav: true, // 1,2,3... navigation
        controlNavThumbs: false, // Use thumbnails for Control Nav
        controlNavThumbsFromRel: false, // Use image rel for thumbs
        controlNavThumbsSearch: '.jpg', // Replace this with...
        controlNavThumbsReplace: '_thumb.jpg', // ...this in thumb Image src
        keyboardNav: true, // Use left & right arrows
        pauseOnHover: true, // Stop animation while hovering
        manualAdvance: false, // Force manual transitions
        captionOpacity: 0.8, // Universal caption opacity
        prevText: 'Prev', // Prev directionNav text
        nextText: 'Next', // Next directionNav text
        beforeChange: function(){}, // Triggers before a slide transition
        afterChange: function(){}, // Triggers after a slide transition
        slideshowEnd: function(){}, // Triggers after all slides have been shown
        lastSlide: function(){}, // Triggers when last slide is shown
        afterLoad: function(){} // Triggers when slider has loaded

	}).attr('position','absolute');
	/*$('#hero-space .wrap').cycle({ 
    fx: 'fade',//scrollLeft
    speed: '2000',
    timeout: '4000',
    next: '#next',
    prev: '#prev',
    slideResize: 0,
    fit: 0,
    pause: 1,
});*/
	
	
}
/*
if($("#cf3_field_9").length > 0){
$.getJSON('/wp-content/themes/walkon/assets/js/districts.json',function(data){
	var i=0;
	$("#cf3_field_8").append( '<option>Select One</option>' );
	
	$.each(data, function(key,value){
		//console.log(value.District);
		if(value.District.search(/DISTRICT/i)<0 && value.District.search(/SCHOOL/i)>0){
			$("#cf3_field_8").append( '<option>&nbsp;--'+value.District+'</option>' );
		}else{
			$("#cf3_field_8").append( '<option>'+value.District+'</option>' );
		}
	});
});
	//set first option as selected
	$("#cf3_field_8 option").eq(0).attr('selected','selected');
}//end if field_5
*/

//if we find the toolkit form then populate districts
if( $("#cforms3form").length > 0 ) {
	populate_events();
	load_validation();
}

/*
//Challenge Success form - handles the dynamic data from sqllite
		#input_1_4 - district dropdown
		#input_1_5 - school dropdown based on district selected
		#input_1_6 - school address based on district selected
		#input_1_20 - school city_state_zip based on district selected
*/
if( $("#input_1_4").length > 0) {
	populate_districts('input_1_4');	
   	//set the 'district' menu
    $("#input_1_4").live('change',function(){
    	if( $(this).val() != undefined ){
    		//school name, elem, address, city, state, zip
	   		populate_schools( $(this).val(), 'input_1_5', 'input_1_6', 'input_1_20', 'input_1_34', 'input_1_35' );
	   		$("#field_1_31").val( $(this).val() ).slideUp('fast'); //hide other districts but first set field to selected district value

	    	$("#input_1_6, #input_1_20, #input_1_34, #input_1_35").val('');//clear out address field(s)
	    	$("#field_1_31:visible").slideUp('fast'); //hide other school field but populate with name for submission sake.
	    	$("#input_1_31").val( $(this).val() );
    	}else{
    		$("#input_1_31").val(''); // reset district value
    		$("#field_1_31:hidden").show(); // show other district if not found in menu
    	}
    	
    });	
    //school drop down, when changes let's populate the related fields in the form (if they exist)
    $("#input_1_5").live('change',function(){
    	
    	//get the selected address / city_state_zip from the option , data-attributes
    	var address = $(this).find('option:selected').attr('data-address');
    	var city = $(this).find('option:selected').attr('data-city');
    	var state = $(this).find('option:selected').attr('data-state');
    	var zip = $(this).find('option:selected').attr('data-zip');
    	
    	// apply the address value
    	if( address != '' && $("#input_1_6").length > 0 ){
    		$("#input_1_6").val( address );
    	}
    	// apply the city value
    	if( city != '' && $("#input_1_20").length > 0 ){
    		$("#input_1_20").val( city );
    	}   
    	// apply the state value
    	if( state != '' && $("#input_1_34").length > 0 ){
    		$("#input_1_34").val( state );
    	} 
    	// apply the state value
    	if( zip != '' && $("#input_1_35").length > 0 ){
    		$("#input_1_35").val( zip );
    	}    
    	
    	if( $(this).val() === '' ){
    		$("#input_1_32").val(''); 					//reset school field for manual entry
    		$("#field_1_32:hidden").show();				//show other school container
    	}else{
    		$("#input_1_32").val( $(this).val() ); 		//set school field for submission sake.
    		$("#field_1_32:visible").slideUp('fast');	//hide field container for other school if selected
    	}	     
    	//console.log( $(this).val() ); 	 	
    });	
    	
}

});
function load_validation(){
/*
 * 
Phone filed needs Error checking [no alphabet] and auto formatting (555) 555-5555
Number of Students needs Error checking [number only] and 3 digits max
Number of Classrooms needs Error checking [number only] and 2 digits max
When Other School is entered; District and School dropdown selection clears
 */
//first set required rules 
$("#cf3_field_2, #cf3_field_3, #cf3_field_4, #cf3_field_6, #cf3_field_7").addClass('required').attr('style','border-right:3px solid #EBC2C2');
//$("#cf3_field_3").addClass('lname');
$("#cf3_field_4").addClass('email');
//$("#cf3_field_5").addClass("{messages:{required:'Please enter a valid phone number'}}");
//load validate function on form
$("#cforms3form").validate({
		rules: {
			//first name
			cf3_field_2: {
		      minlength: [2],
    		},
    		//last name
			cf3_field_3: {
		      minlength: [2],
    		},    
    		//# of students
    		cf3_field_6: {
    			//max: 3,
    			//range: [1, 999],
    			maxlength: 3,
    			digits: true
    		},
    		cf3_field_7: {
    			//max: 2,
    			//range: [1, 99],
    			maxlength: 2,
    			digits: true
    		},    		
    			
/*			cf3_field_5: {
				//digits: true,
				//required: true
				required: function(element) {
				    var phoneNumberPattern = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;  
				    //alert( phoneNumberPattern.test( $("#cf3_field_5").val() ) );
				    //if( phoneNumberPattern.test( $("#cf3_field_5").val() ) ){ return true; }
		    		return phoneNumberPattern.test( $("#cf3_field_5").val());
	    		} 
			}*/
			//required: 'Enter a valid phone number.'
	    },
	    messages: {
			cf3_field_2: {
		      required: 'Enter a valid first name.'
    		},
			cf3_field_3: {
		      required: 'Enter a valid last name.'
    		},
			cf3_field_4: {
			  required: 'Enter a valid email.'
			},
    		cf3_field_6: {
    			required: 'Enter the number of students.',
    		},
    		cf3_field_7: {
    			required: 'Enter the number of classrooms.',
    		}	    			
    		
		/*	cf3_field_5: {
		      required: 'Enter a valid phone number.'
    		},*/
    }
	}
);


}
function populate_events(){
	//hide 'other' fields until requested by user
	$("#li-3-12, #li-3-13, #li-3-14, #li-3-15, #li-3-16").hide();
	$("#li-3-11").attr('style','cursor:pointer'); //add pointer for toggle event
	
	//Until $(#li-3-11).click() then show all 'other' fields (toggle), reset menus: _9 / _10
	$("#li-3-11").click(function(){
		$("#li-3-12, #li-3-13, #li-3-14, #li-3-15, #li-3-16").toggle();
		//reset school / district menus
		$("#cf3_field_9 option, #cf3_field_10 option").eq(0).attr('selected','selected');
		//set fields as req'd
		$("#cf3_field_12, #cf3_field_13, #cf3_field_14, #cf3_field_16").addClass('required').attr('style','border-right:3px solid #EBC2C2');
		/*		
		$("#cforms3form").validate({
		rules: {
			cf3_field_16: {
    			//max: 2,
    			digits: true,
    			maxlength: 5
    		},
		},
	    messages: {
			cf3_field_16: {
		      required: 'Enter the school zip code.'
    		},     		    		    		  		
	    }
		});//end validate
		*/
    		
	});
}
function populate_schools(name, elem, address, city, state, zip){
	$.getJSON('/sqlite-districts/index.php', { district: name },
	   function(data){

	    var el = 		$("#"+elem);			// #input_1_5
	    var address = 	$("#"+address); 		// #input_1_6
	    var city = 		$("#"+city);			// #input_1_20
	    var state = 	$("#"+state);			// #input_1_34
	    var zip = 		$("#"+zip);				// #input_1_35
	    
	    //var other_school = $("#"+other_school);
	    el.empty();//clear out in case of old items remaining
	    el.append('<option value="">select one</option>');
	
	    var items = [];
	    $.each(data, function(key, val) {
	    	var school = 	val.NAME;
	    	var addr = 		val.ADDRESS;
	    	var city = 		val.CITY;
	    	var state = 	val.STATE;
	    	var zip = 		val.ZIP;
	        if(school!==undefined) items.push('<option value="'+school+'" data-address="'+ addr + '" data-state="' + state + '" data-zip="' + zip + '" data-city="' + city + '">' + school + '</option>');
	    });
		//populate menu
	    el.append( items.join('') );
	});
}
function populate_districts(elem){
	$.getJSON('/sqlite-districts/index.php', { district: "" },
	   function(data){
	   	//set the 'district' menu
	    var dist = $("#"+elem);
	    //dist.empty();
	    //dist.append('<option value="">select one</option>');
	
	    var items = [];
	    $.each(data, function(key, val) {
	        items.push('<option value="' + val.DISTRICT + '">' + val.DISTRICT + '</option>');
	    });//end each
	
	    dist.append( items.join('') );
	});//end getJSON	
}

function hero_events(){
	
	
	$(".hero-sm a").live('click',function(){
		var hero_sm = $(".hero-sm");
		var hero_links = $(".hero-sm a");
		var hero_lrg = $(".hero-lrg a");
		var hero_lrg_active = $(".hero-lrg a.active");
		var hero_sm_active = $(".hero-sm a.active");
		
		var cname = $(this).attr('class');//get class name
		
		hero_lrg_active.fadeOut('fast', function(){
			$(this).removeClass('active');
			$(".hero-lrg ."+cname).fadeIn('fast').addClass('active');//set large image active state based on link class
		});//remove active states
		//hero_sm_active.removeClass('active');//remove active states
		hero_links.removeClass('active');
		$(this).addClass('active');	
		
		//hero_sm.removeClass('active');
		
		
		//alert(".hero-lrg ."+cname);
		
		
	}).hover(function(){
		//var leftp = $(this).position.left - 25;
		$(this).animate({
			left: '-=25px',
    		//height: '+=25',
		});
	},function(){
		//var leftp = $(this).position.left + 25;
		
		$(this).animate({
			left: '+=25px',
    		//height: '-=25',
		});
		//alert('roll out!');
		
	});
}

// usage: log('inside coolFunc', this, arguments);
window.log = function(){
  log.history = log.history || [];   // store logs to an array for reference
  log.history.push(arguments);
  if(this.console) {
      arguments.callee = arguments.callee.caller;
      console.log( Array.prototype.slice.call(arguments) );
  }
};
(function(b){function c(){}for(var d="assert,count,debug,dir,dirxml,error,exception,group,groupCollapsed,groupEnd,info,log,markTimeline,profile,profileEnd,time,timeEnd,trace,warn".split(","),a;a=d.pop();)b[a]=b[a]||c})(window.console=window.console||{});

