var skip = false;

$(document).ready(function() {
	$("textarea.mce").each(function() {
		//new nicEditor({buttonList: ['bold', 'italic', 'underline', 'left', 'center', 'right', 'ol', 'ul', 'subscript', 'superscript', 'image', 'link', 'unlink', 'xhtml'], iconsPath : '/images/nicEditorIcons.gif'}).panelInstance(this);
	});
	
	$("#spinner").bind("ajaxSend", function() {
		$(this).css({display:"block"});
	}).bind("ajaxComplete", function() {
		$(this).css({display:"none"});
	});
	
	html.links();
	html.zebra();
	html.flash();
	
	html.history();
	html.product();	
	
	html.menu();
	html.quote();
	
	scroller.init();
	gallery.init();
	pan.init();
	flash.init();
	map.init();
	years.init();
	
	html.preload(["/images/li_menu_a.png"]);
});

years = {
	
	left: null,
	right: null,
	leftLink: null,
	rightLink: null,

	tables: null,
	headers: null,
	position: 0,
	positionMax: null,
	
	init: function() {
		if ($(".years").length) {
			
			years.tables = $(".years-content table");
			years.headers = $(".years-header table");
			years.positionMax = years.tables.length - 1;
			
			years.left = $(".years-left");
			years.right = $(".years-right");
			years.leftLink = $(".years-left a");
			years.rightLink = $(".years-right a");
			
			years.leftLink.click(function(event) {
				event.preventDefault();																		
				years.scroll(-1);
			});
			years.rightLink.click(function(event) { 
				event.preventDefault();
				years.scroll(1);
			});
			
			years.setControls();
		}
	},
	
	scroll: function(direction) {
		if (years._allowed(direction)) {
			years.position += direction;
			years.tables.hide();
			years.tables.eq(years.position).show();
			years.headers.hide();
			years.headers.eq(years.position).show();
			years.setControls();
		}		
	},
	
	_allowed: function(direction) {
		var allowed = false;
		if (years.position + direction >= 0 && years.position + direction <= years.positionMax) {
			allowed = true;
		}
		return allowed;
	},
	
	setControls: function() {
		
		years.left.css({top: Math.round(years.tables.eq(years.position).height() / 2) + 24 + "px" });
		years.right.css({top: Math.round(years.tables.eq(years.position).height() / 2) + 24 + "px" });
		
		years.leftLink.removeClass("disabled");
		years.rightLink.removeClass("disabled");
		if (!years._allowed(-1)) {
			years.leftLink.addClass("disabled");
		}
		if (!years._allowed(1)) {
			years.rightLink.addClass("disabled");
		}
	}
}

map = {
	init: function() {
		
		$(window).load(function() {
			if ($("#map").length) {
				var map = new YMaps.Map($("#map")[0]);
				map.setCenter(new YMaps.GeoPoint(37.89205, 55.75351), 16);
				map.setType(YMaps.MapType.HYBRID);
				map.addControl(new YMaps.ToolBar());
				map.addControl(new YMaps.Zoom());
				
				var point = new YMaps.Placemark(new YMaps.GeoPoint(37.89205, 55.75351));
				point.name = "Русский Алкоголь";
				map.addOverlay(point); 
			}
		});
	}
}

function initializeMap() {
	if (("#map").length) {
		$("#map").each(function() {
			map = new google.maps.Map2(this);
			map.setCenter(new google.maps.LatLng(55.7864, 37.9416), 10);
			map.addControl(new GSmallMapControl());
			
			var point = new GLatLng(55.7864, 37.9416);
			var marker = new GMarker(point, { "title": "Парламент" });
			map.addOverlay(marker);
		});
	}
}

ajax = {
}

flash = {
	init: function() {
		if ($("#index-flash").length) {
			var so = new SWFObject("/swf/index.swf", "indexFlash", "100%", "100%", "9");
			so.addParam("wmode", "opaque");
			so.addParam("menu", "false");
			so.addParam("allowscriptaccess", true);
			so.addVariable("lang", "ru");
			if (skip) {
				so.addVariable("main", 1);
			}
			so.write("index-flash");	
		}
		
		if ($("#index-flash-en").length) {
			var so = new SWFObject("/swf/index.swf", "indexFlash", "100%", "100%", "9");
			so.addParam("wmode", "opaque");
			so.addParam("menu", "false");
			so.addParam("allowscriptaccess", true);
			so.addVariable("lang", "en");
			if (skip) {
				so.addVariable("main", 1);
			}
			so.write("index-flash-en");	
		}
		
		if ($("#distribution-flash").length) {
			var so = new SWFObject("/swf/map_new.swf", "mapFlash", "100%", "540px", "9");
			so.addParam("wmode", "transparent");
			so.addParam("menu", "false");
			so.addParam("allowscriptaccess", true);
			so.addVariable("data", "/swf/map.xml");
			so.write("distribution-flash");	
		}
	}
}

