/* hjkhjkhjkhjk*/
/* zuletzt ge�ndert: 20.11.2009 (kai) */

checkForNewVisitors = null;

/* Script f�r Infotip */
info = null;
function showINFO(id, width) {
    info = document.getElementById(id);
    info.style.display = "block"
    info.style.marginLeft = width + "px";	//NEU
}
function showINFO_delay(id, width) { //NEU
    window.setTimeout(function(){
        showINFO(id, width);
    },200);
}
function hideINFO() {
    if(info != null) { //NEU
        info.style.display = "none";
    }
}


function CheckLen(Target)
{
    StrLen = Target.value.length
    if (StrLen == 1 && Target.value.substring(0,1) == " ")
    {
        Target.value = ""
        StrLen = 0
    }
    if (StrLen > 115 )
    {
        Target.value = Target.value.substring(0,115)
        CharsLeft = 0
    }
    else
    {
        CharsLeft = 115 - StrLen
    }
    document.answer.Anzahl.value = CharsLeft
}

function na_open_window(name, url, left, top, width, height, toolbar, menubar, statusbar, scrollbar, resizable)
{
    toolbar_str = toolbar ? 'yes' : 'no';
    menubar_str = menubar ? 'yes' : 'no';
    statusbar_str = statusbar ? 'yes' : 'no';
    scrollbar_str = scrollbar ? 'yes' : 'no';
    resizable_str = resizable ? 'yes' : 'no';
    t = window.open(url, name, 'left='+left+',top='+top+',width='+width+',height='+height+',toolbar='+toolbar_str+',menubar='+menubar_str+',status='+statusbar_str+',scrollbars='+scrollbar_str+',resizable='+resizable_str);
    t.focus();
}


// #############################################################

/* Script f�r Tooltip */
tip = null;
document.onmousemove = updateTIP;
function updateTIP(e) {
    if (tip != null) {
        x = (e.pageX ? e.pageX : window.event.x) + tip.offsetParent.scrollLeft - tip.offsetParent.offsetLeft;
        y = (e.pageY ? e.pageY : window.event.y) + tip.offsetParent.scrollTop - tip.offsetParent.offsetTop;
    
        //x = (document.all) ? window.event.x + tip.offsetParent.scrollLeft - tip.offsetParent.offsetLeft : e.pageX;
        //y = (document.all) ? window.event.y + tip.offsetParent.scrollTop - tip.offsetParent.offsetTop  : e.pageY;
        tip.style.left = (parseInt(x) + 25) + "px";
        tip.style.top   = (parseInt(y) + 20) + "px";
    }
}
function showTIP(id) {
    tip = document.getElementById(id);
    tip.style.display = "block"
}
function hideTIP() {
    tip.style.display = "none";
}

// #############################################################


/* Script f�r Ausklappmen�s */

function showNavi(e) {
    document.getElementById(e).style.display='block';
}

function hideNavi(e) {
    document.getElementById(e).style.display='none';
}

// #############################################################


/* �nderungen von Sascha, 14.11.09, Galleriefunktion f�r show_profil_full */
/* �nderungen von Sascha, 18.11.09, Ajax-Polling f�r Popup bei neuen Besuchern oder Nachrichten */

function isdefined( variable)
{
    return (typeof(window[variable]) == "undefined")?  false: true;
}


