//Top Nav Highlighting
$(document).ready(function(){
       $('.active').find('a').css('color','#fff');
        var navhover = 0;
        $('#topnav li').hover(function(){
              navhover = 1;
              $('.active').find('a').stop().css('color','#6b6a6a');
              $(this).find('a').stop().animate({color: '#fff'});
        },function(){
              navhover = 0;
              $(this).find('a').stop().animate({color: '#6b6a6a'}).delay(200);
        });

        $('#topnav').mouseleave(function(){
              setTimeout(function(){
                     if(!navhover)
                            $('.active').find('a').animate({color: '#fff'})
              }, 500);
        });
        
       
        //Button Animations
        //cache jquery objects
        $fancy_btn = $('.fancy-button');
        $fancy_btn1 = $('.fancy-button1');
        $fancy_btn2 = $('.fancy-button2');
                       
        //call the animation
        fancy_button($fancy_btn, 10);
        fancy_button1($fancy_btn1,10);
        fancy_button2($fancy_btn2, 5);
                       
        //selected state of the buttons
        $fancy_btn.click(function(){
                $fancy_btn.removeClass('selected');
                $(this).addClass('selected');
                $fancy_btn.find('span').show(200);
                $('.selected').find('span').fadeOut(200);
        });
                       
        //animate buttons
        function fancy_button(obj, val){
                obj.hover(function(){
                        $e = $(this);
                        $e.css({
                                'background-color':'#a61060'
                        },3000).find('span').stop().animate({
                                right: '-'+val
                        }, 200,'easeInOutQuad');
                },function(){
                        $e= $(this);
                        $e.css({
                                'background-color':'#8a0e50'
                        },3000).find('span').stop().animate({
                                right: '0'
                        }, 200,'easeInOutQuad'); 
                });  
        }
                        
        //fancy button type 2
        function fancy_button1(obj, val){
                var width = 0;
                obj.hover(function(){
                        $e = $(this);
                        width = $e.find('.extend').width();
                        $e.find('.extend').stop().animate({
                                width: width+val+'px'
                        }, 200,'easeInOutQuad');
                },function(){
                        $e = $(this);
                        $e.find('.extend').stop().animate({
                                width: width+'px'
                        }, 200,'easeInOutQuad');
                });       
        }
        
        //fancy button type 3
        function fancy_button2(obj, val){
                var width = 0;
                obj.hover(function(){
                        $e = $(this);
                        width = $e.find('span').width();
                        $e.find('span').stop().animate({
                                width: width+val+'px'
                        }, 200,'easeInOutQuad');
                },function(){
                        $e = $(this);
                        $e.find('span').stop().animate({
                                width: width+'px'
                        }, 200,'easeInOutQuad');
                });    
        }

				
        // fancybox
        $('.contact').click(function(){
                $.fancybox({
                        href:$(this).attr('href'),
                        centerOnScroll : true,
                        'autoDimensions': true,
                        'width': 820
                        //'height': 420
                });
                return false;
        });
        $(".fancybox").fancybox({
              autoDimensions: true,
              width: 820
              //height: 420
        });
        
        //form styling and validation
        $("select, label, input:checkbox, input:radio, input:file, input:text, input:password,textarea").uniform();			
				//process the form
				function process_form(){
					$.post('sendmail.php', {firstName: $('[name=firstName]').val(), name:$('[name=name]').val(), lastName: $('[name=lastName]').val(), email: $('[name=email]').val(), organisation: $('[name=organisation]').val(), phone_number: $('[name=phone_number]').val(), msgtext: $('[name=msgtext]').val()}, function(data){
				                                 
						//	$("#success_message").html(data.message).fadeIn(1000).css('text-align','center').show(500);
						//	$("#contactform").hide();
							//resize();
					}, 'json');
                                        $("#success_message").html("<p><strong>Thank you!</strong></p><p><strong>Your message has been sent. We will contact you shortly.</strong></p>").fadeIn(1000).css('text-align','center').show(500);
                                          $("#contactform").hide();
				}
				/*****************Custom Functions *********************/
			
				jVal = {
					'firstName' : function(){
						var nameInfo = $('#firstNameInfo');
						var ele = $('#firstName');
							
						if(ele.val().length < 3 ) {
							jVal.errors = true;
							nameInfo.removeClass('correct').addClass('error').html('Please enter your first name (it must be at least 3 characters)').show();
							ele.removeClass('normal').addClass('wrong');
							//resize();
									
						} else {
							nameInfo.removeClass('error').fadeOut(500);
							ele.removeClass('wrong').addClass('normal');
							//resize();
						}
					},
					'lastName' : function(){
						var nameInfo = $('#lastNameInfo');
						var ele = $('#lastName');
							
						if(ele.val().length < 3 ) {
							jVal.errors = true;
							nameInfo.removeClass('correct').addClass('error').html('Please enter your first name (it must be at least 3 characters)').show();
							ele.removeClass('normal').addClass('wrong');
							//resize();
									
						} else {
							nameInfo.removeClass('error').fadeOut(500);
							ele.removeClass('wrong').addClass('normal');
							//resize();
						}
					},
					'email' : function(){
						var emailInfo = $('#emailInfo');
						var ele = $('#email');
						var pattern = /^.+@.+[.].{2,}$/i;
							
						if(!pattern.test(ele.val())) {
							jVal.errors = true;
							emailInfo.removeClass('correct').addClass('error').html('Please enter a valid email address.').show();
							ele.removeClass('normal').addClass('wrong');
							//resize();
						} else {
							emailInfo.removeClass('error').fadeOut(500);
							ele.removeClass('wrong').addClass('normal');
							//resize();
						}
					},
			
					'sendIt' : function (){
						if(!jVal.errors) {
							$('#try_it_now').submit();
							//process_form();
						}
					}
				};
			
				// ====================================================== //
			 
				$('#send').click(function (e){
                                          e.preventDefault();
					jVal.errors = false;
					jVal.firstName();
					jVal.lastName();
					jVal.email();
					jVal.sendIt();
					return false;
				});
                                $('#try_it_now').bind('submit', function(e){
                                   e.preventDefault();
                                   $('input[type=submit]', this).attr('disabled', 'disabled');
                                   $('select', this).attr('disabled', 'disabled');
                                   $('input[type=text]', this).attr('readonly', 'readonly');
                                   $('textarea', this).attr('readonly', 'readonly');
                                   process_form();
                                   });
				
				//call the functions for repective fields
				$('#firstName').blur(jVal.firstName);
				$('#lastName').blur(jVal.lastName);
				$('#email').blur(jVal.email);
});
$(window).load(function() {
       //initialise simplexo topnav			
        $("#topnav").simplexoNav({
                reset: 200
        });
});