html = {
	quote: function() {
		$quote = $("blockquote");
		$quote.append('<div class="tl">').append('<div class="tr">').append('<div class="bl">').append('<div class="br">');
	},
	links: function() {
		$("a[rel=external]").attr({target:"_blank"}).addClass("external");
	},
	zebra: function() {
		$("table.data tr:odd td").addClass("zebra");
	},
	flash: function() {
		setTimeout(function() {
			$("#flashMessage").slideUp(200);
		}, 3000);
	},
	history: function() {
		var $lists = $(".history > ul > li");
		var $pads = $(".history ul li ul li");
		$pads.append("<a class=\"close\"></a>");
		var $openLinks = $(".history a.open");
		var $closeLinks = $(".history a.close");
		
		$openLinks.each(function(i, item) {
			$(this).click(function() {
				$lists.addClass("fade");
				$lists.removeClass("selected");
				$lists.eq(i).addClass("selected");
			});
		});

		$closeLinks.each(function(i, item) {
			$(this).click(function() {
				$lists.removeClass("fade");
				$lists.removeClass("selected");
			});
		});
	},
	product: function() {
		if ($(".product").size()) {
			var $links = $(".product-sizes a");
			var $images = $(".product img");
			var $lists = $(".product-sizes li");
			$images.eq(0).show();
			
			$lists.eq(0).addClass("selected");
			$links.eq(0).replaceWith("<span>" + $links.eq(0).text() +"</span>");
			
			$links.live("click", function() {
				
				var index = $lists.index($(this).parent());

				$images.hide();
				$images.eq(index).show();
				
				$lists.removeClass("selected");
				$spans = $(".product-sizes span");
				$spans.each(function() {
					$(this).replaceWith("<a href=\"javascript://\">" + $(this).text() +"</a>");								 
				});
				
				$(this).parent().addClass("selected");
				$(this).replaceWith("<span>" + $(this).text() +"</span>");	
			});
		}
	},
	menu: function() {
		
		$(".menu li a").mouseover(function() {
			$(this).parent().parent().addClass("hover");
		});
		
		$(".menu li a").mouseout(function() {
			$(this).parent().parent().removeClass("hover");
		});
		
		$(".menu li.zelected").each(function() {
			$element = $(this);
			$html = $element.html();
			$element.html("");
			$element.append("<span class=\"span-left\">" + $html + "</span><span class=\"span-right\">");
		});
	},
	
	preload: function(arguments) {
		for (var i = 0; i<arguments.length; i++) {
			$("<img>").attr("src", arguments[i]);
		}
	}
}

