$(document).ready(function(){
    
    $('#submitNewsLetterButton').live('click',function(){
        var formUserName = $('#newsletter_name').val();
        var formUserEmail = $('#newsletter_email').val();
        var formUserTelephone = $('#newsletter_telephone_number').val();
        var formUserAddress = $('#newsletter_address').val();
        
        $.post('/ajax/newsletter', {
            newsletter_name: formUserName, newsletter_email: formUserEmail, newsletter_telephone_number: formUserTelephone, newsletter_address:formUserAddress
        },function(returnData){
            $('#miniDiv').html(returnData);
        })
    })
    
    $('#linkHome').attr('title','Home');
    
    $('#linkHome').click(function(){
        window.location='/'; 
    })
    
    $('#solicitorsEmail').click(function(){
        window.location='mailto:infolarkhill@russellrussell.co.uk'; 
    })
    
    $('#closeNewsletter').live('click',function(){
        $('#overlay').css('display','none');
        $('#miniDiv').slideUp();
    })
    
    $('.serviceType').click(function(){
    })
    
    $('#servicesLinkContainer').mouseover(function(){
        $(this).stop();
        $(this).children('#servicesTopLink').css('background-image','url(/images/servicesBackTop.jpg)');
        $(this).children('#servicesTopLink').css('color','#3f3c76');
        $(this).children('#servicesLinks').show();
    })
    
    $('#servicesLinkContainer').mouseout(function(){
        $(this).children('#servicesTopLink').css('background-image','none');
        $(this).children('#servicesTopLink').css('color','#fff');
        $(this).children('#servicesLinks').hide();
    })
    
    $('#servicesLinks').css('border-bottom', '1px #8b8b8b solid');
    
    $('#servicesLinks a:even').css('margin-right','32px');
    
    $('.faqQuestion').click(function(){
        $(this).siblings('.faqAnswer').slideToggle();
    })
    
    $('.faqAnswer').click(function(){
        $(this).slideUp();
    })
    
    $('.section').last().css('border-bottom','none');
    
    /**
    *  Newsletter Signup
    */
    
    $('#signUpForNewsletter').click(function(){
        
        $('body').prepend('<div id = "overlay"></div>');
        
        var docHeight = $(document).height();
        var docWidth = $(document).width();
        var addTop = $(document).scrollTop() + 55;
        var docWidth = $(window).width();
        var centralPosition = docWidth/2;

        //only create divs
        if ($('#miniDiv').length == 0) {
            
            $('#overlay').after('<div id ="minDivContainer" class="containerOverlay"><div id = "miniDiv"></div></div><div class="finalSection"></div>');
            $('#miniDiv').load('/ajax/newsletter', function(){
                
                $('#overlay').css('height',docHeight+'px');
                $('#overlay').css('width',docWidth+'px');
                $('#overlay').css('display','block');

                var popupWidth = $('#miniDiv').width();
                var halfEditor = popupWidth/2;
                var popUpPosition = Math.ceil(centralPosition-halfEditor);
                
                $('#miniDiv').css('margin-top',addTop+'px');
                $('#miniDiv').slideDown(400);
            
            });
        
        } else{
            
            $('#overlay').css('height',docHeight+'px');
            $('#overlay').css('width',docWidth+'px');
            $('#overlay').css('display','block');

            var popupWidth = $('#miniDiv').width();
            var halfEditor = popupWidth/2;
            var popUpPosition = Math.ceil(centralPosition-halfEditor);
            
            $('#miniDiv').css('margin-top',addTop+'px');
            $('#miniDiv').slideDown(400);
            
        }
    })

    $('#newsletter-signup').submit(function(){
        return false;
    })
    
    /**
    * End of Newsletter Signup
    */
    
    function removeDefaultBoxes() {
        $('#defaultBoxes').css('display','none');
    }
    
    function restoreDefaultBoxes() {
        $('#defaultBoxes').css('display','block');
    }
    
    function showChoices(IDName) {
         $('#'+IDName).css('display','block');
    }
    
    function removeChoices() {
        $('#sectionFemaleChoices').css('display','none');
        $('#sectionMaleChoices').css('display','none');
    }
    
    function removeChoiceDetails() {
        $('#choiceDetails').slideUp();
        $('#choiceDetails').html('');
    }
    
    //Claims Calculator
    $('.interaction1').click(function(){
        
        //remove default Boxes
        removeDefaultBoxes();
        
        //kill all remaining animation and start again
        $(this).stop(true,true);
        varInitialChoice = this.id;
        $('.sectionGender').css('display','none');
        if (varInitialChoice == 'genderChoiceMale') {
            showChoices('sectionMaleChoices');
            $.scrollTo($('#sectionMaleChoices'),{duration:1000});
        } else if (varInitialChoice == 'genderChoiceFemale') {
            showChoices('sectionFemaleChoices');
            $.scrollTo($('#sectionFemaleChoices'),{duration:1000});
        }

    })
    
    $('.sectionGender .bodySection').click(function(){
        $.ajaxSetup({
            cache: false
        });
        var chosenIllness = this.id;
        $('#choiceDetails').load('/claims/get_injury_details/'+chosenIllness, function(){
            $('#choiceDetails').slideDown('normal',function(){
                $('#disclaimer').css('display','block');
                $.scrollTo($('#choiceDetails'),{duration:1000});
            });
        });
        
        
    })
    
    $('#resetButton').click(function(){
        $.scrollTo($('#banner'),{duration:1500});
        removeChoiceDetails();
        removeChoices();
        restoreDefaultBoxes();
        $('#disclaimer').slideUp();
    })
    
    $('.sectionGender .bodySection').hover(function(){
        
        //reset the other divs to make sure we do not have any divs lying about!!!!!
        $('.sectionGender .bodySection').each(function(){
            if ($(this).children('.choiceOverlay').length == 0) {
                $(this).prepend('<div class="choiceOverlay"></div>'); 
            }
        })
        
        //remove the overlay for the right choice
        $(this).children('.choiceOverlay').remove();
        
        $(this).mouseleave(function(){
            if ($(this).children('.choiceOverlay').length == 0) {
                $(this).prepend('<div class="choiceOverlay"></div>'); 
            }
        })
    })
    
    $('#requestCallBackForm input').click(function() {
        
        var currentText = $(this).val()
        $(this).val('');
        var newText = '';
        
        $(this).blur(function(){
            newText = $(this).val();
            if (newText == '') {
                $(this).val(currentText);
            }
        })
    
    })
    
    $('#requestCallBackForm').submit(function(){
        
        var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
        var emailAddress = $('#your_email').val();
        var theName = $('#your_name').val();
        var theContactNumber = $('#your_name').val();
        
        //removes any default text
        if (theName == 'Your name...') {
            theName = '';
        }
        
        //removes any default text
        if (theContactNumber == 'Your contact number...') {
           theContactNumber = ''; 
        }
        
        if(reg.test(emailAddress) == false || theContactNumber == '' || theName == '') {
            alert('Please fill in your name, your contact number and your email address to continue');
            return false;
        } else {
            return true;
        }
        
    })
    
    $('#commonQuestions .rightQuestion').click(function(){
        $(this).siblings('.rightAnswer').slideToggle();
    })
    
    $('#commonQuestions .rightAnswer').click(function(){
        $(this).slideUp();
    })
    
    /*Testimonial Slider*/
    //couple of options
    var navNormalColor = '#36387E';
    var highlightedNormalColor = '#8B8B8B';
    var timerAmount = 10000;
    var transitionSpeed = 'slow';
    
    //No Further Editing Required
    var testimonialWidth = $('.testimonialPanel').outerWidth(true);
    var testimonialAmount = $('.testimonialPanel').length;
    var completeWidth = testimonialWidth*testimonialAmount;
    var lastReference = completeWidth-testimonialWidth;
    var animatorTracker = 1;
    
    //set widths   
    $('#testimonialInner').css('width',completeWidth);
    $('#testimonialHolder').scrollLeft(0);
    
    //create opening and closing list
    $('#testimonyLinks').append('<ul id = "testimonyNavigation"></ul>');
    
    //Create Navigation Tracker/Links
    $('.testimonialPanel').each(function(i){
        newItemi = $('#testimonyNavigation').append('<li id = "testimonyNav'+i+'"></li>');
        $('#testimonyNav'+i).bind('click',function(){
            //can be used to add links when necessary
            //alert(i);
        })
    })
    
    $('#testimonyNavigation li:first').css('color',navNormalColor);
    
    //this shifts the scroll    
    function animateBox() {
        
        $('#testimonyNavigation li').each(function(){
            $(this).css('color',highlightedNormalColor);
        })
        
        $('#testimonyNav'+animatorTracker).css('color',navNormalColor);
        
        //recheck current position
        currentScroll = $('#testimonialHolder').scrollLeft();
        
        //move to original position
        if (currentScroll == lastReference) {
            currentScroll = -testimonialWidth;
        }
        
        $('#testimonialHolder').animate({
            'scrollLeft':currentScroll+testimonialWidth
        }, transitionSpeed, function(){
            if (animatorTracker == testimonialAmount-1) {
                animatorTracker = 0;
            } else {
                animatorTracker++;
            }
            //this calls itself recursively
            setTimeout(animateBox, timerAmount);
        })
    }
   
    //trigger initial animation and enter loop  
    setTimeout(animateBox, timerAmount);
    
    $('.officeList h5').click(function(){
        $(this).siblings('.officeListDetails').slideToggle();
    })
    
})