var checkForPopup;
if (isdefined('jQuery')) {
    jQuery(document).ready( function($) {
        try {
            var mainProfilePicture = $("#profilbild").attr('src');
            if (mainProfilePicture.substring(0,32) != 'http://www.singletreffen.de/user' && mainProfilePicture.substring(0,32) != 'http://alt.singletreffen.de/user') {
                mainProfilePicture = '';
            }
        } catch(exception) {}

        function setPicture (source) {
            $(".profilbildgallerie").each(function () {
                if ($(this).attr('src') == source) {
                    $(this).addClass("current_foto");
                } else {
                    $(this).removeClass("current_foto");
                }
            });
            $("#profilbild").fadeOut(100, function () {
                $("#profilbild").attr('src', source);
            });
            $("#profilbild").fadeIn(700);
        }

        $("#profil_galerie_browse_left").click(function () {
            if ($("#profilbild").attr('src') == $(".profilbildgallerie:first").attr('src')) {
                if (mainProfilePicture != '') {
                    setPicture ( mainProfilePicture );
                } else {
                    setPicture ( $(".profilbildgallerie:last").attr('src') );
                }
            } else {
                pictureset = false;
                prevpicture = $(".profilbildgallerie:last").attr('src');
                $(".profilbildgallerie").each(function () {
                    if ($("#profilbild").attr('src') == $(this).attr('src')) {
                        setPicture ( prevpicture );
                        pictureset = true;
                        return false;
                    }
                    prevpicture = $(this).attr('src');
                });
                if (pictureset == false) {
                    setPicture ( prevpicture );
                }
            }
        });

        $("#profil_galerie_browse_right").click(function () {
            if ($("#profilbild").attr('src') == $(".profilbildgallerie:last").attr('src')) {
                if (mainProfilePicture != '') {
                    setPicture ( mainProfilePicture );
                } else {
                    setPicture ( $(".profilbildgallerie:first").attr('src') );
                }
            } else {
                flag = false;
                pictureset = false;
                $(".profilbildgallerie").each(function () {
                    if (flag == true) {
                        setPicture ( $(this).attr('src') );
                        pictureset = true;
                        return false;
                    } else if ($("#profilbild").attr('src') == $(this).attr('src')) {
                        flag = true;
                    }
                });
                if (pictureset == false) {
                    setPicture ( $(".profilbildgallerie:first").attr('src'));
                }
            }
        });

        $(".profilbildgallerie").click(function () {
            setPicture ($(this).attr('src'));
        });


        // ajax-funktionen f�r "matching" (singlevorschl�ge auf community/konto
        $("#home_singlevorschlaege_more").click(function () {
            getSingleMatching();
        });

        function getSingleMatching() {
            $(".singlevorschlagImg").each( function( intIndex ) {
                $(this).attr('src', '');
                $(this).addClass("ajax_loading");
                $(this).attr('src', '/media/img/ajaxloading.gif');
            });
            $(".singlevorschlagOnlineStatus").each( function( intIndex ) {
                $(this).html('');
            });
            $.get("/ajax/matching/" + sid, {}, function(data) {
                $(".singlevorschlagImg").each( function( intIndex ) {
                    $(this).removeClass("ajax_loading");
                    $(this).attr('src', '');
                    $(this).attr('src', data.users[intIndex].Bild);
                });
                $(".singlevorschlagLink").each( function( intIndex ) {
                    $(this).attr('href', '/community/show_profil_full/' + data.users[intIndex].UserID + '/' + sid);
                });
                $(".singlevorschlagTxt1").each( function( intIndex ) {
                    $(this).html(data.users[intIndex].Username + ' (' + data.users[intIndex].Geschlecht + ') , ' + data.users[intIndex].Alter + ' Jahre');
                });
                $(".singlevorschlagTxt2").each( function( intIndex ) {
                    $(this).html(data.users[intIndex].PLZ + ' ' + data.users[intIndex].Ort);
                });
                $(".singlevorschlagOnlineStatus").each( function( intIndex ) {
                    if (data.users[intIndex].online == 'online') {
                        $(this).html(data.users[intIndex].online);
                        $(this).css('color', '#00CC00');
                        $(this).css('font-weight', 'bold');
                        $(this).css('display', 'block');
                    } else {
                        $(this).css('display', 'none');
                    }
                });
                $(".singlevorschlagTxt3").each( function( intIndex ) {
                    $(this).html(data.users[intIndex].ShortDesc);
                });
                $(".singlevorschlagButtonSms").each( function( intIndex ) {
                        $(this).attr('href', "/community/smschat/" + data.users[intIndex].UserID);
                        $(this).attr('onclick', "na_open_window('smschat', '/community/smschat/" + data.users[intIndex].UserID + "', 0, 0, 500, 700, 0, 0, 0, 1, 0); return false;");
                });
                $(".singlevorschlagButtonMsg").each( function( intIndex ) {
                    $(this).attr('href', "/community/send_message/" + data.users[intIndex].UserID);
                });
                $(".singlevorschlagButtonChat").each( function( intIndex ) {
                    if (data.users[intIndex].onlineRaw == 'online') {
                        $(this).addClass('suche_button_chat');
                        $(this).attr('href', "/community/chat_einleiten/" + data.users[intIndex].UserID);
                        $(this).html('<span class="hidden help_text">Chat starten</span>');
                        $(this).css('cursor', '');
                    } else {
                        $(this).removeClass('suche_button_chat');
                        $(this).attr('href', "javascript:void(0);");
                        $(this).html('');
                        $(this).css('cursor', 'default');
                    }
                });
                $(".singlevorschlagButtonGruss").each( function( intIndex ) {
                    $(this).removeClass('suche_button_geburtstag');
                    $(this).removeClass('suche_button_gruss');
                    if (data.users[intIndex].Geburtstag == 'ja') {
                        $(this).addClass('suche_button_geburtstag');
                        $(this).attr('href', "/community/send_greeting/geburtstag/" + data.users[intIndex].UserID);
                    } else {
                        $(this).addClass('suche_button_gruss');
                        $(this).attr('href', "/community/send_greeting/" + data.users[intIndex].UserID);
                    }
                });
                $(".singlevorschlagButtonGrussText").each( function( intIndex ) {
                    if (data.users[intIndex].Geburtstag == 'ja') {
                        $(this).html('Geburtstagsgruß senden');
                    } else {
                        $(this).html('Gruß senden');
                    }
                });

                $(".singlevorschlagButtonFavoriten").each( function( intIndex ) {
                    $(this).attr('href', "/community/favoriten/add-" + data.users[intIndex].UserID);
                });
                $(".singlevorschlagButtonBlacklist").each( function( intIndex ) {
                    $(this).attr('href', "/community/add_ignore/" + data.users[intIndex].UserID);
                });
                $(".singlevorschlagButtonMelden").each( function( intIndex ) {
                    $(this).attr('href', "javascript:na_open_window('beschwerde', '/hilfe/beschwerde/" + data.users[intIndex].UserID + "', 0, 0, 550, 450, 0, 0, 0, 0, 0)");
                });

            }, 'json');
        }

        if ($("#home_singlevorschlaege_div").html() != null) {
            getSingleMatching();
        }

        
    });

    // ajax-funktion: neue nachrichten & neue besucher auf dem profil
    function checkNewVisitors () {
        // pruefe nur auf neue nachrichten, falls fenster bisher nicht sichtbar ist
        if ($('#messenger_post_layer').css('display') == 'none') {
            $.get("/messenger/messenger_post_ajax", {
            }, function(data){
                if (parseInt(data) < 2147483647) {
                    $.getJSON("/messenger/messenger_post_json", {
                        'PHPSESSID':sid
                    }, function(data){
                        $(".messenger_post_layer_link").each( function( intIndex ) {
                            $(this).attr('href', "/community/message/inbox/" + data.MsgID);
                        });
                        $(".messenger_post_layer_img").each( function( intIndex ) {
                            $(this).attr('src', data.VorschauImg);
                        });
                        $("#messenger_post_layer_user").html(data.Username);
                        $("#messenger_post_layer_msgtype").html(data.MsgType);
                        $('#messenger_post_layer').show();
                        // wenn es einmal sichtbar war, breche polling ab
                        window.clearInterval(checkForNewVisitors);
                    });
                }
            });
        }
        // pruefe nur auf neue besucher, falls fenster bisher nicht sichtbar ist
        if ($('#messenger_besucher_layer').css('display') == 'none') {
            $.get("/messenger/messenger_profilaufruf_ajax", {
            }, function(data){
                if (parseInt(data) < 10) {
                    $.getJSON("/messenger/messenger_profilaufruf_json", {}, function(data){
                        $(".messenger_besucher_layer_link").each( function( intIndex ) {
                            $(this).attr('href', "/community/show_profil_full/" + data.UserID);
                        });
                        $(".messenger_besucher_layer_img").each( function( intIndex ) {
                            $(this).attr('src', data.VorschauImg);
                        });
                        $(".messenger_besucher_layer_user").each( function( intIndex ) {
                            $(this).html(data.Username);
                        });
                        $(".messenger_besucher_layer_alter").each( function( intIndex ) {
                            $(this).html(data.Alter);
                        });
                        $(".messenger_besucher_layer_stadt").each( function( intIndex ) {
                            $(this).html(data.Stadt);
                        });
                        $('#messenger_besucher_layer').show();
                        // wenn es einmal sichtbar war, breche polling ab
                        window.clearInterval(checkForNewVisitors);
                    });
                }
            });
        }
    }
}
