Shadowbox.init({
	language: 'ru',
	adapter: 'mootools',
	skipSetup: true,
	players:  ['img']
});
window.addEvent('domready', function() {

	// включаем shadowbox
  var Shadowbox_options = {
    resizeDuration: 0.2,
    fadeDuration: 0.2,
    viewportPadding: 10,
    handleOversize: 'drag',
    overlayOpacity: 0.5,
    viewportPadding: 5,
    autoDimensions: true,
		animate: false
  };

	list = $$(document.links);
  list = list.filter(function(item){
  	if (item.hasClass('no')) return false;
    return item.href.test(/\.(jpe?g|png|gif|swf)$/)
  });
  if (list && list.length) {
		Shadowbox.setup(list, Shadowbox_options);
  }
  
  var tips = new Tips($$('*[title]'), {className: 'atip'});


  if ($('tcontent')) {

  	$$('.scrollable').each(function(item) {
			var bar = item.getParent().getLast();
			if (!Browser.ie6) new ScrollBar(item, bar);
		});
  }
  
  // главная страница
  var monitor;
  if ((monitor = $('monitor')) && window.swfobject) {
  	
  	var abc = swfobject.embedSWF('/images/main.swf', 'monitorcontent', 343, 213, "10.0.0", '/js/expressInstall.swf', {
  			video: 'http://video.studioup.ru/files/video/presentation_low.mp4',
  			videoFull: 'http://video.studioup.ru/files/video/presentation_hi.mp4',
  			music: '/files/video/presentation.mp3'
  		}, {
	  		allowFullScreen: true,
	  		allowScriptAccess: true,
	  		wmode: 'opaque'
  	}, {});
  }
  
  // страница с видео
  if ($('videoflash') && window.swfobject) {
		swfobject.embedSWF('/images/video.swf', 'videoflash', 902, 342, "10.0.0", '/js/expressInstall.swf', {
			video: 'http://video.studioup.ru/files/video/presentation_low.mp4'
		}, {
  		allowFullScreen: true,
  		allowScriptAccess: true,
  		wmode: 'transparent'
		}, {});
	
  }
  
  // страница фотографий
  if ($('photo_dynamic')) {
  	slideThis($$('#photo_dynamic img'), 1500);
  }
  
  // photo9
  var photo9;
  if (photo9 = $('photo9')) {
  	var photos = $$('#photo9 .thumb > a');
  	var photosCount = photos.length;
  	var photoActive = 0;
  	
  	// расставляем номера фоток, чтобы каждая знала свой
  	photos.each(function(item, key) {
  		item.key = key;
  		item.parent = item.parentNode.parentNode;
  		item.parentNumber = parseInt(key / 9);
  	});
  	
  	// меняем фотографию с одной на другую
  	var bigPhoto = $$('#photo9 .big')[0];
  	var changePhoto = (function(from, to) {
  		photos[from].removeClass('active'); //.tween('opacity', 0.5);
  		photos[to].addClass('active'); //.tween('opacity', 1);
  		
  		// нужно ли менять родителя?
  		if (photos[from].parent != photos[to].parent) {
  			photos[from].parent.tween('opacity', 0);
  			photos[to].parent.tween('opacity', 1);
  		}
  		
  		// заменяем среднюю фотку
  		var firstImg = bigPhoto.getElement('img');
  		if (firstImg) firstImg.set('tween', {
  			onComplete: function() {
  				tips.detach(this.element);
  				this.element.destroy();
  			}
  		}).tween('opacity', 0);
  		
  		var newImg = new Element('img', {
  			'src': photos[to].getAttribute('thumb2'),
  			'title': firstImg.retrieve('tip:native'),
  			'styles': {
  				opacity: 0
  			}
  			
  		}).tween('opacity', 1);
  		tips.attach(newImg);
  	
  		bigPhoto.grab(newImg, 'top');
  		
  	});
  	
  	// следующая-предыдущая фотографии
  	var nextPhoto = (function(){
  		var old = photoActive;
  		photoActive++;
  		if (photoActive == photosCount) photoActive = 0;
	
  		changePhoto(old, photoActive);
  	});
  	var prevPhoto = (function(){
  		var old = photoActive;
  		photoActive--;
  		if (photoActive < 0) photoActive = photosCount - 1;
	
  		changePhoto(old, photoActive);
  	});
  	
  	// автоматическая смена фотографий
  	var photoAuto;
  	var shadowboxOpened = false;
  	photo9.addEvent('mouseleave', function() {
  		if (shadowboxOpened) return false;
  		
  		photoAuto = nextPhoto.periodical(2000);
  	}).addEvent('mouseenter', function() {
  		clearInterval(photoAuto);  		
  	}).fireEvent('mouseleave');
  	
  	$$('#photo9 .left')[0].addEvent('click', prevPhoto);
  	$$('#photo9 .right')[0].addEvent('click', nextPhoto);
  	
  	// клик на тамбнейл
  	$('photo9').addEvent('click:relay(a)', function(e) {
  		e.stop();
  		
  		var old = photoActive;
  		photoActive = e.target.getParent().key;
  		bigPhoto.href = e.target.getParent().href;
  		changePhoto(old, photoActive);
  		
  	});
  	bigPhoto.href = photos[0].href;
  	
  	// клик на среднюю фотку
  	bigPhoto.addEvent('click', function(e) {
  		shadowboxOpened = true;
  		Shadowbox.open({
  			content: photos[photoActive].href,
  			player: 'img',
  			options: {
  				onClose: function() {
  					shadowboxOpened = false;
  					photo9.fireEvent('mouseleave');
  				}
  			}
  		});
  		
  	});
  	
  	// меняем на 9 вперед
  	if ($('photomore')) $('photomore').addEvent('click', function(e) {
  		e.stop();
  		
  		var old = photoActive;
  		photoActive += 9;
  		if (photoActive >= photosCount) photoActive = 0;
  		photoActive = parseInt(photoActive / 9) * 9;
  		
  		changePhoto(old, photoActive);
  		
  	});
  }

	$(document.body).addClass('js');

});

