// 
// Enlarge image functions
// ver 03.06.2009 DD.MM.YYYY
//
function showImagePopup(sImageSrc, iWidth, iHeight) {
	if(sImageSrc && typeof(sImageSrc) == 'string' && sImageSrc != '')
	{
		var oImage = new Image;
		var iDefWidth = 300;
		var iDefHeight = 300;
		var iLeft = Math.floor((screen.width - iDefWidth)/2-5);
		var iTop = Math.floor((screen.height - iDefHeight)/2-14);
		oImage.src = sImageSrc;
		var sFeatures = 'directories=no,location=no,toolbar=no,status=no,dependent=yes,titlebar=no,scrollbars=no,resizable=yes,width='+iDefWidth+',height='+iDefHeight+',left='+iLeft+',top='+iTop;

		var rsWindow = window.open('', null, sFeatures);
		rsWindow.document.write( 
			'<html><head>' +
			'<' + 'script type="text/javascript">' +
			'function resizeWin(oImage) {' +
			'	var iImgWidth = oImage.width;' +
			'	var iImgHeight = oImage.height;' +
			'	var iWidth = Math.min(iImgWidth, screen.width - 10);' +
			'	var iHeight = Math.min(iImgHeight, screen.height - 28);' +
			'	var iLeft = (iWidth < screen.width - 10) ? Math.floor((screen.width - iWidth)/2-5) : 0;' +
			'	var iTop = (iHeight < screen.height - 28) ? Math.floor((screen.height - iHeight)/2-14) : 0;' +
			'	var iDx = iWidth - 300;' +
			'	var iDy = iHeight - 300;' +
			'	try {' +
			'		window.resizeBy(iDx, iDy);' +
			'		window.moveTo(iLeft, iTop);' +
			'	} catch(e) {}' +
			'}' +
			'function KeyPress() {' +
			'	if(window.event.keyCode == 27) { window.close(); }' +
			'}' +
			'</' + 'script>' +
			'<title> </title></head>' +
			'<body style="padding:0; margin:0;" onkeypress="KeyPress()">' +
			'<img src="' + sImageSrc + '" border="0" alt="" onload="resizeWin(this)" />' +
			'</body></html>'
		);
		rsWindow.focus();
		rsWindow.document.close();
	}
};

function showEnlarge() {
	$('img.enlarge').parent('a').each(function(){
		var sUrl = $(this).attr('href');

		var showPopupHandler = function() {
			showImagePopup(sUrl);
			return false;
		};

		$(this).get(0).onclick = function(){
			return false;
		};
		var jqWrap = null;
		$(this).wrap('<div class="enlarge-wrap-pad"></div>');
		jqWrap = $(this).parent('div.enlarge-wrap-pad');
		$(jqWrap).wrap('<div class="enlarge-wrap-block"></div>');
		jqWrap = $(jqWrap).parent('div.enlarge-wrap-block');
		$(jqWrap).append('<div class="empty"></div>');
		$(jqWrap).click(showPopupHandler);
		var iWidth = $(jqWrap).width();
		if(iWidth && iWidth < 85) {
			$(jqWrap).width(85);
			$(jqWrap).css('padding-top', '1px');
		}
	});
};


// 
// showPopup
//
function showPopup(mUrl, iWidth, iHeight) {
	if(mUrl) {
		var sUrl = false;
		if(typeof(mUrl) == 'object' && mUrl.href) {
			sUrl = mUrl.href;
		} else if(typeof(mUrl) == 'string') {
			sUrl = mUrl;
		}
		if(sUrl) {
			try {
				var rPattern = /\?/;
				var sSign = rPattern.test(sUrl) ? '&' : '?';
				sUrl += sSign + 'popup=Y';
				iWidth = iWidth ? parseInt(iWidth) : 645;
				iHeight = iHeight ? parseInt(iHeight) : 680;
				var iLeft = Math.floor((screen.width - iWidth)/2-5);
				var iTop = Math.floor((screen.height - iHeight)/2-14);

				var sFeatures = 'directories=no,location=no,toolbar=no,status=no,resizeble=yes,scrollbars=yes,dependent=yes,titlebar=no,left='+iLeft+',top='+iTop;
				sFeatures += iWidth > 0 ? (',width=' + iWidth) : '';
				sFeatures += iHeight > 0 ? (',height=' + iHeight) : '';
				var rsWin = window.open(sUrl, null, sFeatures);
				rsWin.focus();
			} catch(e) { }
		}
	}
	return false;
};

