var gallery,panoram,carousel,fixPNG;

var my = {
	lock:0,
	
	onready: new Array(),
	onload: new Array(),
	
	panorams:new Array(),
	carousels:new Array(),
	galleries:new Array(),
	connector: ((new Date()).getTime()),
	
	popup:function(link, width, height){
		window.open(link.href,'order','width='+width+',height='+height+',scrollbars=yes,resizable=yes');
		return false;
	},
	
	/*
	 *  Возвращает абсолютные координаты элемента на странице
	 */
	getXY:function(element)
	{
		var x = 0;
		var y = 0;
		while(element)
		{
			x += element.offsetLeft;
			y += element.offsetTop;
			element = element.offsetParent;
		}
		if(navigator.userAgent.indexOf("Mac") != -1 && typeof document.body.leftMargin != "undefined")
		{
			x += document.body.leftMargin;
			y += document.body.topMargin;
		}
		return {x:x,y:y}
	},
	
	/*
 	 *  Класс для создания плейсхолдера
 	 */
	placeholder: {
		nativePlaceholder:(navigator.userAgent.match(/Safari/)),
	
		blurevent:function()
		{
			if(!this.value || this.value == '' || this.value == this.title)
			{
				this.value = this.title;
				$(this).removeClass('placetext');
			}
		},
	
		focusevent:function()
		{
			if(this.value == this.title)
				this.value = '';
			$(this).addClass('placetext');
		},
	
		init:function(){
			var input = $(this);
			if(my.placeholder.nativePlaceholder)
			{
				input.placeholder = input.title;
			}
			else
			{
				input.focus(my.placeholder.focusevent).blur(my.placeholder.blurevent);	
				input.blur();
			}
		}
	},

	
	/*
 	 *  Класс проигрывает мультик из кадров для объекта image
 	 */
	movie:function(image){
			if( navigator.userAgent.match(/MSIE (5|6)/) )
				return;
			image.css('width',image.width());
			image.css('height',image.height());
			image[0].src = '/i/0.gif';
			image.css('background','url(/i/mixermovie.png) 0 -297px no-repeat')
			image[0].play = function(){
				if( image[0].frame < 11 )
					image[0].frame += 1;
				else
					image[0].frame = 0;
				image[0].style.backgroundPosition = '0 -'+(297 - image[0].frame * 27)+'px';
				if( !image[0].stopped )
					setTimeout(image[0].play,50);
			};
			image[0].frame = 0;
			image.mouseover(function()
			{
				this.stopped = 0;
				setTimeout(this.play,100);
			});
			image.mouseout(function()
			{
				this.stopped = 1;
			});
	},
	
	/*
	 *  Создает объект анимирующий вылетание изображения
	 */
	resizer:function(from, to, image, frames){
		var animation = {
			//Изображение, которое мы перемещаем
			image:0,
			//Позиция с которой перемещаем
			from:0,
			//Позиция в которую перемещаем
			to:0,
			//Текущая позиция в анимации
			step:0,
			
			init:function(picture){
				animation.image = document.createElement('IMG');
				animation.image.src = picture;
				animation.image.style.position = 'absolute';
				animation.image.style.left = '-1000%';
				animation.image.style.zIndex = 10000;
				document.body.appendChild(animation.image);
			},
			
			position:function(from, to){
				animation.from = my.getXY(from);
				animation.from.width = from.clientWidth;
				animation.from.height = from.clientHeight;
				animation.from.object = from;
				
				to.style.visibility = 'hidden';
				to.style.display = '';
				animation.to = my.getXY(to);
				animation.to.width = to.clientWidth;
				animation.to.height = to.clientHeight;
				animation.to.object = to;
				to.style.display = 'none';
				to.style.visibility = 'visible';
			},
			
			show:function(from, to, params){
				animation.position(from, to);
				if( params )
				{
					if( params.towidth )
						animation.to.width = params.towidth;
				
					if( params.towidth )
						animation.to.height = params.toheight;
				}
				animation.image.width = animation.from.width;
				animation.image.height = animation.from.height;
				animation.image.style.top = animation.from.y;
				animation.image.style.left = animation.from.x;
				
							
				animation.side = 0;
				animation.step = 1;
				animation.animation();
			},
			
			hide:function(from, to){
				if( from )
					animation.from.object = from;
				if( to )
					animation.to.object = to;
				animation.image.width = animation.to.width;
				animation.image.height = animation.to.height;
				animation.image.style.top = animation.to.y;
				animation.image.style.left = animation.to.x;
				animation.to.object.style.display = 'none';
				
				animation.side = 1;
				animation.step = 0;
				animation.animation();
			},
			
			animation:function(){
				if( animation.side )
					animation.step += 0.08;
				else
					animation.step -= 0.08;

				if( animation.side && animation.step >= 1 || !animation.side && animation.step <= 0 )
				{
					if( animation.side )
						animation.step = 1;
					else
						animation.step = 0;

					if( animation.onstop )
						animation.onstop();
				}
				animation.image.style.left = Math.round(animation.from.x * animation.step + (1 - animation.step) * animation.to.x)+ 'px';
				animation.image.style.top = Math.round(animation.from.y * animation.step + (1 - animation.step) * animation.to.y) + 'px';
				animation.image.width = Math.round(animation.step * animation.from.width + (1 - animation.step) * animation.to.width);
				animation.image.height = Math.round(animation.step * animation.from.height + (1 - animation.step) * animation.to.height);

				if( animation.step != 1 && animation.step != 0 )
					setTimeout(animation.animation,21);
				else
				{
					if( !animation.side )
						animation.to.object.style.display = '';
					animation.image.style.left = '-1000%';
				}
			}
		};
		
		animation.init(from, to, image, frames);
		
		return animation;
	},
	
	hide:function(){
		if( panoram )
			panoram.hide();
		
		if( carousel )
			carousel.hide();
		
		if( gallery )
			gallery.hide();
	}
};
$(document).ready(
	function(){
		$('input.placeholder').each(my.placeholder.init);
		
		// Добавляем мультик для бетономешалки
		var mixer = $('#mixer');
		if( mixer.size() )
			my.movie(mixer);
		
		var iTop = '180px', iTopAnnounce = '50px';
		
		var vinoski = $('#vinoski .rect'), oObjectSelected = $('#vinoski .rect.selected'),oObjectAnnounceSelected = $('#vinoski .announce.selected'), oPosition = $('#pos'), bLocked = 0;
		
		var iComplete = 0;
		
		var finished = function(){
			iComplete++;
			if( iComplete < 2 )
				return;
			
			bLocked = 0;
		};
		
		var back = function(oShow){
			iComplete++;
			if( iComplete < 2 )
				return;
			
			iComplete = 0;
				
			oObjectSelected.css('margin-top', '0');
			oObjectSelected.removeClass('hidden');
			oObjectAnnounceSelected.addClass('hidden');
			
			oObjectSelected = oShow;
			oObjectAnnounceSelected = $('#vinoski .' + oShow.attr('id'));
			oObjectAnnounceSelected.css('margin-top',iTopAnnounce);
			oObjectAnnounceSelected.css('opacity', 0);
			oObjectAnnounceSelected.removeClass('hidden');
			
			oShow.removeClass('hidden');
			oPosition.css('left', oShow.css('left'));
			oShow.addClass('hidden');
			
			$t(oObjectAnnounceSelected).tween({
					time:0.5,
					opacity:1,
					marginTop:'-='+iTopAnnounce,
					onComplete:finished
				}
			);
			
			$t(oPosition).tween({
					time:0.5,
					marginTop:'-='+iTop,
					onComplete:finished
				}
			);
		};
		
		vinoski.click(
			function(){
				var that = $(this);
				if( that.hasClass('selected') || bLocked )
					return false;
				
				vinoski.removeClass('selected');
				that.addClass('selected');
				
				bLocked = 1;
				
				iComplete = 0;
				
				that.addClass('hidden');
				
				$t(oPosition).tween({
						time:0.5,
						marginTop:'+='+iTop,
						onComplete:back,
						onCompleteParams:[that]
					}
				);
				
				$t(oObjectAnnounceSelected).tween({
						time:0.5,
						opacity:0,
						marginTop:'+='+iTopAnnounce,
						onComplete:back,
						onCompleteParams:[that]
					}
				);
			}
		);
	}
).keydown(
	function(e){
		/*
		  *  Добавляем навигацию с клавиатуры
		  */
		if(e.ctrlKey){
			var link = null;
			var href = null;
			switch (e.keyCode ? e.keyCode : e.which ? e.which : null){
				case 0x25:
					link = $('#prevLink');
					break;
				case 0x27:
					link = $('#nextLink');
					break;
        	    case 0x26:
					link = $('#upLink');
					break;
				case 0x28:
					link = $('#downLink');
					break;
				case 0x24:
					link = $('#homeLink');
					break;
			}
			if (link && link[0]) document.location = link[0].href;
			if (href) document.location = href;
		}
	}
);