/*
 * FOTOSOESIN - Javascript
 *
 * Copyright (c) 2008-2010 Jeamy Lee
 * Licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 */
Desktop = function(){
    var setMenuStyle = function(desktop, item){
        $j(desktop.lastMenu).css({
            color: menu_gray
            //color: "yellow"
        //            textDecoration: "line-through"
        });
        $j(item).css({
            color: menu_active_main,
            textDecoration: "none"
        });
        desktop.lastMenu = item;
    };
    var buildMenu = function(fields, parent){
        $j.each(fields, function(i, items) {
            $j.each(items, function(j, item) {
                itemId = "menu" + app.menuId++;
                if (item.sticky == ITEM_GRAY) {
                    tpl = "<div class=\"menu-item\" id=\"" + itemId + "\">" + item.titel + ", </div>";
                }
                else {
                    tpl = "<div class=\"menu-item-red\" id=\"" + itemId + "\">" + item.titel + ": </div>";
                }
                $j(tpl).appendTo(parent);
                if (item.sticky == ITEM_GRAY) {
                    $j("#" + itemId).attr({
                        value: item.id
                    }).click(function(){
                        var type = item.type;
                        app.loadPage({
                            type : type,
                            id: $j("#" + this.id).attr("value")
                        }, JOB);
                        setMenuStyle(app, this);
                    });
                }
            });
        });
    };
    return {
        lastStyle: "desktop",
        items: new Items({
            output:"#output-main",
            navigation:"#output-top-nav",
            navigationPost:"",
            setUrl: GET_ITEMS,
            setType: SET,
            set:new Items({
                infoNav:true,
                output:"#output-main",
                navigation:"#output-bottom-nav",
                navigationPost:"set-"
            })
        }),
        menuIndex: 0,
        menuMaxIndex: 6,
        menuId: 0,
        lastProjectId: undefined,
        lastProjectMenu: undefined,
        lastMenu: undefined,
        loadData: function(desktop){
            $j("#nav-panel").click(function(){
                app.loadPage(app, START);
            });
            $j("#nav-panel").hover(function(){
                $j("#menu-panel").css({
                    visibility:"visible",
                    zIndex:"1000"
                }).animate({
                    width:940
                }, {
                    duration:100
                });
            }, function(){
            });
  
            //create menu output layer
            $j("<div id=\"menu-out\"></div>\n").appendTo("#menu-panel");
            desktop.loadMenu(desktop);
            $j("#menu-panel").hover(function(){
            }, function(){
                $j("#menu-panel").animate({
                    width:0
                }, {
                    duration:100
                }).css({
                    visibility:"hidden"
                });
            });
            app.loadPage(app, START);
        },
        loadPage: function(data, type){
            app.setVisibility(type);
            switch (type) {
                case START:
                    app.emptyPage(app);
                    $j("#output-main").html("<img class=\"start_image\" src=\"start_bild.jpg\" alt=\"start bild\"/>");
                    $j("#output-panel").css({
                        visibility: "visible"
                    });
                    break;
                case INFO:
                    app.loadInfoPage(app, INFO);
                    break;
                case CLIENTS:
                    app.loadInfoPage(app, CLIENTS);
                    break;
                case PARTNERS:
                    app.loadInfoPage(app, PARTNERS);
                    break;
                case CONTACT:
                    app.loadInfoPage(app, CONTACT);
                    break;
                case JOB:
                    app.loadJobPage(app, data);
                    break;
            }
        },
        emptyPage: function(){
            $j("#output-top-nav").empty();
            $j("#output-main").empty();
            $j("#output-bottom-nav").empty();
            $j("#info-panel").empty();
        },
        setVisibility: function(status){
            $j("#menu-panel").css({
                visibility:"hidden"
            });
            $j("#info-panel").css({
                visibility: "hidden"
            });
            $j("#output-panel").css({
                visibility: "hidden"
            });
        },
        loadMenu: function(desktop){
            $j("#menu-out").empty();
            
            $j.getJSON(GET_MENU, function(data){
                // get menu data
                var works = data['works'];
                var exhibitions = data['exhibitions'];
                var std_menu = data['std_menu'];

                // standard items, mouse clicks
                $j("<div id=\"menu-info\"></div>\n").appendTo("#menu-out");
                var itemId = "menu" + desktop.menuId++;
                var tpl = "<div class=\"menu-item-main\" id=\"" + itemId + "\">" + std_menu.info + "</div>";
                $j(tpl).appendTo("#menu-info");
                $j("#" + itemId).click(function(){
                    desktop.loadPage(desktop.menuId, INFO);
                    setMenuStyle(desktop, this);
                });

                //build WORKS
                $j("<div id=\"menu-works\"><div id=\"menu-works-main\"></div>\n</div>\n").appendTo("#menu-out");
                itemId = "menu" + desktop.menuId++;
                tpl = "<div class=\"menu-item-main\" id=\"" + itemId + "\">" + std_menu.works + "</div>";
                $j(tpl).appendTo("#menu-works-main");
                $j("<div id=\"menu-works-out\"></div>\n").appendTo("#menu-works");
                buildMenu(works, "#menu-works-out");
                $j("#menu-works-main").height($j("#menu-works-out").height());
                
                //build EXHIBITIONS
                $j("<div id=\"menu-exhibitions\"><div id=\"menu-exhibitions-main\"></div>\n</div>\n").appendTo("#menu-out");
                itemId = "menu" + desktop.menuId++;
                tpl = "<div class=\"menu-item-main\" id=\"" + itemId + "\">" + std_menu.exhibitions + "s</div>";
                $j(tpl).appendTo("#menu-exhibitions-main");
                $j("<div id=\"menu-exhibitions-out\"></div>\n").appendTo("#menu-exhibitions");
                buildMenu(exhibitions, "#menu-exhibitions-out");
                $j("#menu-exhibitions-main").height($j("#menu-exhibitions-out").height());
                
                //build CLIENTS
                itemId = "menu" + desktop.menuId++;
                $j("<div id=\"menu-clients\"></div>\n").appendTo("#menu-out");
                tpl = "<div class=\"menu-item-main\" id=\"" + itemId + "\">" + std_menu.clients + "</div>";
                $j(tpl).appendTo("#menu-clients");
                $j("#" + itemId).click(function(){
                    desktop.loadPage(desktop.menuId, CLIENTS);
                    setMenuStyle(desktop, this);
                });

                //build PARTNERS
                itemId = "menu" + desktop.menuId++;
                $j("<div id=\"menu-partners\"></div>\n").appendTo("#menu-out");
                tpl = "<div class=\"menu-item-main\" id=\"" + itemId + "\">" + std_menu.partners + "</div>";
                $j(tpl).appendTo("#menu-partners");
                $j("#" + itemId).click(function(){
                    desktop.loadPage(desktop.menuId, PARTNERS);
                    setMenuStyle(desktop, this);
                });
                
                //build CONTACT
                itemId = "menu" + desktop.menuId++;
                $j("<div id=\"menu-contact\"></div>\n").appendTo("#menu-out");
                tpl = "<div class=\"menu-item-main\" id=\"" + itemId + "\">" + std_menu.contact + "</div>";
                $j(tpl).appendTo("#menu-contact");
                $j("#" + itemId).click(function(){
                    desktop.loadPage(desktop.menuId, CONTACT);
                    setMenuStyle(desktop, this);
                });
            });
        },
        loadInfoPage: function(desktop, type){
            desktop.emptyPage();
            $j.getJSON(GET_ITEMS, {
                type: type
            }, function(data){
                if (data != "") {
                    switch(type) {
                        case CLIENTS:
                            $j("#info-panel").css({
                                width: "1000px"
                            });
                            var columns = data.split("###BREAK###");
                            var html = "<table border=\"0\"><tr><colgroup><col width=\"300\"><col width=\"300\"></colgroup>";
                            for(var i=0; i<columns.length; i++) {
                               html += "<td valign=\"top\">" + columns[i] + "</td>";
                            }
                            html += "</tr></table>";
                            $j("#info-panel").html(html);
                        break;
                        default:
                            $j("#info-panel").css({
                                width: "500px"
                            });
                            $j("#info-panel").html(data);
                        break;
                    }
                    $j("#info-panel").css({
                        visibility: "visible"
                    });
                }
            });
        },
        loadJobPage: function(desktop, data){
            desktop.emptyPage();
            $j.getJSON(GET_ITEMS, {
                type: JOB,
                id: data.id
            }, function(list){
                if (list == undefined || list.length == 0) {
                    return;
                }
                // id of memu item
                list.menuid = data.id;
                // type of menu item (works, exhibitions)
                list.type = data.type;
                desktop.items.setList(list);
                desktop.items.setNav(app, -1);
                desktop.items.getItem(0);
                $j("#output-panel").css({
                    visibility: "visible"
                });
            });
        },
        loadItemsCompanyPage: function(desktop, id){
            $j.getJSON(GET_ITEMS_PROJECTS, function(data){
                desktop.viewItems(desktop, data);
            });
        },
        lastDummy: function(){
        // dedicated to the IE comma "bug" ;)
        }
    }
    
};
function Items(description){
    var desk = this;
    var lastNavId = undefined;
    var itemList = undefined;
    var index = 0;
    var infoItemId = 1000000;
    var getImgHtml = function(id){
        if(itemList[id] == undefined || itemList[id].link == undefined) {
            return "";
        }
        return "<img id=\"imageDetailId\" class=\"detail_image\" src=\"" + PROJECT_ROOT + itemList[id].link + "/" + itemList[id].image + "\">";
    };
    var addInfoHtml = function(){
        $j(description.output).empty();
        var tpl = "<div id=\"nav-info\" class=\"info-text\">" + 
        itemList.infoItem.text.replace(/&lt;br&gt;/g, "<br>") + "</div>\n";
        $j(tpl).appendTo(description.output);
    };
    var addHtmlToPage = function(tpl){
        $j(description.output).empty();
        $j(tpl).appendTo(description.output);
        $j("#imageDetailId").click(function(){
            desk.getNext();
        });
    };
    var setActiveNav = function(id){
        if(description.set != undefined){
            getSet(id);
        }
        if (lastNavId != undefined) {
            $j(lastNavId).css({
                color: menu_gray
            });
        }
        $j("#nav-" + description.navigationPost + id).css({
            color: menu_active
        });
        lastNavId = "#nav-" + description.navigationPost + id;
    };
    var getSet = function(id){
        if (itemList.type == MENU_EXHIBITIONS || itemList.length == 0) {
            //there is no need to create a set
            return;
        }
        $j.getJSON(description.setUrl, {
            type: description.setType,
            id: itemList[id].id
        }, function(data){
            description.set.setList(data);
            description.set.setNav(desk, id);
        });
    };
    return {
        emptyNavigation: function(){
            $j(description.navigation).empty();
        },
        setNav: function(parent, isSet){
            $j(description.navigation).empty();
            if(itemList.length == 0) {
                return;
            }
            lastNavId = undefined;
            if (itemList.length > 1) {
                var tpl = "<div id=\"nav-left" + description.navigationPost + "\" class=\"menu-nav\">&lt;</div><div class=\"menu-nav-spacer\">&nbsp;</div>\n";
                tpl += "<div id=\"nav-right" + description.navigationPost + "\" class=\"menu-nav\">&gt;</div><div class=\"menu-nav-spacer\">&nbsp;</div>\n";
                $j(tpl).appendTo(description.navigation);
                $j("#nav-left" + description.navigationPost).click(function(){
                    if (isSet != -1) {
                        $j("#nav-all" + description.navigationPost).css({
                            color: menu_gray
                        });
                    }
                    desk.getPrev();
                });
                $j("#nav-right" + description.navigationPost).click(function(){
                    if (isSet != -1) {
                        $j("#nav-all" + description.navigationPost).css({
                            color: menu_gray
                        });
                    }
                    desk.getNext();
                });
            }else {
                tpl = "<div id=\"nav-leftd\" class=\"menu-nav\">&nbsp;</div><div class=\"menu-nav-spacer\">&nbsp;</div>\n";
                tpl += "<div id=\"nav-rightd\" class=\"menu-nav\">&nbsp;</div><div class=\"menu-nav-spacer\">&nbsp;</div>\n";
                $j(tpl).appendTo(description.navigation);
            }
            if(itemList.infoItem == undefined) {
                itemList.infoItem = {
                    id: -1,
                    text: "Infotext",
                    index: -1
                };
                // get info text, rebuild item list
                var bx = 0;
                var b = new Array();
                for (var i = 0; i < itemList.length; i++) {
                    if(itemList[i].type == ITEM_INFO){
                        itemList.infoItem.id = itemList[i].id;
                        itemList.infoItem.text = itemList[i].text;
                    }else{
                        var value = itemList[i];
                        if(value != undefined)
                        {
                            b[bx++] = value;
                        }
                    }
                }
                b['menuid'] = itemList['menuid'];
                b['type'] = itemList['type'];
                b['infoItem'] = itemList['infoItem'];
                delete itemList;
                itemList = b;
            }
            //build html
            tpl = "";
            for (i = 0; i < itemList.length; i++) {
                tpl += "<div id=\"" + ("nav-" + description.navigationPost + i) + "\" class=\"menu-nav\">" + (i+1) + "</div>\n";
            }

//            if(itemList.type == MENU_EXHIBITIONS && itemList.infoItem.id != -1) {
            if((itemList.type == MENU_EXHIBITIONS || isSet!=-1) && 
                itemList.infoItem.id != -1 && itemList.infoItem.text != ""
                && itemList.infoItem.text != "Infotext" && itemList.infoItem.text != undefined) {
                // exhibition needs an info link (info text)
                // set info link (info text)
                tpl = "<div id=\"" + ("nav-" + infoItemId) + "\" class=\"menu-nav\">info</div>\n" + tpl;
            }
					
            if (tpl != "") {
                if (isSet!=-1) {
                    tpl = "<div id=\"" + ("nav-all" + description.navigationPost) + "\" class=\"menu-nav\">all</div><div class=\"menu-nav-spacer\">&nbsp;</div>\n" + tpl;
                    index = -1;
                }
                $j(tpl).appendTo(description.navigation);
                for (i = 0; i < itemList.length; i++) {
                    $j("#nav-" + description.navigationPost + i).attr({
                        value: i
                    }).click(function(){
                        if (isSet != -1) {
                            $j("#nav-all" + description.navigationPost).css({
                                color: menu_gray
                            });
                        }
                        desk.getItem($j("#" + this.id).attr("value"));
                    });
                }
//                if (itemList.type == MENU_EXHIBITIONS && itemList.infoItem.id != -1) {
                if((itemList.type == MENU_EXHIBITIONS || isSet!=-1)  &&
                itemList.infoItem.id != -1 && itemList.infoItem.text != ""
                && itemList.infoItem.text != "Infotext" && itemList.infoItem.text != undefined) {
                    $j("#nav-" + infoItemId).click(function(){
                        addInfoHtml();
                    });
                }
                if (isSet != -1) {
                    $j("#nav-all" + description.navigationPost).css({
                        color: menu_active
                    }).attr({
                        value: isSet
                    }).click(function(){
                        parent.getItem($j("#" + this.id).attr("value"));
                    });
                }
            }
        },
        setList: function(list){
            delete itemList;
            itemList = list;
            index = 0;
            desk = this;
        },
        getNext: function(){
            if (itemList == undefined) {
                return -1;
            }
            if (index == itemList.length - 1) {
                index = -1;
            }
            index++;
            var tpl = getImgHtml(index);
            addHtmlToPage(tpl);
            setActiveNav(index);
            return itemList.length - 1;
        },
        getPrev: function(){
            if (index == 0) {
                index = itemList.length;
            }
            index--;
            var tpl = getImgHtml(index);
            addHtmlToPage(tpl);
            setActiveNav(index);
        },
        getItem: function(id){
            if (id == undefined || id < 0 || id >= itemList.length) {
                id = 0;
            }
            index = id;
            var tpl = getImgHtml(index);
            addHtmlToPage(tpl);
            setActiveNav(index);
        },
        clear: function(){
            delete itemList;
        },
        length: function(){
            return itemList.length;
        }
    };
}