// 
// CMainMenu
// ver 11.05.2009 DD.MM.YYYY
//
CMainMenu = function() {
	var _this = this;
	this.arItems = new Array();
	this.iDefIdx = '';
	this.iTimer = '';
	
	this.init = function(arItems, iDefIdx) {
		this.iDefIdx = iDefIdx;
		this.arItems = arItems;
		addEvent(window, 'load', this.__init);
	};

	this.__init = function() {
		var sId;
		for(var i in _this.arItems) {
		  	sId = _this.arItems[i];
		
			addEvent('menu_main_' + sId, 'mouseover', (function(_sId) {
				return function() {
					_this.ShowHideMenu(_sId);
				}
			})(sId));
			addEvent('menu_main_' + sId, 'mouseout', (function(_sId) {
				return function() {
					_this.ShowHideMenu(_sId, true);
				}
			})(sId));
	        }
	};
	
	this.ShowHideMenu = function(sId, bHide) {
		clearTimeout(this.iTimer);
		var fCallback = function() {
			if(bHide) {
				_this.showSubMenu(_this.iDefIdx);
			} else {
				_this.showSubMenu(sId);
			}
		}
		this.iTimer = setTimeout(fCallback, 200);
	};
	
	this.showSubMenu = function(sId) {
		this.hideSubMenu(sId);
		var oMainItem = getObjectByMixedParam('menu_main_' + sId);
		var oSubItem = getObjectByMixedParam('menu_sub_' + sId);
		if(oMainItem) {
			oMainItem.className = 'main-menu-item-block main-menu-item-selected';
		}
		if(oSubItem) {
			oSubItem.style.display = 'block';
		}
	};

	this.hideSubMenu = function(sActiveId) {
		var sId, oMainItem, oSubItem;
		for(var i in this.arItems) {
			sId = this.arItems[i];
			if(sId != sActiveId) {
	        		var oMainItem = getObjectByMixedParam('menu_main_' + sId);
				var oSubItem = getObjectByMixedParam('menu_sub_' + sId);
				if(oMainItem) {
					oMainItem.className = 'main-menu-item-block';
				}
				if(oSubItem) {
					oSubItem.style.display = 'none';
				}
			}
	        }
	};
};

// 
// CLeftMenu 
// ver 20.05.2009 DD.MM.YYYY
// 
CLeftMenu = function() {
	var _this = this;
	this.arItems = new Array();
	this.sCurShowed = '';
							
	this.init = function(arItems) {
		this.arItems = arItems;
		/*$(document).ready(function(){		íå ðàáîòàëî â Dicsovery 4, çàêàìåíòèë 29.10.09	*/
			var sId;
			for(var i in _this.arItems) {
		  		sId = _this.arItems[i];
	                	$('#left_menu_idx_' + sId).hover(
	                		function() {
	                			this.className = 'left-menu-item-bullet-hover ie-cursor';
		                	},
					function() {
	        	        		this.className = 'left-menu-item-bullet ie-cursor';
	            			}
	                	);
	                	_this.setOnclick(sId);
		        }
		/*});*/
	};
	
	this.setOnclick = function(sId) {
               	$('#left_menu_idx_' + sId).bind('click', function() {
			_this.toggleSubMenu(sId);
               	});
	};
	
	this.toggleSubMenu = function(sId) {
		var sDisplay = _this.sCurShowed == sId ? 'none' : 'block';
		_this.sCurShowed = _this.sCurShowed == sId ? '' : sId;
               	$('#left_menu_idx_' + sId + '_sub').css({'display': sDisplay});
               	_this.hideUnactive();
	};

	this.hideUnactive = function() {
		var sId;
		for(var i in _this.arItems) {
	  		sId = _this.arItems[i];
	  		if(sId != _this.sCurShowed) {
				$('#left_menu_idx_' + sId + '_sub').css({'display': 'none'});
	  		}
	        }
	};
};

