var RESIDENCE = {

    ie6: function() {
        //IE6
        if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {
            $('body').prepend('<div style="width:960px;padding:20px;margin:0 auto;background:#FEEFB3;color:#9F6000;font-size:120%;font-weight:bold;line-height:1.5;">You are using a very old browser (Internet Explorer 6) to be able to enjoy our web site to the max please update your browser <a href="http://www.beautyoftheweb.com/" title="The latest version of Internet Explorer">Here</a> or download one of the following browsers <a href="http://www.mozilla.com/en-US/firefox/new/" title="Firefox">Firefox</a> ,<a href="http://www.google.com/chrome" title="Google Chrome">Google Chrome</a>,<a href="http://www.opera.com/" title="Opera">Opera</a>,<a href="http://www.apple.com/safari/" title="Safari">Safari</a>.</div>');
        };
    },

    helper: function() {
        var b = document.documentElement;
        b.className = b.className.replace('no-js', 'js');
        b.setAttribute('data-useragent', navigator.userAgent);
        b.setAttribute('data-platform', navigator.platform);
    },
    newsletterValidation: function(){
        $('#subForm').submit(function (e) {
            e.preventDefault();
            $.getJSON(
            this.action + "?callback=?",
            $(this).serialize(),
            function (data) {
                if (data.Status === 400) {
                    alert("Error: " + data.Message);
                } else { // 200
                    alert("Success: " + data.Message);
                }
            });
        });
    },
    externalLinks: function(){
        var extLinks = $("a[href^=http]");
            extLinks.attr("target","_blank");
    },
    datePicker:function(){
        var dates = $('input.date');

        if (jQuery().datepicker){
            dates.datepicker({
                showButtonPanel: true,
                changeMonth: true,
                changeYear: true,
                dateFormat:"yy-mm-dd",
                showOn: "both",
                buttonImage: "img/calendar.png",
                buttonImageOnly: true
            });
        }
    },
    ratesTables:function(){
        var tables = $('[id^="period-"]'),
            links = $('h3 a[href^="#period-"]');

            tables.not(":first").hide();

            links.click(function(e){
                var href = $(this).attr("href");
                e.preventDefault();

               if(href){
                    tables.hide();
                   $('table#'+href).stop(true,true).fadeToggle();
               }else{
                   return;
               }
            });

    },
    validateForms:function(){
        var reserveForm = $('#reserve-form'),
            contactForm = $('#contact-form');

        if(jQuery().validate){
            reserveForm.validate({
           validClass: "success",
           errorElement: "span",
           rules:{
            name:{
                required:true,
                minlengnth: 5
            },
            email:{
                required:true,
                email:true
            },
            phone:{
                required:true,
                number:true
            },
            country:{
                required:true
            },
            arrival:{
                required:true
            },
            departure:{
                required:true
            },
            adults:{
                required:true,
                number:true
            }
           },
           messages:{
            name:{
                required:"Please enter your name.",
                minlength: jQuery.format("At least {0} characters required!")
            },
            email:{
                required:"Please enter your Email.",
                email:"Please enter a valid Email."
            },
            phone:{
                required:"Please enter your phone number."
            },
            country:{
                required:"Please select your country."
            },
            arrival:{
                required:"Please enter your arrival date."
            },
            departure:{
                required:"Please enter your departure date."
            },
            adults:{
                required:"Please specify the number of Adults."
            }
            }
           });


           contactForm.validate({
           validClass: "success",
           errorElement: "span",
           rules:{
            "full-name":{
                required:true,
                minlengnth: 5
            },
            "contact-email":{
                required:true,
                email:true
            },
            country:{
                required:true
            },
            comments:{
                required:true
            }
           },
           messages:{
            "full-name":{
                required:"Please enter your name.",
                minlength: jQuery.format("At least {0} characters required!")
            },
            "contact-email":{
                required:"Please enter your Email.",
                email:"Please enter a valid Email."
            },
            country:{
                required:"Please select your country."
            },
            comments:{
                required:"Please write your comment."
            }
            }
           });
        }
    },
    fakeVirtual:function(){
        var nav = $('#virtual [role="navigation"]');

        nav.after('<div class="fake"/>');
    }
};


jQuery(function($) {

    RESIDENCE.ie6();
    RESIDENCE.helper();
    RESIDENCE.newsletterValidation();
    RESIDENCE.externalLinks();
    RESIDENCE.datePicker();
    RESIDENCE.ratesTables();
    RESIDENCE.validateForms();
    RESIDENCE.fakeVirtual();

});
