var IE = '\v' == 'v';
function ImgBig(el,src)
{
	var src = src || el.src;
	var flag = 0;
	var i = document.getElementById('i');
	var e = document.getElementById('imgs');
	var imgdes = document.getElementById('imgs-des');
	
	setTimeout(function(){
					if(flag == 0){
						//ii.src = "/img/loading.gif";
						var coor = Block(300,50);
						e.style.top = coor[1] + 'px';
						e.style.left = coor[0] + 'px';
						e.style.width = "300px";
						e.style.height = "50px";
								
						if(imgdes.lastChild != null) imgdes.removeChild(imgdes.lastChild);
						imgdes.appendChild(document.createTextNode("загрузка фото ..."));
						e.style.display = 'block';
					}
	}, 1000);
		
	var pic = new Image();
		pic.onload = function()
		{
			flag = 1;
			var title = el.alt || el.title;
			    title = document.createTextNode(title)
			var coor = Block(pic.width,pic.height);
				e.style.top = coor[1] + 'px';
				e.style.left = coor[0] + 'px';
				e.style.width = pic.width + "px";
				e.style.height = pic.height + 60 + "px";
				i.style.display = 'block';
				i.src = pic.src;
				if(imgdes.lastChild != null) imgdes.removeChild(imgdes.lastChild);
				imgdes.appendChild(title);
				e.style.display = 'block';
		}
	pic.src = src;
}

function Block(width,height)
{
	var d = document.documentElement;
	var top = d.scrollTop || document.body.scrollTop;
	var y = Math.floor((d.clientHeight - height)/2) + top;
	var x = Math.floor((d.clientWidth - width)/2);
	return [x,y];
}

function H()
{
	var h = document.getElementById('des').offsetHeight;
	if(h<700){ document.getElementById('des').style.height = 600 + "px"; }
}
var popup;

function Sub(){
	var first = document.getElementById('first').value;
	var second = document.getElementById('second').value;
	var pat = document.getElementById('pat').value;
	if(first.length<2){
		alert('Необходимо заполнить поле Фамилия!');
		return false;
	}
	else if(second.length<2){
		alert('Необходимо заполнить поле Имя!');
		return false;
	}
	else{
    	var width = 638;
    	var height = 317;
    	width=width+100;
    	height=height+100;
    	if(popup && !popup.closed){popup.close()}
    	popup=window.open('/skidka_popup.php?first='+first+'&second='+second+'&pat='+pat, '_blank', 'scrollbars=yes,height='+height+',width='+width+',top=100,left=200');
    	return false;
	}
}

function MapBig(w,h,m,cD,cSh,bD,bSh,des)        //m - zoom, cD - center Dolgota, pSh - baloon Shirota
{
    if(document.getElementById('yam')) { $('#yam').remove(); }
    $(document.body).append('<div id="yam"><div class="close" onclick="$(\'#yam\').remove()">закрыть [x]</div><div id="YMapsID" style="width:700px;height:500px;margin:5px;"></div></div>');
    var yam = document.getElementById('yam');
    var coor = Block(w,h);
        yam.style.top = coor[1] + 50 + 'px';
        yam.style.left = coor[0] + 'px';
        yam.style.width = w + 10 + "px";
        yam.style.height = h + 40 + "px";
        yam.style.position = 'absolute';
        yam.style.zIndex = '2';
        yam.style.display = 'block';
        yam.style.border = '1px solid #000000';
        yam.style.backgroundColor = '#ffffff';

    var map = new YMaps.Map(document.getElementById("YMapsID"));
        map.setCenter(new YMaps.GeoPoint(cD,cSh), m);
        map.openBalloon(new YMaps.GeoPoint(bD,bSh), des, {hasCloseButton:true});
    var zoom = new YMaps.Zoom({ customTips: [ { index: 1, value: "Мелко" },{ index: 9, value: "Средне" },{ index: 16, value: "Крупно" } ] });
        map.addControl(zoom);
        map.addControl(new YMaps.TypeControl()); 
    }

var popup2;


