$(function() {
/*    $("#language a")
        .css("text-decoration", "none")
        .hover(
            function () {
                $(this)
                    .stop()
                    .animate(
                        {
                            color : "#002d62"
                        },
                        250
                    );
            },
            function () {
                $(this)
                    .stop()
                    .animate(
                        {
                            color : "#f9f9f9"
                        },
                        250
                    );
            }
        );
    */
    $("#breadcrumbs a")
        .css("text-decoration", "none")
        .hover(
            function () {
                $(this)
                    .stop()
                    .animate(
                        {
                            color : "#ce1126"
                        },
                        250
                    );
            },
            function () {
                $(this)
                    .stop()
                    .animate(
                        {
                            color : "white"
                        },
                        250
                    );
            }
        );
    
    $("#tools a")
        .css("opacity", "0.5")
        .hover(
            function () {
                $(this)
                    .stop()
                    .animate(
                        {
                            opacity : 1
                            //paddingTop : 5
                        },
                        250
                    );
            },
            function () {
                $(this)
                    .stop()
                    .animate(
                        {
                            opacity : 0.5
//                            paddingTop : 0
                        },
                        250
                    );
            }
        );
    
    $("#navigation h3 a, #navigation ul li a")
        .hover(
            function () {
                $(this)
                    .stop()
                    .animate(
                        {
                            paddingRight : 26
                        },
                        250
                    );
            },
            function () {
                $(this)
                    .stop()
                    .animate(
                        {
                            paddingRight : 20
                        },
                        250
                    );
            }
        );
        
        
    $("#fmailpage")
        .css({
            display : "none",
            position : "fixed",
            top : -500,
            left : -500,
            opacity : 0,
            width : 700
        })
        .prepend(
            $(document.createElement("a"))
                .html("Close")
                .attr("href", "#")
                .addClass("close")
                .attr("alt", "Close the 'email this page' window")
                .click(function () {
                    $("#fmailpage")
                        .animate(
                            {
                                top : -500,
                                left : -500,
                                opacity : 0
                            },
                            "easeOutExpo",
                            function () {
                                $(this).css("display", "none")
                            }
                        );
                    $("#shade")
                        .animate(
                            {
                                opacity : 0
                            },
                            function () {
                                $(this).remove();
                            }
                        );
                })
        );
        
    $("#mailpage")
        .click(function () {
            $("body")
                .append(
                    $(document.createElement("div"))
                        .attr("id", "shade")
                        .css({
                            position : "fixed",
                            width : "100%",
                            height : "100%",
                            top : 0,
                            left : 0,
                            opacity: 0,
                            backgroundColor : "#002d62"
                        })
                        .animate(
                            {
                                opacity : 0.8
                            }
                        )
                )
            $("#fmailpage")
                .appendTo("body")
                .css("display", "block")
                .animate(
                    {
                        top : 100,
                        left : ($(window).width() / 2) - 350,
                        opacity : 1
                    },
                    "easeOutExpo"
                );
            $("#fmailaddy").focus();
                return false;
        });
        
        /* we wanna stop the mailpage and contactus forms from submitting as usual. we have special ajax scripts for them. */
        $('#fcontactus_form')
            .submit(function () {
                var inputchecked = validateContactForm();
                
                if (inputchecked) {
                    $("#submit").val("Sending...");
                    $("#submit").attr('disabled', true);
                    $("#fname").attr('disabled', true);
                    $("#fmailaddy").attr('disabled', true);
                    $("#fmessage").attr('disabled', true);
                    
                    //make our js object, which we send as json
                    var fData = {
                        fparity : 'parity',
                        fname : $('#fname').val(),
                        fmailaddy : $('#fmailaddy').val(),
                        fmessage : $('#fmessage').val()
                    };
                    
                    $.ajax({
                        type : "POST",
                        url : "ajax_contact.php",
                        dataType : "html",
                        data : fData,
                        success : function (data) {
                            $('#thanks').stop().css("opacity", "1").html(data).delay(5000).animate({opacity : 0}, 600, function () { $(this).empty(); });
                            
                            $("#submit").val("Send");
                            $("#submit").attr('disabled', false);
                            $("#fname").attr('disabled', false);
                            $("#fmailaddy").attr('disabled', false);
                            $("#fmessage").attr('disabled', false);
                            
                        }
                    });
                }
                return false;
            });
        
        
        /* we wanna stop the mailpage and contactus forms from submitting as usual. we have special ajax scripts for them. */
        $('#mailto_form')
            .submit(function () {
                var inputchecked = validateMailToForm();
                
                if (inputchecked) {
                    $("#submit").val("Sending...");
                    $("#submit").attr('disabled', true);
                    $("#fname").attr('disabled', true);
                    $("#fmailaddy").attr('disabled', true);
                    $("#fmessage").attr('disabled', true);
                    
                    //make our js object, which we send as json
                    var fData = {
                        fparity : 'parity',
                        fname : $('#fname').val(),
                        fmailaddy : $('#fmailaddy').val(),
                        fmessage : $('#fmessage').html(),
                        fpageurl : $('#fpageurl').val(),
                        fpagename : $('#fpagename').val()
                    };
                    
                    $.ajax({
                        type : "POST",
                        url : "ajax_mailpage.php",
                        dataType : "html",
                        data : fData,
                        success : function (data) {
                            $('#thanks').stop().css("opacity", "1").html(data).delay(5000).animate({opacity : 0}, 600, function () { $(this).empty(); });
                            
                            $("#submit").val("Send");
                            $("#submit").attr('disabled', false);
                            $("#fname").attr('disabled', false);
                            $("#fmailaddy").attr('disabled', false);
                            $("#fmessage").attr('disabled', false);
                            
                        }
                    });
                }
                return false;
            });
        
        //fix the minimenu content
        $('#minimenu a')
            .click(function () {
                var _id = $(this).attr('href');
                
                //fade out all divs
                $('#content div[id^="_"]')
                    .fadeOut('fast');
                //and fade in just the one we want
                $('#content div[id^="_"]')
                    .each(function () {
                        if ($(this).attr('id') == _id.replace("#", "")) {
                            $(this).fadeIn('fast');
                        }
                    });
                
                //set all minimenu links to links
                $('#minimenu a')
                    .css(
                        {
                            backgroundColor : 'transparent',
                            color : "#002d62",
                            textDecoration : "none"
                        }
                    );
                
                //except the current one, which we make plaintext
                $('#minimenu a')
                    .each(function () {
                        if ($(this).attr('href') == _id) {
                            $(this)
                                .css(
                                    {
                                        backgroundColor : '#ce1126',
                                        color : "whitesmoke",
                                        textDecoration : "none"
                                    }
                                );
                        }
                    });
                
                return false;
            });
        
        //remove the now redundant back to top links
        $('.backtotop')
            .remove();
        
        //to start with, hide all the divs...
        $('#content div[id^="_"]')
            .css('display', 'none');
        //... and just show the first one
        $('#content div[id^="_"]').eq(0).css('display', 'block');
        
        //and set up the links correctly
        $('#minimenu a')
            .css(
                {
                    backgroundColor : 'transparent',
                    color : "#002d62",
                    textDecoration : "none"
                }
            );
        
        //except the current one, which we make plaintext
        $('#minimenu a').eq(0)
            .css(
                {
                    backgroundColor : '#ce1126',
                    color : "whitesmoke",
                    textDecoration : "none"
                }
            );
        
});



