$(document).ready(function () {
    var imghdTimer = {};
    var tcuTimer = null;
    
    $('span.jsuser').hover(function (e) {
        var classname = $(this).attr('class');
        var classes = classname.split(' ');
        var user_id = null;
        
        if (classes.length > 0) {
            for (var i=0; i<classes.length; i++) {
                if (classes[i].substring(0,5) == 'user_') {
                    user_id = classes[i].substring(5);
                }
            }
        }
        
        if (user_id != '') {
            // var user_id = $(this).attr('id').substring(7);
            var imghd = $('#imghd_' + user_id);
            
            window.clearTimeout(imghdTimer['imghd_' + user_id]);
            
            if (imghd.length > 0) {
                imghd.show();
            }
            else {
                imghd = $('<div id="imghd_' + user_id + '" class="imghd">')
                    .append($('<div>')
                        .append($('<ul>')
                            .append('<li class="jsprofile"><a href="/contacts/userprofile/' + ZeroFill(user_id, 4) + '/"><span>&nbsp;&nbsp;&nbsp;</span></a></li>')
                            .append('<li class="jsmessage"><a href="/messages/sendmessage/' + ZeroFill(user_id, 4) + '/"><span>&nbsp;&nbsp;&nbsp;</span></a></li>')
                        )
                        .append($('<img src="/photos/flashPhoto/?user_id=' + user_id + '" alt="" />'))
                    )
                    .appendTo($('body'));
                imghd.hover(function () {
                    window.clearTimeout(imghdTimer[this.id]);
                },function () {
                    $(this).hide();
                });
            }
            
            var left = 0;
            var top = 0;
            
            left = e.pageX + 10;
            top = e.pageY + 10;
            
            if (($(window).height() - (e.pageY - $(document).scrollTop()) - 120 - 10 < 0)) {
                top = e.pageY - 120;
            }
            else {
                top = (e.pageY + 20);
            }
            
            imghd.css({
                'left': left + 'px',
                'top': top + 'px'
            });
        }
    }, function (e) {
        var classname = $(this).attr('class');
        var classes = classname.split(' ');
        var user_id = null;
        if (classes.length > 0) {
            for (var i=0; i<classes.length; i++) {
                if (classes[i].substring(0,5) == 'user_') {
                    user_id = classes[i].substring(5);
                }
            }
        }
        if (user_id != '') {
            imghdTimer['imghd_' + user_id] = window.setTimeout(function () {
                $('#imghd_' + user_id).hide();
            },200);
        }
    });
    
    $('span.jsuser2').hover(function (e) {
        var classname = $(this).attr('class');
        var classes = classname.split(' ');
        var user_id = null;
        if (classes.length > 0) {
            for (var i=0; i<classes.length; i++) {
                if (classes[i].substring(0,5) == 'user_') {
                    user_id = classes[i].substring(5);
                }
            }
        }
        if (user_id != '') {
            // var user_id = $(this).attr('id').substring(7);
            var imghd = $('#imghd2_' + user_id);
            
            window.clearTimeout(imghdTimer['imghd2_' + user_id]);
            
            if (imghd.length > 0) {
                imghd.show();
            }
            else {
                var userdata = $('#userdata_' + user_id).val();
                if (userdata.length > 0) {
                    datas = userdata.split(';');
                    var datetime = eval(datas[0]);
                    var view_count = eval(datas[1]);
                    
                    var userdiv = ($('<div class="userdata1">')
                        .append('naposledy: <br />' + datetime)
                        .append('<br />')
                        .append('celkovo: ' + view_count + 'x')
                    );
                }
                imghd = $('<div id="imghd2_' + user_id + '" class="imghd">')
                    .append($('<div>')
                        .append($('<ul>')
                            .append('<li class="jsprofile"><a href="/contacts/userprofile/' + ZeroFill(user_id, 4) + '/"><span>&nbsp;&nbsp;&nbsp;</span></a></li>')
                            .append('<li class="jsmessage"><a href="/messages/sendmessage/' + ZeroFill(user_id, 4) + '/"><span>&nbsp;&nbsp;&nbsp;</span></a></li>')
                        )
                        .append($('<img src="/photos/flashPhoto/?user_id=' + user_id + '" alt="" />'))
                        .append(userdiv)
                    )
                    .appendTo($('body'));
                imghd.hover(function () {
                    window.clearTimeout(imghdTimer[this.id]);
                },function () {
                    $(this).hide();
                });
            }
            
            var left = 0;
            var top = 0;
            
            left = e.pageX + 10;
            top = e.pageY + 10;
            
            if (($(window).height() - (e.pageY - $(document).scrollTop()) - 120 - 10 < 0)) {
                top = e.pageY - 120;
            }
            else {
                top = (e.pageY + 20);
            }
            
            imghd.css({
                'left': left + 'px',
                'top': top + 'px'
            });var left = 0;
            var top = 0;
            
            left = e.pageX + 10;
            top = e.pageY + 10;
            
            if (($(window).height() - (e.pageY - $(document).scrollTop()) - 170 - 10 < 0)) {
                top = e.pageY - 170;
            }
            else {
                top = (e.pageY + 20);
            }
            
            imghd.css({
                'left': left + 'px',
                'top': top + 'px'
            });
        }
    }, function (e) {
        var classname = $(this).attr('class');
        var classes = classname.split(' ');
        var user_id = null;
        if (classes.length > 0) {
            for (var i=0; i<classes.length; i++) {
                if (classes[i].substring(0,5) == 'user_') {
                    user_id = classes[i].substring(5);
                }
            }
        }
        if (user_id != '') {
            imghdTimer['imghd2_' + user_id] = window.setTimeout(function () {
                $('#imghd2_' + user_id).hide();
            },200);
        }
    });
    
    
    $('#today_celebration').hover(function (e) {
        window.clearTimeout(tcuTimer);
        var tcu = $('#todays_celebrations ul');
        // tcu.css({
        //     'left': (e.pageX + 10) + 'px',
        //     'top': (e.pageY + 10) + 'px'
        // });
        tcu.show();
        tcu.hover(function () {
            window.clearTimeout(tcuTimer);
        }, function () {
            $(this).hide();
        });
    },function (e) {
        tcuTimer = window.setTimeout(function () {
            $('#todays_celebrations ul').hide();
        },150);
    });
    
    // vyber mesta - update profilu
    $('#city').change(function () {
        if ($(this).val() == '0') {
            $('#otherCity').show();
        }
        else {
            $('#otherCity').hide();
        }
    });
    if ($('#city').val() == '0') {
        $('#otherCity').show();
    }
    else {
        $('#otherCity').hide();
    }
    
    // fotky
    $('.thumbnailed a').hover(function (e) {
        var id = $(this).attr('rel');
        if (typeof(id) != 'undefined' && id != '') {
            var left = 0;
            var top = 0;
            if ((e.pageX + 400 + 40) > $(document).width()) {
                left = (e.pageX - 400 - 20);
            }
            else {
                left = (e.pageX + 20);
            }
            if (($(window).height() - (e.pageY - $(document).scrollTop()) - 40 < 300)) {
                top = (e.pageY - 300 - 20);
            }
            else {
                top = (e.pageY + 20);
            }
            
            if ($('#bigimage_' + id).is('.imgLoaded')) {
                $('#bigimage_' + id)
                .css({
                    'left': left + 'px',
                    'top': top + 'px'
                })
                .show();
            }
            else {
                var img = $('<div id="bigimage_' + id + '" class="bigimage imgLoaded">')
                    .css({
                    'left': left + 'px',
                    'top': top + 'px'
                })
                .append($('<img src="/photos/bigimage/?id=' + id + ' alt="" />'));
                img.appendTo(document.body);
            }
        }
    }, function (e) {
        var id = $(this).attr('rel');
        $('#bigimage_' + id).hide();
    });
    $('.thumbnailed a').mousemove(function (e) {
        var id = $(this).attr('rel');
        if (typeof(id) != 'undefined' && id != '') {
            var left = 0;
            var top = 0;
            if ((e.pageX + 400 + 40) > $(document).width()) {
                left = (e.pageX - 400 - 20);
            }
            else {
                left = (e.pageX + 20);
            }
            if (($(window).height() - (e.pageY - $(document).scrollTop()) - 40 < 300)) {
                top = (e.pageY - 300 - 20);
            }
            else {
                top = (e.pageY + 20);
            }
            
            $('#bigimage_' + id)
            .css({
                'left': left + 'px',
                'top': top + 'px'
            });
        }
    });
    $('.bigimage').mouseout(function () {
        $(this).hide();
    });
    
    // spravy
    $('#selectall').click(function () {
        $('.msgcheck').each(function () {
            this.checked = true;
        });
    });
    $('#selectnone').click(function () {
        $('.msgcheck').each(function () {
            this.checked = false;
        });
    });
    
    // bannery b2m
    $('.adv iframe').css('margin', '0px auto');
    
    if (typeof($('#b_home_left_125x125 div embed').attr('src')) != 'undefined' && $('#b_home_left_125x125 div embed').attr('src') != '') {
        var bhl_embed_valid = true;
    }
    else {
        var bhl_embed_valid = false;
    }
    if ($('#b_home_left_125x125 iframe').html() != null && $('#b_home_left_125x125 iframe').html() != '' && typeof($('#b_home_left_125x125 iframe').html()) != 'undefined') {
        var bhl_iframe_valid = true;
    }
    else {
        var bhl_iframe_valid = false;
    }
    if (typeof($('#b_home_left_125x125 a img').attr('src')) != 'undefined' && $('#b_home_left_125x125 a img').attr('src') != '') {
        var bhl_img_valid = true;
    }
    else {
        var bhl_img_valid = false;
    }
    if (bhl_embed_valid || bhl_iframe_valid || bhl_img_valid) {
    }
    else {
        $('#b_home_left_125x125').hide();
    }
    
    if (typeof($('#b_home_right_1_60x465 div embed').attr('src')) != 'undefined' && $('#b_home_right_1_60x465 div embed').attr('src') != '') {
        var bhrt_embed_valid = true;
    }
    else {
        var bhrt_embed_valid = false;
    }
    if ($('#b_home_right_1_60x465 iframe').html() != null && $('#b_home_right_1_60x465 iframe').html() != '' && typeof($('#b_home_right_1_60x465 iframe').html()) != 'undefined') {
        var bhrt_iframe_valid = true;
    }
    else {
        var bhrt_iframe_valid = false;
    }
    if (typeof($('#b_home_right_1_60x465 a img').attr('src')) != 'undefined' && $('#b_home_right_1_60x465 a img').attr('src') != '') {
        var bhrt_img_valid = true;
    }
    else {
        var bhrt_img_valid = false;
    }
    if (bhrt_embed_valid || bhrt_iframe_valid || bhrt_img_valid) {
    }
    else {
        $('#b_home_right_1_60x465').hide();
    }
    
    if (typeof($('#b_home_right_2_60x465 div embed').attr('src')) != 'undefined' && $('#b_home_right_2_60x465 div embed').attr('src') != '') {
        var bhrb_embed_valid = true;
    }
    else {
        var bhrb_embed_valid = false;
    }
    if ($('#b_home_right_2_60x465 iframe').html() != null && $('#b_home_right_2_60x465 iframe').html() != '' && typeof($('#b_home_right_2_60x465 iframe').html()) != 'undefined') {
        var bhrb_iframe_valid = true;
    }
    else {
        var bhrb_iframe_valid = false;
    }
    if (typeof($('#b_home_right_2_60x465 a img').attr('src')) != 'undefined' && $('#b_home_right_2_60x465 a img').attr('src') != '') {
        var bhrb_img_valid = true;
    }
    else {
        var bhrb_img_valid = false;
    }
    if (bhrb_embed_valid || bhrb_iframe_valid || bhrb_img_valid) {
    }
    else {
        $('#b_home_right_2_60x465').hide();
    }
    
    // banner 300x300
    if (typeof($('#b300x300 div embed').attr('src')) != 'undefined' && $('#b300x300 div embed').attr('src') != '') {
        var b300x300_embed_valid = true;
    }
    else {
        var b300x300_embed_valid = false;
    }
    if ($('#b300x300 iframe').html() != null && $('#b300x300 iframe').html() != '' && typeof($('#b300x300 iframe').html()) != 'undefined') {
        var b300x300_iframe_valid = true;
    }
    else {
        var b300x300_iframe_valid = false;
    }
    if (typeof($('#b300x300 a img').attr('src')) != 'undefined' && $('#b300x300 a img').attr('src') != '') {
        var b300x300_img_valid = true;
    }
    else {
        var b300x300_img_valid = false;
    }
    if (b300x300_embed_valid || b300x300_iframe_valid || b300x300_img_valid) {
    }
    else {
        $('#b300x300').hide();
        $('#homepageActionRotator').show();
    }
    
    /* fotka vo fotogalerii - navigacia dopredu-dozadu */
    var pgimage_height = parseInt($('#pgimage').height());
    if (pgimage_height < 380) {
        pgimage_height = 380;
    }
    // $('#pgim').css({
    //     height: pgimage_height,
    //     width: parseInt($('#pgimage').width()) + 'px'
    // });
    $('#pgim').css({
        height: pgimage_height + 'px'
    });
    $('#pgimage_a_prev').css({
        height: pgimage_height + 'px'
    });
    $('#pgimage_a_next').css({
        height: pgimage_height + 'px'
    });
});

function addEvent(o,e,f){
	if (o.addEventListener){ o.addEventListener(e,f,true); return true; }
	else if (o.attachEvent){ return o.attachEvent("on"+e,f); }
	else { return false; }
}

function ZeroFill(number, length) {
    if (number.length >= length) {
        return number;
    }
    else {
        return ZeroFill('0' + number, length);
    }
}

function LaunchChat(page) { 
    OpenWin = this.open(page, "chat_workspace", "toolbar=no,menubar=no,location=no,scrollbars=no,resizable=no,width=770,height=500"); 
}

function insertNoflashUsersBannerText() {
    var text = 'Nemáte nainštalovanú potrebnú verziu Flash playera. Nájdete ho na <a href="http://www.adobe.com/go/getflash/" onclick="window.open(this.href); return false;">tejto adrese</a>.';
    $('#onlineUsersFlash').append($('<div class="noflashusersbanner">').append(text));
}
function insertNoVideoText() {
    $('#player').empty();
    var text = 'Nemáte nainštalovanú potrebnú verziu Flash playera.<br />Nájdete ho na <a href="http://www.adobe.com/go/getflash/" onclick="window.open(this.href); return false;">tejto adrese</a>.';
    $('#player').append($('<div class="novideo">').append(text));
}