scroller = {
	left: null,
	right: null,
	wrap: null,
	div: null,
	names: null,
	slide: null,
	part: 0,
	ratio: 0,
	bottles: null,
	links: null,
	position: 0,
	positionMax: 0,
	width: null,
	
	init: function() {
		if ($(".scroll-wrap").length) {
			scroller.left = $(".control-left a");
			scroller.right = $(".control-right a");
			scroller.wrap = $(".scroll-wrap");
			scroller.div = $(".scroller");
			scroller.bottles = $(".bottle");
			//scroller.links = $(".control-names a");
			scroller.width = $(".scroll-wrap").width();

			//scroller.names = $(".control-names");
			//scroller.slide = $(".control-slide");
			//scroller.setRatio();

			scroller.left.click(function() { scroller.left.unbind(); scroller.scroll(1); });	
			scroller.right.click(function() { scroller.right.unbind(); scroller.scroll(-1); });
			scroller.positionMax = scroller.getPositionMax();
			scroller.setControls();
			
			
			
			$(window).resize(function(){
				scroller.position = 0;
				scroller.div.css({left: "0px"});
				scroller.width = $(".scroll-wrap").width();
				scroller.positionMax = scroller.getPositionMax();
				setTimeout(function() {
					scroller.setControls(); 
				}, 50);
				//scroller.setRatio();
			});
			
			
			
			//scroller.links.each(function(i, item) {
			//	$(this).click(function() {
			//		scroller.scrollTo(i);
			//	});
			//});
			var count = scroller.bottles.length;
			scroller.bottles.each(function(i, item) {
				$(this).css({zIndex: count});
				count--;
			});
		}
	},
	
	scroll: function(direction) {
		if (scroller._allowed(direction)) {
			scroller.position = scroller.position + 237 * direction;
			scroller.div.animate({left: scroller.position + "px"}, 350);
			//scroller.slide.animate({left: Math.round(scroller.position * scroller.part * -1) + "px"}, 350);
		}
		scroller.left.click(function() { scroller.left.unbind(); scroller.scroll(1); });	
		scroller.right.click(function() { scroller.right.unbind(); scroller.scroll(-1); });
		scroller.setControls();
	},

	scrollTo: function(index) {
		if (index > scroller.positionMax) {
			index = scroller.positionMax;
		}		
		scroller.position = index * -1 * 237;
		scroller.div.animate({left: scroller.position + "px"}, 350);
		//scroller.slide.animate({left: Math.round(scroller.position * scroller.part * -1) + "px"}, 350);
	},

	_allowed: function(direction) {
		var allowed = true;
		if (direction < 0) {
			if (scroller.width > scroller.div.width() + scroller.position - 10) {
				allowed = false;
			}			
		}
		else {
			if (scroller.position >= 0) {
				allowed = false;
			}
		}
		return allowed;
	},
	
	getPositionMax: function() {
		var gap = scroller.div.width() - scroller.width - 10;
		var positionMax = Math.ceil(gap / 237);
		return positionMax;
	},
	
	setRatio: function() {
		scroller.part = scroller.wrap.width() / scroller.div.width();
		var width = Math.round(scroller.part * scroller.names.width());
		scroller.slide.css({ width: width + "px", left: "0px" });
		scroller.ratio = scroller.slide.width() / scroller.names.width();
	},
	
	setControls: function() {
		scroller.left.removeClass("disabled");
		scroller.right.removeClass("disabled");
		if (!scroller._allowed(1)) {
			scroller.left.addClass("disabled");
		}
		if (!scroller._allowed(-1)) {
			scroller.right.addClass("disabled");
		}
	}
}

gallery = {
	
	div: [],
	cutter: [],
	holder: [],
	allLink: [],
	closed: [],
	
	init: function() {
		$(".gallery").each(function(i, item) {
			gallery.closed[i] = true;
			gallery.cutter[i] = $(item).find(".gallery-cutter");
			gallery.holder[i] = $(item).find(".gallery-holder");
			gallery.allLink[i] = $(item).find(".all-link");
			gallery.allLink[i].click(function() {
				if (gallery.closed[i]) {
					gallery.cutter[i].animate({height: gallery.holder[i].height() + "px"}, 350);
					gallery.closed[i] = false;
					if ($("div.menu > ul").hasClass("ru")) {
						gallery.allLink[i].find("a").text("Закрыть");
					}
					if ($("div.menu > ul").hasClass("en")) {
						gallery.allLink[i].find("a").text("Close");
					}					
					gallery.allLink[i].addClass("full-view");
				}
				else {
					gallery.cutter[i].animate({height: "180px"}, 350);
					gallery.closed[i] = true;
					if ($("div.menu > ul").hasClass("ru")) {
						gallery.allLink[i].find("a").text("Все фотографии");
					}
					if ($("div.menu > ul").hasClass("en")) {
						gallery.allLink[i].find("a").text("All photos");
					}					
					gallery.allLink[i].removeClass("full-view");
				}
			});

			$(window).resize(function(){
				gallery.cutter[i].animate({height: "180px"}, 350);
				gallery.closed[i] = true;
			});

			gallery.lightbox();
		})
	},
	
	lightbox: function() {
		$(".gallery a.lightbox").lightbox({
			overlayBgColor: '#000',
			overlayOpacity: 0.75,
			fileLoadingImage: '/images/lightbox-ico-loading.gif',
			imageBtnClose: '/images/a_close.png',
			imageBtnPrev: '/images/a_prev.png',
			imageBtnNext: '/images/a_next.png',
			containerResizeSpeed: 350,
			txtImage: 'Фото',
			txtOf: 'из'
   });
	}
}