window.addEvent('load', function() {
	YmapsInit();
});

function slideThis(collection, interval) {

	try {
		var total = collection.length;
		if (!total) return false;
	} catch(e) {}

	collection.currentNode = 0;

	var change = function() {
		if (collection.inactive) return true;


		collection[collection.currentNode].morph({
			'opacity': 0
		});
		collection.currentNode++;
		if (collection.currentNode == total) collection.currentNode = 0;

		if (collection[collection.currentNode].getStyle('visibility') == 'hidden')
			collection[collection.currentNode].setStyle('opacity', 0);

		collection[collection.currentNode].morph({
			'opacity': 1
		});

	}

	change.periodical(interval);
	collection[collection.currentNode].setStyles({
		opacity: 1,
		visibility: 'visible'
	});

}

var ScrollBar = new Class({

	Implements: [Options],

	options: {
		horizontal: false,
		wheel: true,
		adaptive: false,
		handle: null
	},

	initialize: function(content, scrollbar, options) {
		this.setOptions(options);

		var content = document.id(content);
		var scrollbar = document.id(scrollbar);

		if (!this.options.handle) {
			var handle = document.id(scrollbar.getFirst());
		} else {
			var handle = document.id(handle);
		}

		content.getLast().setStyle('margin-bottom', 0);
		scrollbar.setStyle('display', 'block');
		content.setStyle('overflow', 'hidden');

		if (this.options.horizontal) {
			var scrollsize = content.getScrollSize().x;
			var size = content.getSize().x;

			if (this.options.adaptive) {
				handle.setStyle('width', size / scrollsize * size);
			}
		} else {
			var scrollsize = content.getScrollSize().y;
			var size = content.getSize().y;

			if (this.options.adaptive) {
				handle.setStyle('height', size / scrollsize * size);
			}
		}

		var steps = scrollsize - size;

		if (steps < 1) {
			scrollbar.setStyle('display', 'none');
			content.setStyle('overflow', 'auto');
			return false;
		} else {

		}

		var horizontal = this.options.horizontal;

		var slider = new Slider(scrollbar, handle, {
			steps: steps,
			mode: (horizontal ? 'horizontal' : 'vertical'),
			onChange: function(step) {
				// Scrolls the content element in x or y direction.
				var x = (horizontal ? step : 0);
				var y = (horizontal ? 0 : step);
				content.scrollTo(x, y);
			}
		}).set(0);

		if (this.options.wheel){
			// Scroll the content element when the mousewheel is used within the
			// content or the scrollbar element.
			$$(content, scrollbar).addEvent('mousewheel', function(e) {
				e = new Event(e).stop();
				var step = slider.step - e.wheel * 30;
				slider.set(step);
			});
		}
		// Stops the handle dragging process when the mouse leaves the document body.
		$(document.body).addEvent('mouseleave', function() {
			slider.drag.stop()
		});

	}



});


function YmapsInit() {

	/*
	<div class="ymap"><img alt="Адрес" src="http://static-maps.yandex.ru/1.x/?ll=37.123,37.123&amp;z=15&amp;l=map&amp;size=500,320&amp;pt=37.123,37.123,pmlbl&amp;wizard=draw&amp;key=APIKEY" width="500" /></div>
	*/
	$$('.ymap').each(function(item) {
		try {
			var img = item.getFirst();
			var params = img.src.match(/\?(.*)$/)[1].parseQueryString();
		} catch(ex) {
			return;
		}

		var rest = function() {
			params.geocenter = params.ll.match(/([^,]+),([^,]+)/);
			params.sizes = params.size.match(/([^,]+),([^,]+)/);
			params.point = params.pt.match(/([^,]+),([^,]+)/);

			item.setStyles({
				'width': params.sizes[1] + 'px',
				'height': params.sizes[2] + 'px'
			});

			var map = new YMaps.Map(item);
			map.setCenter(new YMaps.GeoPoint(params.geocenter[1], params.geocenter[2]), params.z, YMaps.MapType.MAP);


			map.addControl(new YMaps.ToolBar());
			map.addControl(new YMaps.Zoom());
			map.addControl(new YMaps.ScaleLine());
			map.addControl(new YMaps.TypeControl());


			var content = document.createElement('span');
			content.innerHTML = img.alt;

			map.openBalloon(new YMaps.GeoPoint(params.point[1], params.point[2]), content);
		}

		if (0 && !this.loaded) {
			this.apikey = params.key;
			this.loaded = true;

			var api = new Element('script');
			api.type = "text/javascript";
			api.src = "http://api-maps.yandex.ru/1.1/?key=" + this.apikey;
			$(document.body).grab(api);
		}

		rest();


	});

}


