if (!window.console || !console.firebug) {
    var names = [
        "log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
        "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"
    ];

    window.console = {};
    for (var i = 0; i < names.length; ++i) {
        window.console[names[i]] = function() {};
    };
};

jQuery.fn.extend({
    /**
     * Checkbox (de)selektieren
     *
     */
    toggleChecked: function () {
        return this.each(function () {
            if(this.checked != undefined) {
                this.checked = !this.checked;
            }
        });
    },

    /**
     * Formularelement deaktivieren
     *
     */
    disable: function () {
        return this.each(function () {
            if(this.disabled != undefined) {
                this.disabled = true;
            }
        });
    },

    /**
     * Formularelement aktivieren
     *
     */
    enable: function () {
        return this.each(function () {
            if(this.disabled != undefined) {
                this.disabled = false;
            }
        });
    }
});

animationIsRuning = false;

var Marker = {
    ShowSuccess: function (message, link) {
        showBox('success', message, 1, link);
    },

    ShowError: function (message, link) {
        showBox('error', message, 3, link);
    }
};

function removeItem(id) {
    // Box ausgrauen
//    $j("#" + id).toggleClass("disabled");
    $j("#" + id).hide("slow");
    $j("#" + id + " :checkbox").toggleChecked();

//    // Bilder ausgrauen
//    $j("#" + id + " img").each(function () {
//        if($j("#" + id).hasClass("disabled")) {
//            $j(this).css("opacity", .50);
//        } else {
//            $j(this).css("opacity", 1);
//        }
//    });

    // Formular ausgrauen
    if($j(":checked").length == 0) {
        $j(".fields").hide("slow");
        $j(".Actions").hide("slow");
        $j(".fields :input").disable();
        $j(".Actions :submit").disable();
    } else {
        $j(".fields").show("slow");
        $j(".Actions").show("slow");
        $j(".fields :input").enable();
        $j(".Actions :submit").enable();
    }
};

function ajaxHandler(data) {
    try {
        eval(data);
        initAjaxLinks("#marker .flag");
    } catch(e) {
        console.error(e);
    }
};

function showBox(id, message, timeout, link) {
    if(window.animationIsRuning) {
        $j('#success').stop().hide();
        $j('#error').stop().hide();
    }
    window.animationIsRuning = true;

    // Position ermitteln
    if(link != null && link != undefined) {
        var offset = $j(link).offset();
        var top    = (offset && offset.top) ? offset.top : 221;
    } else {
        var top    = 221;
    }

    // Box formatieren
    $j("#" + id).css({
        "position": "absolute",
        "left": (($j(window).width() / 2) - ($j('#' + id).width() / 2)) + 'px',
        "top":  (top - ($j('#' + id).height() / 2)) + 'px',
        "z-index": 100,
        "opacity": .90
    });

    // Box einblenden
    $j("#" + id).text(message).fadeIn("slow", function () {
        (function () {
            $j("#" + id).fadeOut("slow", function () {
                window.animationIsRuning = false;
            });
        }).delay(timeout);
    });
};

function initAjaxLinks(selector) {
    $j(selector).click(function () {
        // Ajax Request ausführen
        $j.post(this.href + "&ajax=1", ajaxHandler.bind(this));
        return false;
    });
};

$j(function () {
    initAjaxLinks(".flag");

    if(String(location.search).indexOf("getMarkedProducts") != -1) {
        $j(".operations").each(function () {
            $j(this).show();
        });
    }
});
