//------------Apply cufon
Cufon.replace('.slider .right');
Cufon.replace('h1');
Cufon.replace('ul.post-categories li a');
Cufon.replace('.widget h2');
Cufon.replace('.widget-inner h2');
Cufon.replace('.widget-inner h3');
Cufon.replace('.poll-inner h3');
//Cufon.replace('ul.dropdown .root-item a');
Cufon.replace('.post-title h2');
Cufon.replace('.category-title h3');

function adjustContentHeight(){
/*----------Set height of main content area to be the same as sidebar height if it's less to allow
	vertical grey line to go all the way to the bottom-------------------*/
	var contentHeight = $('.middle .wrapper').height();
	var sidebarHeight = $('.sidebar').height();
	if(sidebarHeight > contentHeight){
		var newHeight = sidebarHeight-30;
		$('.middle').css('min-height', newHeight);
	}
}
function validateGrantInfo(){
	var fNameValue = $('#firstName').val();
	var lNameValue = $('#lastName').val();
	var emailValue = $('#email').val();
	var emailRegExp = /^[^@]+@[a-zA-Z0-9._-]+\.[a-zA-Z]+$/;
	var errors = '';
	if (!emailRegExp.test(emailValue)) {
		errors = 'emailValid';
	}
	if(emailValue == ''){ errors = 'email'; }
	if(lNameValue == ''){ errors = 'lastName'; }
	if(fNameValue == ''){ errors = 'firstName'; }
	return errors;
}

//----------------------------------------------------
//--------------Iwi Registration Form functions-------
//----------------------------------------------------

function goToNextStep(){
	var thisStep = $('.step-active-content').attr('id');
	var thisTab = $('.form-step-active').attr('id');
	switch(thisStep){
		case 'step1content': var nextStep = 'step2content'; var nextTab = 'form-step-2'; break;	
		case 'step2content': var nextStep = 'step3content'; var nextTab = 'form-step-3'; break;
		case 'step3content': var nextStep = 'step4content'; var nextTab = 'form-step-4'; break;
	}
	$('#'+thisStep).hide('slow', function(){
		$('#'+nextStep).show('slow').addClass('step-active-content');
	}).removeClass('step-active-content');
	$('#'+thisTab).removeClass('form-step-active');
	$('#'+nextTab).addClass('form-step-active');
}

function goToPreviousStep(){
	var thisStep = $('.step-active-content').attr('id');
	var thisTab = $('.form-step-active').attr('id');
	switch(thisStep){
		case 'step2content': var prevStep = 'step1content'; var prevTab = 'form-step-1'; break;	
		case 'step3content': var prevStep = 'step2content'; var prevTab = 'form-step-2'; break;
		case 'step4content': var prevStep = 'step3content'; var prevTab = 'form-step-3'; break;
	}
	$('#'+thisStep).hide('slow', function(){
		$('#'+prevStep).show('slow').addClass('step-active-content');
	}).removeClass('step-active-content');
	$('#'+thisTab).removeClass('form-step-active');
	$('#'+prevTab).addClass('form-step-active');
}

function validateAddress(){
	var errors = '';
//Get values
	var dobDayValue = $('#dobDayValueSelect').val();
	var dobMonthValue = $('#dobMonthValueSelect').val();
	var dobYearValue = $('#dobYearValueSelect').val();
	
	var fNameValue = $('#fName').val();
	var lNameValue = $('#lName').val();
	var address1Value = $('#address1').val();
	var address2Value = $('#address2').val();
	var townValue = $('#town').val();
	var postcodeValue = $('#postcode').val();
	var countryValue = $('#country').val();
//Check against default values	
	if(countryValue == ''){ errors = 'country'; }
	if(postcodeValue == ''){ errors = 'postcode'; }
	if(townValue == ''){ errors = 'town'; }
	if((address2Value == 'Suburb')||(address2Value == '')){ errors = 'address2'; }
	if((address1Value == 'Number, Street')||(address1Value == '')){ errors = 'address1'; }
	if(dobYearValue == ''){ errors = 'dobYearValueSelect'; }
	if(dobMonthValue == ''){ errors = 'dobMonthValueSelect'; }
	if(dobDayValue == ''){ errors = 'dobDayValueSelect'; }
	if((lNameValue == 'Last Name')||(lNameValue == '')){ errors = 'lName'; }
	if((fNameValue == 'First Name')||(fNameValue == '')){ errors = 'fName'; }

	return errors;
}

