var imageDir = 'wp-content/themes/shandcycles/images/front_page/'; 

var timer;
var fadeDuration;

function initDots(){
	var image1 = new Image( 960,530 ); 
	image1.src= imageDir + 'frontpage1.jpg'; 
	
	var image2 = new Image( 960,530 ); 
	image2.src= imageDir + 'frontpage2.jpg'; 
	
	var image3 = new Image( 960,530 ); 
	image3.src= imageDir + 'frontpage3.jpg';

	$( 'dot1' ).observe( 'click', function(){ showPic( '1' ); } )
	$( 'dot2' ).observe( 'click', function(){ showPic( '2' ); } )
	$( 'dot3' ).observe( 'click', function(){ showPic( '3' ); } )
	
	showPic(); 
	timer = setInterval ( showPic, 10000 );
}

function showPic( inPic ){
	
	var outPic = 0;
	if( $( 'frontPagePic1' ).getOpacity() > 0 ){
	 	outPic = 1;
	}else if( $( 'frontPagePic2' ).getOpacity() > 0 ){
	 	outPic = 2;
	}else if( $( 'frontPagePic3' ).getOpacity() > 0 ){
	 	outPic = 3;
	}	

	if(!inPic){
		inPic = outPic + 1;
		if( inPic > 3 )
			inPic = 1;
		fadeDuration = 1;	
	}else{
		if( timer )
			clearInterval( timer );
		fadeDuration = "0.2"; 
	}	

	YUI().use('anim', function(Y) {
	    var out = new Y.Anim({
		    node: '#frontPagePic' + outPic,
			  duration : fadeDuration,
	        to: { opacity: 0 }
	    });
	
	if( outPic > 0 )
		out.run();
			
	});

	YUI().use('anim', function(Y) {
	    var inx = new Y.Anim({
		    node: '#frontPagePic' + inPic,
			  duration : fadeDuration,
	        to: { opacity: 1 }
	    });
	
	if( inPic > 0 )
		inx.run();
			
	});

	setDotsColours( inPic );
}

function setDotsColours( picNumber ){
	$$( '.dot' ).each( function( element ){ setDotColour( element, picNumber ); } );
}

function setDotColour( element, picNumber ){
	 if( element.id == 'dot' + picNumber ){
	 	element.addClassName( "blue" );
		element.removeClassName( "white" );
	 }else{
	 	element.addClassName( "white" );
		element.removeClassName( "blue" );
	 }
}

function doOnContactLoad(){
	var contactSuccess = getQueryVariable( 'contact_success' );
	if( contactSuccess && contactSuccess == "1" ){
		$('contactSuccessMessage').show();
	}

	var messageSuccess = getQueryVariable( 'message_success' );
	if( messageSuccess && messageSuccess == "1" ){
		$('messageSuccessMessage').show();
	}
}

function validateContactForm(){
	var name_id = 'name_input';
	var email_id = 'email_input';
	var valid = true;
	
	if( !validateNotEmpty( name_id ) ){
		valid = false;
	}

	if( !validateEmail( email_id ) ){
		valid = false;
	}
	
	return valid;
}

function validateMessageForm(){
	var name_id = 'name_input_msg';
	var email_id = 'email_input_msg';
	var valid = true;
	
	if( !validateNotEmpty( name_id ) ){
		valid = false;
	}

	if( !validateEmail( email_id ) ){
		valid = false;
	}
	
	return valid;
}

function validateCustomFrameForm(){
	var valid = true;
	
	if( !validateEmail( 'cff_email' ) ){
		valid = false;
	}	

	if( !validateNotEmpty( 'cff_name' ) ){
		valid = false;
	}	

	var genderSelected = ( $F('gender_male') != null || $F('gender_female') != null );
	if( genderSelected == false ){
		highlightError( 'gender', 'this field is mandatory' ); 
		valid = false;
	}
	
	if( !valid ){
		$('cff_name').scrollTo();
	}
	
	return valid;
}

function validateNotEmpty( formElementId ){
	if( $F(formElementId).length < 1  ){
		highlightError( formElementId, 'this field is mandatory' );
		return false;
	}else{
		removeError( formElementId );
		return true;
	}
}

function validateEmail( formElementId ){
	if( !validEmailAddress( $F(formElementId) ) ){
		highlightError( formElementId, 'not a valid email address' );
		return false;
	}else{
		removeError( formElementId );
		return true;
	}
}

function validEmailAddress( emailAddress ){
	return /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailAddress);
}

function highlightError( formElementId, errorMessage ){
	if( $( formElementId ) ){
		$(formElementId).addClassName( 'error' );
		$(formElementId).title = errorMessage;
	}
}

function removeError( formElementId ){
	if( $( formElementId ) ){
		$(formElementId).removeClassName( 'error' );
		$(formElementId).title = null;
	}
}

function getQueryVariable(variable){
	
  var query = window.location.search.substring(1); 
  var vars = query.split("&"); 
  for (var i=0;i<vars.length;i++){ 
    var pair = vars[i].split("="); 
    if (pair[0] == variable){ 
      return pair[1]; 
    } 
  }
  return -1; //not found 
}

function privacyWindow(){
	window.open( 'privacy.html', '_blank');
}

