Kalinka = {
	
	POPUP_DIMENSIONS : {
		width : 500,
		height : 550
	},
	
	init : function(){
		this.domInit();
		this.attachEvents();
		this.initSelfLinks();
		this.initKeyboardNavigation();
		this.initImagesCorners();
		this.initPopups();
	},
	
	domInit : function(){
		this.jPage = $("#page");
	},
	
	attachEvents : function(){
		var that = this;
		$(window).load(
			function(){
				that.preloadImages();
			}
		);
	},
	
	initSelfLinks : function(){
		$("a.self").click(
			function(evt){
				evt.preventDefault();
				return false;
			}
		);
	},
	
	initKeyboardNavigation : function(){
		var that = this;
		this.aKeyboardLinks = [
			{ sName: "Home", iKey: 0x24 }, // Home key
			{ sName: "Parent", iKey: 0x26 }, // Up arrow
			{ sName: "Prev", iKey: 0x25 }, // Left arrow
			{ sName: "Next", iKey: 0x27 }  // Right arrow
		];
		
//		считывыем единожды при загрузке страницы из её head'а адреса для клавиатурной навигации
		this.aKeyboardLinks.foreach(
			function(oLink, i){
				that.aKeyboardLinks[i].sHref = $("#" + oLink.sName + "Link").attr("href");
			}
		);
		
		$(document).keydown(
			function(evt){
				that.keyboardNavigate(evt);
			}
		);
	},
	
	keyboardNavigate : function(evt){
		if(evt.ctrlKey){
			var sHref = null;
			
			this.aKeyboardLinks.foreach(
				function(oLink){
					if(oLink.iKey === evt.keyCode){
						sHref = oLink.sHref;
						return false;
					}
				}
			);

			if(sHref){
				document.location = sHref;
			}
		}
	},
	
	initImagesCorners : function(){
		var that = this;
		$(window).load(
			function(){
				$("img.ictinus").each(
					function(){
						ictinus.cutCorners(this, that.getIctinusParams(this));
					}
				);
			}
		);
	},
	
	getIctinusParams : function(eImg){
		var
			oParams = {},
			sMatch = eImg.className.match(/ictinus_params\-\-\-(.+?)(\s|$)/);
		
		if(sMatch){
			aParams = sMatch[1].split("--");
			aParams.foreach(
				function(sParam){
					var aParam = sParam.split("-");
					sPrefix = aParam[0] === "strokeColor" ? "#" : "";
					oParams[aParam[0]] = sPrefix + aParam[1];
				}
			);
		}
		
		return oParams;
	},
	
	initPopups : function(){
		var that = this;
		$("a.popup").each(
			function(){
				var
					elem = this,
					oDims = {};
				["width", "height"].foreach( // достаем особые размеры всплывающего окна из класса ссылки
					function(sDim){
						var aMatch = elem.className.match(new RegExp(sDim + "_(\\d+)"));
						oDims[sDim] = aMatch ? aMatch[1] : null;
					}
				);
				$(this).click(
					function(evt){
						evt.preventDefault();
						that.popup(this.href, oDims.width, oDims.height);
						return false;
					}
				);
			}
		);
	},
	
	popup : function(sUrl, iCustomWidth, iCustomHeight){
		var
			width = iCustomWidth || this.POPUP_DIMENSIONS.width,
			height = iCustomHeight || this.POPUP_DIMENSIONS.height;
			
		window.open(
			sUrl,
			"",
			[
				"width=" + width,
				"height=" + height,
				"left=" + (screen.availWidth - width) / 2,
				"top=" + (screen.availHeight - height) / 2,
				"menubar=no",
				"toolbar=no",
				"resizable=yes",
				"scrollbars=yes",
				"status=yes"
			].join(",")
		);
	},
	
	addPreload : function(sSrc){
		if(this.aPreloadImages){
			this.aPreloadImages.push(sSrc);
		}
		else{
			this.aPreloadImages = [sSrc];
		}
	},
	
	preloadImages : function(){
		if(this.aPreloadImages){
			var
				that = this;
				fEmpty = function(){},
				
			this.aPreloadImages.foreach(
				function(sImageSrc, i){
					var eImage = document.createElement('img');
					eImage.src = sImageSrc;
					eImage.onload = fEmpty;
					that.aPreloadImages[i] = eImage;
				}
			);
			
			/* Выгружаем все обработчики событий искусственных картинок против утечек памяти в IE */
			$(window).unload(
				function(){
					that.aPreloadImages.foreach(
						function(eImage){
							that.purge(eImage);
						}
					);
				}
			);
		}
	},
	
	purge : function(mInput) {
		if(mInput instanceof Array){
			mInput.foreach(
				function(element){
					this.purgeElement(element);
				}
			);
		}
		else{
			this.purgeElement(mInput);
		}
	},
	
	purgeElement : function(elem){
		var
			a = elem.attributes,
			i;
		if(a){
			for(i = 0, l = a.lengthl ; i < l; i += 1){
				var n = a[i].name;
				if (typeof elem[n] === 'function'){
					elem[n] = null;
				}
			}
		}
		a = elem.childNodes;
		if(a){
			for(i = 0, l = a.length; i < l; i++){
				purge(elem.childNodes[i]);
			}
		}
	}
	
}

$(document).ready(
	function(){
		Kalinka.init();
	}
);