/**
 * Init layout interface
 */

// if console is not available -> mock it to prevent errors
if(!window.console) {
    window.console = {
        log: function () { }, 
        debug: function () { },
        warn: function () { },
        error: function () { },
        dir: function () { },
        trace: function() { }
    };
}

$(function(){
    // Init language button (list)
    $('.lang a').click(function(){
        if ($('.lang').hasClass('lang-list-hide')) {
            $('.lang a').show();
            $('.lang').removeClass('lang-list-hide');
        } else {
            return true;
        }

        return false;
    });

    lang = $('#current_lang_label');
    lang.html($("#lang_" + lang.find('img').attr('alt')).text() + lang.html());

    // Init keyword searching
    keyword = $('#top-search input[name="q"]');
    keyword.focus(function(){
        if (Lang._('Search') == $(this).val()) {
            $(this).val('');
        }
    });

    keyword.blur(function(){
        if ('' == $(this).val()) {
            $(this).val(Lang._('Search'));
        }
    });

    $('#top-search form').submit(function(){
        if ('Search' == keyword.val()) {
            return false;
        }

        return true;
    });

    // Init select boxes
    $(".replaced").selectbox();    

    // Workaround problems with detecting mousedown && drag-drop action in IE 7/8
    $(document).mousedown(function(event){
        if($(event.target).hasClass('selectbox-container')) {
            if(!$(event.target).hasClass('non-blurable')) {
                $(event.target).addClass('non-blurable');
            }
            
            return false;
        }
    })
    
    $(document).mouseout(function(event){
        $('.selectbox-container').removeClass('non-blurable');
    });
    
    $('input[type=checkbox],input[type=radio]').prettyCheckboxes();

    // Init fade checking buttons
    FadeCheck.init();

    // Init scrolled panel on page for property view
    $('.scroll-pane').jScrollPane({
        bottomCapHeight: '4'
    });

    // Tooltips
    Tooltips.init();

    // Load widgets
    Widgets.loadAll();

    $('input').placeholder();
    
    // Work around IE7 bug with processing z-index of absolute divs
    if (Browser.isIE()) {
        var zIndexNumber = 10000;
        $('div').each(function() {
            $(this).css('zIndex', zIndexNumber);
            zIndexNumber -= 10;
        });

        $('.selectbox-container').css('zIndex', 12000);
        $('#locator_map').css('zIndex', 14000);
        $('#submenu-container').css('zIndex', 10000);
        $('.autocomplete').css('zIndex', 12000);
        $('.fade-check, .fade-check ul, .fade-check li').css('zIndex', 12500);
        $('.tooltip-content').css('zIndex', 13000);
    }
});

var Interface = {};

Interface.openup = function(url, windowName) {
	win=window.open(url, windowName, "height=600,width=750,top=200,left=400");
	if (window.focus) {
	    win.focus();
	}
	
	return false;
}

