// JavaScript Document
$(function() {

$('#imageSlide').cycle({
	   fx : 'fade',
	   timeout : 5000,
	   speed : 2000
	   })

$('.pressRightHead a').hover(function(){
		$(this).css({'text-decoration':'underline'});									
}, function(){
		$(this).css({'text-decoration':'none'});									
})

function fadeEle(ele, addFocus, time){
	addFocus.focus();
	setTimeout(function(){
						ele.fadeOut();
						},time);
}

l = $('.pressItem').size();
if (l <= 1){
	$('.press_arrows').hide();
}

function changeLink(l){
$('#nav li').each(function() {
	   p = $(this);
	   t = p.text();
	   if (t == 'home') {t = 'index'};
	   if (t == l) {
		   $('a', p).css({'color':'#fff', 'cursor' : 'default'})};
	   })
}

var path = window.location.pathname,
color = '',
ex = path.split('/'),
num = parseInt(ex.length)-1,
file = ex[num],
myLink = file.substring(0, file.length-4);
changeLink(myLink);

$('#nav li').click(function(e){
			name = $(this).text();
			if (name == myLink){
				e.preventDefault();
			}
			});

$('#nav li a, #pullDown, #sendMail').hover(function() {
	color = $(this).css('color');
	$(this).stop().animate({color : '#fff'},300);
				}, function(){
	if (color === '#fff' || color === 'rgb(255,255,255)'){
		return false;
	}else{									
		$(this).stop().animate({color : '#A0A0A0'},300);
	}
});

$('.mailUp').live('click', function() {
$('#mailingListCont').slideDown({
		   duration : 800,
		   easing : 'easeOutCubic'
		   });
$(this).removeClass('mailUp').addClass('mailDown');	
});

$('.mailDown').live('click', function() {
$('#mailingListCont').slideUp({
		   duration : 800,
		   easing : 'easeOutCubic'
			   });
$(this).removeClass('mailDown').addClass('mailUp');
});

$('body').click(function(){
		 if ($('#mailingListCont').is(':visible')){
		  $('#mailingListCont').slideUp({
					   duration : 800,
					   easing : 'easeOutCubic'
						   }).siblings('.mailUp').removeClass('mailUp').addClass('mailDown');
		 }
		 })

$('#mailingListCont').click(function(event){
event.stopPropagation();
});


function loader(ele){
	ele.ajaxStart(function() {
		$(this).show();
	})
	ele.ajaxStop(function() {
	 $(this).hide();
					   })
}


//MAILING LIST FORM
$('[name=sendEmail]').click(function(e){
$('#mailingListCont').css({'height':'105px'});
e.preventDefault();
	var email = $('[name=mailEmail]').val(),
	checkVases = $('[name=checkVases]').is(':checked'),
	checkJewellery = $('[name=checkJewellery]').is(':checked'),
	checkCourses = $('[name=checkCourses]').is(':checked');
	if (email == ''){
		$('#mailingError').html('Please enter an email address').show();
		fadeEle($('#mailingError'), $('[name=mailEmail]'), 2000);
		return false;
	}
	if (!(validateEmail(email))){
		error = 'Invalid email address';
		return false;
	}
	
	//loader($('#mailingAjaxLoader'));
	
	dataString = 'email='+email+'&vases='+checkVases+'&jewellery='+checkJewellery+'&courses='+checkCourses;
	$.ajax({
		   type : 'POST',
		   data : dataString,
		   url : 'phpScripts/sendMailing.php',
		   success : function(data) {
if (data == 1){
$('[name=mailEmail]').val('');	
$('#mailFormCont').fadeOut();
setTimeout(function(){
$('#successDiv').fadeIn();
$('#mailingListCont').animate({
	   'height' : '60px',
	   easing : 'easeInOutBack'
	   },300)														 
},1000)
setTimeout(function(){
$('#mailingListCont').slideUp({
						  duration : 300,
						  easing : 'easeInOutBack'
						  });
}, 4000)

}else{//success
$('#mailingError').html('Sorry but your email was not saved.  Please try again').show('fast');
}

}
})//$.ajax

})





//CONTACT FORM 

$('[name=name]').attr('value', 'name :').focus(function(){
		if ($(this).attr('value') == 'name :'){
			$(this).attr('value', '').css({'color':'#fff'})
		}
		}).blur(function(){
		if ($(this).attr('value') == ''){
			$(this).attr('value', 'name :').css({'color':'#fff'});
		}
		});

$('[name=contact_phone]').attr('value', 'phone :').focus(function(){
		if ($(this).attr('value') == 'phone :'){
			$(this).attr('value', '').css({'color':'#fff'})
		}
		}).blur(function(){
		if ($(this).attr('value') == ''){
			$(this).attr('value', 'phone :').css({'color':'#fff'});
		}
		});

$('[name=contact_email]').attr('value', 'email :').focus(function(){
		if ($(this).attr('value') == 'email :'){
			$(this).attr('value', '').css({'color':'#fff'})
		}
		}).blur(function(){
		if ($(this).attr('value') == ''){
			$(this).attr('value', 'email :').css({'color':'#fff'});
		}
		});

$('[name=message]').attr('value', 'message :').focus(function(){
		if ($(this).attr('value') == 'message :'){
			$(this).attr('value', '').css({'color':'#fff'})
		}
		}).blur(function(){
		if ($(this).attr('value') == ''){
			$(this).attr('value', 'message :').css({'color':'#fff'});
		}
		});

function fadeError(ele){
	setTimeout(function() {
					ele.fadeOut();
					}, 4000);
}

$('#submit').click(function(e) {
e.preventDefault();
name = $('[name=name]').val(),
phone = $('[name=contact_phone]').val(),
email = $('[name=contact_email]').val(),
message = $('[name=message]').val();

if (name == '' || name == 'name :') {
		$('.formError').html('Please enter a name').fadeIn();
		$('[name=name]').focus();
		fadeError($('.formError'));
		return false;
}

noPhone = false;
noEmail = false;

if (phone == '' || phone == 'phone :'){
		noPhone = true;
}

if (email == '' || email == 'email :'){
		noEmail = true;
}

if (noPhone && noEmail){
		$('.formError').show().html('Please enter a phone number or email').fadeIn();
		fadeError($('.formError'));
		return false;
}

if (!(validateNames(name))) {
		$('.formError').show().html('Invalid name, please try again').fadeIn();
		$(['name=name']).focus();
		fadeError($('.formError'));
		return false;
}

if (!(phone == 'phone :')){
if (!(validatePhone(phone))) {
		$('.formError').show().html('Invalid phone number, please try again').fadeIn();
		$(['name=contact_phone']).focus();
		fadeError($('.formError'));
		return false;
}
}

if (!(email == 'email :')){
if (!(validateEmail(email))) {
		$('.formError').show().html('Invalid email, please try again').fadeIn();
		$(['name=contact_email']).focus();
		fadeError($('.formError'));
		return false;
}
}

if (message == '' || message == 'message :') {
message = "Did not leave a message"; 
}
										 
dataString = 'name='+name+'&phone='+phone+'&email='+email+'&mess='+message;

$.ajax({
		type:'POST',
		url:'phpScripts/sendEmail.php',
		data:dataString,
		success:function(data) {
			if (data == 1){
			$('#contactForm, #sendMess').fadeOut();
			setTimeout(function(){
								$('<div>').addClass('thankyou').prependTo('#conHolder').slideDown('slow');
								$('<div>').addClass('thankyouHeader').html('Thank you.').appendTo('.thankyou');
								$('<div>').addClass('thankyouText').html('Olivia will be in contact soon.').appendTo('.thankyou');
								}, 1000)
			setTimeout(function() {
								$('.thankyou').slideUp();
								$('[name=name]').attr('value', 'name :');
								$('[name=contact_phone]').attr('value', 'phone :');
								$('[name=contact_email]').attr('value', 'email :');
								$('[name=message]').attr('value', 'message :'); 
								$('#contactForm, #sendMess').prependTo('#conHolder').fadeIn()
								}, 5000);
		}else if(data == 0){
			$('.formError').html('Uh oh, there was a problem.<br />Your details were not stored.<br />Please try again.').fadeIn();
			setTimeout(function(){
								$('.formError').fadeOut();
								}, 4000);
		}else if(data == 2){
			$('.formError').html('Uh oh, there was a problem.<br />Your details were not sent.<br />Please try again.').fadeIn();
			setTimeout(function(){
								$('.formError').fadeOut();
								}, 4000);
		}
		}
		});
});

});
