var can_load_new_image = true;
var pictureId = ''; // Глобальная переменная содержащая id текущего слайда в галлереи

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }	

/*
    if (window['console'] === undefined) {
        window.console = { log: Prototype.emptyFunction, info: Prototype.emptyFunction, debug: Prototype.emptyFunction };
    }
*/


//plural(count, "фотография", "фотографии", "фотографий");
function plural(n, form1, form2, form5) {
    n = Math.abs(n) % 100;
    n1 = n % 10;
    if (n > 10 && n < 20) return form5;
    else if (n1 > 1 && n1 < 5) return form2;
    else if (n1 == 1) return form1;

    return form5;
}


function ShowLoaderForum(text) {	
        $('div.search-window').hide();
        $("div.mainblock").fadeTo("fast", 0.2);
        $("div.pager").fadeTo("fast", 0.4);
        $("div.loading-window").show();
        if (text && Settings.string_filter != '') {
                $("div.exit").show();
        }
        Settings.Fetch();
}
	
	
function HideLoaderForum(text) {	
        $("div.mainblock").fadeTo("fast", 1.0);
        $("div.pager").fadeTo("fast", 1.0);
        $("div.loading-window").hide();
        if (Settings.string_filter == '') {
                $("div.exit").hide();
        }
}	


function Switch(item, multi) {
        if (multi) {
                $(item).parent().parent().parent().parent().find('li').removeClass('select').find('div.corner').remove();
                $(item).parent().addClass('select').append('<div class="corner tl"></div><div class="corner bl"></div>');
        } else {
                $(item).parent().parent().find('li').removeClass('select').find('div.corner').remove();
                $(item).parent().addClass('select').append('<div class="corner tl"></div><div class="corner bl"></div>');
        }
}


function selectPhoto(element) {
        $('li.download a').attr('href',$(element).find('img').attr('full'));
        pictureId = parseInt($(element).find('img').attr('id').split('_')[1]);
        $('#thumbnails li.select').removeClass('select').find('div.border').remove();
        $(element).addClass('select').append('<div class="border"> </div>');
        can_load_new_image = false;
        $('#gallery_main').fadeOut('slow', function() {
                $('#gallery_main').attr('src',$(element).find('img').attr('full'));
                can_load_new_image = true;
        });
        $('#loading').fadeIn('slow');
        set_np();
}
	
	
function set_np() {
        if ($('#thumbnails li').length < 2) {
                $('div.photo a.back').hide();
                $('div.photo a.next').hide();
        } else {
                $('div.photo a.back').show();
                $('div.photo a.next').show();
                var first_selected =  $('#thumbnails li:first').hasClass('select');
                var last_selected =  $('#thumbnails li:last').hasClass('select');
                if (first_selected) {
                        $('div.photo a.back').hide();
                }
                if (last_selected) {
                        $('div.photo a.next').hide();
                }
        }
}
	
function show_image() {      
        if (can_load_new_image) {
                $('#loading').hide();
                $('#gallery_main').fadeIn('slow');
        } else {
                setTimeout ('show_image();', 200 );
        }
}
	
	
// Заполнение попап окна домов из подготовленного в шаблоне массива данных
function fillHouses(floor) { 
        if (typeof houses !== 'undefined') {
                var ct = 0;
                if (floor) {
                        for (house in houses[floor]) {
                                ct++;
                        }
                } else {
                        for (flr in houses) {
                                for (house in houses[flr]) {
                                        ct++;
                                }
                        }
                }

                var pos = 0;

                if (floor) {
                        for (house in houses[floor]) {
                                pos++;
                                $('#list_houses_popup ul').eq(ct-Math.ceil(ct / pos)).append('<li><label><input type="checkbox" value="'+houses[floor][house].id+'"/>'+houses[floor][house].name+'</label></li>');
                        }
                } else {
                        for (floor in houses) {
                                for (house in houses[floor]) {
                                        pos++;
                                        $('#list_houses_popup ul').eq(ct-Math.ceil(ct / pos)).append('<li><label><input type="checkbox" value="'+houses[floor][house].id+'"/>'+houses[floor][house].name+'</label></li>');
                                }
                        }
                }

                $('#house_select').attr('disabled', true);
        }
}

function in_array( needle, haystack, strict ) {
    var strict = !!strict;

    for(var key in haystack){
        if( (strict && haystack[key] === needle) || (!strict && haystack[key] == needle) ){
            return true;
        }
    }
     return false;
}

// Проверка правильности формата email
// Параметры - имя формы, имя поля
// Возвращает Boolean
function isEmailCorrect(formname, fieldname) { 
    var re = /^\w+([\.-]?\w+)*@(((([a-z0-9]{2,})|([a-z0-9][-][a-z0-9]+))[\.][a-z0-9])|([a-z0-9]+[-]?))+[a-z0-9]+\.([a-z]{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/i;
    if (re.test(eval("document.forms['"+formname+"']."+fieldname+".value"))) {
        return true;
    } else {
        return false;
    }
}


// Простая проверка на минимаьную коректность пароля
function isPasswordCorrect(passwrd){
    if (passwrd.trim().length < 6) {
        return false;
    }
    re = /^\w+$/;
    if (!re.test(passwrd)) {
        return false;
    }
    return true;
}

