try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}

$(function() {
    
    /* 1st level menu highlighting. */
    $('#mainMenu > ul > li:not(.on)').hover(
        function() {
            $('#mainMenu > ul > li').each(function(i) {
                if ($(this).hasClass('on')) {
                    $(this).data('on', 'on');
                }
                $(this).removeClass('on');
            });
            $(this).addClass('on');
        },
        function() {
            $('#mainMenu > ul > li').each(function(i) {
                var data = $(this).data('on');
                if (data) {
                    $(this).addClass(data);
                    $(this).removeData('on');
                }
            });
            $(this).removeClass('on');
        }
    );
    
    /* Show photos in fancybox. */
    $("#thumbnails a").fancybox({ 
        'overlayShow'    : true,
        'overlayOpacity' : 0.7
    });

    /* Show and hide hardcoded tooltips. */
    $("#builder_map > area").hoverIntent(
        function() {
            var id = '#' + $(this).attr('id').replace(/_area$/i, '');
            $(id).show();
        },
        function() {
            var id = '#' + $(this).attr('id').replace(/_area$/i, '');
            $(id).hide();
        }
    );
    
    /* Bounce arrows on homeowner section. */
    $('div.arrow.right').effect("bounce", { direction: 'left',  times: 2 }, 300);
    $('div.arrow.left').effect("bounce",  { direction: 'right', times: 2 }, 300);
    
    /* Offer form validation. */      
    $('#offerForm').validate({
        onsubmit: true,
        onblur: true,
        /* Complicated rules here. Others in classes. */
        rules: {
            /*
            fFreeSpaceSpecial: {
                required: {
                    depends: function(element) {
                        return "ule_350mm" == $("#fFreeSpace").val();
                    }
                }
            },
            */
            fColourSpecial: {
                required: {
                    depends: function(element) {
                        return "erivarv" == $("#fColour").val();
                    }
                }
            },
            fMaterialSpecial: {
                required: {
                    depends: function(element) {
                        return "muu" == $("#fMaterial").val();
                    }
                }
            },
            fObjectSpecial: {
                required: {
                    depends: function(element) {
                        return "muu" == $("#fObject").val();
                    }
                }
            },
            fAutomaticsSpecial: {
                required: {
                    depends: function(element) {
                        return "muu" == $("#fAutomatics").val();
                    }
                }
            },
            fCompany: {
                minlength: 2
            }
        },
        messages: {
            fName: '<img src="/img/icon/cross.png" />',
            fCompany: '<img src="/img/icon/cross.png" />',
            fLocation: '<img src="/img/icon/cross.png" />',
            fPhone: '<img src="/img/icon/cross.png" />',
            fEmail: '<img src="/img/icon/cross.png" />',
            fWidthHeight: '<img src="/img/icon/cross.png" />',
            fFreeSpace: '<img src="/img/icon/cross.png" />',
            fFreeSpaceSpecial: '<img src="/img/icon/cross.png" />',
            fColourSpecial: '<img src="/img/icon/cross.png" />',
            fObject: '<img src="/img/icon/cross.png" />',
            fPostHeight: '<img src="/img/icon/cross.png" />',
            fMaterial: '<img src="/img/icon/cross.png" />',
            fOpenDirection: '<img src="/img/icon/cross.png" />',
            fMaterialSpecial: '<img src="/img/icon/cross.png" />',
            fObjectSpecial: '<img src="/img/icon/cross.png" />',
            fGateWeight: '<img src="/img/icon/cross.png" />',
            fUsage: '<img src="/img/icon/cross.png" />',
            fBarrierLength: '<img src="/img/icon/cross.png" />',
            fAutomaticsSpecial: '<img src="/img/icon/cross.png" />'
        },
        success: function(label) {
          label.addClass("valid").html('<img src="/img/icon/accept.png" />');
        }
    });
    
    /*
    $('#fSendButton').css("opacity", 0.5);
    $('#offerForm input').bind('blur', function(){
       if ($('#offerForm').valid()) {
          $('#fSendButton').css("opacity", 1);
       };
    });
    */
    
    /* Offer form. */

    $('#fColourSpecialRow').hide();
    $('#fFreeSpaceRow').hide();
    $('#fVentilationCoverAmount').hide();
    $('#fWindowsAmount').hide();
    $('#fRemoteLabel').hide();
    $('#fRemoteAmount').hide();
    $('#fMaterialSpecialRow').hide();
    $('#fObjectSpecialRow').hide();
    $('#fAutomaticsSpecialRow').hide();
    
    
    $('#fColour').bind('change', function() {
        var selected = $(this).val();
        if ('erivarv' == selected) {
            $('#fColourSpecialRow').fadeIn('fast');
        } else {
            $('#fColourSpecialRow').fadeOut('fast');            
        }
    });
    
    $('#fFreeSpace').bind('change', function() {
        var selected = $(this).val();
        if ('ule_350mm' == selected) {
            $('#fFreeSpaceRow').fadeIn('fast');
        } else {
            $('#fFreeSpaceRow').fadeOut('fast');            
        }
    });
    
    $('#fVentilationCover').bind('change', function() {
        var selected = $(this).val();
        if ('ei_soovi' == selected) {
            $('#fVentilationCoverAmount').fadeOut('fast');
            $('#fVentilationCoverAmount').val("0");
        } else {
            $('#fVentilationCoverAmount').val("2");
            $('#fVentilationCoverAmount').fadeIn('fast');
        }
    });
    
    $('#fWindows').bind('change', function() {
        var selected = $(this).val();
        if ('ei_soovi' == selected) {
            $('#fWindowsAmount').fadeOut('fast');
            $('#fWindowsAmount').val("0");
        } else {
            $('#fWindowsAmount').val("2");
            $('#fWindowsAmount').fadeIn('fast');
        }
    });
    
    $('#fRemote').bind('change', function() {
        var selected = $(this).val();
        if ('ei' == selected) {
            $('#fRemoteAmount').fadeOut('fast');
            $('#fRemoteLabel').fadeOut('fast');
            $('#fRemoteAmount').val("0");
        } else {
            $('#fRemoteAmount').val("2");
            $('#fRemoteLabel').fadeIn('fast');
            $('#fRemoteAmount').fadeIn('fast');
        }
    }); 
    
    $('#fAutomatics').bind('change', function() {
        var selected = $(this).val();
        if ('puldiga' == selected || 'kaardiga' == selected) {
            $('#fAutomaticsSpecialRow').hide();
            $('#fRemoteAmount').val("2");
            $('#fRemoteAmount').fadeIn('fast');
            $('#fRemoteLabel').fadeIn('fast');
        } else if ('muu' == selected) {
            $('#fRemoteAmount').hide();
            $('#fAutomaticsSpecialRow').fadeIn('fast');
            $('#fRemoteLabel').fadeOut('fast');
            $('#fRemoteAmount').val("0");
        } else {
            $('#fRemoteAmount').hide();
            $('#fAutomaticsSpecialRow').fadeOut('fast');
            $('#fRemoteLabel').fadeOut('fast');
            $('#fRemoteAmount').val("0");
        }
    });
    
    $('#fObject').bind('change', function() {
        var selected = $(this).val();
        if ('muu' == selected) {
            $('#fObjectSpecialRow').fadeIn('fast');
        } else {
            $('#fObjectSpecialRow').fadeOut('fast');            
        }
    });
    
    $('#fMaterial').bind('change', function() {
        var selected = $(this).val();
        if ('muu' == selected) {
            $('#fMaterialSpecialRow').fadeIn('fast');
        } else {
            $('#fMaterialSpecialRow').fadeOut('fast');            
        }
    });
    
});
