// jQuery functions for DC Website
// by M.Hill, Aug 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'));
	 }
});


// 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;
});


// Add smooth anchor scroll
$(function() {$('body').localScroll();});

// Close jQuery object
});
 
//*******************************************************************************************
//		HOME PAGE SLIDE SHOW
//*******************************************************************************************

jQuery(document).ready(function($){
   $('#slides').cycle({
      fx:      'scrollHorz',		 
		speed:	1000,
      timeout:  15000,
      prev:    '#prev',
      next:    '#next'
   });
});

//*******************************************************************************************
//		XMAS REASONS SLIDE SHOW
//*******************************************************************************************

jQuery(document).ready(function($){
	$('.page-template-t-xmasreasons-php #slides').cycle({
		fx:			'fade',
		speed:		500,
		timeout:	5000,
		pager:		'#controls',
		pause:		1,
		autostop:	0,
		sync:		1,
		after:		refreshScreen
	});
	
		
	function refreshScreen(currSlideElement, nextSlideElement, options, forwardFlag) {
			// $('.counter span:first').text((options.currSlide)+1);
			// $('.counter span:last').text(options.slideCount);
			// $('#slideshow i').html(options.currSlide);
			if (options.currSlide==0) {
				$('#xmasform #submit,#comments-title a,#reason-modal').css('background-color','#42a743');
				if ($.browser.msie  && parseInt($.browser.version) < 9) {
  					
					} else {
						$('.commentlist li.odd').css('background-color','rgba(66,166,67,0.1)');
					}
				$('h1,h2').css('color','#42a743');
				Cufon.replace('h1,h2');
			} else if (options.currSlide==1) {
				$('#xmasform #submit,#comments-title a,#reason-modal').css('background-color','#814094');
				if ($.browser.msie  && parseInt($.browser.version) < 9) {

					} else {
						$('.commentlist li.odd').css('background-color','rgba(129,64,148,0.1)');
					}
				$('h1,h2').css('color','#814094');
				Cufon.replace('h1,h2');
			} else if (options.currSlide==2) {

				$('#xmasform #submit,#comments-title a,#reason-modal').css('background-color','#f15f3a');
				if ($.browser.msie  && parseInt($.browser.version) < 9) {

					} else {
						$('.commentlist li.odd').css('background-color','rgba(241,95,58,0.1)');
					}
				$('h1,h2').css('color','#f15f3a');
				Cufon.replace('h1,h2');
			}			
		}
});

//*******************************************************************************************
//		XMAS REASONS POPUP
//*******************************************************************************************

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

    $("#popup").prettyPhoto({
   	modal: false,
   	theme: 'light_square',
   	default_width: 520,
   	show_title: false,
	deeplinking: false,
	social_tools: ''
	});
    

});

//*******************************************************************************************
//		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."			
		}
	});
	

	$("#commentform").validate({
		rules: {
			'email': {
				required: true,
				email: true
			},
			author: "required",
			comment: "required"
		},
		messages: {
			author: "Please enter your name",
			email: "Please enter a valid email address",
			comment: "Please enter a reason to be cheerful :-)"
		}
	});
	
	$("#subForm").validate({
		rules: {
			'cm-utiudj-utiudj': {
				required: true,
				email: true
			}
		},
		messages: {
			'cm-utiudj-utiudj': "Please enter a valid email address."
		}
	});		
	
});	
	
//*******************************************************************************************
//		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;
	}	
});
