var Item = function() {
    this.construct.apply(this, arguments);
};

Item.WRAPPER_HTML = "<div id=\"innerContentWrapper\">";

Item.reg = new Object();
Item.topZ = 20;
Item.frontId = null;
Item.doubleclicked = false;

Item.getId = function(elem) {
    return elem.id.substring(0, elem.id.indexOf("_"));
};


Item.dblOnTitle = function() {
    var id = Item.getId(this);
    var url;
    var item;
//    if (id.indexOf("ps3") == 0 || id == "buehne") {
//        item = Item.reg["buehne"];
//        item.bringToFront();
//    }
    if (id == "cd") {
        item = Item.reg["buehne"];
        item.bringToFront();
        url = "buehne/politsatire3_cd.html";
    }
    else {
        item = Item.reg[id];
    }
    if (!item) return;
    Item.doubleclicked = true;

    if (!item.isOpened) {
        item.showContent(url);
    }
    else {
        item.hideContent();
    }
};


Item.downOnContent = function(evt) {
    var id = Item.getId(this);
    Util.cancelEvent(evt);
    var item = Item.reg[id];
    item.bringToFront();
};


Item.loadContent = function(id, url) {
    var item = Item.reg[id];
    item.loadContent(url);
};

Item.hint = function() {
    if (Item.doubleclicked) return;
    Item.showHint();
    window.setTimeout(Item.hideHint, 100);
    window.setTimeout(Item.showHint, 150);
    window.setTimeout(Item.hideHint, 250);
    window.setTimeout(Item.hint, 8500);
};

Item.showHint = function() {
    var item = Item.reg["doppelklick"];
    item.title.style.display = "block";
};

Item.hideHint = function() {
    var item = Item.reg["doppelklick"];
    item.title.style.display = "none";
};

Item.prototype.construct = function(id) {
    this.id = id;
    this.title = document.getElementById(id + "_title");

//    if (id == "thiel" || id.indexOf("ps3") == 0 || id == "doppelklick" ) {
    if (id == "thiel" || id == "doppelklick" || id == "cd") {
        Drag.enableDragging(this.title, null, this);
        if (id == "doppelklick") {
            var title = this.title;
            title.ondblclick = function() {
                title.style.display = "none";
            };
        }
//        else if (id.indexOf("ps3") == 0) {
        else if (id.indexOf("cd") == 0) {
            this.title.ondblclick = Item.dblOnTitle;
        }
    }
    else {
        this.image1 = document.getElementById(id + "_image_1");
        this.image2 = document.getElementById(id + "_image_2");
        this.link = document.getElementById(id + "_link");
        this.contentWrapper = document.getElementById(id + "_contentWrapper");
        this.content = document.getElementById(id + "_content");

        var url = document.getElementById(id + "_url");
        this.url = url ? url.value : id + ".html";

        this.isOpened = false;
        this.isLoaded = false;

        this.title.ondblclick = Item.dblOnTitle;
        this.content.onmousedown = Item.downOnContent;

        if (id != "cartoons") {
            Drag.enableDragging(this.title, this.contentWrapper, this);
            Drag.enableDragging(this.contentWrapper, this.title, this);
        }
    }

    Item.reg[id] = this;
};


Item.prototype.dye = function() {
    if (!this.image2) return;
    this.image1.style.display = "none";
    this.image2.style.display = "";
};


Item.prototype.black = function() {
    this.image1.style.display = "";
    this.image2.style.display = "none";
};


Item.prototype.bringToFront = function() {
    if (Item.frontId == this.id) return;
    if (this.contentWrapper) {
        this.contentWrapper.style.zIndex = Item.topZ++;
    }
    if (this.id != "cartoons") {
        this.title.style.zIndex = Item.topZ++;
        Rocket.rocket.style.zIndex = Item.topZ++;
    }
    Item.frontId = this.id;
};


Item.prototype.showContent = function(url) {
    if (!this.isLoaded) {
        var item = this;
        var f = function() {
            item.loadContent(url ? url : item.url);
        };
        window.setTimeout(f, 1);
        this.isLoaded = true;
    }
    else {
        if (url) this.loadContent(url);
    }
    this.dye();
    this.contentWrapper.style.display = "block";
    if (this.link) this.link.blur();
    this.isOpened = true;
};


Item.prototype.loadContent = function(url) {
    try {
        if (url.indexOf(".gif") != -1) {
            this.content.innerHTML = '<img src="' + url + '">';
        }
        else {
            var html = Util.getHTTP(url);
            var start = html.indexOf(Item.WRAPPER_HTML) + Item.WRAPPER_HTML.length;
            var end = html.lastIndexOf("</div>");
            html = html.substring(start, end);
            this.content.innerHTML = html;
        }
    }
    catch (e) {

    }
};


Item.prototype.hideContent = function() {
    this.black();
    this.contentWrapper.style.display = "none";
    if (this.link) this.link.blur();
    this.isOpened = false;
};


