// jQuery functions for Workplace Giving
// by M.Hill, Oct 2009 - Jan 2010, http://www.friskdesign.com
// for Design Culture

//*******************************************************************************************
//
//		GENERAL FUNCTIONS
//
//*******************************************************************************************

jQuery(document).ready(function($) {

// Function to swap two classes
$.fn.swapClass = function(c1, c2){
    return this.each(function(){
        var t = $(this);
        (!t.is('.'+c1)) ? 
            t.addClass(c1).removeClass(c2) : t.addClass(c2).removeClass(c1);
    });
}


// Clear postcode input fields when focussed, return original value if nothing input
 $('input.postcode').focus(function() {
	 if($(this).val() == $(this).attr('defaultValue')) {
		 $(this).val('');
	}
 })
 .blur(function() {
	 if($(this).val().length == 0) {
		 $(this).val($(this).attr('defaultValue'));
	 }
});


// Click state for Translate button
$("#translate p").toggle(
	function() {
		// Do on mouseover
		$('#transmenu').css({'left':'auto'});
		$('#translate p').addClass("on");
		$('#translate p').addClass("clicked");		
	},
	// Undo on mouseout
	function() {
		$('#transmenu').css({'left':'-1000em'});
		$('#translate p').removeClass("on");
		$('#translate p').removeClass("clicked");
	}
);

 
// Fix IE not responding to change events on radio buttons:
// http://norman.walsh.name/2009/03/24/jQueryIE
$(function() {
    if ($.browser.msie) {
        $('input:radio').click(function () {
            this.blur();
            this.focus();
        });
    }
});
	
// New Windows
$('a.newwindow').bind('click',function(){
    window.open(this.href);
    return false;
});

// Close jQuery object
});
 
//*******************************************************************************************
//
//		LIMIT INPUT FIELDS TO SPECIFIC DATA ENTRY
//
//*******************************************************************************************

// Uses "jquery-alphanumeric.js" by Paulo P. Marinas
// http://itgroup.com.ph/alphanumeric/
jQuery(document).ready(function($){
	// Homepage
		$('#salaryamount').numeric({allow:"."});
	// Donor Form Birthday
		$('input.bday').numeric();
	// Donor form amounts to donate for each charity:
		$('input.amount').numeric({allow:"."});
	// Postcode fields
		$('input.postcode').alphanumeric({allow:" "});
	// Calculator page
		$('#calc-donateamount').numeric({allow:"."});
		$('#calc-annualsalary').numeric({allow:"."});		

 });
 
//*******************************************************************************************
//
//		FAQS
//
//*******************************************************************************************

// FAQ slider
jQuery(document).ready(function($) {
	$('#faqlist li div').hide();
	$('#faqlist li h2').css({'cursor' : 'pointer'}).toggle(
    function(){
		$(this).parent().addClass('open');
		$(this).parent().find('div').slideDown('fast');
		return false;		
    },
    function() {
		$(this).parent().removeClass('open');
		$(this).parent().find('div').slideUp('fast');
		return false;		
    } 		
	);

	var queryhash = window.location.hash
	switch (queryhash) {
		case "#faq1":
			$('#faq1').addClass('open');
			$('#faq1 div').toggle();
			break;
		case "#faq2":
			$('#faq2').addClass('open');
			$('#faq2 div').toggle();
			break;
		case "#faq3":
			$('#faq3').addClass('open');
			$('#faq3 div').toggle();
			break;
		default:
			break;
	}
	
	
});

//*******************************************************************************************
//
//		EMPLOYER PAGES DROPDOWNS
//
//*******************************************************************************************

// FAQ slider
jQuery(document).ready(function($) {
	$('.showhide li div').hide();
	$('.showhide li h4').css({'cursor' : 'pointer'}).toggle(
    function(){
		$(this).parent().addClass('open');
		$(this).parent().find('div').slideDown('fast');
		return false;		
    },
    function() {
		$(this).parent().removeClass('open');
		$(this).parent().find('div').slideUp('fast');
		return false;		
    } 		
	);
});

//*******************************************************************************************
//
//		GIVING PAGE: OTHER WAYS ICONS FADER
//
//*******************************************************************************************

jQuery(document).ready(function($){
   $('#otherways-loop').cycle({
       fx:      'fade',
       timeout:  3000,
		 speed:    700
   });
});

