window.addEvent('domready', function () {
	if (isBorderRadiusSupported()) {
		$$('BODY').removeClass('no-border-radius');
		$$('BODY').addClass('border-radius');
	}
	if ($('canvas')) {
		if ($chk(Browser.Engine.trident)) setTimeout('canvasInit();', 500); else canvasInit();
	}
	if ($('nav-rooms')) {
		$$('#nav-rooms .buttons A').each(function (element) {
			element.addEvent('mouseover', function () {
				$$('#nav-rooms .buttons A').each(function (el) {
					if (el != this) el.fade(0.3);
				}.bind(this));
			}.bind(element));
			element.addEvent('mouseout', function () {
				$$('#nav-rooms .buttons A').fade(1);
			}.bind(element));
		});

		$$('#nav-rooms .name A').each(function (element) {
			element.addEvent('mouseover', function () {
				$$('#nav-rooms .buttons A').each(function (el) {
					if (el.get('id') != this.get('oid')) el.fade(0.3);
				}.bind(this));
			}.bind(element));
			element.addEvent('mouseout', function () {
				$$('#nav-rooms .buttons A').fade(1);
			}.bind(element));
		});


	}
	
	
	if ($$('.mooflow').length) {
		$$('.mooflow').each(
			function (item) {
				var mooflow = {
					start: function() {
						var mf = new MooFlow(item, {
							bgColor: 'transparent',
							startIndex: 2,
							useSlider: true,
							useAutoPlay: false,
							useCaption: false,
							useResize: false,
							useMouseWheel: false,
							useKeyInput: true,
							useViewer: true
						});
						mf.attachViewer();
					}
				};
				mooflow.start();
			}
		);
	}
	$$('#languages A').addEvent('mouseover', function (e) {
		$$('#languages .' + this.get('id')).addClass('hover');
	});
	$$('#languages A').addEvent('mouseout', function (e) {
		$$('#languages .' + this.get('id')).removeClass('hover');
	});
	$$('INS.pseudo-link').addEvent('mouseover', function (e) {
		this.removeClass('pseudo-link'); this.addClass('pseudo-link-hover');
	});
	$$('INS.pseudo-link').addEvent('mouseout', function (e) {
		if (!$defined(this.get('oid')) || $$('#' + this.get('oid')).getStyle('display') == 'none') {
			this.removeClass('pseudo-link-hover');
			this.addClass('pseudo-link');
		}
	});
	$$(".description").setStyle('display', 'none');
	
	$$("#text .pseudo-link").addEvent('click', function (e) {
		if ($defined(this.get("oid"))) {
			var obj = $$('#' + this.get("oid"));
			if (obj.getStyle('display') == 'block') {
				this.removeClass("pseudo-link-hover");
				this.removeClass("pseudo-link-open");
				this.addClass("pseudo-link");
				obj.setStyle('display', 'none');
				//var objVerticalSlide = new Fx.Slide(this.get("oid"), { duration: 'short', onComplete: function() { obj.setStyle('display', 'none'); } });
				//objVerticalSlide.slideOut();
			}
			else {
				this.removeClass("pseudo-link");
				this.addClass("pseudo-link-hover");
				this.addClass("pseudo-link-open");
				obj.setStyle('display', 'block');
				//var objVerticalSlide = new Fx.Slide(this.get("oid"), { duration: 'short', onComplete: function() { obj.getParent().setStyles({overflow: 'visible', height: 'auto'}); }});
				//objVerticalSlide.slideIn();
			}
		}
	});
	
	$$('.gallery').each( function (element, index) { new Gallery(element, {id: index}); } );
	
	SqueezeBox.initialize({});
	SqueezeBox.assign($$('INS[rel=boxed]'));

	$$('INS[rel=panorama]').addEvent('click', function (e) {
		SqueezeBox.open(this.get('href'), {
			handler: 'iframe'
		});
	})
	
	$$('#guide-link .pseudo-link').addEvent('click', function (e) {
		SqueezeBox.open('/guide.php', {
			handler: 'iframe',
			size: {x: 870, y: 540}
		});
	});
	
	$$('#stopper-rent A').addEvent('mouseover', function (e) {
		var obj = $('stopper-rent-ani');
		if (this.tw) this.tw.cancel();
		this.tw = new Fx.Tween(obj, {duration: 'normal'}).start('top', 0, -27);
	});
	$$('#stopper-rent A').addEvent('mouseout', function (e) {
		var obj = $('stopper-rent-ani');
		if (this.tw) this.tw.cancel();
		this.tw = new Fx.Tween(obj, {duration: 'short'}).start('top', obj.getStyle('top'), 0);
	});
	$$('#stopper-restaurant A').addEvent('mouseover', function (e) {
		var obj = $('stopper-restaurant-ani');
		if (!obj.isVisible()) obj.setStyle('opacity', 0).show();
		if (this.tw) this.tw.cancel();
		this.tw = new Fx.Tween(obj, {duration: 'normal'}).start('opacity', 0, 1);
	});
	$$('#stopper-restaurant A').addEvent('mouseout', function (e) {
		var obj = $('stopper-restaurant-ani');
		if (this.tw) this.tw.cancel();
		this.tw = new Fx.Tween(obj, {duration: 'short'}).start('opacity', 1, 0);
	});
	$$('#stopper-salon A').addEvent('mouseover', function (e) {
		var obj1 = $('stopper-salon-ani1'), obj2 = $('stopper-salon-ani2');
		if (this.mover1) this.mover1.cancel();
		if (this.mover2) this.mover2.cancel();
		obj1.fade(0.5);
		obj2.fade(0.5);
		this.mover1 = new Fx.Move(obj1, {
			duration: 'normal',
		    relativeTo: $('stopper-salon'),
		    position: 'bottomLeft',
			edge: 'bottomLeft',
		    offset: {x: 0, y: 0}
		}).start();
		this.mover2 = new Fx.Move(obj2, {
			duration: 'normal',
		    relativeTo: $('stopper-salon'),
		    position: 'upperRight',
			edge: 'upperRight',
		    offset: {x: 0, y: 0}
		}).start();
	});
	$$('#stopper-salon A').addEvent('mouseout', function (e) {
		var obj1 = $('stopper-salon-ani1'), obj2 = $('stopper-salon-ani2');
		if (this.mover1) this.mover1.cancel();
		if (this.mover2) this.mover2.cancel();
		this.mover1 = new Fx.Move(obj1, {
			duration: 'short',
		    relativeTo: $('stopper-salon'),
		    position: 'upperLeft',
			edge: 'upperLeft',
		    offset: {x: 62, y: 110}
		}).start();
		this.mover2 = new Fx.Move(obj2, {
			duration: 'short',
		    relativeTo: $('stopper-salon'),
		    position: 'upperLeft',
			edge: 'upperLeft',
		    offset: {x: 139, y: 20}
		}).start();
		obj1.fade(1);
		obj2.fade(1);
	});
});

