﻿Event.observe(window, 'load', function() {
	//site map
	var categoryTree = $("tree");
	if (categoryTree) convertTrees();
	//tooltip
	initializeTooltip();
	
	//frontBanners
	var frontBanners = $("vitrina");
	if (frontBanners){
		new imageBlockExtention("vitrina");
	}
	
	//image gallery (scrolling) 
	initImageGallery();
	
});
//tooltips
function initializeTooltip(){
	var smallImageDiv = $$("div.imageSmall");
	var middleDiv = $$("div.imageMiddle");
	if(smallImageDiv.length > 0){
		smallImageDiv.each(function(smallDiv, index) {
			var smallImg = smallDiv.select("img");
			smallImg[0].observe('mouseover',function(){
        		var loadImg = middleDiv[index].down(0);	
				if(loadImg){ 
					new Tooltip(smallDiv.id, middleDiv[index].id);
					var srcDiv = loadImg.next();
					var divImg = srcDiv.next();
					divImg.innerHTML = "<img src='"+ srcDiv.innerHTML +"' />";
					var imgMidd = divImg.down(0);
					imgMidd.observe('load', function(){
						middleDiv[index].down(0).innerHTML = divImg.innerHTML;
					});
				}
    		});
		});	
	}
}

//gallery
function moveToPositionHorizontal(container, position_x){
  Position.prepare();
  var container_x = Position.cumulativeOffset($(container))[0];
  new Effect.Scroll(container, {x:(position_x-container_x), y:0});
  return false;
}

function prohibitSelection(obj){
	obj.style.cursor = "default";
	if (typeof obj.onselectstart!="undefined") //IE
		obj.onselectstart=function(){return false;}
	else if (typeof obj.style.MozUserSelect!="undefined") //Firefox
		obj.style.MozUserSelect="none";
	else if (typeof obj.style.KhtmlUserSelect!="undefined") //WebKit
		obj.style.KhtmlUserSelect="none";
	else obj.onmousedown=function(){return false;} //All other(Opera)
}

var VIEW_IMAGE_LENGTH = 4;
var productList;
var productPositions = [];
var currentScrollItem;

function initImageGallery(){
	var imgGal = $("imgGal");
	if (!imgGal){
		return false;
	}
	var productContainer = $("productContainer");
	productList = productContainer.select("div.itemProduct");
	productList.each(function(product, index) {
		productPositions[index] = Position.cumulativeOffset(product)[0];
	});
	
	var prevButton = $("prevButton");
	prevButton.observe('click', scrollGalleryLeft);
	
	var nextButton = $("nextButton");
	nextButton.observe('click', scrollGalleryRight);
}

function getCurrentPosition(currentItem){
	var currentIndex = 0;
	productList.each(function(product, index) {
		if (product == currentItem){
			currentIndex = index;
		}
	});
	return currentIndex;
}

function scrollGalleryLeft(){
	var imgGal = $("imgGal");
	if ($("imageBlock")) {
		prohibitSelection($("imageBlock"));
	}
	
	if (productList.length > VIEW_IMAGE_LENGTH){
	
		if (currentScrollItem != null){
			if (currentScrollItem.previous() != null){
				currentScrollItem = currentScrollItem.previous();
				var position = getCurrentPosition(currentScrollItem);
				moveToPositionHorizontal(imgGal, productPositions[position]);
			}
		}	
		
	}
	return false;
}

function scrollGalleryRight(){
	var imgGal = $("imgGal");
	if ($("imageBlock")) {
		prohibitSelection($("imageBlock"));
	}
	
	if (productList.length > VIEW_IMAGE_LENGTH){
	
		if (typeof currentScrollItem == "undefined"){
			currentScrollItem = productList[1];
		} else {
			if (currentScrollItem != null){
				if (currentScrollItem.next((VIEW_IMAGE_LENGTH-1))){
					currentScrollItem = currentScrollItem.next();
				}
			} 
		}
		
		if (currentScrollItem != null){
			var position = getCurrentPosition(currentScrollItem);
			moveToPositionHorizontal(imgGal, productPositions[position]);
		}
		
	}
	return false;
}
//set default homepage
function setHomePage(a){
	var url = location.href;
		
	if(Prototype.Browser.IE) {
		a.style.behavior='url(#default#homepage)';
		a.setHomePage(url);
  		 
	} 
}

function showFriendMailPopup() {
	window.open(friendMailUrl, "FriendMail", "resizable=no,width=567,height=600,toolbar=no,scrollbars=no,location=no,menubar=no,status=no");
}

// validate fast subscribe form
function checkSubscriber(){
	var email = $("subscriber");
	if (isEmpty(email)){
		setErrorClass(email);
		alert("Укажите Ваш электронный адрес.");
		return false;
	}
	if (!isEmail(email.value)){
		setErrorClass(email);
		alert("Адрес некорректный. Электронный адрес должен быть вида: john@doe.com");
		return false;
	}
	return true;
}
