if(!window.Blocks) window.Blocks = false;
if(!window.ZMax) window.ZMax = 1;

function GenerateId() {var date = new Date(); return "CmsNew" + date.getTime() + Math.round((Math.random()*1000));}
function GetLeft(Obj) {var result = 0; do result += Obj.offsetLeft; while (Obj = Obj.offsetParent); return result;}
function GetTop(Obj) {var result = 0; do result += Obj.offsetTop; while (Obj = Obj.offsetParent); return result;}
//function GetLeft(Obj) {return Obj.getBoundingClientRect().left-2+document.body.scrollLeft;}
//function GetTop(Obj) {return Obj.getBoundingClientRect().top-2+document.body.scrollTop;}


function function_null() {;}

function function_OpenMenu(sSourceId, sTargetId, sPosition, bSub) {
	var oSource = document.getElementById(sSourceId);
	var oTarget = document.getElementById(sTargetId);
	clearTimeout(oTarget.iHoldTimerId);
	if (!oTarget.sMode || oTarget.sMode=="closed") {
		if (!bSub) {
			oTarget.style.left = GetLeft(oSource);
			oTarget.style.top = GetTop(oSource);
		} else {
			oTarget.style.left = oSource.offsetLeft;
			oTarget.style.top = oSource.offsetTop;
		}
		switch (sPosition) {
			case "right":
				oTarget.style.left = parseInt(oTarget.style.left) + oSource.offsetWidth;
				var iStopValue = oTarget.offsetWidth;
				oTarget.style.width = "1px";
				break;
			case "bottom":
				oTarget.style.top = parseInt(oTarget.style.top) + oSource.offsetHeight;
				if (oTarget.offsetWidth < oSource.offsetWidth) oTarget.style.width = oSource.offsetWidth;
				var iStopValue = oTarget.offsetHeight;
				oTarget.style.height = "1px";
				break;
		}
		oTarget.style.overflow = "hidden";
		oTarget.style.visibility = "visible";
		oTarget.iTimerId = setInterval("SlideMenu('"+sTargetId+"','"+sPosition+"',"+iStopValue+");", 10);
	}
	if (oTarget.sMode!="opened") oTarget.sMode = "open";
}

function function_CloseMenu(sTargetId, sPosition) {
	var oTarget = document.getElementById(sTargetId);
	oTarget.iHoldTimerId = setTimeout("CloseMenuNow('"+sTargetId+"','"+sPosition+"');", 300);
}

function CloseMenuNow(sTargetId, sPosition) {
	var oTarget = document.getElementById(sTargetId);
	if (oTarget.sMode=="opened") {
		switch (sPosition) {
			case "right": var iStopValue = oTarget.offsetWidth; break;
			case "bottom": var iStopValue = oTarget.offsetHeight; break;
		}
		oTarget.style.overflow = "hidden";
		oTarget.iTimerId = setInterval("SlideMenu('"+sTargetId+"','"+sPosition+"',"+iStopValue+");", 10);
	}
	if (oTarget.sMode!="closed") oTarget.sMode = "close";
}

function SlideMenu(sTargetId, sPosition, iStopValue) {
	var oTarget = document.getElementById(sTargetId);
	switch (oTarget.sMode+"+"+sPosition) {
		case "open+right":
			if (parseInt(oTarget.style.width) < iStopValue) oTarget.style.width = parseInt(oTarget.style.width) + 5;
			else {oTarget.style.overflow = "visible"; oTarget.style.width = iStopValue; clearInterval(oTarget.iTimerId); oTarget.sMode = "opened";}
			break;
		case "open+bottom":
			if (parseInt(oTarget.style.height) < iStopValue) oTarget.style.height = parseInt(oTarget.style.height) + 5;
			else {oTarget.style.overflow = "visible"; oTarget.style.height = iStopValue; clearInterval(oTarget.iTimerId); oTarget.sMode = "opened";}
			break;
		case "close+right":
			if (parseInt(oTarget.style.width) > 5) oTarget.style.width = parseInt(oTarget.style.width) - 5;
			else {oTarget.style.visibility = "hidden"; oTarget.style.overflow = "visible"; oTarget.style.width = ""; clearInterval(oTarget.iTimerId); oTarget.sMode = "closed";}
			break;
		case "close+bottom":
			if (parseInt(oTarget.style.height) > 5) oTarget.style.height = parseInt(oTarget.style.height) - 5;
			else {oTarget.style.visibility = "hidden"; oTarget.style.overflow = "visible"; oTarget.style.width = ""; clearInterval(oTarget.iTimerId); oTarget.sMode = "closed";}
			break;
	}
}