//Проверка поддержки браузером свойства borderRadius
function isBorderRadiusSupported () {
	var s = document.documentElement.style;
	return typeof s.borderRadius === "string" || typeof s.WebkitBorderRadius === "string" || typeof s.KhtmlBorderRadius === "string" || typeof s.MozBorderRadius === "string";
}

//Рисование кривых
function canvasInit () {
	sStrokeStyle = "rgba(255, 255, 255, 0.5)";
	eLine1 = new elasticLine({
		canvas : { id : 'dome-canvas', containerId : 'dome-canvas-container' },
		strokeStyle : sStrokeStyle,
		lineType : 'bezier',
		linePoints : [ { xFixed : false, xRatio : 0, yFixed : true, y : 0 }, { xFixed : false, xRatio : 1, yFixed : true, y : 30}],
		controlPoints : [ { xFixed : false, xRatio : 0.3, yFixed : true, y : 15}, { xFixed : false, xRatio : 0.7, yFixed : true, y : 25}]
	});
	eLine2 = new elasticLine({
		canvas : { id : 'kite-canvas', containerId : 'kite-canvas-container' },
		strokeStyle : sStrokeStyle,
		lineType : 'bezier',
		linePoints : [ { xFixed : false, xRatio : 0.02, yFixed : true, y : 0 }, { xFixed : false, xRatio : 1, yFixed : true, y : 230}],
		controlPoints : [ { xFixed : false, xRatio : 0.3, yFixed : true, y : 110}, { xFixed : false, xRatio : 0.6, yFixed : true, y : 70}]
	});
	window.setInterval ("redrawEverything()", 350);
}

function redrawEverything () {
	eLine1.redraw();
	eLine2.redraw();
}


/* Рисование линий */
elasticLine = function (oParams) {
	this.canvas = document.getElementById(oParams.canvas.id);
	this.canvasContainer = document.getElementById(oParams.canvas.containerId);
	this.currentWidth = this.canvasContainer.clientWidth;
	this.currentHeight = this.canvasContainer.clientHeight;
	this.lineType = oParams.lineType;
	this.linePoints = oParams.linePoints;
	if (this.lineType == 'bezier') this.controlPoints = oParams.controlPoints
	this.ctx = null;
	if (this.canvas.getContext) {
		this.ctx = this.canvas.getContext('2d');
		this.ctx.strokeStyle = oParams.strokeStyle || "rgba(255, 255, 255, 0.2)";
	}
	if (!oParams.doNotDraw) this.draw();
}