//*******************************************************************************************
//
//		OTHER WAYS TO FUNDRAISE
//
//*******************************************************************************************

jQuery(document).ready(function($) {

	$("#way1,#way1b").colorbox({width:"690px", inline:true, href:"#popup1", opacity:"0.6"});
	$("#way2,#way2b").colorbox({width:"690px", inline:true, href:"#popup2", opacity:"0.6"});
	$("#way3,#way3b").colorbox({width:"690px", inline:true, href:"#popup3", opacity:"0.6"});
	$("#way4,#way4b").colorbox({width:"690px", inline:true, href:"#popup4", opacity:"0.6"});
	$("#way5,#way5b").colorbox({width:"690px", inline:true, href:"#popup5", opacity:"0.6"});
	$("#way6,#way6b").colorbox({width:"690px", inline:true, href:"#popup6", opacity:"0.6"});
	
});

//*******************************************************************************************
//
//		TESTIMONIALS SIDE SCROLLER
//
//*******************************************************************************************

jQuery(document).ready(function($){
   $('#testimonials').cycle({
       fx:      'scrollHorz',
       timeout:  0,
       prev:    '#tprev',
       next:    '#tnext'
   });
   function pagerFactory(idx, slide) {
       var s = idx > 2 ? ' style="display:none"' : '';
       return '<li'+s+'><a href="#">'+(idx+1)+'</a></li>';
   };
});

//*******************************************************************************************
//
//		CONTACT FORM VALIDATION
//
//*******************************************************************************************

jQuery(document).ready(function($){
	$("#contact-formpanel form").validate({
		rules: {
			name: "required",
			email: {
				required: true,
				email: true
			},
			employername: "required",
			usercomments: "required"
		},
		messages: {
			name: "Please enter your full name.",
			email: "Please enter a valid email address.",
			employername: "Please enter the name of your employer.",
			usercomments: "Please enter your enquiry."			
		}
	});
});	
	
//*******************************************************************************************
//
//		SIGN UP VALIDATION / HELPER
//
//*******************************************************************************************

jQuery(document).ready(function($){
	$("#pagetail form").validate({
		rules: {
			email: {
				required: true,
				email: true
			}
		},
		messages: {
			email: "Please enter a valid email address"
		}
	});
	
	// The Contact Form 7 plugin we're using generates terrible HTML, especially
	// it creates <spans> as labels for radio buttons and doesn't associate them
	// with their correct inputs. Rather than hack the plugin, this jQuery will
	// fix the issue
	
	$(".wpcf7-list-item input[type='radio']").each(function(){
		var name = $(this).attr('name');
		var value = $(this).attr('value');
		var cleanvalue = (value.split(' ').join('')).toLowerCase();

		var html = '<input type="radio" name="'+name+'" id="cf-'+cleanvalue+'" value="'+value+'" />';
		$(this).after(html).remove();
	});
	$("span.wpcf7-list-item-label").each(function(){
		var elemclass = $(this).attr('class');
		var text = $(this).text();
		var cleantext = (text.split(' ').join('')).toLowerCase();

		var html = '<label for="cf-'+cleantext+'" class="'+elemclass+'">'+text+'</label>';
		$(this).after(html).remove();
	});	
		
	
});

//*******************************************************************************************
//
//		HOW IT WORKS ANIMATION SCROLLER
//
//*******************************************************************************************

jQuery(document).ready(function($){
   $('#howframes').after('<div id="howcontrol">').cycle({
		fx:			'scrollHorz',
		speed:		1200,
		timeout:		6500,
		timeoutFn:	calculateTimeout,
		next:			'.nextslide',
		pager:		'#howcontrol',
		pause:		0,
		autostop:	0,
		after:		textHilite
   });

	// Grab the current slide and use it to determine which text panel to update
	function textHilite(currSlideElement, nextSlideElement, options, forwardFlag) {
		$('#howdesc li').removeClass('current');
		var index = (options.currSlide)+1;
		if (index==4) index=3;
		var toHilite = "#howstep"+index;
		$(toHilite).addClass('current');
		Cufon.refresh('#howdesc h2'); // required because text color changed in the addClass
	}

   // Set last slide to a 20 second timeout before restarting, all others to 6.5
	function calculateTimeout(currElement, nextElement, options, isForward) { 
	   var index = (options.currSlide)+1;
	   return index==4 ? 20000 : 6500;
	}	
});