function_OpenSubMenu = function_OpenMenu;

/*function function_OpenMenu(source, menu, position) {
	if (!position) position = "right";
	menu.style.left = GetLeft(source);
	menu.style.top = GetTop(source);
	switch (position) {
		case "right": menu.style.left = parseInt(menu.style.left) + source.offsetWidth -1; break;
		case "bottom": menu.style.top = parseInt(menu.style.top) + source.offsetHeight -1; break;
	}
	menu.style.visibility = "visible";
	menu.style.zIndex = window.ZMax++;
	if (Blocks) Blocks.RefreshHighlights();
}
function function_OpenSubMenu(source, menu, position) {
	if (!position) position = "right";
	menu.style.left = source.offsetLeft;
	menu.style.top = source.offsetTop;
	switch (position) {
		case "right": menu.style.left = parseInt(menu.style.left) + source.offsetWidth -1; break;
		case "bottom": menu.style.top = parseInt(menu.style.top) + source.offsetHeight -1; break;
	}
	menu.style.visibility = "visible";
	menu.style.zIndex = window.ZMax++;
	if (Blocks) Blocks.RefreshHighlights();
}
function function_CloseMenu(menu) {
	menu.style.visibility = "hidden";
	if (Blocks) Blocks.RefreshHighlights();
}*/

function function_OpenMenuClick(menu) {
	switch (menu.style.display) {
		case "block": menu.style.display = "none"; break;
		case "none": menu.style.display = "block"; break;
	}
	if (Blocks) Blocks.RefreshHighlights();
}

OpenMenu = function_OpenMenu;
OpenSubMenu = function_OpenSubMenu;
CloseMenu = function_CloseMenu;
OpenMenuClick = function_OpenMenuClick;

function GET(name) {
    var p = name + "=";
    var si = document.URL.indexOf(p);
    if (si == -1)
        return null;
    var ei = document.URL.indexOf("&", si + p.length);
    if (ei == -1)
        ei = document.URL.length;
    return unescape(document.URL.substring(si + p.length, ei));
}

function cms_document_hotkeys() {
	if (event.ctrlKey && event.altKey)
		switch (String.fromCharCode(event.keyCode)) {
			case 'S':
				var url = document.URL.search(/\?/)==-1 ? document.URL + "?" : document.URL;
				url = url.replace(/viewmode/, "").replace(/editmode/, "").replace(/structmode/, "");
				if (!/\?$/.test(url) && !/\&$/.test(url)) url += "&";
				url += getCookie("STRUCT_MODE") ? "viewmode" : "structmode";
				location = !getCookie("STRUCT_MODE") ? "cms/auth.php?prev_url=" + escape(url) : url;
				break;
		}
}
document.attachEvent("onkeydown", cms_document_hotkeys);

function ShowOriginal(obj, target) {
	target = target ? target : "_blank";
	imgs=obj.getElementsByTagName('img');
	if (imgs[0]) {
		var src = imgs[0].src;
		var n = src.lastIndexOf('/');
		src = src.substring(0,n) + '/originals' + src.substring(n);
		window.open(src, target, "", true);
	}
}

function ShowOriginalIMG(obj, id) {
	imgs=obj.getElementsByTagName('img');
	if (imgs[0]) {
		var src = imgs[0].src;
		var n = src.lastIndexOf('/');
		src = src.substring(0,n) + '/originals' + src.substring(n);
		
		document.getElementById(id).src = src;
	}
}

function InsertPng(Src) {
	if(!navigator.appMinorVersion) document.write("<img alt='Амурпромстрой - филиал Екатеринбург официальный дилер Foton-Lovol' border=0 src='"+Src+"'/>");
		else document.write("<img alt='Амурпромстрой - филиал Екатеринбург официальный дилер Foton-Lovol' border=0 src='/cms/pic/transparent.gif' style=\"filter: progid:DXImageTransform.Microsoft.AlphaImageLoader (src='"+Src+"');\"/>");
}