function addChildCloseButton(numChildBlocks){
//Hide close buttons for all but the last child block
	$('.child-top .close').each(function(index){
		$(this).css('display','none');
	});
	$('#child-block-'+numChildBlocks+' .close').css('display','block');
}

function changeStep(changeFrom, changeTo, currentButton, previousButton){
	$(changeFrom).hide('slow');
	$(changeTo).show('slow');
	$(currentButton).css('display','none');
	$(previousButton).css('display','block');
}

//---------------------------------------------
$(document).ready(function(){
//Iwi registration date validation for first page
	if($('.container').hasClass('iwi-reg-wrap')){
		$('.dob-row select').click(function(){
			$(this).addClass('clicked');
		});
		/*$('#step1next').click(function(e){
			
		});*/
	}
//Clear default values of contact form when focused on
	$('.yourname input').focus(function(){ if($(this).attr('value') == 'Name (required)') $(this).attr('value', ''); });
	$('.email input').focus(function(){ if($(this).attr('value') == 'Email (required)') $(this).attr('value', ''); });
//Apply title to Latest Panui widget
	//$('.widget_recent_entries h3').html('Latest Panui');
//Replace titles of links in third homepage box if on homepage
	if($('.bodywrap').hasClass('homepage')){
		$('#homepageBox3 .yd_rp_widget ul li h4 a').each(function(){
			var thisCrapTitle = $(this).attr('title');
			var splitString = thisCrapTitle.split('Permanent link to: ',2);
			$(this).attr('title', splitString[1]);
		});
	}
//Navigation submenu rollovers
	$('ul.dropdown li.root-item ul li').mouseenter(function(){
		$(this).addClass('roll');
		$(this).parents('ul').siblings('a').addClass('over');
	}).mouseleave(function(){
	  $(this).removeClass('roll');
	  $(this).parents('ul').siblings('a').removeClass('over');
	});
//Provide popup functionality for comment box so it doesn't show by default (it's ugly)
	/*$('#respond .comment-box').css('display','none');
	$('#respond .comment-avatar').bind('mouseover', function(){
		$(this).siblings('.comment-box').show('fast');
	});
	$('#respond .comment-close').bind('mouseenter', function(){
		$(this).css('background','#D38A33');						
	}).bind('mouseleave', function(){
		$(this).css('background','#999');
	});
	$('.comment-close').bind('click', function(){
    	$('#respond .comment-box').hide('fast');
    });*/
// Clear value in search box if is default
	$('#s').bind('focus', function(){
		var searchboxValue = $(this).attr('value');
		if(searchboxValue == 'Enter Keyword(s)'){
			$(this).attr('value', '');	
		}
	});
// Set up default values for text fields
	$('.getDefault').each(function(index) {
	//Loop through all fields that should have a default value and insert default value
		var currentVal = $(this).val();
		if(currentVal == ''){
			var thisID = $(this).attr('id');
			switch(thisID){
				case 'address1': $(this).val('Number, Street').addClass('defaultText'); break;
				case 'address2': $(this).val('Suburb').addClass('defaultText'); break;
			}
			if((thisID=='fName')||(thisID=='sFname')||(thisID=='c1fName')||(thisID=='c2fName')||(thisID=='c3fName')||(thisID=='c4fName')||(thisID=='c5fName')||(thisID=='c6fName')||(thisID=='c7fName')||(thisID=='c8fName')||(thisID=='c9fName')||(thisID=='c10fName')){
				$(this).val('First Name').addClass('defaultText');
			}
			if((thisID=='lName')||(thisID=='sLname')||(thisID=='c1lName')||(thisID=='c2lName')||(thisID=='c3lName')||(thisID=='c4lName')||(thisID=='c5lName')||(thisID=='c6lName')||(thisID=='c7lName')||(thisID=='c8lName')||(thisID=='c9lName')||(thisID=='c10lName')){
				$(this).val('Last Name').addClass('defaultText');
			}
		}
	});
//Remove default value when focusing on field only if value is default
	$('.getDefault').bind('focus', function(){
		if( $(this).hasClass('defaultText') ){
			$(this).val('').removeClass('defaultText');
		}
	});
	$('.clearFirstName').bind('focus', function(){
		var thisVal = $(this).val();
		if(thisVal == 'First Name'){ $(this).val(''); }
	});
	$('.clearLastName').bind('focus', function(){
		var thisVal = $(this).val();
		if(thisVal == 'Last Name'){ $(this).val(''); }
	});
//Toggle between steps when "Next Step" button is clicked
	$('.button-next-step').bind('click', function(){
		goToNextStep();
	});
	
//Toggle between steps when "Previous Step" button is clicked
	$('.button-prev-step').bind('click', function(){
		goToPreviousStep()
	});
//Concatenate address fields from first step and place into hidden address field which can then be accessed when inserting address for children/dependants on step 3
	$('#step1next').bind('click', function(event){
		var clickCounter = 0;
		$('.dob-row select').each(function(){
			if($(this).hasClass('clicked')){ clickCounter++; }
		});
		if(clickCounter != 3){
			event.preventDefault();
			//alert('Please enter your date of birth');
		}
	//Check that default values have been replaced before allowing user to go onto next step	
		var step1errors = validateAddress();
	//Get values of address fields when "Next Step" is clicked if there are no errors
		if(step1errors == ''){
			var address1 = $('#address1').val();
			var address2 = $('#address2').val();
			var town = $('#town').val();
			var postcode = $('#postcode').val();
			var country = $('#country').val();
		//Concatenate values into one string and insert into hidden field
			var hiddenFieldValue = address1+', '+address2+', '+town+' '+postcode+', '+country;
			$('#hiddenAddress').val(hiddenFieldValue);
			goToNextStep();
		}else{	
			$('#'+step1errors).addClass('error-box');
		}
	});
	$('input').bind('focus', function(){
		if($(this).hasClass('error-box')){ $(this).removeClass('error-box');}
	});
//Insert address from hidden field into address field on step 3 when "same as mine" link is clicked
	$('.magic-address').bind('click', function(){
		var hiddenAddress = $('#hiddenAddress').val();
		$('#c1address').val(hiddenAddress);
	});
//Check that declaration box is ticked when submit button is clicked
	$('#iwi-reg-form').bind('click', function(event) {
		var thisForm = this;
		var isChecked = $('#declare').attr('checked');
		if( isChecked == false ){
			event.preventDefault();
			$('.dec-error').css('opacity',0).css('display','block').animate({opacity:1}, 999);
		}
	});
//Hide "tick the box" error when previous step is clicked on step 4
	$('#step4prev').bind('click', function(){
		$('.dec-error').animate({opacity:0}, 999, function(){$(this).css('display','none');});
	});
//Set values on select menus if form has been posted
	$('.hidden-select').each(function(index) {
		var thisID = $(this).attr('id');
		var thisVal = $(this).val();
		if(thisVal != ''){
			$('#'+thisVal).attr('selected', 'selected');
		}
	});
//Fix up image on post page
	/*$('.post-content img').each(function(index){
		var thisP = $(this).parents('a').parents('p');
		var imgWidth = $(this).width(); imgWidth = imgWidth+24;
		thisP.width(imgWidth).css('float','right').css('margin-left','12px').css('top','-12px');
	});*/
//--------Functionality for adding and removing child blocks--------------
//Hide child block and edit value of #numChildBlocks hidden field when child block close button is clicked
	$('.child-top .close'). bind('click', function(){
		var thisBoxNum = $(this).children('span').html();
		var numChildBlocks = $('#numChildBlocks').val();
		var thisBlock = $(this).parents('.child-top').parents('.iwi-reg-child-block');
		numChildBlocks = numChildBlocks - 1;
		$('#numChildBlocks').val(numChildBlocks);
		thisBlock.hide('slow');
		addChildCloseButton(numChildBlocks);
	});
//Show next child block and edit value of #numChildBlocks hidden field when "Add another child" is clicked
	$('.add-child-button').bind('click', function(){
		var numChildBlocks = $('#numChildBlocks').val();
		parseInt(numChildBlocks); numChildBlocks++;
		$('#numChildBlocks').val(numChildBlocks);
		$('#child-block-'+numChildBlocks).show('slow');
		addChildCloseButton(numChildBlocks);
	});
//-------------GRANT FORMS----------------------
//--------Previous and next functionality for grant forms-----------
	$('.apply-button').bind('click', function(){
		changeStep('.grant-form-content', '.grant-form-part1', this, '.grant-prev1');
	});
//-------Validate name and email-------------
	$('.continue-button').bind('click', function(){
		var result = validateGrantInfo();
		if(result == ''){
			changeStep('.grant-form-part1', '.grant-form-part2', this, '.grant-prev2');
			$('.continue-button2').css('display','block');
		  }else{
			alert('please enter details');
			if(result == 'emailValid'){
				$('.grant-email-message').show('slow');
			}else{
				$('#'+result).addClass('error-box');
			}
		}
	});
	$('#email').bind('focus',function(){$('.grant-email-message').hide('slow');});
	$('.continue-button2').bind('click', function(){
		changeStep('.grant-form-part2', '.grant-form-part3', this, '.grant-prev3');
	});
	$('.grant-prev1').bind('click', function(){
		changeStep('.grant-form-part1', '.grant-form-content', this, '.apply-button');
	});
	$('.grant-prev2').bind('click', function(){
		changeStep('.grant-form-part2', '.grant-form-part1', this, '.continue-button');
	});
	$('.grant-prev3').bind('click', function(){
		changeStep('.grant-form-part3', '.grant-form-part2', this, '.continue-button');
		$('.continue-button2').css('display','block');
	});
//-------Check that confirmation box is ticked before submitting form on education grant app form-----
	/*$('#education-grant-form').bind('click', function(event){
		event.preventDefault();
		$('.please-tick').css('display','none');
		var checkboxValue = $('#check-confirm').attr('checked');
		var captchaValue = $('#norobot').val();
		
		if((checkboxValue == true) && (captchaValue != '')){
			$('#EducationGrantForm').submit();
		}else{
			if(checkboxValue == false){
				$('.please-tick').show('slow');
			}
			if(captchaValue == ''){
				$('#norobot').addClass('error-box');
			}
		}
	});
	//$('#norobot').removeClass('error-box');
	$('#check-confirm').bind('click', function(){$('.please-tick').css('display','none');});*/
	
//Disable submit button on education grant form so it can be enable when confirmation box is ticked
	$('#education-grant-form').attr('disabled',true).addClass('disabled');
	$('#check-confirm').bind('change', function(){
		var checkValue = $(this).attr('checked');
		if(checkValue == true){
			$('#education-grant-form').attr('disabled',false).removeClass('disabled');
		}else{
			$('#education-grant-form').attr('disabled',true).addClass('disabled');
		}
	});
	
//---------------------------------------------------
//Highlight area on documents page when mousedover

	$('.documents-main').mouseenter(function() {
		$(this).addClass('doc-hover');
	}).mouseleave(function(){
		$(this).removeClass('doc-hover');
	});
//Split date and title in latest Panui cos dude who wrote the YD Recent Posts plugin didn't do it
	$('.yd_rp_widget ul li h4 a').each(function(index){
		var dateAndTitle = $(this).html();
		var stringArray = dateAndTitle.split(': ');
		var date = '<span class="date">'+stringArray[0]+'</span>';
		var title = '<span class="title">'+stringArray[1]+'</span>';
		$(this).html(date+title);
	});
	
//Keep menu item selected when sub-menu is active
	$('.sub-menu').mouseenter(function(){
		$(this).siblings('a').addClass('highlight');
	}).mouseleave(function(){
	   $(this).siblings('a').removeClass('highlight');
	});
//Wrap markup around poll content
	//$('.PDS_Poll').before('<div class="poll-wrap"><div class="poll-inner"><h3>Opinion Poll<span></span></h3>');
	//$('.PDS_Poll').after('</div></div>');
	$('#sub-page-side-nav').siblings('.textwidget').wrap('<div class="poll-wrap"><div class="poll-inner">');
	var pollContent = $('.poll-inner').html();
	var newPollContent = '<h3>Opinion Poll<span></span></h3>'+pollContent;
	$('.poll-inner').html(newPollContent);

});// end $(document).ready(function()

window.onload = function(){
	adjustContentHeight();	
}
