﻿var Part = Class.create();
Part.prototype = {
	initialize: function (navPoints) {
		this.navPoints = navPoints;
		this.partLoadedCallback = null;
		this.partBlocksCount;
		this.partBlocksLoaded = 0;
	},
	loadPart: function (navPointIndex, partIndex) {
		if (this.navPoints[navPointIndex].parts == null) return;
		this.navPoints.curPage = (this.navPoints.curPage != null) ? this.navPoints.curPage : 0;
		var fromPage = this.navPoints.curPage;
		if (navPointIndex == 1 && (!this.navPoints[navPointIndex].menuIsLoaded || fromPage != navPointIndex)) {
			var studioMenu = new Menu().loadMenu();
			this.navPoints[navPointIndex].menuIsLoaded = true;
		}
		if (this.navPoints[navPointIndex].currentPart != null) {
			if (partIndex != this.navPoints[navPointIndex].currentPart) {
				var curPart = this.navPoints[navPointIndex].currentPart;
				var partContainer = this.navPoints[navPointIndex].parts[curPart].container;
				if (partContainer != null) $(partContainer).style.visibility = "hidden";
				if (navPointIndex != 1 && partIndex != 6) $(this.navPoints[1].parts[3].blocks[0].ipixContainer).style.display = "none";
				else $(this.navPoints[1].parts[3].blocks[0].ipixContainer).style.display = "block";
			}
			if (partIndex == curPart) return;
		}
		this.navPoints[navPointIndex].currentPart = partIndex;
		this.navPoints.curPage = navPointIndex;
		if (this.navPoints[navPointIndex].parts == undefined) {
			this.partLoadedCallback.call(this, navPointIndex, this.navPoints[navPointIndex].currentPart);
			return;
		}
		this.partBlocksLoaded = 0;
		var part = this.navPoints[navPointIndex].parts[partIndex];
		if (part.container != null) {
			$(part.container).style.visibility = "visible";
			if (part.blocks != null) {
				$(part.blocks[0].container).scrollTop = "0px";
				if (part.blocks[0].textContainer != null) $(part.blocks[0].textContainer).scrollTop = "0px";
			}
		}
		if (part.blocks == null || part.isLoaded) {
			this.partLoadedCallback.call(this, navPointIndex, this.navPoints[navPointIndex].currentPart);
			return;
		}
		this.partBlocksCount = part.blocks.length;
		for (var i = 0; i < this.partBlocksCount; i++) {
			var category = part.blocks[i].category;
			var block = new Block(part.blocks[i]);
			if (category == "item") block.getItem();
			if (category == "itemList") block.getItemList();
			if (category == "itemGroupList") block.getGroupList();
			block.blockCallback = this.onBlockLoaded.bind(this);
		}
	},
	onBlockLoaded: function () {
		this.partBlocksLoaded++;
		if (this.partLoadedCallback != null && this.partBlocksLoaded == this.partBlocksCount) this.partLoadedCallback.call(this, this.navPoints.curPage, this.navPoints[this.navPoints.curPage].currentPart);
	}
};
var Block = Class.create();
Block.prototype = {
	initialize: function (block) {
		this.block = block;
		this.params = block.params;
		this.blockCallback = null;
		this.moduleName = block.moduleName;
		this.container = block.container;
		this.startIndex = (this.block.startIndex != undefined) ? this.block.startIndex : 1;
		this.count = (this.block.itemsCount != undefined && this.block.itemsCount != null) ? this.block.itemsCount : 500;
		this.loadLinkedItems = (this.block.loadLinkedItems != undefined && this.block.loadLinkedItems != null) ? this.block.loadLinkedItems : true;
	},
	getItem: function () {
		var xml = "<?xml version='1.0' encoding='utf-8'?>";
		xml += "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>";
		xml += "<soap:Body>";
		xml += "<GetItem xmlns='http://jeltofiol.com/freefiol/web/services/'>";
		xml += "<Catalog>";
		xml += "<ID>" + this.params.catalogId + "</ID>";
		xml += "</Catalog>";
		xml += "<Item>";
		xml += "<ID>" + this.params.id + "</ID>";
		xml += "</Item>";
		xml += "<LoadLinkedItems>true</LoadLinkedItems>";
		xml += "<PropertyList>";
		xml += "<add name='*' />";
		xml += "</PropertyList>";
		xml += "</GetItem>";
		xml += "</soap:Body>";
		xml += "</soap:Envelope>";
		new Ajax.Request("/services/DataProvider.asmx", {
			method: "post",
			postBody: xml,
			contentType: "text/xml",
			onSuccess: this.onItemLoaded.bind(this),
			onFailure: this.onError.bind(this)
		});
	},
	getItemList: function () {
		var xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
		xml += "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">";
		xml += "<soap:Body>";
		xml += "<GetItemList xmlns=\"http://jeltofiol.com/freefiol/web/services/\">";
		xml += "<Catalog>";
		xml += "<ID>" + this.params.catalogId + "</ID>";
		xml += "</Catalog>";
		if (this.params.folderId != '') {
			xml += "<Folder>";
			xml += "<ID>" + this.params.folderId + "</ID>";
			xml += "</Folder>";
		}
		xml += "<LoadLinkedItems>" + this.loadLinkedItems + "</LoadLinkedItems>";
		xml += "<PropertyList>";
		if (this.block.propertyList == null) xml += "<add name='*' />";
		else if (this.block.propertyList.length != 0) {
			for (var i = 0; i < this.block.propertyList.length; i++) xml += "<add name='" + this.block.propertyList[i] + "' />";
		}
		xml += "</PropertyList>";
		xml += "<StartIndex>" + this.startIndex + "</StartIndex>";
		if (this.count != '') xml += "<Count>" + this.count + "</Count>";
		if (this.block.filter != undefined) {
			xml += "<Filter>";
			for (var i = 0; i < this.block.filter.length; i++) {
				if (this.block.filter[i] != null) {
					xml += "<FilterParameter>";
					xml += "<ColumnName>" + this.block.filter[i].column + "</ColumnName>";
					xml += "<IsAttribute>true</IsAttribute>";
					xml += "<Value>" + this.block.filter[i].value + "</Value>";
					xml += "<SearchCondition>" + this.block.filter[i].condition + "</SearchCondition>";
					xml += "</FilterParameter>";
				}
			}
			xml += "</Filter>";
		}
		if (this.block.orderSettings != undefined) {
			xml += "<OrderSettings>";
			for (var i = 0; i < this.block.orderSettings.length; i++) {
				if (this.block.orderSettings[i] != null) {
					var isAttribute = (this.block.orderSettings[i].isAttribute != null) ? this.block.orderSettings[i].isAttribute : true;
					xml += "<Rule>";
					xml += "<ColumnName>" + this.block.orderSettings[i].column + "</ColumnName>";
					xml += "<IsAttribute>" + isAttribute + "</IsAttribute>";
					xml += "<OrderType>" + this.block.orderSettings[i].rule + "</OrderType>";
					xml += "</Rule>";
				}
			}
			xml += "</OrderSettings>";
		}
		xml += "</GetItemList>";
		xml += "</soap:Body>";
		xml += "</soap:Envelope>";
		new Ajax.Request("/services/DataProvider.asmx", {
			method: "post",
			postBody: xml,
			contentType: "text/xml",
			onSuccess: this.onItemListLoaded.bind(this),
			onFailure: this.onError.bind(this)
		});
	},
	getGroupList: function () {
		var xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
		xml += "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">";
		xml += "<soap:Body>";
		xml += "<GetItemGroupList xmlns=\"http://jeltofiol.com/freefiol/web/services/\">";
		xml += "<Catalog>";
		xml += "<ID>" + this.params.catalogId + "</ID>";
		xml += "</Catalog>";
		xml += "<Folder>";
		xml += "<ID>" + this.params.folderId + "</ID>";
		xml += "</Folder>";
		xml += "</GetItemGroupList>";
		xml += "</soap:Body>";
		xml += "</soap:Envelope>";
		new Ajax.Request("/services/DataProvider.asmx", {
			method: "post",
			postBody: xml,
			contentType: "text/xml",
			onSuccess: this.onGroupListLoaded.bind(this),
			onFailure: this.onError.bind(this)
		});
	},
	onItemLoaded: function (transport) {
		var json = XmlConverter.getJson(transport.responseXML, " ");
		var content = null;
		eval('content = ' + json);
		var content = content["soap:Envelope"]["soap:Body"]["GetItemResponse"];
		if (this.moduleName == "StudioText") var text = new StudioText(content, this.container);
		if (this.moduleName == "StudioFiles") var files = new StudioFiles(content, this.container);
		if (this.blockCallback != null) this.blockCallback.call(this, content);
	},
	onItemListLoaded: function (transport) {
		var json = XmlConverter.getJson(transport.responseXML, " ");
		var content = null;
		eval('content = ' + json);
		var content = content["soap:Envelope"]["soap:Body"]["GetItemListResponse"];
		if (this.moduleName == "StudioTribes") var list = new Tribes(content, this.container);
		if (this.moduleName == "Vacancies") var vacancy = new Vacancy(content, this.container, this.block.textContainer);
		if (this.moduleName == "Services") var services = new Services(content, this.container, this.block.textContainer);
		if (this.moduleName == "StudioFiles") var cases = new Cases(content, this.container);
		if (this.moduleName == "FAQ") var faq = new FAQModule(content, this.container, this.block.textContainer);
		if (this.moduleName == "Persons") var persons = new Persons(content, this.container, this.block.fotoContainer);
		if (this.moduleName == "StudioIpix") var ipix = new Ipix(content, this.container, this.block.ipixContainer);
		if (this.moduleName == "fullJ") var ipix = new FullJ(content, this.container);
		if (this.moduleName == "ClientsList") var clientList = new ClientList(content, this.container, this.block.textContainer);
		if (this.moduleName == "brendList") var brendList = new BrendList(content, this.container);
		if (this.moduleName == "StudioProjects") var projectList = new SelfProjects(content, this.container);
		if (this.moduleName == "PortfolioProjects") var portfolio = new Portfolio(content, this.container, this.block.itemContainer).start();
		if (this.blockCallback != null) this.blockCallback.call(this, this.container, content);
	},
	onGroupListLoaded: function (transport) {
		var json = XmlConverter.getJson(transport.responseXML, " ");
		var content = null;
		eval('content = ' + json);
		var content = content["soap:Envelope"]["soap:Body"]["GetItemGroupListResponse"];
		if (this.moduleName == "StudioFotos") var fotos = new EventFotos(content, this.container, this.block.fotosContainer);
		if (this.blockCallback != null) this.blockCallback.call(this);
	},
	onError: function () {
		$(this.container).clear();
		$(this.container).innerHTML = "<font style='font: 11px 'Verdana';'>При загрузке данных произошла ошибка. Попробуйте обновить страницу.</font>";
	}
}