// 
// CHomeAdvBlock 
// ver 12.06.2009 DD.MM.YYYY
// 
CHomeAdvBlock = function() {
	var _this = this;
	this.arItems = new Array();
	this.arImages = new Object();
	this.arShowedObject = new Object();
	this.arSlides = new Array();
	this.iTimeSlide = new Array();
	this.iTimer = 0;
	this.iTimeoutMenuHover = 0;
	this.iMenuEnterTimer = 0;
	this.iMenuLeaveTimer = 0;
	this.bMenuHidden = true;
	this.iCurSlide = 0;
	this.bCanAnimate = false;
	this.oImagesLoaded = new Object();
	this.iWaitLoading = 0;
	this.iImagesCount = 0;
	this.iImagesLoaded = 0;
		
	this.init = function(arItems, arImages) {
		this.arItems = arItems;
		this.arImages = arImages;
		$(document).ready(function(){
			_this.preloadImages();
			var sId;
			for(var i in _this.arItems) {
		  		sId = _this.arItems[i];

	                	$('#menu_hover_' + sId).bind('mouseenter', (function(_sId) {
	                		return function() {
						clearTimeout(_this.iMenuEnterTimer);
						_this.iMenuEnterTimer = setTimeout(function(){
							_this.showMenuItem(_sId);
						}, _this.iTimeoutMenuHover);
						_this.iTimeoutMenuHover = 150;
	                		};
	                	})(sId));

	                	$('#menu_hover_' + sId).bind('mouseleave', (function(_sId) {
	                		return function() {
						clearTimeout(_this.iMenuEnterTimer);
						clearTimeout(_this.iMenuLeaveTimer);
						_this.iMenuLeaveTimer = setTimeout(function(){
							_this.hideMenuItem(_sId);
						}, 100);
	                		};
	                	})(sId));
		        }

		        $('#home-adv-back-menu-block').bind('mouseenter', function() {
				_this.showHoverMenu();
		        });

		        $('#home-adv-hover-menu-block').bind('mouseleave', function() {
		        	_this.hideHoverMenu();
		        });

		        $('#home-adv-block').bind('mouseleave', function() {
		        	_this.hideHoverMenu();
		        });

			$('#home-adv-shade').bind('mouseenter', function() {
		        	_this.hideHoverMenu();
			});
		});
	};
	
	this.showLoadingWait = function() {
		$('#home-adv-loading-shade').css('display', 'block');
		$('#home-adv-loading-shade').bind('click', function() {
			$('#home-adv-loading-shade').css('display', 'none');
		});
	};
	
	this.hideLoadingWait = function() {
		$('#home-adv-loading-shade').css('display', 'none');
	};

	this.processAllImages = function() {
		var oImg;
		for(var i in _this.oImagesLoaded) {
			oImg = _this.oImagesLoaded[i];
			$('#'+oImg.id).css({'background-image': 'url('+oImg.oImage.src+')'});
		}
		_this.hideLoadingWait();
	};

	this.preloadImagesItems = function(i) {
		//document.getElementById('test').innerHTML += ' preloadImagesItems i ='+i+' - '+_this.oImagesLoaded[i].oImage.src+' <br /> ';
		_this.iImagesLoaded++;
		var oImg;
		oImg = _this.oImagesLoaded[i];
		$('#'+oImg.id).css({'background-image': 'url('+oImg.oImage.src+')'});
		if(_this.iImagesLoaded >= _this.iImagesCount) {
			//_this.processAllImages();
			_this.hideLoadingWait();
			_this.bCanAnimate = true;
		}
	};

	this.preloaderAddEvent = function(i) {
		$(_this.oImagesLoaded[i].oImage).bind('load', function() {
			_this.preloadImagesItems(i);
		});
	};

	this.preloadImages = function() {
		_this.showLoadingWait();
		var oImg;
		for(var i in _this.arImages) {
	  		oImg = _this.arImages[i];
			if(oImg.img) {
				_this.iImagesCount++;
				if(oImg.bWaitLoading) {
					_this.iWaitLoading++;
				}
                                _this.oImagesLoaded[i] = {
                                	'oImage': new Image,
                                	'id': oImg.id,
                                	'waitLoading': oImg.bWaitLoading
                                };
                                _this.preloaderAddEvent(i);
				_this.oImagesLoaded[i].oImage.src = oImg.img;
			}
	        }
	};
	
	this.addShowedObject = function(sId) {
		_this.arShowedObject[sId] = true;
	};
	
	this.removeShowedObject = function(sId) {
		_this.arShowedObject[sId] = false;
	};

	this.removeButObject = function(sButId) {
		var sId;
		for(var i in _this.arItems) {
	  		sId = _this.arItems[i];
	  		if(sId != sButId) {
	  			_this.removeShowedObject(sId);
			}
		}
	};

	this.isShowedObject = function(sId) {
		return _this.arShowedObject[sId] ? _this.arShowedObject[sId] : false
	};

	this.showHoverMenu = function() {
		clearTimeout(_this.iMenuEnterTimer);
		clearTimeout(_this.iMenuLeaveTimer);
		_this.bMenuHidden = false;
		$('#home-adv-hover-menu-block').css({'display': 'block'});
		_this.showShade();
	};

	this.hideHoverMenu = function() {
		clearTimeout(_this.iMenuEnterTimer);
		clearTimeout(_this.iMenuLeaveTimer);
		if(!_this.bMenuHidden) {
			_this.hideShade(function(){
				$('#home-adv-hover-menu-block').css({'display': 'none'});	
			});
		}
		_this.bMenuHidden = true;
	};

	this.showShade = function() {
		$('#home-adv-hover-menu-block').css({'display': 'block'});
		$('#home-adv-shade').stop();
		$('#home-adv-shade').css({'display': 'block', 'opacity': '0'});
		$('#home-adv-shade').fadeTo(200, 0.55, function(){
			$('#home-adv-shade').css({'display': 'block', 'opacity': '0.55'});
		});
	};

	this.hideShade = function(fCalback) {
		_this.iTimeoutMenuHover = 0;
		$('#home-adv-shade').stop();
		_this.hideAllItems();
		$('#home-adv-shade').fadeTo(200, 0, function(){
			$('#home-adv-shade').css({'display': 'none'});
			if(fCalback && typeof(fCalback) == 'function') {
				fCalback();
			}
		});
	};

	this.showMenuItem = function(sActiveId) {
		_this.removeButObject(sActiveId);
		if(!_this.isShowedObject(sActiveId)) {
			_this.addShowedObject(sActiveId);
        	       	_this.stopItemAnimationById(sActiveId);
			_this.showItemById(sActiveId);
		}
		_this.hideUnactiveItem();
	};

	this.hideMenuItem = function(sActiveId) {
		if(_this.isShowedObject(sActiveId)) {
			_this.removeShowedObject(sActiveId);
		}
		_this.hideUnactiveItem();
	};

	this.hideUnactiveItem = function() {
		var sId;
		for(var i in _this.arItems) {
	  		sId = _this.arItems[i];
		  	if(!_this.isShowedObject(sId)) {
				_this.stopItemAnimationById(sId);
				_this.hideItemById(sId);
               		}
		}
	};

	this.hideAllItems = function() {
		var sId;
		for(var i in _this.arItems) {
	  		sId = _this.arItems[i];
	  		_this.removeShowedObject(sId);
			_this.stopItemAnimationById(sId);
			_this.hideItemById(sId);
		}
	};

	this.stopAllItemAnimation = function() {
           	clearTimeout(_this.iMenuEnterTimer);
		var sId;
		for(var i in _this.arItems) {
	  		sId = _this.arItems[i];
		  	if(!_this.isShowedObject(sId)) {
				_this.stopItemAnimationById(sId);
               		}
		}
	};

	this.stopItemAnimationById = function(sId) {
		var jqItem_menu = $('div.home-adv-hover-menu-item-img', '#menu_hover_' + sId);
		var jqItem_popup = $('div.home-adv-hover-menu-item-popup', '#menu_hover_' + sId);
		$(jqItem_menu).stop();
		$(jqItem_popup).stop();		
	};
		
	this.showItemById = function(sId) {
		var jqItem_menu = $('div.home-adv-hover-menu-item-img', '#menu_hover_' + sId);
		var jqItem_block = $('div.home-adv-hover-menu-item-elements', '#menu_hover_' + sId);
		var jqItem_submenu = $('div.home-adv-hover-menu-item-popup-submenu', '#menu_hover_' + sId);
		var jqItem_popup = $('div.home-adv-hover-menu-item-popup', '#menu_hover_' + sId);

		$(jqItem_submenu).css({'visibility': 'hidden', 'display': 'none'});
		$(jqItem_menu).css({'visibility': 'visible', 'display': 'block', 'opacity': '0'});
		$(jqItem_block).css({'visibility': 'visible', 'display': 'block', 'opacity': '1'});
		$(jqItem_popup).css({'visibility': 'visible', 'display': 'block', 'opacity': '0'});

		$(jqItem_menu).fadeTo(450, 1, function() {
			_this.hideUnactiveItem();
		});
		
		$(jqItem_popup).fadeTo(400, 1, function(){
			$(jqItem_popup).css({'visibility': 'visible', 'display': 'block'});
			$(jqItem_submenu).css({'visibility': 'visible', 'display': 'block'});
		});
	};

	this.hideItemById = function(sId) {
		var jqItem_menu = $('div.home-adv-hover-menu-item-img', '#menu_hover_' + sId);
		var jqItem_block = $('div.home-adv-hover-menu-item-elements', '#menu_hover_' + sId);
		var jqItem_submenu = $('div.home-adv-hover-menu-item-popup-submenu', '#menu_hover_' + sId);
		var jqItem_popup = $('div.home-adv-hover-menu-item-popup', '#menu_hover_' + sId);
		$(jqItem_submenu).css({'visibility': 'hidden', 'display': 'none'});
		$(jqItem_menu).fadeTo(350, 0);
		$(jqItem_popup).fadeTo(300, 0, function(){
			$(jqItem_popup).css({'visibility': 'hidden', 'display': 'none'});
		});
	};

	this.initSlides = function(arSlides, iTimeSlide) {
		this.iCurSlide = 0;
		this.arSlides = arSlides;
		this.iTimeSlide = iTimeSlide;
		$(document).ready(function(){
			_this.showSlides();
		});
	};

	this.showSlides = function() {
		clearTimeout(_this.iTimer);
		_this.iTimer = setTimeout(_this.showNextSlide, _this.iTimeSlide);
	};
	
	this.showNextSlide = function() {
		clearTimeout(_this.iTimer);
		if(_this.bCanAnimate) {
			var iMax = _this.arSlides.length - 1;
			var iShowIdx = (_this.iCurSlide + 1) > iMax ? 0 : (_this.iCurSlide + 1);
			var sNextId = _this.arSlides[iShowIdx];
			var sCurId = _this.arSlides[_this.iCurSlide];
			var jqCurSlide = $('#' + sCurId);
			var jqNextSlide = $('#' + sNextId);
			$(jqCurSlide).fadeTo(500, 0, function(){
				$(jqCurSlide).css({'visibility': 'hidden', 'display': 'none'});
				$(jqNextSlide).css({'visibility': 'visible', 'display': 'block', 'opacity': '0'});
				$(jqNextSlide).fadeTo(700, 1, function(){
					$(jqNextSlide).css({'visibility': 'visible', 'display': 'block', 'opacity': '1'});
					_this.iCurSlide = iShowIdx;
					_this.iTimer = setTimeout(_this.showNextSlide, _this.iTimeSlide);
				});
			});
		} else {
			_this.iTimer = setTimeout(_this.showNextSlide, _this.iTimeSlide);
		}
	}
};

