/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
$(document).ready(function(){
    $(".zoom").fancybox();
    $(".cta").jCarouselLite({
    visible: 1,
    start: 0,
    speed: 1400,
    auto: 5000,
    btnGo:
    [".externalControl .1", ".externalControl .2",
    ".externalControl .3", ".externalControl .4",
    ".externalControl .5"],
     afterEnd:function(a){
             $(".externalControl li a").each(function(){
                    $(this).removeClass("selected");
            });

            var temp = a.index();
            if(temp==6){
                temp = 1;
            }
            temp = $(".externalControl li").eq(temp-1);
            temp.find("a").addClass("selected");
        }
    });

    $("#booking-form").submit(function(){
        var validate = true;
        
        $("#booking-form select, #booking-form input, #booking-form textarea").each(function(i,e) {
                //Validate Required
                if($(this).hasClass("require")){
                        if($(this).val()==''){
                                $(this).animate({'background-color': '#ff9292'},500);
                                validate = false;
                        }else{
                                $(this).animate({'background-color': '#9aff92'},500);

                        }
                }
                //Validate Email
                if($(this).hasClass("email")){
                        if(!validate_email($(this).val())){
                                $(this).animate({'background-color': '#ff9292'},500);
                                validate = false;
                        }else{
                                $(this).animate({'background-color': '#9aff92'},500);
                        }
                }
                //Validate Number
                if($(this).hasClass("number")){
                        if(isNaN($(this).val())){
                                $(this).animate({'background-color': '#ff9292'},500);
                                validate = false;
                        }else{
                                $(this).animate({'background-color': '#9aff92'},500);
                        }
                }

        });
        if(validate){
            $.post("http://www.holidayinverness.com/ajax/",$("#booking-form").serialize(), function(data){
                if(data.status){
                    $(".booking-form-inner .status").html(data.msg);
                    $(".booking-form-inner .status").fadeIn(800);
                    var to = setTimeout(function(){
                        $('.booking-form-arrow').css({backgroundPosition: '-206px -30px'});
                        $(".booking-bubble").fadeTo(800,1);
                        $('.booking-module').animate({
                            right :  '-450px'
                            },1200);
                        form_state = false;
                    },3000);
                }else{
                    alert(data.msg);
                }
            }, 'json');
            return false;
        }else{
            return false;
        }
    });

    $(".nav ul li a").hoverIntent(
        function () {
            change_left_right_bp($(this).parent("li"), 'hover');
            $(this).parent("li").find(".n-left").show();
            $(this).parent("li").find(".n-right").show();
            $(this).addClass("hover");
        },
        function () {

            if(!$(this).hasClass('selected')){
                $(this).parent("li").find(".n-left").hide();
                $(this).parent("li").find(".n-right").hide();
            }
            change_left_right_bp($(this).parent("li"), 'default');
            $(this).removeClass("hover");
            $(this).removeClass("mousedown");
        }
        );
    $('.nav ul li a').mousedown(function() {
        change_left_right_bp($(this).parent("li"), 'mousedown');
        $(this).parent("li").find(".n-left").show();
        $(this).parent("li").find(".n-right").show();
        $(this).addClass("mousedown");
    });
    var form_state = false;
    $('.booking-form-arrow, #bf').click(function(){
        if(form_state){
            $(this).css({backgroundPosition: '-206px -30px'});
            $(".booking-bubble").fadeTo(800,1);
            $('.booking-module').animate({
                right :  '-450px'
                },1200);
            form_state = false;
        }else{
            $(this).css({backgroundPosition: '-223px -30px'});
            $(".booking-bubble").fadeTo(800,0);
            $('.booking-module').animate({
                right : '0px'
                },1200);
            form_state = true;
        }
    });
});

function change_left_right_bp(obj, state){
    if(state=='hover'){
        obj.find(".n-left").css({
            backgroundPosition: '-206px 0'
        });
        obj.find(".n-right").css({
            backgroundPosition: '-213px 0'
        });
    }else if(state=='mousedown'){
        obj.find(".n-left").css({
            backgroundPosition: '-223px 0'
        });
        obj.find(".n-right").css({
            backgroundPosition: '-230px 0'
        });
    }else if(state=='default'){
        obj.find(".n-left").css({
            backgroundPosition: '-238px 0'
        });
        obj.find(".n-right").css({
            backgroundPosition: '-247px 0'
        });
    }
}
function validate_email(email){
        var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
        if(reg.test(email) == false) {
        return false;
        }
        return true;
}
