﻿$(function() {
    if (!!$('.rovster-form').length) {
        $('.rovster-form').jqTransform();
    }
});
// homepage tabbed search box configuration
$(function() {
    $('#search-box nav li').click(function(event) {
        event.preventDefault();
        // get selected tab index
        var index = $(this).index();
        // hide all visible search boxes
        $(this).parents('nav').nextAll('div:visible').hide();
        // de-select tabs
        $(this).siblings().removeClass('selected');
        // show relevant search box and select tab
        $(this).addClass('selected');
        $('#search-box > div:eq(' + index + ')').effect('slide', function() {
            // ensure multiple boxes do not appear if click occurs before last slide completes
            $(this).parent().children('div').each(function() {
                if (index + 1 != $(this).index()) {
                    $(this).hide();
                }
            })
        });
        // change background color based on selected tab
        var tabName = $(this).attr('class').replace(/ ?selected ?/, '');
        $('#search-box').removeClass();
        $('#search-box').addClass(tabName);
    });
});

$(function() {
    $.fn.preload = function() {
        var local = /localhost/.test(window.location.host);
        this.each(function() {
            $('<img/>')[0].src = '//' + window.location.host + (local ? '/rovsterV3' : '') + '/images/' + this;
        });
    }
    $(['v4PropertyTabOn.png', 'v4PropertyTabOff.png',
       'v4BusinessTabOn.png', 'v4BusinessTabOff.png',
       'v4EstateAgentTabOn.png', 'v4EstateAgentTabOff.png',
       'v4ShopTabOn.png', 'v4ShopTabOff.png']).preload();
    $('#main-nav > ul > li').click(function(event) {
        event.preventDefault();
        // get selected tab index
        var index = $(this).index();
        // hide all visible search boxes
        $(this).parents('nav').nextAll('div:visible').hide();
        // de-select tabs
        $(this).siblings().removeClass('selected');
        // show relevant search box and select tab
        $(this).addClass('selected');
        $('#tabBox > div:eq(' + index + ')').show();
        // change background color based on selected tab
        var tabName = $(this).attr('class').replace(/ ?selected ?/, '');
        $('#tabBox').removeClass();
        $('#tabBox').addClass(tabName);
        $(this).parent().find('li img').each(function() {
            $(this).attr('src', $(this).attr('src').replace(/On\.png/, 'Off.png'));
        });
        var $img = $(this).find('img');
        $img.attr('src', $img.attr('src').replace(/Off\.png/, 'On.png'));
    });

    $("#main-nav li.selected").each(function() {

    //alert($(this).index());

    var index = $(this).index();
    // hide all visible search boxes
    $(this).parents('nav').nextAll('div:visible').hide();
    // de-select tabs
    $(this).siblings().removeClass('selected');
    // show relevant search box and select tab
    $(this).addClass('selected');
    $('#tabBox > div:eq(' + index + ')').show();
    // change background color based on selected tab
    var tabName = $(this).attr('class').replace(/ ?selected ?/, '');
    $('#tabBox').removeClass();
    $('#tabBox').addClass(tabName);
    $(this).parent().find('li img').each(function() {
        $(this).attr('src', $(this).attr('src').replace(/On\.png/, 'Off.png'));
    });
    var $img = $(this).find('img');
    $img.attr('src', $img.attr('src').replace(/Off\.png/, 'On.png'));
    
    });
});