elasticLine.prototype.test = function () {
	alert(this.normalWidth + 'x' + this.normalHeight);
}

elasticLine.prototype.calculatePoints = function (aPoints){
	var tmpPoints = new Array();
	var _x = 0;
	var _y = 0;
	for (var i = 0; i < aPoints.length ; i++) {
		(aPoints[i].xFixed) ? _x = aPoints[i].x : _x = this.currentWidth * aPoints[i].xRatio;
		(aPoints[i].yFixed) ? _y = aPoints[i].y : _y = this.currentHeight * aPoints[i].yRatio;
		tmpPoints.push({x : _x, y : _y});
	}
	return tmpPoints;
}

elasticLine.prototype.drawStraightLine = function(){
	var linePoints = this.calculatePoints (this.linePoints);
	this.ctx.beginPath();
	this.ctx.moveTo (linePoints[0].x, linePoints[0].y);
	this.ctx.lineTo (linePoints[1].x, linePoints[1].y);
	this.ctx.stroke();
}

elasticLine.prototype.drawBezierLine = function(){
	var linePoints = this.calculatePoints (this.linePoints);
	var controlPoints = this.calculatePoints (this.controlPoints);
	this.ctx.beginPath();
	this.ctx.moveTo (linePoints[0].x, linePoints[0].y);
	this.ctx.bezierCurveTo (controlPoints[0].x, controlPoints[0].y, controlPoints[1].x, controlPoints[1].y, linePoints[1].x, linePoints[1].y);
	this.ctx.stroke();
}

elasticLine.prototype.draw = function(){
	switch(this.lineType){
		case "bezier" : this.drawBezierLine(); break;
		case "straight" : this.drawStraightLine(); break;
	}
}

elasticLine.prototype.redraw = function(){
	if (this.currentWidth != this.canvasContainer.clientWidth || this.currentHeight != this.currentHeight) {
		this.ctx.clearRect(0, 0, this.canvas.offsetWidth, this.canvas.offsetHeight);
		this.currentWidth = this.canvasContainer.clientWidth;
		this.currentHeight = this.canvasContainer.clientHeight;
		this.draw();
	}
}

