$(document).ready(function() {
    $("input#txtSearchField").focus();
    $("body").append("<input type='hidden' name='lastpage' id='lastpage' />");
    setupLinks();
    resizeContent();

    $(window).wresize(resizeContent);
    resizeContent();
});

function resizeContent() {
    $("#content").height($(window).height() - $("#logo").height() - $("#menuTop").height() - $("#subMenu").height());

    $("#googleAds").css("left", (($(window).width() - 550) / 2) + "px");
}

function disableRating() {
    try {
        var details = $(document).find("div.details");
        if (details.length > 0) {
            var rs = details.find("div");
            var c_id = rs.get(1).id;
            var r_id = c_id.substring(3, c_id.length);

            var entries = readCookie("rn");
            var ratedEntries = entries.split(",");

            for (var reIdx = 0; reIdx < ratedEntries.length; reIdx++) {
                if (r_id == ratedEntries[reIdx]) {
                    document.getElementById('rs_' + r_id).style.visibility = 'hidden';
                    break;
                }
            }
        }
    }
    catch (e) { }
}

function readCookie(cookieName) {
    a = document.cookie;
    res = '';
    while (a != '') {
        currentCookieName = a.substring(0, a.search('='));
        currentCookieValue = a.substring(a.search('=') + 1, a.search(';'));
        if (currentCookieValue == '') { currentCookieValue = a.substring(a.search('=') + 1, a.length); }
        if (cookieName == currentCookieName) { res = currentCookieValue; }
        i = a.search(';') + 1;
        if (i == 0) { i = a.length }
        a = a.substring(i, a.length);
    }
    return res;
}
function writeCookie(n, v, e) {
    var a = new Date();
    a = new Date(a.getTime() + e);
    document.cookie = n + '=' + v + '; expires=' + a.toGMTString() + ';';
}
function updateNameRating($rs) {
    $cookieName = 'rn';
    $elemId = $rs.split(';')[0];
    $newAvg = $rs.split(';')[1];
    aV = document.getElementById('aV_' + $elemId);
    aV.innerHTML = parseInt(aV.innerHTML) + 1;
    avg = document.getElementById('avg_' + $elemId);
    avg.innerHTML = $newAvg;
    document.getElementById('rs_' + $elemId).style.visibility = 'hidden';
    writeCookie($cookieName, readCookie($cookieName) + ',' + $elemId, 1000 * 60 * 60 * 24 * 365);
}

function updateTable(rs) {
    var names = eval('(' + rs + ')');

    $("table#name_table").find("tr.data").remove();

    for (var i = 0; i < names.length; i++) {
        var rowClass = "odd";
        if ((i + 1) % 2 == 0) { rowClass = "even"; }
        if (names[i].sex == 1) { imgUrl = '<img src="/images/male.gif" title="männlicher Vorname" alt="männlicher Vorname" />'; }
        else if (names[i].sex == 2) { imgUrl = '<img src="/images/female.gif" title="weiblicher Vorname" alt="weiblicher Vorname" />'; }
        else { imgUrl = '<img src="/images/neutral.gif" title="männlicher und weiblicher Vorname" alt="männlicher und weiblicher Vorname" />'; }

        newTableRow = '<tr class="data ' + rowClass + '"><td class="name"><a href="/vorname/' + names[i].name + '">' + names[i].name + '</a></td><td class="sex">' + imgUrl + '</td><td class="origin">' + names[i].origin + '</td><td class="ratingCol">' + names[i].avg + ' aus ' + names[i].count + ' abgegebenen Stimmen</td></tr>';

        $("table#name_table").append(newTableRow);
    }
}

function replaceElems(data) {
    var div = document.createElement("body");
    div.innerHTML = data;
    $("#content").html($(div).find("#content").html());
    setupLinks();
    disableRating();
    if ($("a.back").length) {
        $("a.back").attr("onclick", "window.location.href='" + $("input#lastpage").attr("value") + "'; return false;");
        $("a.back").attr("href", $("input#lastpage").attr("value"));
    }
}

function setupLinks() {
    $("#content").find("a").each(function() {
        setupLink($(this));
    });
}

function setupLink(anchor) {
    anchor.click(function() {
        try {
            href = anchor.attr("href");
            if (!$(this).parents("td.name").length) {
                $("input#lastpage").attr("value", href);
            }
            $.get(href, replaceElems); return false;
        }
        catch (e) { return true; }
    });
}

/*   
=============================================================================== 
WResize is the jQuery plugin for fixing the IE window resize bug 
............................................................................... 
Copyright 2007 / Andrea Ercolino 
------------------------------------------------------------------------------- 
LICENSE: http://www.opensource.org/licenses/mit-license.php 
WEBSITE: http://noteslog.com/ 
=============================================================================== 
*/

(function($) {
    $.fn.wresize = function(f) {
        version = '1.1';
        wresize = { fired: false, width: 0 };

        function resizeOnce() {
            if ($.browser.msie) {
                if (!wresize.fired) {
                    wresize.fired = true;
                }
                else {
                    var version = parseInt($.browser.version, 10);
                    wresize.fired = false;
                    if (version < 7) {
                        return false;
                    }
                    else if (version == 7) {
                        //a vertical resize is fired once, an horizontal resize twice 
                        var width = $(window).width();
                        if (width != wresize.width) {
                            wresize.width = width;
                            return false;
                        }
                    }
                }
            }

            return true;
        }

        function handleWResize(e) {
            if (resizeOnce()) {
                return f.apply(this, [e]);
            }
        }

        this.each(function() {
            if (this == window) {
                $(this).resize(handleWResize);
            }
            else {
                $(this).resize(f);
            }
        });

        return this;
    };

})(jQuery);