// configure menu links when on homepage
$(function() {
    if (!!$('#search-box').length) {
        $('#main-nav li.property > a, #main-nav li.property nav li:nth-child(-n+3) a').click(function(event) {
            event.preventDefault();
            $('#search-box nav li').removeClass('selected');
            $('#search-box > div').hide();
            $('#search-box nav li.property').addClass('selected');
            $('#property-search').effect('slide');
            $('#search-box').removeClass();
            $('#search-box').addClass('property');
            // check relevant property type checkbox dependent on index of submenu clicked
            var childIndex = $(this).parent().index();
            $('#property-search input[type=radio]:eq(' + childIndex + ')').attr('checked', 'checked');
            $('#property-search a.jqTransformRadio').removeClass('jqTransformChecked');
            $('#property-search input[type=radio]:eq(' + childIndex + ')').siblings('a').addClass('jqTransformChecked');
        });
        $('#main-nav li.business > a, #main-nav li.business ul li:first-child a').click(function(event) {
            event.preventDefault();
            $('#search-box nav li').removeClass('selected');
            $('#search-box > div').hide();
            $('#search-box nav li.business').addClass('selected');
            $('#business-search').effect('slide');
            $('#search-box').removeClass();
            $('#search-box').addClass('business');
        });
        $('#main-nav li.agent > a, #main-nav li.agent ul li:first-child a, a#agent-button').click(function(event) {
            event.preventDefault();
            $('#search-box nav li').removeClass('selected');
            $('#search-box > div').hide();
            $('#search-box nav li.agent').addClass('selected');
            $('#agent-search').effect('slide');
            $('#search-box').removeClass();
            $('#search-box').addClass('agent');
        });
    }
});
// configure homepage search box appearance on hash link
$(function() {
    if (!!$('#tabBox').length) {
        switch (window.location.hash) {
            case '#property':
                //$('#search-box nav li.property').addClass('selected');
                //$('#property-search').show();
                //$('#search-box').addClass('property');
                break;
            case '#business':
                //$('li#business > A > IMG').addClass('selected');
                $('li#businessTab > A > IMG').attr("src", "http://www.rovster.com/images/v4BusinessTabOn.png");
                $('li#propertyTab > A > IMG').attr("src", "http://www.rovster.com/images/v4PropertyTabOff.png");
                $('#business-panel').show();
                $('#property-panel').hide();
                //$('#search-box').addClass('business');
                //window.scrollTo(0, 0);


                break;
            case '#agent':
                $('li#agentTab > A > IMG').attr("src", "http://www.rovster.com/images/v4EstateAgentTabOn.png");
                $('li#propertyTab > A > IMG').attr("src", "http://www.rovster.com/images/v4PropertyTabOff.png");
                $('#agent-panel').show();
                $('#property-panel').hide();
                //window.scrollTo(0, 0);
                break;

            case '#shop':
                $('li#shopTab > A > IMG').attr("src", "http://www.rovster.com/images/v4ShopTabOn.png");
                $('li#propertyTab > A > IMG').attr("src", "http://www.rovster.com/images/v4PropertyTabOff.png");
                $('#shop-panel').show();
                $('#property-panel').hide();
                //window.scrollTo(0, 0);
                break;
        }
    }
});
$(function() {
    if (!!$('div#partners a[href*=mortgage]').length) {
        $('div#partners a[href*=mortgage]').fancybox({
            'type': 'iframe',
            'titleShow': false,
            'padding': 0,
            'width': 187,
            'height': 150
        });
    }
    if (!!$('div#partners a[href*=currency]').length) {
        $('div#partners a[href*=currency]').fancybox({
            'type': 'iframe',
            'titleShow': false,
            'padding': 0,
            'width': 200,
            'height': 260
        });
    }
});
function googleTranslateElementInit() {
    new google.translate.TranslateElement({
        pageLanguage: 'en',
        gaTrack: true,
        layout: google.translate.TranslateElement.InlineLayout.SIMPLE
    }, 'google_translate_element');
}
$(function() {
    if (!!$('#news-bar-scroll').length && !!$('#news-bar-scroll div').length) {
        $('#news-bar-scroll').scrollable({ circular: true, speed: 7000, easing: 'linear' }).autoscroll();
    }
});
$(function() {
    //hover and link for new properties section of hompage
    $('#latest article').click(function() {
        window.location = ($(this).find('strong a').attr('href'));
    })
});
$(function() {
    //select all the a tag with name equal to modal
    $('a[name=modal]').click(function(e) {
        //Cancel the link behavior
        e.preventDefault();
        //Get the A tag
        var id = $(this).attr('href');
        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();
        //Set heigth and width to mask to fill up the whole screen
        //$('#mask').css({'width':maskWidth,'height':maskHeight});
        //transition effect        
        $('#mask').fadeIn(1000);
        $('#mask').fadeTo("slow", 0.8);
        //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).fadeIn(0);
    });
    //if close button is clicked
    $('.window .close').click(function(e) {
        //Cancel the link behavior
        e.preventDefault();
        $('#mask').hide();
        $('.window').hide();
    });
    //if mask is clicked
    $('#mask').click(function() {
        $(this).hide();
        $('.window').hide();
    });
});
/*
$(function() {    
    if (!!$('.myrovster-tabs').length) {
        var tabContainer = $get('ctl00_cpMain_TabContainer1');
        if (typeof tabContainer != 'undefined' && typeof tabContainer.control != 'undefined') {
            tabContainer = tabContainer.control
            switch (window.location.hash) {
                case '#sale':
                    tabContainer.set_activeTabIndex(1);
                    break;
                case '#rent':
                    tabContainer.set_activeTabIndex(2);
                    break;
                case '#share':
                    tabContainer.set_activeTabIndex(3);
                    break;
                case '#business':
                    tabContainer.set_activeTabIndex(4);
                    break;
            }
        }
    }
});
*/
