$(function(){
    $('.language-navigation').hover(function(){
        $('.language-navigation .countries').show();
    },
    function(){
        $('.language-navigation .countries').hide();
    });

    $('.js').css('visibility', 'visible');
    $('.noscript').remove();
    
    //show/hide blocks
    $('.block').not(':first').hide();
    changeBlock();
    $(window).hashchange(changeBlock);
    
    //more options
    $('.more-content').not('.more-open').hide();

    $('.more-link').click(function(){
        $link = $(this);
        if($link.hasClass('more-open')) {
            $('#' + $link.attr('rel')).slideUp(function(){
                $link.removeClass('more-open');
            });                     
        } else {
            $('#' + $link.attr('rel')).slideDown(function(){
                $link.addClass('more-open');
            });   
        }
        return false;
    });

    $('a').each(function() {
       var a = new RegExp('/' + window.location.host + '/');
       if(!a.test(this.href) && this.href.indexOf('addthis.com') < 0 && this.href.indexOf('simplychateau') < 0) {
           $(this).click(function(event) {
               event.preventDefault();
               event.stopPropagation();
               window.open(this.href, '_blank');
           });
       }
    });
   
   //date picker
   $('#arrival-date').datepicker({
        dateFormat : 'D d MM yy'
   });
   $('#date-icon').click(function(){
    $('#arrival-date').focus();
    return false;
   });
   
   //keywords
   $('.field.inactive').focus(clearField = function(){
        $(this).removeClass('inactive').attr('value', '').unbind('focus', clearField);
        return false;
   });
   
    //submit form on select change
    $('select.change').change(function(){
        $(this).closest('form').submit();
    });
    
    $('.sortresults').change(function(){
        var baseurl = $(this).closest("form").attr('action');
        var segment = $(this).attr('name');
        var value = $(this).attr('value');

        window.location.href = baseurl + '/' + segment + '/' + value;
        return false;
    });
    

      $('#contact-owner-form').validate({
        rules: {
            email: {
                required: true,
                email: true
            },
            message: "required",
            first_name: "required",
            last_name: "required"
        },
        submitHandler: function(form){
            $('#contact-owner-form button').text('Sending...')
                                           .unbind('click')
                                           .attr('disabled', 'disabled');
            
            $('#contact-owner-form').attr('disabled', 'disabled').unbind('submit');
            
            $.post($(form).attr('action'), $(form).serialize(), function(data){
                if(data) {
                    $('#contact-owner-form button').text(data);
                    //$('#contact-owner-form button').text('Thank you');                    
                } else {
                    $('#contact-owner-form button').text('Error');
                }
                setTimeout(function(){
                    $('#contact-owner').click();
                }, 2000);                
            });        
        },
        errorPlacement: function(){ return true; }
      });
      
      $('#feedback-form').validate({
        rules: {
            email: {
                required: true,
                email: true
            },
            message: "required",
            first_name: "required",
            last_name: "required"
        },
        submitHandler: function(form){
            $('#feedback-form button').text('Sending...')
                                           .unbind('click')
                                           .attr('disabled', 'disabled');
            
            $('#feedback-form').attr('disabled', 'disabled').unbind('submit');
            
            $.post($(form).attr('action'), $(form).serialize(), function(data){
                if(data) {
                    $('#feedback-form button').text(data);
                } else {
                    $('#feedback-form button').text('Error');
                }
                setTimeout(function(){
                    $('#submit-feedback').click();
                }, 2000);
            });        
        },
        errorPlacement: function(){ return true; }
      });      
      
      var loader = new Image();
      loader.src = "/fileadmin/templates/images/ajax-loader-small.gif";
      
      $('#newsletter-form').validate({
        rules: {
            email: {
                required: true,
                email: true
            }
        },
        errorPlacement: function(){ return true; },
        submitHandler: function(form) {
            
            $('#newsletter-form button')
            .unbind('click')
            .attr('disabled', 'disabled')
            //.fadeOut()
            .replaceWith('<img src="/fileadmin/templates/images/ajax-loader-small.gif" style="float:right; margin: 2px 20px 0 0" />');
            
            $('#newsletter-form').attr('disabled', 'disabled').unbind('submit');
            $.post($(form).attr('action'), $(form).serialize(), function(data) {
            
                $('#newsletter-form').slideUp();
                $('<p></p>')
                .attr('id', 'newsletter-status')
                .css({ 'margin-top' : '10px', 'margin-left' : '2px', 'font-size' : '1.2em' })
                .appendTo($('#newsletter-form').parent())
                .text(data);
                
            })
        }
      });
      
});

function changeBlock()
{
    if(location.hash.replace('#', '') != '') {
        if($(location.hash).length > 0) {
            $('.block-link.on').removeClass('on');
            $('.block').hide();
            $('.block-link[rel=' + location.hash.replace('#', '') + ']').addClass('on');
            $(location.hash).show();
            if(typeof initMap == 'function') {
                initMap();
            }
            $.scrollTo(location.hash, 300);
        }
    }
    return false;
}


