function log() {
    console.log(arguments);
}

jQuery.fn.log = function () {
    return this;
};

$.fn.clearForm = function () {
    return this.each(function () {
        var type = this.type,
            tag = this.tagName.toLowerCase();
        if (tag == 'form') return $(':input', this).clearForm();
        if (type == 'text' || type == 'password' || tag == 'textarea') this.value = '';
        else if (type == 'checkbox' || type == 'radio') this.checked = false;
        else if (tag == 'select') this.selectedIndex = -1;
        return true;
    });
};

function ISODateString(d) {
    function pad(n) {
        return n < 10 ? '0' + n : n;
    }
    return d.getUTCFullYear() + '-' + pad(d.getUTCMonth() + 1) + '-' + pad(d.getUTCDate()) + 'T' + pad(d.getUTCHours()) + ':' + pad(d.getUTCMinutes()) + ':' + pad(d.getUTCSeconds()) + 'Z';
}

function curpage() {
    var sPath = $(location).attr('href');
    var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
    if (sPage.length === 0) {
        sPage = "index";
    }
    return sPage;
}

var filtertimer;
var filtertimerInterval = 1000;

function filterEventsKeyUp(filter, dest) {
    filtertimer = setTimeout(

    function () {
        $.ajax({
            url: "ajax",
            type: "GET",
            data: {
                category: curpage(),
                ajaxfilter: "ajax",
                search: $("#" + filter).val()
            },
            cache: true,
            dataType: "html",
            success: function (html) {
                $("#" + dest).html(html);
                liveEditing();
            }
        });
    }, filtertimerInterval);
    return true;
}

function filterEventsKeyDown() {
    clearTimeout(filtertimer);
}

function statusMsg(t, d) {
    d = typeof (d) != 'undefined' ? d : "#language";
    $("#message_box").remove();
    $(d).after($('<a id="message_box"/a>').html(t).fadeIn(2000, function () {
        $(this).fadeOut(3000);
    }));
}

function getEvent(id) {
    var event;
    $.ajax({
        url: "ajax",
        type: "GET",
        data: {
            jsonevent: id
        },
        cache: true,
        async: false,
        dataType: "json",
        success: function (e) {
            event = e;
        }
    });
    return event;
}

function eventID(e) {
    var id = e.attr("id");
    if (!id) {
        id = e.parents(".listevent:first").attr("id");
    }
    return (id.replace("id", "")).replace("embed", "");
}

function generateShareElement(id) {
    $.ajax({
        url: "ajax",
        type: "GET",
        data: {
            getshare: id
        },
        cache: true,
        dataType: "html",
        success: function (html) {
            $("#share" + id).empty().append($('<div class="shareevent"/>').append($(html)));
            FB.XFBML.parse($('#share' + id).is());
        }
    });
    return true;
}

function createEmbed(id) {
    $("body").css("cursor", "progress");
    $.ajax({
        url: "ajax",
        type: "GET",
        data: {
            getembed: id
        },
        cache: true,
        dataType: "html",
        success: function (html) {
            $("#embed" + id).empty().append($(html));
            FB.XFBML.parse($('embed' + id).is());
            $("body").css("cursor", "auto");
            return true;
        }
    });
    return true;
}


function confirmaction(s) {
    if (confirm(s)) {
        return true;
    }
    else {
        return false;
    }
}

function throbIcon(e) {
    e.width(e.width());
    e.height(e.height());
    img = e.find("img").not(':animated').each(function () {
        $(this).data("w", $(this).width());
        $(this).data("m", $(this).css("margin-left"));
        $(this).data("t", $(this).css("margin-top"));
        $(this).data("o", $(this).css("opacity"));
        $(this).animate({
            opacity: 0.1,
            width: '+=2em',
            marginLeft: '-=1em',
            marginTop: '+=0em'
        }, 1000, "", function () {
            $(this).animate({
                opacity: $(this).data("o"),
                width: $(this).data("w"),
                marginLeft: $(this).data("m"),
                marginTop: $(this).data("t")
            }, 1000);
        });
    });
}

function setShareFade() {
    $(".normalmode  .eventclass").hover(function () {
        $(this).find(".shareevent:first").fadeIn(1000, function () {});
    }, function () {
        $(this).find(".shareevent:first").fadeOut(1000, function () {});
    });
}

function animationsEnable() {
    $('.throb .eventicon,.smallpopupimage,.normalmode .zoomimg .eventicon').hover(function () {
        throbIcon($(this));
    }, function () {
        $(this).stop(false, true);
    });
}

function documentReady() {

    /* glow text */
    $('.normalmode #pages a').addGlow({
        textColor: 'yellow',
        haloColor: '#aaa',
        radius: 100
    });

    $('.normalmode .glowtitle .title a').addGlow({
        textColor: 'gold',
        haloColor: '#aaa',
        radius: 200
    });
    $('.normalmode .glowdate .date a').addGlow({
        textColor: 'green',
        haloColor: '#aaa',
        radius: 200
    });
    $('.normalmode .glowsubject .subject a').addGlow({
        textColor: 'yellow',
        haloColor: '#aaa',
        radius: 200
    });
    $('.normalmode .glow a').addGlow({
        textColor: 'gold',
        haloColor: '#aaa',
        radius: 200
    });

    setShareFade();

    $('.importantnotice .title,.normalmode  .zoomtitle .title,.normalmode  .zoomsubject .subject').hover(

    function () {
        $(this).width($(this).width());
        $(this).height($(this).height());
        a = $(this).find("a").stop(true, true);
        a.data("w", a.css("font-size"));
        a.data("m", a.css("margin-left"));
        a.data("t", a.css("margin-top"));
        a.data("o", a.css("opacity"));
        a.animate({
            opacity: 0.1,
            fontSize: '+=1em',
            marginLeft: '-=0.5em',
            marginTop: '+=0em'
        }, 1000, "", function () {
            $(this).animate({
                opacity: $(this).data("o"),
                fontSize: $(this).data("w"),
                marginLeft: $(this).data("m"),
                marginTop: $(this).data("t")
            });
        });
    }, function () {
        a = $(this).find("a");
        a.css("opacity", a.data("o")).stop(true, true);
        a.css("font-size", a.data("w"));
        a.css("margin-left", a.data("m"));
        a.css("margin-top", a.data("t"));
    });

    $("input:submit,input:reset,.fbaction a").button();

    $(".tooltipurl,#pages a,#footerlinks a,#language, .normalmode a").tooltip();

    $(".imgfadeoi .eventicon").fadeOut(3000, function () {
        $(this).fadeIn(3000);
    });
    $(".imgfadeio .eventicon").hide().fadeIn(3000, function () {
        $(this).fadeOut(3000);
    });
    $(".imgfadeo .eventicon").fadeOut(5000);
    $(".imgfadei .eventicon").hide().fadeIn(5000);

    $(".bigtext").resizable({
        animate: true,
        autoHide: true,
        minWidth: "32em"
    });

    $(".delayembed").click(

    function () {
        createEmbed(
        eventID($(this)));
    }).hide().fadeIn(10000);

    $("#search").keydown(function () {
        filterEventsKeyDown();
    }).keyup(function () {
        filterEventsKeyUp("search", "centercontainer");
    });

    $("#wcbg > #language").change(

    function () {
        location.href = "?lang=" + $(this).val();
    });

    $(".eventicon img").error(function () {
        $(this).hide();
    });

}

$(document).ready(function () {
    documentReady();
});

