/*
// 2008-07-03
// Copyright (c) Art. Lebedev | http://www.artlebedev.ru/
// Author - Vladimir Tokmakov
*/
var oMain = {
	make_pseudo: function (eThis) {
		var eINS = document.createElement('ins');
		eINS.className = 'pseudo';
		eThis.insertBefore(eINS, eThis.firstChild);
		eINS.appendChild(eThis.childNodes[1]);
		if (this.need_fix_hover()) {
			this.fix_hover(eINS, 'pseudo');
		}
		return eINS;
	},
	fix_hovers: function () {
		if (this.need_fix_hover()) {
			Common.Event.add(window, 'load', function () {
				var aeLABEL = document.getElementsByTagName('label');
				for (var i = 0; i < aeLABEL.length; i++) {
					oMain.fix_hover(aeLABEL[i]);
				}
			});
		}
	},
	fix_hover: function (eThis, sClass) {
		sClass = (sClass ? sClass + '_' : '') + 'hover';
		Common.Event.add(eThis, 'mouseover', function () {
			Common.Class.add(eThis, sClass);
		});
		Common.Event.add(eThis, 'mouseout', function () {
			Common.Class.remove(eThis, sClass);
		});
	},
	need_fix_hover: function () {
		return window.attachEvent;
	},
	URL_append: function (sURL, sParameters) {
		return sURL + (sURL.indexOf('?') < 0 ? '?' : '&') + sParameters;
	},
	before_popup_open: function () {
		if (navigator.userAgent.match(/MSIE\s[56]/)) {
			var oPopup_min = Common.Dom.getAbsoluteCoords(this.oCurrent_popup.eShow_what);
			oPopup_min.iLeft += 24;
			oPopup_min.iTop += 10;
			if (this.oMark.bUnplaced && this.oCurrent_popup.eShow_what.id == this.oMark.oPopup.eShow_what.id) {
				oPopup_min.iLeft += this.oMark.eOverflow.scrollLeft;
				oPopup_min.iTop += this.oMark.eOverflow.scrollTop;
			}
			var oPopup_max = {
				iLeft: oPopup_min.iLeft + this.oCurrent_popup.eShow_what.offsetWidth - 20,
				iTop: oPopup_min.iTop + this.oCurrent_popup.eShow_what.offsetHeight - 3
			};
			var aeSelect = document.getElementsByTagName('select');
			for (var i = 0, oMin, oMax; i < aeSelect.length; i++) {
				oMin = Common.Dom.getAbsoluteCoords(aeSelect[i]);
				oMax = {
					iLeft: oMin.iLeft + aeSelect[i].offsetWidth,
					iTop: oMin.iTop + aeSelect[i].offsetHeight
				};
				if (oMin.iLeft < oPopup_max.iLeft && oMax.iLeft > oPopup_min.iLeft && oMin.iTop < oPopup_max.iTop && oMax.iTop > oPopup_min.iTop) {
					aeSelect[i].style.visibility = 'hidden';
				}
			}
		}
	},
	after_popup_closed: function () {
		if (navigator.userAgent.match(/MSIE\s[56]/)) {
			var aeSelect = document.getElementsByTagName('select');
			for (var i = 0; i < aeSelect.length; i++) {
				aeSelect[i].style.visibility = 'visible';
			}
		}
	}
};
Common.Event.add(document, 'click', function (oEvent) {
	if (oMain.oCurrent_popup && !oMain.bDont_close_popup) {
		oMain.oCurrent_popup.close();
	}
});
oMain.Popup = function (oParams) {
	this.fOn_open = oParams.fOn_open;
	this.eOn = oParams.eOn;
	this.sHide_class = oParams.sHide_class;
	this.eShow_on = oParams.eShow_on;
	this.eShow_what = oParams.eShow_what;
	if (oParams.bCreate_frame) {
		var eFrame = document.createElement('ins');
		eFrame.className = 'framed popup-box';
		eFrame.innerHTML = '<ins class="f"><ins class="t l"><ins></ins></ins><ins class="t r"><ins></ins></ins><ins class="c"></ins><ins class="b l"><ins></ins></ins><ins class="b r"><ins></ins></ins></ins>';
		this.eContent = Common.Dom.getElementsByClassName(eFrame, 'c')[0];
		if (this.eOn) {
			this.eOn.appendChild(eFrame);
		} else if (this.eShow_what) {
			this.eShow_what.parentNode.appendChild(eFrame);
		}
		if (!this.eShow_what) {
			this.eShow_what = eFrame;
		}
	}
	var oThis = this;
	this.eClose = document.createElement('div');
	this.eClose.className = 'close';
	this.eShow_what.appendChild(this.eClose);
	this.eClose.innerHTML = '<ins></ins>';
	oMain.fix_hover(this.eClose, 'close');
	Common.Event.add(this.eClose, 'click', function (oEvent) {
		oThis.close();
	});
	Common.Event.add(this.eShow_on, 'click', function (oEvent) {
		if (!oThis.bOpened) {
			oThis.open();
		} else {
			oThis.close();
		}
		Common.Event.cancel(oEvent);
	});
	Common.Event.add(this.eShow_what, 'click', function (oEvent) {
		oMain.bDont_close_popup = true;
		setTimeout('oMain.bDont_close_popup = false;', 0);
		//Common.Event.cancel(oEvent);
	});
	this.close();
	if (this.eOn) {
		Common.Class.add(this.eOn, 'inited');
	}
};
oMain.Popup.prototype = {
	open: function () {
		if (oMain.oCurrent_popup) {
			oMain.oCurrent_popup.close();
		}
		this.bOpened = true;
		oMain.oCurrent_popup = this;
		this.eShow_what.style.display = 'block';
		oMain.before_popup_open();
		if (this.fOn_open) {
			this.fOn_open();
		}
	},
	close: function () {
		this.bOpened = false;
		oMain.oCurrent_popup = null;
		oMain.after_popup_closed();
		this.eShow_what.style.display = 'none';
	}
};
oMain.Filmstrip = function (oParams) {
	var oThis = this;
	oThis.eOriginal = oParams.eOriginal;
	var eBorder = document.createElement('ins');
	eBorder.className = 'border';
	oThis.eOriginal.appendChild(eBorder);
	if (!oParams.sThumbnail_tag_name) {
		oParams.sThumbnail_tag_name = 'a';
	}
	var aeThumbnail = oParams.eThumbnails.getElementsByTagName(oParams.sThumbnail_tag_name);
	this.iCount = 0;
	if (oParams.sThumbnail_tag_name.toLowerCase() != 'a') {
		for (var i = 0, eA; i < aeThumbnail.length; i++) {
			aeA = aeThumbnail[i].getElementsByTagName('a');
			if (aeA.length) {
				if (Common.Class.match(aeA[0], 'selected')) {
					oThis.eSelected = aeA[0];
				}
				oThis.create_thumbnail(aeA[0]);
				this.iCount++;
			}
		}
	} else {
		for (var i = 0; i < aeThumbnail.length; i++) {
			if (Common.Class.match(aeThumbnail[i], 'selected')) {
				oThis.eSelected = aeThumbnail[i];
			}
			oThis.create_thumbnail(aeThumbnail[i]);
			this.iCount++;
		}
	}
	if (oParams.iLimit && aeThumbnail.length > oParams.iLimit) {
		for (i = oParams.iLimit; i < aeThumbnail.length; i++) {
			Common.Class.add(aeThumbnail[i], 'hidden');
		}
		var eThumbnail = document.createElement(oParams.sThumbnail_tag_name);
		eThumbnail.className = 'remove_limit';
		oParams.eThumbnails.appendChild(eThumbnail);
		eThumbnail.innerHTML = oParams.sRemove_limit_HTML;
		var eThumbnail_pseudo = oMain.make_pseudo(eThumbnail);
		Common.Event.add(eThumbnail_pseudo, 'click', function () {
			Common.Class.add(oParams.eThumbnails, 'show_hidden');
		});
	} else if (oParams.asDescription) {
		oThis.eOn = oParams.eOn;
		oThis.eDescription = oParams.eDescription;
		oThis.asDescription = oParams.asDescription;
		oThis.ePrevious = oParams.ePrevious;
		oThis.eNext = oParams.eNext;
		var eClose_pseudo = oParams.eClose;
		oMain.fix_hover(eClose_pseudo, 'close');
		Common.Event.add(eClose_pseudo, 'click', function () {
			oThis.close_photo();
		});
		oThis.aeThumbnail = oParams.eThumbnails.getElementsByTagName('a');
		if (oThis.aeThumbnail.length) {
			for (var i = 0; i < oThis.aeThumbnail.length; i++) {
				oThis.create_popup_photo(i);
			}
		}
		oThis.create_arboxs_photo(oThis.ePrevious, -1);
		oThis.create_arboxs_photo(oThis.eNext, 1);
	}
};
oMain.Filmstrip.prototype = {
	create_thumbnail: function (eOn) {
		var oThis = this;
		Common.Event.add(eOn, 'click', function (oEvent) {
			if (oThis.eSelected) {
				Common.Class.remove(oThis.eSelected, 'selected');
			}
			oThis.eSelected = eOn;
			Common.Class.add(oThis.eSelected, 'selected');
			oThis.modify_original(eOn);
			Common.Event.cancel(oEvent);
		});
	},
	modify_original: function (eOn) {
		var eImage = this.eOriginal.getElementsByTagName('img')[0];
		eImage.src = eOn.href;
		eImage.alt = eOn.title;
	},
	create_popup_photo: function (iOn) {
		var oThis = this;
		var eOn = oThis.aeThumbnail[iOn];
		Common.Event.add(eOn, 'click', function (oEvent) {
			oThis.popup_photo(iOn);
			Common.Event.cancel(oEvent);
		});
	},
	create_arboxs_photo: function (eOn, iStep) {
		var oThis = this;
		oMain.fix_hover(eOn);
		Common.Event.add(eOn, 'click', function (oEvent) {
			var iePhoto = iStep;
			if (oThis.eSelected.iOn) {
				iePhoto = oThis.eSelected.iOn + iStep;
			}
			oThis.popup_photo(iePhoto);
		});
	},
	popup_photo: function (iOn) {
		var oThis = this;
		var eOn = oThis.aeThumbnail[iOn];
		if (!eOn) {
			return false;
		}
		if (oThis.eSelected) {
			Common.Class.remove(oThis.eSelected, 'selected');
		}
		oThis.eSelected = eOn;
		oThis.eSelected.iOn = iOn;
		Common.Class.add(oThis.eSelected, 'selected');
		oThis.eOriginal.innerHTML = '<img src="' + eOn.href + '" alt="' + eOn.title + '" />';
		oThis.eDescription.innerHTML = oThis.asDescription[iOn];
		if (iOn && iOn == oThis.aeThumbnail.length - 1) {
			Common.Class.add(oThis.eNext, 'disabled');
			Common.Class.remove(oThis.ePrevious, 'disabled');
		} else if (!iOn) {
			Common.Class.remove(oThis.eNext, 'disabled');
			Common.Class.add(oThis.ePrevious, 'disabled');
		} else {
			Common.Class.remove(oThis.eNext, 'disabled');
			Common.Class.remove(oThis.ePrevious, 'disabled');
		}
		Common.Class.add(oThis.eOn, 'framed_open');
	},
	close_photo: function (oParams) {
		var oThis = this;
		if (oThis.eSelected) {
			Common.Class.remove(oThis.eSelected, 'selected');
		}
		Common.Class.remove(oThis.eOn, 'framed_open');
	}
};
oMain.Viewer = function (oParams) {
	var oThis = this;
	setTimeout(function () {
		oThis.eOn = oParams.eOn;
		if (oParams.sNavigation_title) {
			var eNavigation = document.createElement('p');
			eNavigation.className = 'navigation';
			eNavigation.innerHTML = oParams.sNavigation_title;
			eParent.parentNode.insertBefore(eNavigation, eParent);
			oParams.eNavigation_insert_before.parentNode.insertBefore(eNavigation, oParams.eNavigation_insert_before);
		}
		var eNavigation = document.createElement('ul');
		oParams.eNavigation_insert_before.parentNode.insertBefore(eNavigation, oParams.eNavigation_insert_before);
		eNavigation.className = 'navigation viewer';
		for (var i = 0, eItem; i < oParams.aoNavigation.length; i++) {
			eItem = document.createElement('li');
			eItem.className = 'item';
			eNavigation.appendChild(eItem);
			eItem.innerHTML = oParams.aoNavigation[i].sTitle;
			oThis.create_navigation_item(eItem, oParams.aoNavigation[i].sClass, oParams.aoNavigation[i].bSelected);
		}
	}, 0);
};
oMain.Viewer.prototype = {
	create_navigation_item: function (eOn, sClass, bSelected) {
		var oThis = this;
		var ePseudo = oMain.make_pseudo(eOn);

		function click() {
			if (oThis.eSelected) {
				Common.Class.remove(oThis.eSelected, 'selected');
			}
			if (oThis.sClass) {
				Common.Class.remove(oThis.eOn, oThis.sClass);
			}
			oThis.eSelected = eOn;
			Common.Class.add(oThis.eSelected, 'selected');
			if (sClass) {
				oThis.sClass = sClass;
				Common.Class.add(oThis.eOn, oThis.sClass);
			}
		}
		Common.Event.add(ePseudo, 'click', click);
		if (!oThis.eSelected || bSelected) {
			click();
		}
	}
};
oMain.Filter = function (oParams) {
	this.eInput = oParams.eInput;
	this.aoChild = [];
	for (var i = 0; i < oParams.eOn.childNodes.length; i++) {
		if (oParams.eOn.childNodes[i].nodeType != 3) {
			this.aoChild[this.aoChild.length] = {
				eOn: oParams.eOn.childNodes[i],
				sText: oParams.eOn.childNodes[i].innerHTML.replace(/<[a-z\d]+\s[^<>]*(alt|title)\s*=\s*(\"([^\"]+)\"|\'([^\']+)\'|([^\s]+))[^<>]*>/gi, ' $3$4$5 ').replace(/<[^<>]+>/g, ' ').replace(/\s+/g, ' ')
			};
			this.sText += '|||' + this.aoChild[this.aoChild.length - 1].sText;
		}
	}
	this.eOn = this.aoChild[0].eOn.parentNode;
	var oThis = this;
	Common.Event.add(this.eInput, 'keyup', function () {
		setTimeout(function () {
			oThis.match_text();
		}, 0);
	});
};
oMain.Filter.prototype = {
	match_text: function () {
		var sValue = this.eInput.value ? this.eInput.value.replace(/\s+/g, ' ').replace(/(^\s+|\s+$)/g, '') : '';
		if (sValue) {
			var pValue = new RegExp(sValue, 'i');
			if (this.sText.match(pValue)) {
				Common.Class.remove(this.eOn, 'unmatched');
				Common.Class.add(this.eOn, 'matched');
				for (var i = 0; i < this.aoChild.length; i++) {
					if (!this.aoChild[i].sText.match(pValue)) {
						Common.Class.add(this.aoChild[i].eOn, 'unmatched');
					} else {
						Common.Class.remove(this.aoChild[i].eOn, 'unmatched');
					}
				}
			} else {
				Common.Class.remove(this.eOn, 'matched');
				Common.Class.add(this.eOn, 'unmatched');
				for (var i = 0; i < this.aoChild.length; i++) {
					Common.Class.add(this.aoChild[i].eOn, 'unmatched');
				}
			}
		} else {
			for (var i = 0; i < this.aoChild.length; i++) {
				Common.Class.remove(this.aoChild[i].eOn, 'unmatched');
			}
		}
	}
};
oMain.oMark = {
	iClip_rotate_sensivity: 20,
	iGhost_steps: 20,
	iMin_speed: 1,
	iMax_speed: 20,
	iAcceleration: 1,
	create: function (oParams) {
		setTimeout(function () {
			oMain.oMark.init(oParams);
		}, 0);
	},
	init: function (oParams) {
		this.oCurrent_position = {};
		this.eOn = oParams.eOn;
		this.ePlace = oParams.ePlace;
		this.eData = oParams.eData;
		this.eShow_on = oParams.eShow_on;
		this.eOverflow = document.getElementById('overflow');
		if (!this.eOverflow) {
			this.eOverflow = document.body;
		}
		this.create_control(oParams);
		this.oMarks.create();
		this.oPopup = new oMain.Popup(Common.Object.extend(oParams, {
			sHide_class: 'hide'
		}, true));
		var oThis = this;
	},
	create_control: function (oParams) {},
	onresize: function () {
		this.set_position(Common.Dom.getAbsoluteCoords(this.eOn));
		if (this.bUnplaced) {
			var oFinish_position = {
				iLeft: this.oCurrent_position.iLeft,
				iTop: this.oCurrent_position.iTop
			};
			if (this.oCurrent_position.iTop > this.eOverflow.offsetHeight) {
				oFinish_position.iTop = this.eOverflow.offsetHeight;
				this.oClip.rotate(90, true);
				this.set_position(oFinish_position);
				oFinish_position.iTop = oFinish_position.iTop - this.iHeight;
			} else if (this.oCurrent_position.iTop < 0) {
				oFinish_position.iTop = -this.iHeight;
				this.oClip.rotate(270, true);
				this.set_position(oFinish_position);
				oFinish_position.iTop = 0;
			}
			if (this.oCurrent_position.iLeft > this.eOverflow.offsetWidth) {
				oFinish_position.iLeft = this.eOverflow.offsetWidth - this.iWidth;
				this.oClip.rotate(180, true);
				this.set_position(oFinish_position);
				oFinish_position.iLeft = oFinish_position.iLeft - this.iWidth;
			} else if (this.oCurrent_position.iLeft < 0) {
				oFinish_position.iLeft = -this.iWidth;
				this.oClip.rotate(0, true);
				this.set_position(oFinish_position);
				oFinish_position.iLeft = 0;
			}
		}
	},
	set_position: function (oPosition) {
		if (this.oCurrent_position.iLeft != oPosition.iLeft) {
			this.oCurrent_position.iLeft = oPosition.iLeft;
			this.eOn.style.left = this.oCurrent_position.iLeft + 'px';
		}
		if (this.oCurrent_position.iTop != oPosition.iTop) {
			this.oCurrent_position.iTop = oPosition.iTop;
			this.eOn.style.top = this.oCurrent_position.iTop + 'px';
		}
	},
	oMarks: {
		create: function () {
			var aeMark = Common.Dom.getElementsByClassName(document.body, 'mark');
			for (i = 0; i < aeMark.length; i++) {
				new this.Link({
					eOn: aeMark[i].getElementsByTagName('a')[0]
				});
			}
		}
	}
};
oMain.oMark.oMarks.Link = function (oParams) {
	this.eOn = oParams.eOn;
	oMain.fix_hover(this.eOn.parentNode.parentNode);
	this.bSelected = Common.Class.match(this.eOn, 'selected');
	var oThis = this;
	Common.Event.add(oParams.eOn, 'click', function (oEvent) {
		oThis.click();
		Common.Event.cancel(oEvent);
	});
	Common.Event.add(oParams.eOn, 'mouseover', function () {
		Common.Class.add(oMain.oMark.eShow_on, 'hover');
	});
	Common.Event.add(oParams.eOn, 'mouseout', function () {
		Common.Class.remove(oMain.oMark.eShow_on, 'hover');
	});
};
oMain.oMark.oMarks.Link.prototype = {
	click: function () {
		this.bSelected = Common.Class.match(this.eOn, 'selected');
		if (this.bSelected) {
			this.load(this.eOn.href + '&cancel=true' + '&AJAX=true');
			Common.Class.remove(this.eOn, 'selected');
		} else {
			if (!oMain.oMark.bShowed) {
				oMain.oMark.bShowed = true;
			} else {}
			this.load(this.eOn.href.replace(/cancel=true/g, '') + '&AJAX=true');
			Common.Class.add(this.eOn, 'selected');
		}
		this.bSelected = !this.bSelected;
	},
	load: function (sURL) {
		if (!this.oLoader) {
			var oThis = this;
			this.oLoader = new Common.Ajax.Helper('', function (o) {
				oThis.onload(o)
			});
		}
		this.oLoader.sUrl = sURL;
		this.oLoader.fErrorCallback = function () {};
		this.oLoader.send();
	},
	onload: function (oResponse) {
		oMain.oMark.eData.innerHTML = oResponse.responseText;
	}
};