// 
// CImgGallery 
// ver 21.05.2009 DD.MM.YYYY
// 
CImgGallery = function() {
	var _this = this;
	this.arItems = new Array();
	this.sCurActiveId = '';
	this.sIdPrefix = '';
	this.arOptions = new Array();
	this.iItemsCount = 0;

	this.init = function(arItems, sCurActiveId, sIdPrefix, arOptions) {
		this.arItems = arItems;
		this.sCurActiveId = sCurActiveId;
		this.sIdPrefix = sIdPrefix;
		this.arOptions = arOptions;
                if(this.arItems) {
			$(document).ready(function(){
				if(_this.sIdPrefix) {
					var oItem = null;
					for(var i in _this.arItems) {
					        ++_this.iItemsCount;
		  				oItem = _this.arItems[i];
	                			$('#' + _this.sIdPrefix + oItem.ID).hover(
	                				function() {
	                					$(this).addClass(_this.arOptions['sHoverClass']);
				                	},
							function() {
	        		        			$(this).removeClass(_this.arOptions['sHoverClass']);
	            					}
	                			);
		                		_this.setOnclick(oItem.ID);
				        }
				}
			        _this.showLargePic(sCurActiveId);
			});
		}
	};
	
	this.getObjectBySid = function(sId) {
		var oItem = null;
		if(sId) {
			for(var i in _this.arItems) {
				if(_this.arItems[i].ID == sId) {
					oItem = _this.arItems[i];
				}
		        }
		}
	        return oItem;
	};
	
	this.setOnclick = function(sId) {
               	$('#' + _this.sIdPrefix + sId).bind('click', function() {
               		return _this.showLargePic(sId);
               	});
		var oDomEl = null;
		if(oDomEl = $('a.replace-url', '#' + _this.sIdPrefix + sId).get(0)) {
			oDomEl.onclick = function(){
				return false;
			};
		}
	};
	
	this.showLargePic = function(sId) {
		if(sId && _this.arOptions['sLargeImgBlock']) {
			var oItem = null;
			if(oItem = _this.getObjectBySid(sId)) {
				var jqTmp;
				var oImage = new Image;
				oImage.onload = function() {
					$(_this.arOptions['sLargeImgBlock']).html('<img src="'+this.src+'" alt="" />');
				};
				oImage.src = oItem.SRC;

               			if(_this.arOptions['sActiveClass'] && _this.sCurActiveId) {
	               			$('#' + _this.sIdPrefix + _this.sCurActiveId).removeClass(_this.arOptions['sActiveClass']);
	           		}

               			if(_this.arOptions['sActiveClass']) {
	               			$('#' + _this.sIdPrefix + sId).addClass(_this.arOptions['sActiveClass']);
	           		}

               			if(_this.arOptions['sNameBlock']) {
	               			$(_this.arOptions['sNameBlock']).html(oItem.NAME);
	           		}

               			if(_this.arOptions['sEnlarge']) {
               				var oDomEl = null;
               				if(oDomEl = $(_this.arOptions['sEnlarge']).get(0)) {
               					oDomEl.onclick = function(){
               						return _this.enlargeShow(oItem.ENLARGE_PAGE_URL);
               					};	
               				}
	           		}

               			if(_this.arOptions['sNavPrev']) {
               				var oDomEl = null;
               				if(oDomEl = $(_this.arOptions['sNavPrev']).get(0)) {
               					oDomEl.onclick = function(){
               						return _this.showLargePic(oItem.PREV_ID);
               					};
               					oDomEl.href = oItem.PREV_PAGE_URL;
               				}
               			}
					
               			if(_this.arOptions['sNavNext']) {
               				var oDomEl = null;
               				if(oDomEl = $(_this.arOptions['sNavNext']).get(0)) {
               					oDomEl.onclick = function(){
               						return _this.showLargePic(oItem.NEXT_ID);
               					};
               					oDomEl.href = oItem.NEXT_PAGE_URL;
               				}
               			}

               			if(_this.arOptions['sNavPage']) {
               				jqTmp = $(_this.arOptions['sNavPage']);
               				$(jqTmp).text(oItem.CUR_IDX);
               			}

				_this.sCurActiveId = sId;
			}
		};
		return false;
	};
	
	this.enlargeShow = function(sEnlargeUrl) {
		if(sEnlargeUrl && sEnlargeUrl != '') {
			var sFeatures = 'directories=no,location=no,toolbar=no,status=no,resizeble=no,dependent=yes,titlebar=no';
			sFeatures += _this.arOptions['iPopupWidth'] > 0 ? (',width=' + _this.arOptions['iPopupWidth']) : '';
			sFeatures += _this.arOptions['iPopupHeight'] > 0 ? (',height=' + _this.arOptions['iPopupHeight']) : '';
			var rsWin = window.open(sEnlargeUrl,null,sFeatures);
		}
		return false;
	};
};
