/* User Scripts */

$(document).ready(function(){

/* Images hover */	

		$(".portfolio_full img, .portfolio_two img, .content_picture_prettyphoto img, .image-nav").hover(
		function() {
		$(this).animate({"opacity": "0.6"}, "slow");
		},
		function() {
		$(this).animate({"opacity": "1"}, "slow");
		});	
		
/* Social icons hover */
		
		$('#footer-widget-fourth .social-links img').fadeTo("fast","0.5");
		$("#footer-widget-fourth .social-links img").hover(
		function() {
		$(this).animate({"opacity": "1"}, "fast");
		},
		function() {
		$(this).animate({"opacity": "0.5"}, "fast");
		}); 		
		
		
/* TipTip Tooltips settings */
	  
    $(function(){
	$(".tt_bottom").tipTip({edgeOffset: 5, defaultPosition: "bottom"});
	});
	
    $(function(){
	$(".tt_top").tipTip({edgeOffset: 5, defaultPosition: "top"});
	});
	
	$(function(){
	$(".tt_left").tipTip({edgeOffset: 5, defaultPosition: "left"});
	});
	
	$(function(){
	$(".tt_right").tipTip({edgeOffset: 5, defaultPosition: "right"});
	});	
	
/* Scroll to Top */
	
	$('a[href=#top]').click(function(){
        $('html, body').animate({scrollTop:0}, 'slow');
        return false;
    });	


/* Contact Form */

function checkemail(emailaddress){
    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i); 
	return pattern.test(emailaddress); 
}

$(document).ready(function(){ 
	var $math1 = Math.floor( Math.random() * (100) );
	var $math2 = Math.floor( Math.random() * (100) );
	$('#contactform #math1').text($math1);
	$('#contactform #math2').text($math2);
	var $mathsum = $math1 + $math2;
	$('#contactform a#formsend').click(function(){ 
		var $name 	= $('#name').val();
		var $email 	= $('#email').val();
		var $subject = $('#subject').val();
		var $message = $('#message').val();
		var $confirm = $('#confirm').val();
		var $contactemail = $('#contactemail').val();
		if($name != '' && $name.length < 3){ $nameshort = true; }else{ $nameshort = false; }
		if($name != '' && $name.length > 30){ $namelong = true; }else{ $namelong = false; }
		if($email != '' && checkemail($email)){ $emailerror = true; }else{ $emailerror = false; }
		if($subject != '' && $subject.length < 3){ $subjectshort = true; }else{ $subjectshort = false; }
		if($subject != '' && $subject.length > 100){ $subjectlong = true; }else{ $subjectlong = false; }
		if($message != '' && $message.length < 3){ $messageshort = true; }else{ $messageshort = false; }
		if($confirm != '' && $confirm != $mathsum){ $confirmerror = true; }else{ $confirmerror = false; }
		$('.loading').fadeIn('fast');
		if($name != '' && $nameshort != true && $namelong != true && $email != '' && $emailerror != false && $subject != '' && $subjectshort != true && $subjectlong != true && $message != '' && $messageshort != true && $confirm != '' && $confirmerror != true && $contactemail != ''){ 
			$.post('php/sendmail.php', 
				{contactemail: $contactemail, name: $name, email: $email, subject: $subject, message: $message}, 
				function(data){
					$('.loading').fadeOut('fast');
					$('#contactform').fadeOut();
					$('#name, #subject, #url, #email, #message, #confirm').val('').css({'border':'none'});
					$('#contactform label span').remove();
					$('label.overlabel-apply').show();
					
					$('<div class="box2 ok"><span>Thank you, ' + $name + '! <br />Your message has been send successfully.</span></div>').insertBefore('#contactform');

					$('.box2').delay(5000).fadeOut(1000, function(){ 
						$(this).remove(); 
						$('#contactform').fadeIn();
						$('label.overlabel-apply').fadeIn();
					});
				}
			);
			return false;
		}else{
			$('.loading').fadeOut('fast');
			
			$('<div class="box2 err"><span>Your message has not been send successfully.<br />It was an error on page!</span></div>').insertBefore('#contactform');

			$('.box2').delay(5000).fadeOut(1000, function(){ $(this).remove(); });
			if($name == ''){ 
				$('#name').parent('div').find('label').find('span').remove(); 
				$('#name').parent('div').find('label').append('<span>&laquo; You forgot to enter your name.</span>'); 
			}else if($nameshort == true){ 
				$('#name').parent('div').find('label').find('span').remove(); 
				$('#name').parent('div').find('label').append('<span>&laquo; Entered name must not be shorter than 3 characters.</span>'); 
			}else if($namelong == true){ 
				$('#name').parent('div').find('label').find('span').remove(); 
				$('#name').parent('div').find('label').append('<span>&laquo; Entered name must not be longer than 30 characters.</span>'); 
			}else{ 
				$('#name').parent('div').find('label').find('span').remove(); 
			}
			if($email == ''){ 
				$('#email').parent('div').find('label').find('span').remove(); 
				$('#email').parent('div').find('label').append('<span>&laquo; You forgot to enter your email address.</span>'); 
			}else if($emailerror == false){ 
				$('#email').parent('div').find('label').find('span').remove(); 
				$('#email').parent('div').find('label').append('<span>&laquo; You entered an invalid email address.</span>'); 
			}else{ 
				$('#email').parent('div').find('label').find('span').remove(); 
			}
			if($subject == ''){ 
				$('#subject').parent('div').find('label').find('span').remove(); 
				$('#subject').parent('div').find('label').append('<span>&laquo; You forgot to enter the subject of your message.</span>'); 
			}else if($subjectshort == true){ 
				$('#subject').parent('div').find('label').find('span').remove(); 
				$('#subject').parent('div').find('label').append('<span>&laquo; Entered subject must not be shorter than 3 characters.</span>'); 
			}else if($subjectlong == true){ 
				$('#subject').parent('div').find('label').find('span').remove(); 
				$('#subject').parent('div').find('label').append('<span>&laquo; Entered subject must not be longer than 100 characters.</span>'); 
			}else{ 
				$('#subject').parent('div').find('label').find('span').remove(); 
			}
			if($message == ''){ 
				$('#message').parent('div').find('label').find('span').remove(); 
				$('#message').parent('div').find('label').append('<span>&laquo; You forgot to enter the text of your message.</span>'); 
			}else if($messageshort == true){ 
				$('#message').parent('div').find('label').find('span').remove(); 
				$('#message').parent('div').find('label').append('<span>&laquo; Entered message must not be shorter than 3 characters.</span>'); 
			}else{ 
				$('#message').parent('div').find('label').find('span').remove(); 
			}
			if($confirm == ''){ 
				$('#confirm').parent('div').find('label').find('span').remove(); 
				$('#confirm').parent('div').find('label').append('<span>&laquo; You forgot to fill the confirmation field.</span>'); 
			}else if($confirmerror == true){ 
				$('#confirm').parent('div').find('label').find('span').remove(); 
				$('#confirm').parent('div').find('label').append('<span>&laquo; Entered confirmation value is incorrect.</span>'); 
			}else{ 
				$('#confirm').parent('div').find('label').find('span').remove(); 
			}
			return false;
		}
	});
});



