﻿$(document).ready(function () {

    //
    //  PRELOAD MENU IMAGES
    //

    jQuery.preLoadImages("/_sites/devine/_media/images/navigation_home_designs_bg.png",
                         "/_sites/devine/_media/images/navigation_communities_bg.png",
                         "/_sites/devine/_media/images/navigation_column_short_flipped_bg.png",
                         "/_sites/devine/_media/images/navigation_column_bg.png");
    //
    //  SITE SEARCH
    //

    $('.search_box').keyup(function () {
        $(this).addClass('search_box_loading');

        if ($(this).val().length > 3) {
            $('.search_results').load('/search.aspx ul', { query: $(this).val() }, function () {
                $('.search_results_header,.search_results').show();
                $('.search_box').removeClass('search_box_loading');
            });
        } else {
            $('.search_results_header,.search_results').hide();
            $('.search_results').html('');
            $(this).removeClass('search_box_loading');
        }

    });

    //  hover on biglink
    $('.triangle_go').hover(function () { $(this).attr('src', '/_sites/devine/_media/images/search_go_hover.png'); },
                           function () { $(this).attr('src', '/_sites/devine/_media/images/search_go.png'); });

    //  search go
    $('.search_go').click(function (e) { e.preventDefault(); $('.search_box').trigger('keyup'); return false })

    //
    //  E-NEWS SIGNUP
    //

    //  clears e-news text
    $('.signup_suburb').focus(function () {
        if ($(this).val() == 'Enter Your Suburb') {
            $(this).val('');
            $(this).addClass('signup_box_active');
        }
        $(this).addClass('signup_box_focus');
    }).blur(function () {
        if ($(this).val() == '') {
            $(this).val('Enter Your Suburb');
            $(this).removeClass('signup_box_active');
        }
        $(this).removeClass('signup_box_focus');
    });

    $('.signup_name').focus(function () {
        if ($(this).val() == 'Enter Your Name') {
            $(this).val('');
            $(this).addClass('signup_box_active');
        }
        $(this).addClass('signup_box_focus');
    }).blur(function () {
        if ($(this).val() == '') {
            $(this).val('Enter Your Name');
            $(this).removeClass('signup_box_active');
        }
        $(this).removeClass('signup_box_focus');
    });

    $('.signup_email').focus(function () {
        if ($(this).val() == 'Enter Your Email Address') {
            $(this).val('');
            $(this).addClass('signup_box_active');
        }
        $(this).addClass('signup_box_focus');
    }).blur(function () {
        if ($(this).val() == '') {
            $(this).val('Enter Your Email Address');
            $(this).removeClass('signup_box_active');
        }
        $(this).removeClass('signup_box_focus');
    });

    //
    //  HOMEPAGE PANELS
    //

    var designs_open = false,
        communities_open = false;

    $('.open_home_designs').click(function () {

        if (!designs_open && !communities_open) {

            //  nothings open
            $('.homepage_search_container').animate({ left: 0 }, 500);
            $('.homepage_communities_container').animate({ left: 22 }, 500);
            $('.banner_text').animate({ left: 332 }, 500);
            designs_open = true;

        } else if (designs_open && !communities_open) {

            //  just designs is open
            $('.homepage_search_container').animate({ left: -212 }, 500);
            $('.homepage_communities_container').animate({ left: -190 }, 500);
            $('.banner_text').animate({ left: 120 }, 500);
            designs_open = false;

        } else if (!designs_open && communities_open) {

            //  communities open, need designs open
            $('.homepage_search_container').animate({ left: 0 }, 500);
            designs_open = true;
            communities_open = false;

        }

    }).hover(function () { $('.homepage_search_container').addClass('homepage_search_container_hover'); },
             function () { $('.homepage_search_container').removeClass('homepage_search_container_hover'); });

    $('.open_communities').click(function () {

        if (!communities_open && !designs_open) {

            //  nothings open
            $('.homepage_communities_container').animate({ left: 22 }, 500);
            $('.banner_text').animate({ left: 332 }, 500);
            communities_open = true;

        } else if (communities_open && !designs_open) {

            //  just communities is open
            $('.homepage_communities_container').animate({ left: -190 }, 500);
            $('.banner_text').animate({ left: 120 }, 500);
            communities_open = false;

        } else if (!communities_open && designs_open) {

            //  designs open, need communities open
            $('.homepage_search_container').animate({ left: -212 }, 500);
            $('.homepage_communities_container').css({ left: 22 }, 500);
            designs_open = false;
            communities_open = true;

        }

    }).hover(function () { $('.homepage_communities_container').addClass('homepage_communities_container_hover'); },
             function () { $('.homepage_communities_container').removeClass('homepage_communities_container_hover'); });

    //
    //  HOVER CLASS
    //

    $('.hover').hover(function () { $(this).addClass('hover_active') },
                      function () { $(this).removeClass('hover_active') });

    //  hover on biglink
    $('.view_home_designs').hover(function () { $(this).find('img').attr('src', '/_sites/devine/_media/images/navigation_view_home_designs_hover.png'); },
                                  function () { $(this).find('img').attr('src', '/_sites/devine/_media/images/navigation_view_home_designs.png'); });

    //
    //  MAP HOVER
    //



    $('a[community_id]').hover(
        function () {
            if (!$(this).hasClass('map_active')) {
                var community_id = $(this).attr('community_id');
                var image_src = ($.browser.msie && $.browser.version == "6.0") ? 'ie/map_dot_active.gif' : 'map_dot_active.png';
                $(this).parent().find('a[community_id=' + community_id + '] img').attr('src', '/_sites/devine/_media/images/' + image_src);
            }
        },
        function () {
            if (!$(this).hasClass('map_active')) {
                var community_id = $(this).attr('community_id');
                var image_src = ($.browser.msie && $.browser.version == "6.0") ? 'ie/map_dot.gif' : 'map_dot.png';
                $(this).parent().find('a[community_id=' + community_id + '] img').attr('src', '/_sites/devine/_media/images/' + image_src);
            }
        }
   );

    //
    //  PROPERTY DETAILS
    //

    //  bind detail click
    $('.details_hook').click(function () {
        if (!$(this).parent().hasClass('result_disabled')) {
            if (!$.browser.msie)
                $(this).parent().find('.facade,.details_hook').fadeOut('slow');
            else
                $(this).parent().find('.facade,.details_hook').hide();
            $(this).parent().find('.details').show();
            $(this).parent().find('.virtual_tours_hook_white').hide();
        }
    });

    //  bind detail click
    $('.details_close_hook').click(function () {
        if (!$(this).parent().parent().hasClass('result_disabled')) {
            if (!$.browser.msie)
                $(this).parent().parent().find('.facade,.details_hook').fadeIn('slow');
            else
                $(this).parent().parent().find('.facade,.details_hook').show();
            $(this).parent().parent().find('.details').hide();
            $(this).parent().parent().find('.virtual_tours_hook_white').show();
        }
    });

    //
    //  STATE SELECTION
    //

    var open_geolocator = function (e) {

        //  prevent default click behaviour
        e.preventDefault();
        var id = '.geolocator';

        //  resize overlay
        $('.modal_overlay').css({ 'width': $(window).width(), 'height': $(document).height(), opacity: 0.5 }).show();

        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        //Set the popup window to center
        $(id).css('top', winH / 2 - $(id).height() / 2);
        $(id).css('left', winW / 2 - $(id).width() / 2);

        //transition effect
        $(id).appendTo('body').show();

    };

    //  select all the a tag with name equal to modal
    $('a.whats_available').click(open_geolocator);

    //  hover state for states
    $('.geolocator_links area')
            .click(
                function (e) {
                    //e.preventDefault();
                    //$('.new_state').val($(this).attr('href'));
                    //$('.change_state').trigger('click');
                }
            )
            .hover(
                function () {
                    var css_class = $(this).attr('class');
                    $('.geolocator_map .' + css_class).show();
                },
                function () {
                    var css_class = $(this).attr('class');
                    $('.geolocator_map .' + css_class).hide();
                }
            );

    //  open geolocator on first visit
    if ($('.geolocator .first_visit').length) {
        $('.geolocator_close').hide();
        $('.geolocation a.whats_available').trigger('click');
        $(window).resize(open_geolocator);
    }

    //if close button is clicked
    $('.geolocator_close').click(function (e) {
        e.preventDefault();
        $('.modal_overlay, .geolocator').hide();
    });

    //if mask is clicked
    //$('.modal_overlay').click(function() {
    //    $(this).hide();
    //    $('.geolocator').hide();
    //});

    //
    //  DEBUG
    //

    /*
    $('div,img').hover(
    function() {
    if ($(this).attr('class') != '') {
    $('<div>').html($(this).attr('class')).addClass('div_detail').appendTo('.div_info');
    }
    },
    function() {
    $('.div_detail').remove();
    });
    */

});

//  custom e-news validator
function alreadySubscribed(source, arguments) {
    if ($('.signup_box').val().length > 0) {
        var exists;
        $.ajax({
            type: "POST",
            url: "/default.aspx",
            data: { email: $('.signup_box').val() },
            async: false,
            success: function(result) {
                exists = (result == 'EXISTS');
            }
        });
        arguments.IsValid = !exists;
    }
}

//  preload image script
(function ($) {
    var cache = [];
    // Arguments are image paths relative to the current page.
    $.preLoadImages = function () {
        var args_len = arguments.length;
        for (var i = args_len; i--; ) {
            var cacheImage = document.createElement('img');
            cacheImage.src = arguments[i];
            cache.push(cacheImage);
        }
    }
})(jQuery)

