$(document).ready(function() {
    $('.no-label').labels();
	
	$('#main-nav li.home a').attr('href','/');
    
    // home page
    $('.banner > ul').cycle({fx:'fade',timeout:10000,pager:'#banner-nav'});
    
    // subscribe form
    $(".subscribe-form").validate({
            rules: {
                email: {
                        required: true,
                        email: true
                }
            },
            messages: {                    			
                email: "Please enter a valid email address"
            },
            errorLabelContainer: $('.errors')
    });
    //contact form
    $('form#contact-us').validate({
            rules: {
                Title: {
                        required: true
                },
                Name: {
                        required: true
                },
                Email: {
                        required: true,
                        email: true
                },
                EnquiryType: {
                        required: true
                },
                Message: {
                        required: true
                }
            },
            messages: {                    			
                Title: "Please enter your Title",	
                Name: "Please enter your Name",	
                Email: "Please enter a valid email address",	
                EnquiryType: "Please enter an Enquiry Type",		
                Message: "Please enter a Message"
            },
            errorLabelContainer: $('.errors')
    });
    // show datepicker
    $('.datepicker input').datepicker({
        dateFormat: 'dd/mm/yy',
        changeMonth: true,
        changeYear:true
    }); 
    $('select#EnquiryType').change(toggleDatePicker);
    function toggleDatePicker() {
        var val = $('select#EnquiryType option:selected').val();
        if(val === 'Arrange an appointment') {
            $('#preferredDate').slideDown();            
        } else {
            $('#preferredDate').slideUp();
        }
    }
    toggleDatePicker();
    $('input[name="PreferredDate"]').datepicker({
        dateFormat: 'dd/mm/yy'
    }); 
    
    // navigation
    $('#nav-hold #main-nav > li > ul').hide();
    $('#nav-hold #main-nav > li:not(.home)').hoverIntent({
        over: function() {
            //
            $(this).find('ul').slideDown('fast');
        },
        out: function() {
            $(this).find('ul').slideUp('fast');
        },
        timeout:300
    });
    
    // team page
    $('.the-team .content table tr td:first-child').addClass('first-child');
    
    
    //our practice
    $('.gallery #last-nav a').click(function() {
        $('.gallery #last-nav a').removeClass('selected');
        $('.gallery a.gallery-link').removeClass('selected');
        $(this).addClass('selected');
        $.get('handler.php?action=get-content&page='+$(this).attr('href')+'',
        function(m) {
            $('.gallery-content').html(m);
        });
        
        return false;
    });
    $('.gallery a.gallery-link').click(function() { 
        $('.gallery #last-nav a').removeClass('selected');       
        $(this).addClass('selected');
        $.get('handler.php?action=get-content&page='+$(this).attr('href')+'',
        function(m) {
            $('.gallery-content').html(m);
        });
        
        return false;
    });
    
    var height = $('.our-practice .content').height();
    var diff = height-543;    
    // add height to this
    var margin = diff+90;
    $('.internal .heading .sidebar .gallery h2').css('marginTop',margin+'px');
    
    $('#History').textareaCount({
        maxCharacterSize: 400,
        displayFormat: "#input of #max characters"
    });
    $('#Reason').textareaCount({
        maxCharacterSize: 200,
        displayFormat: "#input of #max characters"
    });
});