//checks the input before we ajax it
function validateContactForm () {
    
    //get the values and check they are valid
    var fnamecheck = $('#fname').val();
    var fmailaddycheck = $('#fmailaddy').val();
    var fmessagecheck = $('#fmessage').val();
    
    if (!fnamecheck) {
        $('#name_msg').stop().css("opacity", "1").html('Please enter your name.').delay(2000).animate({opacity : 0}, 600, function () { $(this).empty(); });
        $('#fname').focus();
        return false;
    }
    
    if (!fmailaddycheck) {
        $('#mailaddy_msg').stop().css("opacity", "1").html('Please enter your email address.').delay(2000).animate({opacity : 0}, 600, function () { $(this).empty(); });
        $('#fmailaddy').focus();
        return false;
    }
    if (fmailaddycheck.indexOf('@') == -1) {
        $('#mailaddy_msg').stop().css("opacity", "1").html('Please enter a valid email address.').delay(2000).animate({opacity : 0}, 600, function () { $(this).empty(); });
        $('#fmailaddy').focus();
        return false;
    }
    
    if (!fmessagecheck) {
        $('#message_msg').stop().css("opacity", "1").html('Please enter a message.').delay(2000).animate({opacity : 0}, 600, function () { $(this).empty(); });
        $('#fmessage').focus();
        return false;
    }
    
    return true;
}

function validateMailToForm () {
    
    //get the values and check they are valid
    var fnamecheck = $('#fname').val();
    var fmailaddycheck = $('#fmailaddy').val();
//    var fmessagecheck = $('#fmessage').val();
    
    if (!fnamecheck) {
        $('#name_msg').stop().css("opacity", "1").html('Please enter your name.').delay(2000).animate({opacity : 0}, 600, function () { $(this).empty(); });
        $('#fname').focus();
        return false;
    }
    
    if (!fmailaddycheck) {
        $('#mailaddy_msg').stop().css("opacity", "1").html('Please enter your email address.').delay(2000).animate({opacity : 0}, 600, function () { $(this).empty(); });
        $('#fmailaddy').focus();
        return false;
    }
    if (fmailaddycheck.indexOf('@') == -1) {
        $('#mailaddy_msg').stop().css("opacity", "1").html('Please enter a valid email address.').delay(2000).animate({opacity : 0}, 600, function () { $(this).empty(); });
        $('#fmailaddy').focus();
        return false;
    }
    
/*    if (!fmessagecheck) {
        $('#message_msg').stop().css("opacity", "1").html('Please enter a message.').delay(2000).animate({opacity : 0}, 600, function () { $(this).empty(); });
        $('#fmessage').focus();
        return false;
    }*/
    
    return true;
}

