var Portfolio = Class.create();
Portfolio.prototype =
{
    initialize: function(content, container, itemContainer) {
        this.content = content;
        this.containerId = container;
        this.container = $(container);
        this.pagerContainer = $("PortfolioPager");
        this.listContainer = $("pListContainer");
        this.yearListContainer = $("portfolioYearList");
        this.itemContainer = itemContainer;
        this.infoContainer = null;
        this.commentContainer = null;
        if (content != undefined)
            this.fullCount = content["GetItemListResult"]["@Count"];
        this.curItem = 1;
        this.curPage = 1;
        this.maxCount = 12;
        this.startYear = 2002;
        this.endYear = new Date().getFullYear();
        this.activeYear = null;
        this.part = "Web-сайты";
        this.partId = 1;
        this.sphere = null;
        this.sphereId = null;
        this.client = null;
        this.clientId = null;
        this.work = null;
        this.workId = null;
        this.settings = null;
        this.lastIndex = 6;
        this.floatDiv;
        this.pageCount = Math.ceil(this.fullCount / this.maxCount);
        this.applicationRoot = applicationRoot || "/";
        this.browser = new Browser();
        this.blocks = [{ id: 0, container: "PortfolioPartDiv", category: "itemList", params: { folderId: 0, catalogId: 9 }, moduleName: "PortfolioMenu", itemsCount: 1, orderSettings: [{ column: "ListOrder", rule: "ASC"}] },
 { id: 1, container: "PortfolioSphereDiv", category: "itemList", params: { folderId: 0, catalogId: 10 }, moduleName: "PortfolioMenu", itemsCount: 1, loadLinkedItems: false },
 { id: 2, container: "PortfolioClientDiv", category: "itemList", params: { folderId: '', catalogId: 7 }, moduleName: "PortfolioMenu", itemsCount: 1, loadLinkedItems: false, orderSettings: [{ column: "Name", rule: "ASC", isAttribute: false }, { column: "Date", rule: "DESC"}], propertyList: [] },
 { id: 3, container: "PortfolioWorksDiv", category: "itemList", params: { folderId: '', catalogId: 12 }, moduleName: "PortfolioMenu", itemsCount: 1, loadLinkedItems: false, orderSettings: [{ column: "ShippingDate", rule: "DESC"}], propertyList: [] }
 ];
    },
    start: function() {
        this.pagerContainer.clear();
        this.loadPager = new ListPager(this.curPage, this.pageCount, this.pagerContainer, this.loadPage.bind(this));
        this.loadYearNav();
        this.portfolioMenu = new PortfolioMenu(this);
        this.initImages(this.content);
    },
    reloadPortfolio: function(menu) {
        this.part = menu.part;
        this.partId = menu.partId;
        this.sphere = menu.sphere;
        this.sphereId = menu.sphereId;
        this.client = menu.client;
        this.clientId = menu.clientId;
        this.activeYear = menu.year;
        this.loadPage(this, 1);
    },
    initImages: function(content) {
        this.container.clear();
        $(this.itemContainer).style.visibility = "hidden";
        var itemList = content["GetItemListResult"];

        if (this.fullCount == 0) {
            this.container.innerHTML = "По указанными критериям фильтра работ не найдено.";
            return;
        }
        if (itemList["Item"].length != null) {
            for (var i = 0; i < itemList["Item"].length; i++)
                this.addItem(itemList["Item"][i], i);
        }
        else
            this.addItem(itemList["Item"], 0);

    },
    addItem: function(item, index) {
        var props = item["Properties"]["Property"];
        var title = item["Name"];
        var type = item["Description"];
        var imageSrc = (props["Value"] != null) ? this.applicationRoot + props["Value"]/* + "&w=124&h=89&crop=1"*/ : "/Images/blank.gif";
        var div = document.createElement("div");
        var img = document.createElement("img");
        img.src = imageSrc;
        if (props["Value"] == null) {
            img.width = 124;
            img.height = 89;
        }
        img.onclick = this.callInfo.bindAsEventListener(this, item["ID"]);
        div.appendChild(img);
        if (title != undefined) {
            var a = document.createElement("a");
            a.href = "javascript:void(0)";
            a.appendChild(document.createTextNode(title));
            div.appendChild(a);
            a.onclick = this.callInfo.bindAsEventListener(this, item["ID"]);
        }
        if (type != undefined) {
            div.appendChild(document.createElement("br"));
            div.appendChild(document.createTextNode(type));
        }
        this.container.appendChild(div);
    },
    callInfo: function(evt, itemId) {
        $(this.itemContainer).clear();
        $(this.itemContainer).innerHTML = "<img src='/Images/preload.gif' style='margin: 170px 0 0 250px;' alt=''/>";
        $(this.itemContainer).style.visibility = "visible";
        this.listContainer.style.visibility = "hidden";
        $("RubricsPreloaderDiv").style.display = "block";
        var block = { category: "item", params: { id: itemId, folderId: '', catalogId: 12} };
        var pBlock = new Block(block);
        pBlock.getItem();
        pBlock.blockCallback = this.initInfo.bind(this);

    },
    initInfo: function(content) {
        var item = content["GetItemResult"];
        this.showInfo(this, item, true);
        this.setAsViewed(item["ID"]);
        init_document(); //Инициализация элементов с классом thickbox


    },
    setAsViewed: function(id) {
        var args = {
            method: "post",
            parameters: "item=" + id + "&nocache=" + Math.random(1048576),
            onSuccess: this.onSetAsViewed.bind(this),
            onFailure: this.onError.bind(this)
        };
        var ar = new Ajax.Request(this.applicationRoot + "/counters/", args);
    },
    onSetAsViewed: function(request, headers) {
        /*alert(request.responseText);*/
    },
    onError: function() {
        alert(e.message);
    },
    loadPage: function(evt, index) {	
        this.curPage = index;
        $(this.itemContainer).style.visibility = "hidden";
        this.listContainer.style.visibility = "visible";
        this.container.clear();
        this.container.innerHTML = "<img src='/Images/preload.gif' style='margin: 170px 0 0 250px;' alt=''/>";
        this.pagerContainer.clear();
        var start = (index == 1) ? 1 : index * this.maxCount - this.maxCount + 1;
        var block = { category: "itemList", params: { folderId: '', catalogId: 12 }, itemsCount: this.maxCount, startIndex: start, orderSettings: [{ column: "ShippingDate", rule: "DESC"}], propertyList: ["MainImage"] };
        block.filter = [];
        var date1;
        var date2;
        if (this.activeYear != null) {
            var date1 = "01.01." + this.activeYear;
            var date2 = (this.activeYear == this.startYear) ? "01.01." + parseInt(this.endYear - 2) : "01.01." + (parseInt(this.activeYear) + 1);
        }
		
		/*if(this.part != null && this.activeYear != null){
			this.part = null;
		}
		if(this.sphere != null && this.activeYear != null){
			this.sphere = null;
		}*/
		if(this.client != null && this.activeYear != null){
			this.client = null;
			this.clientId = null;
		}
		
		jQuery('#screenshot_container').hide();
		
        block.filter[0] = (this.activeYear != null) ? { column: "ShippingDate", value: date1, condition: "GreaterOrEqual"} : null;
        block.filter[1] = (this.activeYear != null) ? { column: "ShippingDate", value: date2, condition: "Less"} : null;
        block.filter[2] = (this.part != null) ? { column: "Section_ItemID", value: this.partId, condition: "Equal"} : null;
        block.filter[3] = (this.sphere != null) ? { column: "Sphere_ItemID", value: this.sphereId, condition: "Equal"} : null;
        block.filter[4] = (this.client != null) ? { column: "Client_ItemID", value: this.clientId, condition: "Equal"} : null;
		
        
		var portfolioBlock = new Block(block);
        portfolioBlock.getItemList();
        portfolioBlock.blockCallback = this.newYearList.bind(this, index);


    },
    loadYearNav: function() {
        this.yearListContainer.clear();
        var font = document.createElement("font");
        var a1 = document.createElement("a");
        a1.href = "javascript:void(0)";
        a1.appendChild(document.createTextNode("2002 ... " + (this.endYear - 3)));
        font.appendChild(a1);
        font.appendChild(document.createTextNode(" / "));
        a1.onclick = this.reloadList.bindAsEventListener(this, 2002);
        if (this.activeYear == 2002)
            Element.addClassName(a1, "active");
        for (var i = this.endYear - 2; i <= this.endYear; i++) {
            var a = document.createElement("a");
            a.href = "javascript:void(0)";
            a.appendChild(document.createTextNode(i));
            font.appendChild(a);
            font.appendChild(document.createTextNode(" / "));
            if (i == this.activeYear){
                Element.addClassName(a, "active");
				//if(this.work != null){
					a.onclick = this.backToPortfolio.bindAsEventListener(this);
				//}
			}else{
				a.onclick = this.reloadList.bindAsEventListener(this, i);
			}
        }
        /*ссылка За все время*/
        var span = document.createElement("span");
        var link = document.createElement("a");
        link.href = "javascript:void(0)";
        link.appendChild(document.createTextNode("За все время"));
        if (this.activeYear == null)
            Element.addClassName(link, "active");
        span.appendChild(link);
        font.appendChild(span);
        link.onclick = this.reloadList.bindAsEventListener(this, null);
        this.yearListContainer.appendChild(font);
        /*сброс настроек*/
        var b = document.createElement("b");
        var textImg = document.createElement("img");
        textImg.src = "/Images/attention1.gif";
        var link2 = document.createElement("a");
        link2.onmouseover = this.replaceImage.bindAsEventListener(this, textImg, "attention.gif");
        link2.onmouseout = this.replaceImage.bindAsEventListener(this, textImg, "attention1.gif");
        link2.href = "javascript:void(0)";
        link2.appendChild(document.createTextNode("Начать сначала!"));
        b.appendChild(document.createTextNode("Заблудились?"));
        b.appendChild(textImg);
        b.appendChild(link2);
        this.yearListContainer.appendChild(b);
        link2.onclick = this.clearFilter.bindAsEventListener(this);
    },
    replaceImage: function(evt, img, source) {
        img.src = "/Images/" + source;
    },
    reloadList: function(evt, year) {
        this.client = null;
        this.work = null;
		this.sphere = null;

        var last = this.activeYear;
        this.activeYear = year;
        if (last != year || (last == year && this.work != null)) {
            this.loadPage(this, 1);
            $("RubricsPreloaderDiv").style.display = "block";
            this.portfolioMenu = new PortfolioMenu(this);
        }
    },
    newYearList: function(index, container, content) {
        this.fullCount = content["GetItemListResult"]["@Count"];
        this.curPage = index;
        this.curItem = index;
        this.loadYearNav();
        this.initImages(content);
        if (this.fullCount == 0)
            return;
        this.pageCount = Math.ceil(this.fullCount / this.maxCount);
        this.pagerContainer.clear();
        this.loadPager = new ListPager(this.curPage, this.pageCount, this.pagerContainer, this.loadPage.bind(this));
    },
    sortArray: function(itemList) {

        var listItems = [];
        var newListItems = [];
        var inFormat = true;

        if (itemList.length == undefined) {
            var props = itemList["Properties"]["Property"];


            var im = (props[1]["Value"] != null) ? props[1]["Value"] : "/Images/blank.gif";
            var comm = (props[0]["Value"] != null) ? props[0]["Value"] : "";
            var t = (props[2]["Value"] != null) ? props[2]["Value"] : "";
            listItems.push({ name: itemList["Name"], comment: comm, image: im, text: t });
            return listItems;
        }
        else {
            for (var i = 0; i < itemList.length; i++) {
                var props = itemList[i]["Properties"]["Property"];
                //alert(props[3]["Value"]);

                var im = (props[1]["Value"] != null) ? props[1]["Value"] : "/Images/blank.gif";
                var im_detail = props[3]["Value"];

                var comm = (props[0]["Value"] != null) ? props[0]["Value"] : "";
                var t = (props[2]["Value"] != null) ? props[2]["Value"] : "";
                listItems.push({ name: itemList[i]["Name"], comment: comm, image: im, text: t, image_detail: im_detail });
            }
            for (var i = 0; i < listItems.length; i++) {
                if (isNaN(parseInt(listItems[i].name.charAt(0)))) {
                    inFormat = false;
                    break;
                }
            }
            if (inFormat) {
                newListItems = [];
                for (var i = 0; i < listItems.length; i++) {
                    var j;
                    j = parseInt(listItems[i].name.charAt(0));
                    if (!isNaN(parseInt(listItems[i].name.charAt(1)))) {
                        var h = j.toString();
                        j = parseInt(h + listItems[i].name.charAt(1));
                    }
                    newListItems[j] = listItems[i];
                }
                var newListItems2 = [];
                for (var i = 0; i < newListItems.length; i++) {
                    if (newListItems[i] != undefined && newListItems[i] != null) {
                        var elm = newListItems[i];
                        newListItems2.push(elm);
                    }
                }
                return newListItems2;
            }
            else
                return listItems;
        }
    },
    backToPortfolio: function() {
		if(this.work==null){return;}
        this.work = null;
        this.client = this.settings.client;
        this.sphere = this.settings.sphere;
        this.part = this.settings.part;
        this.clientId = this.settings.clientId;
        this.sphereId = this.settings.sphereId;
        this.partId = this.settings.partId;
        this.portfolioMenu = new PortfolioMenu(this);
        this.loadPage(this, this.curPage);
    },
    showInfo: function(evt, curItem, firstLoad) {
        $(this.itemContainer).clear();

        Element.removeClassName($(this.itemContainer), "shotsList");
        this.curFotoPage = null;
        this.curItem = 1;
        this.lastIndex = 6;
        var props = curItem["Properties"]["Property"];
        var title = curItem["Name"];
        var client = props[0]["Value"]["Properties"]["Property"][10]["Value"];
        var workType = curItem["Description"];
        var type = props[1]["Value"];
        var link = props[3]["Value"];
        var linkText = props[2]["Value"];
        var images = props[7]["Value"];

        var mainImageSrc = (props[6]["Value"] != null) ? this.applicationRoot + props[6]["Value"] : "/Images/blank.gif";
        var itemImages = this.sortArray(images["Item"]);

        this.itemImages = new Array();

        if (firstLoad) {
            this.settings = { part: this.part, partId: this.partId, sphereId: this.sphereId, sphere: this.sphere, client: this.client, clientId: this.clientId };

            if (props[5]["Value"] != null) {
                this.partId = props[5]["Value"]["ID"];
                this.part = props[5]["Value"]["Name"];
            }
            if (props[9]["Value"] != null) {
                this.sphereId = props[9]["Value"]["ID"];
                this.sphere = props[9]["Value"]["Name"];
            }
            if (props[0]["Value"] != null) {
                this.clientId = props[0]["Value"]["ID"];
                this.client = props[0]["Value"]["Name"];
            }
            this.workId = curItem["ID"];
            this.work = title;
            var menu = new PortfolioMenu(this);
        }
        var h2 = document.createElement("h2");
        if (props[8]["Value"]["@Count"] != 0) {
            var iconDiv = document.createElement("span");
            var iconA = document.createElement("a");
            iconA.href = "javascript:void(0)";
            Element.addClassName(iconDiv, "iconDiv");
            iconA.appendChild(document.createTextNode("Как мы это сделали?!"));
            iconA.onclick = this.showProcess.bindAsEventListener(this, curItem);
            iconDiv.appendChild(iconA);
            h2.appendChild(iconDiv);
        }
        if (title != null)
            h2.appendChild(document.createTextNode(title));
        $(this.itemContainer).appendChild(h2);
        var h4 = document.createElement("h4");
        if (workType != null)
            h4.appendChild(document.createTextNode(workType));
        $(this.itemContainer).appendChild(h4);
        this.commentContainer = null;
        var infoDiv = document.createElement("div");
        Element.addClassName(infoDiv, "infoDiv");
        var shotsDiv = document.createElement("div");
        var fotoDiv = document.createElement("div");
        shotsDiv.appendChild(fotoDiv);
        var commentDiv = document.createElement("b");
        this.infoContainer = commentDiv;
        var pagerDiv = document.createElement("div");
        Element.addClassName(pagerDiv, "pager");
        var pDiv = document.createElement("div");
        var table1 = document.createElement("table");
        var tbody1 = document.createElement("tbody");
        var tr1 = document.createElement("tr");
        this.pageContainer = pagerDiv;
        this.pager = table1;
        var count = (images["@Count"] == 0) ? 1 : parseInt(images["@Count"]);
        for (var i = 1; i <= count; i++) {
            var pg = document.createElement("a");
            pg.href = "javascript:void(0)";
            pg.appendChild(document.createTextNode(i));
            var td1 = document.createElement("td");
            td1.appendChild(pg);
            var item = { element: pg, props: props, index: i - 1 };
            if (images["@Count"] != 0)
                item.images = itemImages[i - 1];
            if (i == 1) {
                this.curFotoPage = item.element;
                Element.addClassName(this.curFotoPage, "active");
            }
            this.itemImages.push(item);
            if (count != 1)
                pg.onclick = this.loadItemImage.bindAsEventListener(this, item, false);
            tr1.appendChild(td1);
        }
        tbody1.appendChild(tr1);
        table1.appendChild(tbody1);
        pDiv.appendChild(table1);
        pagerDiv.appendChild(pDiv);
        shotsDiv.appendChild(pagerDiv);
        var clientDiv = document.createElement("h5");
        clientDiv.appendChild(document.createTextNode("Клиент"));
        infoDiv.appendChild(clientDiv);
        var backDiv = document.createElement("b");
        Element.addClassName(backDiv, "backDiv");
        var backImg = document.createElement("img");
        backImg.src = this.applicationRoot + "/Images/back.png";
        backDiv.appendChild(backImg);
        backImg.onclick = this.backToPortfolio.bindAsEventListener(this);
        backDiv.appendChild(document.createTextNode("вернуться"));
        var div3 = document.createElement("div");
        div3.setAttribute("class", "client");
        infoDiv.appendChild(div3);
        div3.style.height = "30px";
        if (client != null) {
            div3.style.overflow = "hidden";
            div3.innerHTML = client;
            var arrowsDiv = document.createElement("div");
            var clientArrTop = document.createElement("img");
            clientArrTop.src = "/Images/gup.gif";
            clientArrTop.width = 10;
            clientArrTop.height = 6;
            var clientArrBot = document.createElement("img");
            clientArrBot.src = "/Images/gdown.gif";
            clientArrBot.width = 10;
            clientArrBot.height = 6;
            arrowsDiv.appendChild(clientArrTop);
            arrowsDiv.appendChild(clientArrBot);
            infoDiv.appendChild(arrowsDiv);
            clientArrTop.onmouseover = this.startScroll.bindAsEventListener(this, 0, div3);
            clientArrTop.onmouseout = this.stopScroll.bindAsEventListener(this);
            clientArrBot.onmouseover = this.startScroll.bindAsEventListener(this, 1, div3);
            clientArrBot.onmouseout = this.stopScroll.bindAsEventListener(this);
        }
        var wType = document.createElement("h5");
        wType.appendChild(document.createTextNode("Тип работ"));
        infoDiv.appendChild(wType);
        var div4 = document.createElement("div");
        div4.setAttribute("class", "type_works");
        infoDiv.appendChild(div4);
        div4.style.height = "30px";
        if (type != null) {
            div4.innerHTML = type;
            div4.style.overflow = "hidden";
            var arrowsDiv2 = document.createElement("div");
            var typeArrTop = document.createElement("img");
            typeArrTop.src = "/Images/gup.gif";
            typeArrTop.width = 10;
            typeArrTop.height = 6;
            var typeArrBot = document.createElement("img");
            typeArrBot.src = "/Images/gdown.gif";
            typeArrBot.width = 10;
            typeArrBot.height = 6;
            arrowsDiv2.appendChild(typeArrTop);
            arrowsDiv2.appendChild(typeArrBot);
            infoDiv.appendChild(arrowsDiv2);
            typeArrTop.onmouseover = this.startScroll.bindAsEventListener(this, 0, div4);
            typeArrTop.onmouseout = this.stopScroll.bindAsEventListener(this);
            typeArrBot.onmouseover = this.startScroll.bindAsEventListener(this, 1, div4);
            typeArrBot.onmouseout = this.stopScroll.bindAsEventListener(this);
        }
        var div5 = document.createElement("div");
        var b = document.createElement("h5");
        b.appendChild(document.createTextNode("В жизни:"));
        infoDiv.appendChild(b);
        var linkText = (linkText != null) ? linkText : "фото готового продукта";
        var a = document.createElement("a");
        if (link != null && link != "" && link != "http://")
            a.target = "_blank";
        a.href = (link != null && link != "" && link != "http://") ? link : "javascript:void(0)";
        a.appendChild(document.createTextNode(linkText));
        div5.appendChild(a);
        infoDiv.appendChild(div5);
        var span = document.createElement("span");
        span.style.display = "block";
        if (this.browser.isOpera)
            span.style.height = "328px";
        var fotoSpan = document.createElement("span");
        fotoSpan.style.display = (this.browser.isMozilla || this.browser.isOpera) ? "table" : "inline-block";
        span.appendChild(fotoSpan);
        span.appendChild(commentDiv);
        fotoDiv.appendChild(span);
        this.fotoContainer = fotoSpan;
        this.fotoContainer.innerHTML = "<img src='/Images/preloader.gif' style='margin: 30px;' alt='' />";
        if (parseInt(images["@Count"]) == 0) {
            var img = document.createElement("img");
            img.src = mainImageSrc;
            fotoSpan.appendChild(img);
        }
        else {
            this.loadItemImage(this, this.itemImages[0], false);
        }

        var clientMenu = document.createElement("div");
        Element.addClassName(clientMenu, "clientMenu");
        var d1 = document.createElement("div");
        var a1 = document.createElement("a");
        a1.appendChild(document.createTextNode("Другие работы для клиента"));
        a1.href = "javascript:void(0)";
        a1.onclick = this.filterItems.bindAsEventListener(this, 2, curItem);
        d1.appendChild(a1);
        var d2 = document.createElement("div");
        var a2 = document.createElement("a");
        a2.appendChild(document.createTextNode("Другие работы в этой сфере"));
        a2.href = "javascript:void(0)";
        a2.onclick = this.filterItems.bindAsEventListener(this, 1, curItem);
        d2.appendChild(a2);
        var d3 = document.createElement("div");
        var a3 = document.createElement("a");
        a3.appendChild(document.createTextNode("Остальные работы раздела"));
        a3.href = "javascript:void(0)";
        a3.onclick = this.filterItems.bindAsEventListener(this, 0, curItem);
        d3.appendChild(a3);
        clientMenu.appendChild(d1);
        clientMenu.appendChild(d2);
        clientMenu.appendChild(d3);
        infoDiv.appendChild(clientMenu);
        $(this.itemContainer).appendChild(shotsDiv);
        $(this.itemContainer).appendChild(infoDiv);
        $(this.itemContainer).appendChild(backDiv);
        if (div3.scrollHeight <= 30)
            arrowsDiv.style.display = "none";
        if (div4.scrollHeight <= 30)
            arrowsDiv2.style.display = "none";
        PngTransform.applyTransform(backImg);

    },
    showProcess: function(evt, curItem) {
        $(this.itemContainer).style.visibility = "visible";
        this.listContainer.style.visibility = "hidden";
        $(this.itemContainer).clear();
        this.curFotoPage = null;
        this.curItem = 1;
        this.lastIndex = 6;
        Element.addClassName($(this.itemContainer), "shotsList");
        var props = curItem["Properties"]["Property"];
        var title = curItem["Name"];
        var workType = curItem["Description"];
        var type = props[1]["Value"];
        var images = props[8]["Value"];
        var mainImageSrc = this.applicationRoot + props[6]["Value"];
        var itemImages = this.sortArray(images["Item"]);
        this.itemImages = new Array();
        var h2 = document.createElement("h2");
        var iconDiv = document.createElement("span");
        var iconA = document.createElement("a");
        iconA.href = "javascript:void(0)";
        Element.addClassName(iconDiv, "iconDiv2");
        iconA.appendChild(document.createTextNode("Что же получилось?"));
        iconA.onclick = this.showInfo.bindAsEventListener(this, curItem, false);
        iconDiv.appendChild(iconA);
        h2.appendChild(iconDiv);
        if (title != null) {
            h2.appendChild(document.createTextNode("Как это делалось: "));
            var t = document.createElement("b");
            t.appendChild(document.createTextNode(title));
            h2.appendChild(t);
        }
        $(this.itemContainer).appendChild(h2);
        var h4 = document.createElement("h4");
        if (workType != null)
            h4.appendChild(document.createTextNode(workType));
        $(this.itemContainer).appendChild(h4);
        var infoTable = document.createElement("table");
        var infoTbody = document.createElement("tbody");
        var infoTr = document.createElement("tr");
        var infoTd1 = document.createElement("td");
        var infoTd2 = document.createElement("td");
        var infoTd3 = document.createElement("td");
        var pagerDiv = document.createElement("div");
        Element.addClassName(pagerDiv, "pager");
        var pDiv = document.createElement("div");
        var table1 = document.createElement("table");
        var tbody1 = document.createElement("tbody");
        this.pageContainer = pagerDiv;
        this.pager = table1;
        var count = (images["@Count"] == 0) ? 1 : parseInt(images["@Count"]);
        pDiv.style.height = (parseInt(count) > 6) ? "144px" : parseInt(count) * 24 + "px";
        pagerDiv.style.height = (parseInt(count) > 6) ? "144px" : parseInt(count) * 24 + "px";
        for (var i = 1; i <= count; i++) {
            var pg = document.createElement("a");
            pg.href = "javascript:void(0)";
            pg.appendChild(document.createTextNode(i));
            var tr1 = document.createElement("tr");
            var td1 = document.createElement("td");
            td1.appendChild(pg);
            var item = { element: pg, props: props, index: i - 1 };
            if (images["@Count"] != 0)
                item.images = itemImages[i - 1];
            if (i == 1) {
                this.curFotoPage = item.element;
                Element.addClassName(this.curFotoPage, "active");
            }
            this.itemImages.push(item);
            if (count != 1)
                pg.onclick = this.loadItemImage.bindAsEventListener(this, item, true);
            tr1.appendChild(td1);
            tbody1.appendChild(tr1);
        }
        table1.appendChild(tbody1);
        pDiv.appendChild(table1);
        pagerDiv.appendChild(pDiv);
        infoTd1.appendChild(pagerDiv);
        var fotoDiv = document.createElement("div");
        var commentDiv = document.createElement("b");
        this.infoContainer = commentDiv;
        var wType = document.createElement("h5");
        wType.appendChild(document.createTextNode("Комментарии:"));
        infoTd3.appendChild(wType);
        var div4 = document.createElement("div");
        infoTd3.appendChild(div4);
        this.commentContainer = div4;
        var span = document.createElement("span");
        span.style.display = "block";
        var fotoSpan = document.createElement("span");
        fotoSpan.style.display = (this.browser.isMozilla || this.browser.isOpera) ? "table" : "inline-block";
        span.appendChild(fotoSpan);
        span.appendChild(commentDiv);
        fotoDiv.appendChild(span);
        this.fotoContainer = fotoSpan;
        this.fotoContainer.innerHTML = "<img src='/Images/preloader.gif' style='margin: 30px;' alt='' />";
        infoTd2.appendChild(fotoDiv);
        if (parseInt(images["@Count"]) == 0) {
            var img = document.createElement("img");
            img.src = mainImageSrc;
            fotoSpan.appendChild(img);
        }
        else
            this.loadItemImage(this, this.itemImages[0], true);
        infoTd1.style.width = "105px";
        infoTd1.style.background = "url('/Images/pagerBg2.gif') no-repeat left";
        infoTd2.style.width = "265px";
        infoTd3.style.verticalAlign = "top";
        Element.addClassName(infoTable, "infoTable");
        infoTr.appendChild(infoTd1);
        infoTr.appendChild(infoTd2);
        infoTr.appendChild(infoTd3);
        infoTbody.appendChild(infoTr);
        infoTable.appendChild(infoTbody);
        $(this.itemContainer).appendChild(infoTable);
    },
    startScroll: function(evt, dest, container) {
        var sign = (dest == 1) ? "+" : "-";
        container.scrollTop = (eval(parseInt(container.scrollTop) + sign + 1));
        this.timeoutId = window.setTimeout(this.startScroll.bind(this, this, dest, container), 60);
    },
    stopScroll: function(evt) {
        window.clearTimeout(this.timeoutId);
    },
    filterItems: function(evt, type, item) {
        var type = parseInt(type);
        var menuItem = { id: type };
        this.work = null;
        var curItem;
        if (menuItem.id == 0) {
            this.client = null;
            this.sphere = null;
            var curItem = item["Properties"]["Property"][5]["Value"];
            this.partId = (curItem != null) ? curItem["ID"] : null;
            this.part = (curItem != null) ? curItem["Name"] : null;
        }
        if (menuItem.id == 1) {
            this.client = null;
            this.part = null;
            var curItem = item["Properties"]["Property"][9]["Value"];
            this.sphereId = (curItem != null) ? curItem["ID"] : null;
            this.sphere = (curItem != null) ? curItem["Name"] : null;
        }
        if (menuItem.id == 2) {
            this.part = null;
            this.sphere = null;
            var curItem = item["Properties"]["Property"][0]["Value"];
            this.clientId = (curItem != null) ? curItem["ID"] : null;
            this.client = (curItem != null) ? curItem["Name"] : null;
        }
        $("RubricsPreloaderDiv").style.display = "block";
        var menu = new PortfolioMenu(this);
        this.loadPage(this, 1);
    },
    clearFilter: function() {
        this.part = "Web-сайты";
        this.partId = 1;
        this.sphere = null;
        this.client = null;
        this.work = null;
        this.activeYear = null;
        $("RubricsPreloaderDiv").style.display = "block";
        var menu = new PortfolioMenu(this);
        this.loadPage(this, 1);
    },
    loadItemImage: function(evt, item, wMode) {

        if (this.rightArrow != null)
            Element.remove(this.rightArrow);
        this.rightArrow = null;
        if (this.leftArrow != null)
            Element.remove(this.leftArrow);
        this.leftArrow = null;
        if (parseInt(item.index) == this.lastIndex) {
            if (!wMode)
                this.pager.style.left = parseInt(this.pager.offsetLeft) - 24 + "px";
            else
                this.pager.style.top = parseInt(this.pager.offsetTop) - 24 + "px";
            this.lastIndex++;
        }
        if (parseInt(item.index) == this.lastIndex - 7) {
            if (!wMode)
                this.pager.style.left = parseInt(this.pager.offsetLeft) + 24 + "px";
            else
                this.pager.style.top = parseInt(this.pager.offsetTop) + 24 + "px";
            this.lastIndex--;
        }
        if (parseInt(item.index) != parseInt(this.itemImages.length) - 1) {
            this.rightArrow = document.createElement("img");
            this.rightArrow.src = (wMode) ? this.applicationRoot + "/Images/wBArr.gif" : this.applicationRoot + "/Images/wRArr.gif";
            Element.addClassName(this.rightArrow, "right");
            this.pageContainer.appendChild(this.rightArrow);
            this.rightArrow.onclick = this.loadItemImage.bindAsEventListener(this, this.itemImages[item.index + 1], wMode);
        }
        if (parseInt(item.index) != 0) {
            this.leftArrow = document.createElement("img");
            this.leftArrow.src = (wMode) ? this.applicationRoot + "/Images/wTArr.gif" : this.applicationRoot + "/Images/wLArr.gif";
            this.pageContainer.appendChild(this.leftArrow);
            this.leftArrow.onclick = this.loadItemImage.bindAsEventListener(this, this.itemImages[item.index - 1], wMode);
        }
        if (this.curFotoPage != null) {
            Element.removeClassName(this.curFotoPage, "active");
            this.curFotoPage = item.element;
            Element.addClassName(this.curFotoPage, "active");
        }

        var parent = this.fotoContainer.parentNode.parentNode;


        var sParent = document.getElementById('screenshot_container');
        if (!sParent) {
            Element.remove(parent.firstChild);
            var sParent = document.createElement("span");
            sParent.setAttribute('id', 'screenshot_container');
            sParent.style.display = "block";
            var comDiv = document.createElement("b");
            var fSpan = document.createElement("span");
            this.fotoContainer = fSpan;
            fSpan.style.display = (this.browser.isMozilla || this.browser.isOpera) ? "table" : "inline-block";
            fSpan.style.height = "100px";
            fSpan.style.width = "150px";

            if (wMode)
                parent.appendChild(sParent);
            else
                parent.insertBefore(sParent, parent.firstChild);

            comDiv.appendChild(document.createTextNode(item.images.comment));
            sParent.appendChild(fSpan);
            sParent.appendChild(comDiv);



            /* Данный блок для lightbox */
            for (var i = 0; i < evt.itemImages.length; i++) {

                var spanContainer = document.createElement("span");
                var srcPreview = evt.itemImages[i].images.image;

                var src = evt.itemImages[i].images.image_detail;
                var thkHref = document.createElement("a");
                if (src != null)
                    thkHref.href = src;
                else
                    thkHref.href = "javascript:void(0)";    

                var img = document.createElement("img");
                img.src = evt.itemImages[i].images.image;
                img.style.margin = "0px";

                thkHref.setAttribute('rel', 'lightbox[roadtrip]');

                if(src!=null)
                    thkHref.setAttribute('class', 'detail_photo');
                                        

                thkHref.appendChild(img);

                if (srcPreview != item.images.image)
                    spanContainer.setAttribute('style', 'display:none');
                else
                    spanContainer.setAttribute('style', '');

              
                spanContainer.appendChild(thkHref);

              
                fSpan.appendChild(spanContainer);

                if (wMode) {
                    this.commentContainer.innerHTML = "";
                    this.commentContainer.innerHTML = item.images.text;
                }
                fSpan.style.height = "auto";
                fSpan.style.width = "auto";

            }





        }

        else {
            $j('#screenshot_container b').remove();
            var comDiv = document.createElement("b");
            comDiv.appendChild(document.createTextNode(item.images.comment));
            sParent.appendChild(comDiv);
            for (var i = 0; i < $j('#screenshot_container a').length; i++) {
                //alert($j('.detail_photo').length);
                var src = $j('#screenshot_container a').eq(i).attr('href');
                var srcPreview = $j('#screenshot_container img').eq(i).attr('src');
                if (srcPreview != item.images.image)
                    $j('#screenshot_container span span').eq(i).attr('style', 'display:none');

                else
                    $j('#screenshot_container span span').eq(i).attr('style', '');
            }



        }

        // $('.detail_photo').lightBox();
        /* ---Данный блок для lightbox*/







        init_document(); //Инициализация элементов с классом thickbox



    }

};
var PortfolioMenu = Class.create();
PortfolioMenu.prototype = {
	initialize: function (portfolio) {
		this.content = portfolio.content;
		this.container = portfolio.container;
		this.itemContainer = portfolio.itemContainer;
		this.blocks = portfolio.blocks;
		this.part = portfolio.part;
		this.sphere = portfolio.sphere;
		this.partId = portfolio.partId;
		this.sphereId = portfolio.sphereId;
		this.clientId = portfolio.clientId;
		this.client = portfolio.client;
		this.workId = portfolio.workId;
		this.work = portfolio.work;
		this.year = portfolio.activeYear;
		this.endYear = new Date().getFullYear();
		this.blocksList = new Array(4);
		this.initMenu();
	},
	initMenu: function () {
		if (this.blocks[2].filter == null) this.blocks[2].filter = [];
		if (this.blocks[3].filter == null) this.blocks[3].filter = [];
		var date1;
		var date2;
		if (this.year != null) {
			var date1 = "01.01." + this.year;
			var date2 = (parseInt(this.year) == 2002) ? "01.01." + parseInt(this.endYear - 2) : "01.01." + (parseInt(this.year) + 1);
		}
		this.blocks[2].filter[0] = (this.year != null) ? {
			column: "Date",
			value: date1,
			condition: "GreaterOrEqual"
		} : null;
		this.blocks[2].filter[1] = (this.year != null) ? {
			column: "Date",
			value: date2,
			condition: "Less"
		} : null;
		this.blocks[3].filter[0] = (this.year != null) ? {
			column: "ShippingDate",
			value: date1,
			condition: "GreaterOrEqual"
		} : null;
		this.blocks[3].filter[1] = (this.year != null) ? {
			column: "ShippingDate",
			value: date2,
			condition: "Less"
		} : null;
		this.blocks[3].filter[2] = (this.part != null) ? {
			column: "Section_ItemID",
			value: this.partId,
			condition: "Equal"
		} : null;
		this.blocks[2].filter[2] = (this.part != null) ? {
			column: "LinkedSections",
			value: "%" + this.part + ";%",
			condition: "Like"
		} : null;
		this.blocks[3].filter[3] = (this.sphere != null) ? {
			column: "Sphere_ItemID",
			value: this.sphereId,
			condition: "Equal"
		} : null;
		this.blocks[2].filter[3] = (this.sphere != null) ? {
			column: "LinkedSpheres",
			value: "%" + this.sphere + ";%",
			condition: "Like"
		} : null;
		this.blocks[3].filter[4] = (this.client != null) ? {
			column: "Client_ItemID",
			value: this.clientId,
			condition: "Equal"
		} : null;
		for (var i = 0; i < this.blocks.length; i++) {
			if (this.part == null || (this.part != null && i != 1)) {
				var block = this.blocks[i];
				var menuBlock = new Block(block);
				menuBlock.getItemList();
				menuBlock.blockCallback = this.onLoaded.bind(this, i);
			}
		}
	},
	onLoaded: function (index, container, content) {
		var menuItem = new MenuItem(this, index, container, content["GetItemListResult"]).loadList();
		this.blocksList[index] = menuItem;

		if (index == 0 && this.part != null) {
			var items = content["GetItemListResult"];
			if (items["Item"].length != null) {
				for (var i = 0; i < items["Item"].length; i++) {
					if (parseInt(items["Item"][i]["ID"]) == parseInt(this.partId)) {
						var partItems = items["Item"][i]["Properties"]["Property"][0]["Value"];
						break;
					}
				}
			} else {
				var partItems = items["Item"]["Properties"]["Property"][0]["Value"];
			}
			this.addSpheres(partItems);
		}
		if (this.blocksList.length != 4) return;
		for (var i = 0; i < this.blocksList.length; i++)
			this.blocksList[i].title.onclick = this.showList.bindAsEventListener(this, this.blocksList[i]);
		$("RubricsPreloaderDiv").style.display = "none";
		document.onclick = this.closeMenuList.bindAsEventListener(this);
		this.closeMenuList(this);
	},
	addSpheres: function (partItems) {
		if (partItems == null) return;
		var menuItem = new MenuItem(this, 1, this.blocks[1].container, partItems).loadList();
		this.blocksList[1] = menuItem;
		this.initList(this.blocksList[1], partItems);
	},
	showList: function (evt, menuItem) {
		this.hideLists(menuItem.id);
		if (menuItem.fullCount != 0) {
			//menuItem.container.style.width = "200px";
			jQuery(menuItem.container).show().css({'width':"200px"});
			menuItem.arrow.style.background = "url('/Images/portfolioArr_a.gif') no-repeat left top";
		}
		if (menuItem.isLoaded) return;
		menuItem.container.innerHTML = "<div><img src='/Images/preload.gif' style='margin: 170px 0 0 60px;' alt=''/></div>";
		var block = this.blocks[menuItem.id];
		if (menuItem.id == 3) this.blocks[menuItem.id].loadLinkedItems = true;
		block.itemsCount = parseInt(menuItem.fullCount);
		var menuBlock = new Block(block);
		menuBlock.getItemList();
		menuBlock.blockCallback = this.onLoadedList.bind(this, menuItem);
	},
	onLoadedList: function (menuItem, container, content) {
		var itemList = content["GetItemListResult"];
		this.initList(menuItem, itemList);
	},
	initList: function (menuItem, itemList) {
		var count = itemList["@Count"];
		menuItem.container.innerHTML = "";
		var div = document.createElement("div");
		menuItem.container.appendChild(div);
		if (count != 0) {
			if (itemList["Item"].length != null) for (var i = 0; i < itemList["Item"].length; i++)
			this.addItem(itemList["Item"][i], menuItem);
			else this.addItem(itemList["Item"], menuItem);
			if (menuItem.id == 0) {
				var y = document.createElement("a");
				y.href = "javascript:void(0)";
				y.appendChild(document.createTextNode("Все"));
				menuItem.container.firstChild.appendChild(y);
				y.onclick = this.filterList.bindAsEventListener(this, menuItem, null);
			}
		}
		menuItem.isLoaded = true;
	},
	addItem: function (item, menuItem) {
		var title = (item["Name"] == null) ? "Без названия" : item["Name"];
		var a = document.createElement("a");
		a.href = "javascript:void(0)";
		a.appendChild(document.createTextNode(title));
		menuItem.container.firstChild.appendChild(a);
		a.onclick = this.filterList.bindAsEventListener(this, menuItem, item);
	},
	filterList: function (evt, menuItem, item) {
		var id = (item != null) ? item["ID"] : null;
		//this.blocksList[menuItem.id].container.style.width = "0px";
		jQuery(this.blocksList[menuItem.id].container).hide();
		this.blocksList[menuItem.id].arrow.style.background = "url('/Images/portfolioArr.gif') no-repeat left top";
		if (menuItem.id == 0) {
			this.partId = id;
			this.part = (item != null) ? item["Name"] : null;
			this.sphere = null;
			this.client = null;
			this.work = null;
		}
		if (menuItem.id == 1) {
			this.sphereId = id;
			this.sphere = item["Name"];
			this.client = null;
			this.work = null;
		}
		if (menuItem.id == 2) {
			this.clientId = id;
			this.client = item["Name"];
			this.work = null;
		}
		if (menuItem.id == 3) {
			this.workId = id;
			this.work = item["Name"];
		}
		$("RubricsPreloaderDiv").style.display = "block";
		this.initMenu();
		var portfolio = (menuItem.id != 3) ? new Portfolio(this.content, this.container, this.itemContainer).reloadPortfolio(this) : new Portfolio(this.content, this.container, this.itemContainer).callInfo(this, item["ID"], true);
	},
	hideLists: function (menuItem) {
		if (this.blocksList == null) return;
		for (var i = 0; i < this.blocksList.length; i++) {
			if (i != menuItem.id && menuItem.fullCount != 0) {
				//this.blocksList[i].container.style.width = "0px";
				jQuery(this.blocksList[i].container).hide();
				this.blocksList[i].arrow.style.background = "url('/Images/portfolioArr.gif') no-repeat left top";
			}
		}
	},
	closeMenuList: function (evt) {
		var elem = Event.element(evt);
		if (this.blocksList[0].title == null) return;
		for (var i = 0; i < this.blocksList.length; i++) {
			if (this.blocksList[i].title != null) {
				if (elem != this.blocksList[i].title) {
					//this.blocksList[i].container.style.width = "0px";
					jQuery(this.blocksList[i].container).hide();
					this.blocksList[i].arrow.style.background = "url('/Images/portfolioArr.gif') no-repeat left top";
				}
			}
		}
	}
};
var MenuItem = Class.create();
MenuItem.prototype = {
	initialize: function (menu, index, container, content) {
		this.menu = menu;
		this.content = content;
		this.container = $(container);
		this.title;
		this.menuItem = {};
		this.menuItem.id = index;
		this.applicationRoot = applicationRoot;
		this.browser = new Browser();
		this.fullCount = content["@Count"];
	},
	loadList: function () {
		this.container.clear();
		var id;
		var catalog;
		this.title;
		this.subTitle;
		var count = (this.menuItem.id != 0) ? this.fullCount : -1;
		if (this.menuItem.id == 0) {
			this.title = "/Images/razdelTitle.gif";
			if (this.menu.part != null) {
				id = this.menu.partId;
				catalog = 9;
			}
		}
		if (this.menuItem.id == 1) {
			this.title = "/Images/sphereTitle.gif";
			if (this.menu.sphere != null) {
				id = this.menu.sphereId;
				catalog = 10;
			}
		}
		if (this.menuItem.id == 2) {
			this.title = "/Images/clientTitle.gif";
			if (this.menu.client != null) {
				id = this.menu.clientId;
				catalog = 7;
			}
		}
		if (this.menuItem.id == 3) {
			this.title = "/Images/workTitle.gif";
			if (this.menu.work != null) {
				id = this.menu.workId;
				catalog = 12;
			}
		}
		id = (id == null) ? 0 : id;
		catalog = (catalog == null) ? 1 : catalog;
		this.subTitle = 'blank.gif';//this.applicationRoot + "/drawstring.axd?cId=" + catalog + "&id=" + id + "&count=" + count;
		this.addPartTitle();
		var listTd = document.createElement("td");
		var div = document.createElement("div");
		this.menuItem.container = div;
		this.menuItem.fullCount = this.fullCount;
		listTd.appendChild(div);
		this.container.appendChild(listTd);
		return this.menuItem;
	},
	addPartTitle: function () {
		var partTd = document.createElement("td");
		var b = document.createElement("div");
		var b2 = document.createElement("div");
		b.style.position = "static";
		b2.style.position = "static";
		Element.addClassName(b, "rubricTitle");
		var table = document.createElement("table");
		var tbody = document.createElement("tbody");
		var tr = document.createElement("tr");
		var tr2 = document.createElement("tr");
		var tr3 = document.createElement("tr");
		var td = document.createElement("td");
		var td2 = document.createElement("td");
		var td3 = document.createElement("td");
		table.style.height = "406px";
		table.style.width = "19px";
		td.style.width = "19px";
		td.style.background = "url('/Images/portfolioArr.gif') no-repeat left top";
		var h = (this.menuItem.id != 1) ? 60 : 106;
		td.style.height = "14px";
		td3.style.height = h + "px";
		td2.style.height = parseInt(406 - h - 14) + "px";
		td2.style.background = "url('" + this.subTitle + "') no-repeat left bottom";
		td3.style.background = "url('" + this.title + "') no-repeat 3px bottom";
		tr.appendChild(td);
		tr2.appendChild(td2);
		tr3.appendChild(td3);
		tbody.appendChild(tr);
		tbody.appendChild(tr2);
		tbody.appendChild(tr3);
		table.appendChild(tbody);
		if (this.menuItem.id == 0) b.style.width = "19px";
		var titleDiv = document.createElement("em");
		var titleDiv1 = document.createElement("em");
		titleDiv1.appendChild(titleDiv);
		partTd.appendChild(titleDiv1);
		b2.appendChild(table);
		b.appendChild(b2);
		partTd.appendChild(b);
		this.menuItem.title = titleDiv;
		this.menuItem.arrow = td;
		this.container.appendChild(partTd);
	}
};