/* Галерея */
var Gallery = new Class({

	Implements: [Events, Options],
	
	options: {
		version			: '1.0',
		id				: 1
	},

	initialize: function (element, options) {
		this.root = element;
		this.setOptions(options);
		this.id = this.options.id + 1;
		this.FROM_TOP = 1;
		this.FROM_RIGHT = 2;
		this.FROM_BOTTOM = 4;
		this.FROM_LEFT = 8;
		
		this.anim_dir = this.FROM_TOP;
		this.animating = false;
		this.anim_dur = 'long';
		
		this.items = this.root.getElements('.preview li');
		this.big_image = this.root.getElements('.big-image')[0];
		this.caption = this.big_image.getElements('.caption')[0];
		
		this.attachEvents();
		
		this.selected_item = this.root.getElements('.selected a')[0];
		if (!this.selected_item) {
			this.items.getElements('a')[0].fireEvent('click');
		}
	},
	
	attachEvents: function  () {
		this.root.getElements('.preview').each(function(element) {
			var anim_dir = element.match('.set1') ? this.FROM_TOP : this.FROM_LEFT;
			var me = this;
			element.getElements('a').addEvent('click', function() {
				if (me.animating) return false;
				return me.onPreviewClick(this, anim_dir);
			});
		}.bind(this));
	},
	
	onPreviewClick: function (target, anim_dir) {
		if (target.getParent().match('.selected') || target.getElements('i')[0].match('.on')) return false;
		var href = target.get('href');
		var title = target.get('title');
		if (this.proxy_image) {
			this.proxy_image.onload = null;
			this.proxy_image = null;
		}
		if (this.loader_icon) {
			$clear(this.icon_timeout);
			this.loader_icon.removeClass("on");
			this.loader_icon = null;
		}
		this.proxy_image = new Element('img');
		this.loader_icon = target.getElements('i')[0];
		this.icon_timeout = setTimeout( function () { this.loader_icon.addClass('on'); }.bind(this), 500);
		this.proxy_image.addEvent('load', function () {
			this.swapImages(this.proxy_image.get('src'), title, anim_dir);
			this.swapPreviews(target, anim_dir);
			this.proxy_image.dispose();
			$clear(this.icon_timeout);
			this.loader_icon.removeClass("on");
		}.bind(this));
		this.proxy_image.set('src', href);
		return false;
	},
	
	swapImages: function (img, title, anim_dir) {
		var img_node = new Element('img',{'src': img});
		var old_img = this.big_image.getElements('img')[0];
		if (!old_img) {
			img_node.inject(this.big_image, 'top');
			return;
		}
		anim_dir = anim_dir || this.anim_dir;
		var canvas_width = this.big_image.getSize().x;
		var canvas_height = this.big_image.getSize().y;
		img_node.setStyles({position: 'absolute', zIndex: 2});
		var k = 1, p = '', init_pos, target_pos;
		switch (anim_dir) {
			case this.FROM_BOTTOM:
				img_node.setStyle('top', '100%');
				k = 1;
				p = 'top';
			break;
			case this.FROM_TOP:
				img_node.setStyle('bottom', '100%');
				k = -1;
				p = 'top';
			break;
			case this.FROM_RIGHT:
				img_node.setStyle('left', '100%');
				k = 1;
				p = 'left';
			break;
			case this.FROM_LEFT:
				img_node.setStyle('right', '100%');
				k = -1;
				p = 'left';
			break;
		}
		this.big_image.setStyles({
			width: canvas_width,
			height: canvas_height,
			position: 'relative',
			overflow: 'hidden'
		});
		img_node.inject(this.big_image, 'top');
		if (p == 'left') {
			init_pos = img_node.offsetLeft;
			target_pos = k * canvas_width;
			img_node.setStyles({left: init_pos, right: 'auto'});
		}
		else {
			init_pos = img_node.offsetTop;
			target_pos = k * canvas_height;
			img_node.setStyles({top: init_pos, bottom: 'auto'});
		}
		this.animating = true;
		old_img.setStyles({position: 'relative'});
		
		var old_width = old_img.getSize().x;
		var old_height = old_img.getSize().y;
		
		var tw1 = new Fx.Tween(old_img, {duration: 'long', onComplete: function () { old_img.destroy(); }});
		tw1.start(p, 0, target_pos);
		var tw2 = new Fx.Morph(img_node, {
			duration: 'long',
			onComplete: function () {
				img_node.setStyles({
					position: 'static',
					top: 'auto',
					left: 'auto',
					zIndex: ''
				});
				this.big_image.setStyles({overflow: ''});
				if (this.caption) {
					this.caption.fade(1);
				}
				this.animating = false;
			}.bind(this)
		});
		if (p == "left") {
			tw2.start({'left': [init_pos, 0], 'width': [old_width, img_node.getSize().x], 'height': [old_height, img_node.getSize().y]});
		}
		else {
			tw2.start({'top': [init_pos, 0], 'width': [old_width, img_node.getSize().x], 'height': [old_height, img_node.getSize().y]});
		}
		var d_canvas_width = img_node.getSize().x - old_img.getSize().x;
		var d_canvas_height = img_node.getSize().y - old_img.getSize().y;

		if (d_canvas_height || d_canvas_width) {
			var tw3 = new Fx.Morph(this.big_image, {duration: 'long'});
			tw3.start({'width': [this.big_image.getSize().x, img_node.getSize().x], 'height': [this.big_image.getSize().y, img_node.getSize().y]});
		}

		if (this.caption) {
			/*
			this.caption.fade(0, function() {
				$(this).hide().html(title || '');
			});
			*/
		}
	},
	
	swapPreviews: function  (item, anim_dir) {
		var item_image = item.getElements('img')[0];		
		var w = item_image.getSize().x, h = item_image.getSize().y;
		var pre_anim_state = { 
			width: w,
			height: h,
			display: 'block',
			position: 'relative',
			overflow: 'hidden'
		};
		if (!this.selected_item) {
			this.selected_item = item.getParent();
			this.selected_item.addClass('selected');
			return;
		}
		this.selected_item.getParent().removeClass('selected');
		this.selected_item.setStyles(pre_anim_state);
		var dir = (this.selected_item.getParent('.preview').match('.set1')) ? 'top' : 'left';
		var init_pos = ((dir == 'top') ? h : w) + 2;
		var img = this.selected_item.getElements('img')[0].setStyle(dir, init_pos);
		var tw1 = new Fx.Tween(img, {duration: this.anim_dur, onComplete: function () { img.getParent().setStyles({position: '', overflow: ''}); }});
		tw1.start(dir, init_pos, 0);
		item.setStyles(pre_anim_state);
		var dir = (anim_dir == this.FROM_TOP || anim_dir == this.FROM_BOTTOM) ? 'top' : 'left';
		var tw2 = new Fx.Tween(item_image, {duration: this.anim_dur, onComplete: function () { item_image.getParent().getParent().addClass('selected'); }});
		tw2.start(dir, 0, ((dir == 'top') ? h : w) + 2);
		this.selected_item = item;
	}
});