pan = {
	
	left: null,
	right: null,
	panorama: null,
	description: null,
	images: null,
	holder: null,
	view: null,
	viewHolder: null,
	viewBack: null,
	holderWidth: null,
	viewHolderWidth: null,
	ratio: null,
	title: null,
	titles: null,
	links: null,
	cutter: null,
	position: 0,
	positions: [1, -885, -1700, -2450, -3280, -3860],
	views: [0, 98, 170, 250, 331, 426],
	descClose: null,
	descBlocks: null,
	descImgBlocks: null,
	descLinks: null,
	descImgs: null,
	opened: false,
	openedImg: false,
	imgClose: null,

	init: function() {
		if ($("#panorama").length) {
			
			pan.left = $(".panorama-left a");
			pan.right = $(".panorama-right a")
			pan.leftSpan = $(".panorama-left a span");
			pan.rightSpan = $(".panorama-right a span");
			pan.panorama = $(".panorama");
			pan.description = $(".panorama-description");
			pan.images = $(".panorama-images");
			pan.holder = $(".panorama-image");
			pan.view = $(".panorama-view");
			pan.viewHolder = $("#panorama-pan");
			pan.viewBack = $(".panorama-view .zoom");
			pan.title = $(".panorama-title");
			pan.titleWrap = $(".panorama-title-wrapper");
			pan.titles = $(".panorama-title a");
			pan.links = $(".panorama-text a");
			pan.cutter = $(".panorama-mask-top-cutter");
			
			pan.ratio = 4716 / (pan.viewHolder.width() - 34);
			
			pan.descBlocks = $(".panorama-description .description");
			pan.descImgBlocks = $(".panorama-images .description");
			pan.descClose = $("#descriptionClose");
			pan.descClose.click(function() { pan.toggleDesc(pan.position); });	

			pan.imgClose = $("#imageClose");
			pan.imgClose.hide();
			
			pan.imgClose.click(function() {
				pan.descLinks.removeClass("active");
				$.scrollTo("div.outer", 500);
				pan.images.slideUp(350, function() {
			 		pan.images.hide();
					pan.descImg.hide();	
					pan.imgClose.hide();
					pan.openedImg = false;
				});														
			});	

			pan.view.draggable({ axis: "x", containment: '#panorama-pan', scroll: false, drag: function() {
				pan.move(pan.view.position().left * -1 * pan.ratio, false);
			}});
			
			pan.holder.draggable({ axis: "x", minX: 0, maxX: -3882, drag: function() {
				pan.move(pan.holder.position().left, true);
			}});
			
			pan.setLinks(0);
			pan.setControls();
			
			pan.left.click(function() { pan.scroll(-1); });	
			pan.right.click(function() { pan.scroll(1); });
			pan.links.each(function(i, item) {
				$(this).click(function() {
					pan._scrollTo(i);
				});
			});
			pan.titles.each(function(i, item) {
				$(this).click(function() {
					pan.toggleDesc(i);
				});
			});
			
			$(".jScrollPaneDrag").live("mouseover", function() {
				$(this).addClass("hover");
			});
			$(".jScrollPaneDrag").live("mouseout", function() {
				$(this).removeClass("hover");
			});
		}
	},
	
	move: function(left, moveView) {
		pan.holder.css({ left: left + "px" });
		var oldPosition = pan.position;
		pan.position = pan.getPosition(left);
		if (oldPosition != pan.position) {
			pan.setLinks(pan.position);
			pan.setDescBlocks(pan.position, true);
		}

		pan.viewBack.css({backgroundPosition: left / pan.ratio  + "px 0px"});

		if (moveView) {
			pan.view.css({ left: left * -1 / pan.ratio + "px" });
		}		
	},
	
	scroll: function(direction) {
		if (pan._allowed(direction)) {
			pan.position = pan.position + direction;
			pan.holder.animate({left: pan.positions[pan.position] + "px"}, 350);
			pan.view.animate({left: pan.views[pan.position] + "px"}, 350);
			pan.viewBack.animate({backgroundPosition: pan.views[pan.position] * -1 + "px 0px"}, 350);
			pan.setLinks(pan.position);
			pan.setDescBlocks(pan.position, true);
		}
	},

	_scrollTo: function(index) {
		if (index >= pan.positions.length) {
			pan.position = pan.positions.length - 1;
		}
		else {
			pan.position = index;
		}
		pan.holder.animate({left: pan.positions[pan.position] + "px"}, 350);
		pan.view.animate({left: pan.views[pan.position] + "px"}, 350);
		pan.viewBack.animate({backgroundPosition: pan.views[pan.position] * -1 + "px 0px"}, 350);
		pan.setLinks(pan.position);
		pan.setDescBlocks(pan.position, true);
		pan.setControls();
	},
	
	setLinks: function(index) {
		pan.titles.removeClass("active");
		pan.links.removeClass("active");
		pan.titles.eq(index).addClass("active");
		pan.links.eq(index).addClass("active");
		pan.cutter.hide().css({ width: pan.title.width() - 18 + "px" })
		if (pan.opened)
			pan.cutter.show();
		pan.setControls();
	},
	
	toggleDesc: function(index) {
		if (!pan.opened) {
			pan.description.fadeIn(500);
			pan.viewBack.fadeOut(500);
			pan.opened = true;
			pan.setDescBlocks(index);
			//pan.title.css({ top: "-30px", height: "49px", lineHeight: "48px" });
			//pan.titleWrap.css({ zIndex: 25 });
			pan.cutter.hide().css({ width: pan.title.width() - 18 + "px" }).show();
			pan.title.addClass("opened");
		}
		else {
			pan.description.fadeOut(500);
			pan.viewBack.fadeIn(500);
			pan.opened = false;
			pan.imgClose.hide();
			pan.setDescBlocks(index);
			//pan.title.css({ top: "0px", height: "73px", lineHeight: "71px" });
			//pan.titleWrap.css({ zIndex: 5 });
			pan.cutter.hide();
			pan.title.removeClass("opened");
		}
	},

	setDescBlocks: function(index, closeOverride) {
		
		pan.imgClose.hide();		
		pan.descBlocks.hide();
		
		pan.descImgBlocks.slideUp(350, function() {
			pan.images.slideUp(350);
			pan.descLinks = pan.descBlocks.eq(index).find(".description-images a");
			pan.descImg = pan.descImgBlocks.eq(index).find("img");
			if (!pan.opened) {
				pan.descImg.hide();
			}
			pan.openedImg = false;
		});
		
		if (closeOverride) {
			pan.descImg.hide();
		}
		
		pan.descBlocks.eq(index).show();
		pan.descImgBlocks.eq(index).show();
		pan.descLinks.removeClass("active");
		
		if (pan.opened) {
			pan.descBlocks.eq(index).find(".scroll").jScrollPane({"showArrows": true, "scrollbarOnLeft": true, "scrollbarWidth": 7, "scrollbarMargin": 0});
		}
		
		pan.descLinks.unbind();
		pan.descLinks.each(function(i, item) {
			$(this).click(function() {
														 
				pan.imgClose.show();
				pan.descLinks.removeClass("active");
				pan.descLinks.eq(i).addClass("active");
				
				$.scrollTo(400, 350);
				
				if (pan.openedImg) {
					//pan.images.hide();
					pan.descImg.hide();	
					pan.descImg.eq(i).show();
					//pan.images.slideDown(500, function() { $.scrollTo(400, 350); });
				}
				else {
					//pan.images.slideUp(350, function() {
						pan.images.hide();
						pan.descImg.hide();	
						pan.descImg.eq(i).show();
						pan.images.slideDown(500, function() { $.scrollTo(400, 350); });
					//});
				}
				
				pan.openedImg = true;
			});
		});
	},
	
	getPosition: function(left) {
		var position;
		$.each(pan.positions, function(i, item) {
			if (left < item) {
				position = i;
			}
		});
		return position;
	},

	_allowed: function(direction) {
		var allowed = true;
		if (direction < 0) {
			if (pan.position <= 0) {
				allowed = false;
			}			
		}
		else {
			if (pan.position >= pan.positions.length-1) {
				allowed = false;
			}
		}
		return allowed;
	},
	
	setControls: function() {
		pan.leftSpan.removeClass("disabled");
		pan.rightSpan.removeClass("disabled");
		if (!pan._allowed(-1)) {
			pan.leftSpan.addClass("disabled");
		}
		if (!pan._allowed(1)) {
			pan.rightSpan.addClass("disabled");
		}
	}
}

function printMap()
{
	var cX=(screen.width-700)/2;
	var cY=(screen.height-700)/2;
	var win = window.open('/contacts/print','popup','width='+700+', height='+700+', scrollbars=no, resizeable=no, left='+cX+',top='+cY+',screenX'+cX+',screenY='+cY);
	win.print();
}


function printMapEn()
{
	var cX=(screen.width-700)/2;
	var cY=(screen.height-700)/2;
	var win = window.open('/en/contacts/print','popup','width='+700+', height='+700+', scrollbars=no, resizeable=no, left='+cX+',top='+cY+',screenX'+cX+',screenY='+cY);
	win.print();
}