//main anim
var mist = {};
mist.completed = true;
mist.active = 0;
mist.arr = [];
mist.speed = 50;
mist.delay = 2000;
mist.play = function (id){
	var mistBlock = document.getElementById('mistBlock');
	var pic = new Image();
		pic.onload = function()
		{
			pic.id = id;
			pic.style.display = "block";
			pic.style.opacity = ".0";
			pic.style.marginTop = "-275px";
			mistBlock.appendChild(pic);
			setTimeout(function(){
				var e = document.getElementById(pic.id);
					var op = e.style.opacity;
					if (op != "1") {
						if (op != 0) {
							var a = op.split(".");
							if (a[0] == "1") {
								flag = true;
							}
							op = a[1];
						}
						op = op * 1 + 1;
						if (op < 10) {
							if (IE) {
								e.style.filter = "alpha(opacity=" + (op * 10) + ")";
							}
							e.style.opacity = "0." + op;
							setTimeout(arguments.callee, mist.speed);
						}
						else {
							if (IE) {
								e.style.filter = "alpha(opacity=100)";
							}
							e.style.opacity = "1.0";
							mist.completed = true;
							mist.active = mist.active * 1 + 1;
						}
                        var len = mist.arr.length
                        if(mist.active == len - 1 && mist.completed == true && op == 10){
                            for(var i = 0; i < len; i++){
                                if(document.getElementById(mist.arr[i])) mistBlock.removeChild(document.getElementById(mist.arr[i]));
                            }
                            mist.active = 0;
                        }
					}
			},mist.delay);
		}
	pic.src = id;
	if (IE) {
		pic.style.filter = "alpha(opacity=0)";
	}
}

mist.start = function(){
	setTimeout(function(){
		if(mist.completed == true) {
			mist.completed = false;
			mist.play(mist.arr[mist.active * 1 + 1]);
		}
		setTimeout(mist.start, 300);
	}, 2000);
}

$(function(){
    $('.book').click(function(e){
        var roomTitle = $(e.target).prev().text();
        $('.div-form-book').css({ display:"block", top: ($(e.target).position().top - 130) + "px" });
        var h1 = $('.div-form-book h1');
        var ttl = h1.text().split(":")[0];
        h1.text(ttl + ' : ' + roomTitle);

    });
    $('.close-x img').click(function(){
        $('.div-form-book').css({ display:"none" });
    });
    $('.form-book').submit(function(){
        var f = document.formbook;
        if(f.firstname && f.firstname.value==""){
            f.firstname.style.backgroundColor = 'red';
            f.firstname.focus();
            setTimeout(function(){ document.formbook.firstname.style.backgroundColor = 'white'; }, 2000);
            return false;
        }
        if(f.tel && f.tel.value==""){
            f.tel.style.backgroundColor = 'red';
            f.tel.focus();
            setTimeout(function(){ document.formbook.tel.style.backgroundColor = 'white'; }, 2000);
            return false;
        }
			if(f.email && f.email.value==""){
            f.email.style.backgroundColor = 'red';
            f.email.focus();
            setTimeout(function(){ document.formbook.email.style.backgroundColor = 'white'; }, 2000);
            return false;
        }
        if(f.date1 && f.date1.value==""){
            f.date1.style.backgroundColor = 'red';
            f.date1.focus();
            setTimeout(function(){ document.formbook.date1.style.backgroundColor = 'white'; }, 2000);
            return false;
        }
        if(f.date2 && f.date2.value==""){
            f.date2.style.backgroundColor = 'red';
            f.date2.focus();
            setTimeout(function(){ document.formbook.date2.style.backgroundColor = 'white'; }, 2000);
            return false;
        }

        $.post("/book/",$(this).serialize(),function(data){
            if(data=="ok"){
               $('.div-form-book').css({ display:"none" });
            }
        }, "html");
        return false;
    });
	$('.form-online').submit(function(){
		$.get("/reservation/oles_prices.php",$(this).serialize(),function(data){
			$('.form-online').html(data);
      });
	return false;
	});
});