function BasketAdd(Title, Price) {
	SetCookie("BASKET["+encodeURI(Title)+"][0]", "1");
	SetCookie("BASKET["+encodeURI(Title)+"][1]", encodeURI(Price));
	var bt = document.getElementById("BasketTemplate");
	if (bt) {
		var Id = GenerateId();
		var o = bt.cloneNode(true);
		o.id = Id;
		o.style.display = "";
		bt.parentElement.insertBefore(o, bt);
		if (o.tagName.toLowerCase()!="tr")
			o.innerHTML = o.innerHTML.replace(/##BASKET_TITLE##/, Title).replace(/##BASKET_NUMBER##/, "<input type='text' size='4' onKeyUp='BasketChangeNumber(\""+Title+"\", this.value);' value='1'/>").replace(/##BASKET_PRICE##/, Price).replace(/##BASKET_DEL##/, "<a href='javascript:BasketDel(\""+Id+"\", \""+Title+"\")'>").replace(/##BASKET_DEL_END##/, "</a>");
		else for (var i=0; i<o.cells.length; i++) 
			o.cells[i].innerHTML = o.cells[i].innerHTML.replace(/##BASKET_TITLE##/, Title).replace(/##BASKET_NUMBER##/, "<input type='text' size='4' onKeyUp='BasketChangeNumber(\""+Title+"\", this.value);' value='1'/>").replace(/##BASKET_PRICE##/, Price).replace(/##BASKET_DEL##/, "<a href='javascript:BasketDel(\""+Id+"\", \""+Title+"\")'>").replace(/##BASKET_DEL_END##/, "</a>");
		BasketRecalc();
	}
}
function BasketDel(DomId, Title) {
	DelCookie("BASKET["+encodeURI(Title)+"][0]");
	DelCookie("BASKET["+encodeURI(Title)+"][1]");
	if (DomId!="") {
		var Obj = document.getElementById(DomId);
		Obj.parentElement.removeChild(Obj);
	}
	BasketRecalc();
}
function BasketChangeNumber(Title, Value) {
	if (Value=="") Value = 0;
	var OldValue = GetCookie("BASKET["+encodeURI(Title)+"][0]");
	if (OldValue!=Value) {
		SetCookie("BASKET["+encodeURI(Title)+"][0]", Value);
		BasketRecalc();
	}
}
function BasketRecalc() {
	var oBasket = GetBasket();
	var Num = 0;
	for (var i in oBasket) Num += parseInt(oBasket[i][0])*parseFloat(oBasket[i][1]);
	BasketResultPrice.innerHTML = Num;
}
function GetBasket() {
	var aCookie = document.cookie.split("; ");
	var oBasket = new Object();
	var re = /^BASKET\[([^\]]+)\]\[(\d+)\]$/;
	for (var i=0; i < aCookie.length; i++) {
		var aCrumb = aCookie[i].split("=");
		if (aCrumb[0].match(re)) {
			var v1 = decodeURI(aCrumb[0].replace(re, "$1"));
			var v2 = decodeURI(aCrumb[0].replace(re, "$2"));
			if (!oBasket[v1]) oBasket[v1] = new Object();
			oBasket[v1][v2] = decodeURI(aCrumb[1]);
		}
	}
	return oBasket;
}
function BasketClear() {
	var oBasket = GetBasket();
	for (var i in oBasket) BasketDel("", i);
}

function SetCookie(sName, sValue, dDate) {document.cookie = sName + "=" + sValue + ";" + (dDate!=null ? " expires=" + dDate + ";" : "") + " path=/;";}
function DelCookie(sName) {document.cookie = sName + "=; expires=Fri, 31 Dec 1999 23:59:59 GMT; path=/;";}
function GetCookie(sName) {
	var aCookie = document.cookie.split("; ");
	for (var i=0; i < aCookie.length; i++) {
		var aCrumb = aCookie[i].split("=");
		if (sName == aCrumb[0]) return unescape(aCrumb[1]);
	}
	return null;
}

function GoToArtgk() {var s = "location = '"; s += "http://"; s +="artgk"; s += "."; s+="ru"; s += "';"; eval(s);}
function GoToCmsArtgk() {var s = "location = '"; s += "http://"; s +="artgk-cms"; s += "."; s+="ru"; s += "';"; eval(s);}
