var F_VOTING = 'голосование доступно';
var MAX_WIDTH = 694;

function fitImg(img) {
if(img.id == 'logo') return;
if(img.parentNode.nodeName == 'DIV' && img.parentNode.className == "c" ) {
if(img.width > 620) {
img.height = 620 * (img.height / img.width);
img.width = 620;
return;
}
}

if(img.width > MAX_WIDTH) {
img.height = MAX_WIDTH * (img.height / img.width);
img.width = MAX_WIDTH;
}
}

function fitImages() {
$('img').each(function() {
fitImg(this);
});
}

function saveTimeZone() {
var rightNow = new Date();
var jan1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);  // jan 1st
var june1 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0); // june 1st
var temp = jan1.toGMTString();
var jan2 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
temp = june1.toGMTString();
var june2 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
var std_time_offset = (jan1 - jan2) / (1000 * 60 * 60);
var daylight_time_offset = (june1 - june2) / (1000 * 60 * 60);
var dst;
if (std_time_offset == daylight_time_offset) {
dst = "0"; // daylight savings time is NOT observed
} else {
var hemisphere = std_time_offset - daylight_time_offset;
if (hemisphere >= 0)
std_time_offset = daylight_time_offset;
dst = "1"; // daylight savings time is observed
}

document.cookie = "__lgtz=" + Math.floor(std_time_offset) + "%3A" + dst;
}

$(window).load(function() {
fitImages();

if(document.cookie.indexOf('__lgtz') == -1) {
saveTimeZone();
}

if($.browser.safari) {
fitImages();
}
/**
* Convert image dimensions to em's
*/

if(screen.width > 1280) {
$('img').filter(function() {
if(this.className == 'savatar') {
return false;
} else
if(this.parentNode.className == 'tmbnail') {
return false;
}
return true;
}).each(function() {
if(this.id == 'logo' || this.height < 90 || this.width < 90 || this.src.indexOf("google") != -1 || this.nsz == 1) return;
var t = $(this);
this.origW = this.width;
this.origH = this.height;
fitImg(this);
var w = 0.0833 * this.width, h = 0.0833 * this.height;
t.height(h + "em");
t.width(w + "em");
});
}
});


/**
* Autosize fonts on window resize
*/
function rsFS() {
var tf = '12px', w = document.body.clientWidth;
if(w > 1850) {
tf = '17px';
} else if(w > 1750) {
tf = '16px';
} else if(w > 1650) {
tf = '15px';
} else if(w > 1550) {
tf = '14px';
} else if(w > 1450) {
tf = '13px';
}
$(document.body).css('fontSize', tf);
}

if(screen.width > 1280) {
$(function() {
rsFS();
})
$(window).resize(rsFS);
}

function showPage( pageInd ) {
var l = String(window.location), path = l.substring(l.indexOf('/', 8)).replace(/[&\?]p=\d+/g, ''), nto = path;

if(pageInd > 0) {
nto += (path.indexOf('?') == -1 ? "?" : "&") + "p=" + pageInd;
}

window.open(nto, "_self");
return false;
}

function jumpToPage() {
var pc = parseInt($('#jpp input:first').val()) - 1;
return showPage( pc );
}

function showPC(o) {
var off = $(o).offset();
$('#jpp').css({
top: off.top + $(o).height() + 6,
left: off.left - ($('#jpp').width() - $(o).width()) + 11
}).toggle('fast', function() { $('#jpp input:first').focus(); })
.keypress(function (e) { if ( e.which == 13 ) { jumpToPage(); } });
}

function ajaxa(link, target) {
$('#' + target).load($(link).attr('href'));
return false;
}

function pleaseRegister(feature) {
new Boxy('<div style="font-size: 1.2em">Извините, но ' + feature + ' только для зарегистрированных пользователей.<br/> Нажмите <a href="/users/register">сюда</a>, чтобы перейти к регистрационной форме.</div>', {unloadOnHide: true, modal: true, title: 'Необходима регистрация', closeText: 'Закрыть', closable: true});

}

function unmark(obj) {
$.get(obj.href, function() {
$(obj).parent().fadeOut();
});

return false;
}

var core = {
showLoginForm : function() {
Boxy.load('/users/loginForm', {unloadOnHide: true, modal: true, title: 'Вход на сайт'});
return false;
}
};

var login = {};