/*
 * jQuery hashchange event - v1.3 - 7/21/2010
 * http://benalman.com/projects/jquery-hashchange-plugin/
 * 
 * Copyright (c) 2010 "Cowboy" Ben Alman
 * Dual licensed under the MIT and GPL licenses.
 * http://benalman.com/about/license/
 */
(function($,e,b){var c="hashchange",h=document,f,g=$.event.special,i=h.documentMode,d="on"+c in e&&(i===b||i>7);function a(j){j=j||location.href;return"#"+j.replace(/^[^#]*#?(.*)$/,"$1")}$.fn[c]=function(j){return j?this.bind(c,j):this.trigger(c)};$.fn[c].delay=50;g[c]=$.extend(g[c],{setup:function(){if(d){return false}$(f.start)},teardown:function(){if(d){return false}$(f.stop)}});f=(function(){var j={},p,m=a(),k=function(q){return q},l=k,o=k;j.start=function(){p||n()};j.stop=function(){p&&clearTimeout(p);p=b};function n(){var r=a(),q=o(m);if(r!==m){l(m=r,q);$(e).trigger(c)}else{if(q!==m){location.href=location.href.replace(/#.*/,"")+q}}p=setTimeout(n,$.fn[c].delay)}$.browser.msie&&!d&&(function(){var q,r;j.start=function(){if(!q){r=$.fn[c].src;r=r&&r+a();q=$('<iframe tabindex="-1" title="empty"/>').hide().one("load",function(){r||l(a());n()}).attr("src",r||"javascript:0").insertAfter("body")[0].contentWindow;h.onpropertychange=function(){try{if(event.propertyName==="title"){q.document.title=h.title}}catch(s){}}}};j.stop=k;o=function(){return a(q.location.href)};l=function(v,s){var u=q.document,t=$.fn[c].domain;if(v!==s){u.title=h.title;u.open();t&&u.write('<script>document.domain="'+t+'"<\/script>');u.close();q.location.hash=v}}})();return j})()})(jQuery,this);
/**
 * jQuery.ScrollTo - Easy element scrolling using jQuery.
 * Copyright (c) 2007-2009 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 5/25/2009
 * @author Ariel Flesler
 * @version 1.4.2
 *
 * http://flesler.blogspot.com/2007/10/jqueryscrollto.html
 */
;(function(d){var k=d.scrollTo=function(a,i,e){d(window).scrollTo(a,i,e)};k.defaults={axis:'xy',duration:parseFloat(d.fn.jquery)>=1.3?0:1};k.window=function(a){return d(window)._scrollable()};d.fn._scrollable=function(){return this.map(function(){var a=this,i=!a.nodeName||d.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!i)return a;var e=(a.contentWindow||a).document||a.ownerDocument||a;return d.browser.safari||e.compatMode=='BackCompat'?e.body:e.documentElement})};d.fn.scrollTo=function(n,j,b){if(typeof j=='object'){b=j;j=0}if(typeof b=='function')b={onAfter:b};if(n=='max')n=9e9;b=d.extend({},k.defaults,b);j=j||b.speed||b.duration;b.queue=b.queue&&b.axis.length>1;if(b.queue)j/=2;b.offset=p(b.offset);b.over=p(b.over);return this._scrollable().each(function(){var q=this,r=d(q),f=n,s,g={},u=r.is('html,body');switch(typeof f){case'number':case'string':if(/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(f)){f=p(f);break}f=d(f,this);case'object':if(f.is||f.style)s=(f=d(f)).offset()}d.each(b.axis.split(''),function(a,i){var e=i=='x'?'Left':'Top',h=e.toLowerCase(),c='scroll'+e,l=q[c],m=k.max(q,i);if(s){g[c]=s[h]+(u?0:l-r.offset()[h]);if(b.margin){g[c]-=parseInt(f.css('margin'+e))||0;g[c]-=parseInt(f.css('border'+e+'Width'))||0}g[c]+=b.offset[h]||0;if(b.over[h])g[c]+=f[i=='x'?'width':'height']()*b.over[h]}else{var o=f[h];g[c]=o.slice&&o.slice(-1)=='%'?parseFloat(o)/100*m:o}if(/^\d+$/.test(g[c]))g[c]=g[c]<=0?0:Math.min(g[c],m);if(!a&&b.queue){if(l!=g[c])t(b.onAfterFirst);delete g[c]}});t(b.onAfter);function t(a){r.animate(g,j,b.easing,a&&function(){a.call(this,n,b)})}}).end()};k.max=function(a,i){var e=i=='x'?'Width':'Height',h='scroll'+e;if(!d(a).is('html,body'))return a[h]-d(a)[e.toLowerCase()]();var c='client'+e,l=a.ownerDocument.documentElement,m=a.ownerDocument.body;return Math.max(l[h],m[h])-Math.min(l[c],m[c])};function p(a){return typeof a=='object'?a:{top:a,left:a}}})(jQuery);
