$('html').removeClass('no-js');

/* BACKGROUND IMAGE */
function resizeBackground()
{
	image = $('#backgroundImage img');
	window_w = $(window).width();
	window_h = $(window).height();
	img_w = parseInt($(image).width());
	img_h = parseInt($(image).height());

	if ( window_w/window_h < img_w/img_h )
	{
		new_img_h = window_h;
		koef = window_h / img_h;
		new_img_w = Math.ceil(img_w*koef);

		$(image).attr({width: new_img_w, height: new_img_h});
		var left = -Math.ceil((new_img_w - window_w) / 2);
		$('#backgroundImage').css({'left': left+'px', 'top': 0});
	}
	else
	{
		new_img_w = window_w;
		koef = window_w / img_w;
		new_img_h = Math.ceil(img_h*koef);

		$(image).attr({width: new_img_w, height: new_img_h});
		var top = -Math.ceil((new_img_h - window_h) / 2);
		$('#backgroundImage').css({'left': 0, 'top': top+'px'});
	}
}

$('#backgroundImage img').load(function(){
	resizeBackground();
	$('#backgroundImage img').css('opacity', 1);
	});
/* / BACKGROUND IMAGE */


/* ROTATOR */
rotator_exists = false;
cont_height = 0;
cont_wrap_height = 0;
current_li = 0;

if ( $('.rotator').length > 0 ) rotator_exists = true;

if ( rotator_exists )
{
	cont_height = $('.rotator .content ul').outerHeight();
	cont_wrap_height = $('.rotator .content').outerHeight();

	$('.rotator .downscroller a').click(function(){
		new_pos = parseInt($('.rotator .content li:eq(' + (current_li+1) + ')').position().top);
		$('.rotator .content ul').animate({'top': '-' + new_pos + 'px'}, 'fast', function(){showRotatorLinks()});
		$('.rotator .upscroller a').show();
		current_li++;
		return false;
	});
	$('.rotator .upscroller a').click(function(){
		new_pos = parseInt($('.rotator .content li:eq(' + (current_li-1) + ')').position().top);
		$('.rotator .content ul').animate({'top': '-' + new_pos + 'px'}, 'fast', function(){showRotatorLinks()});
		$('.rotator .downscroller a').show();
		current_li--;
		return false;
	});
}
function showRotatorLinks()
{
	rotator_pos = parseInt($('.rotator .content ul').css('top'));
	if ( rotator_pos == 0 ) $('.rotator .upscroller a').hide();
	else if ( cont_height + rotator_pos <= cont_wrap_height ) $('.rotator .downscroller a').hide();
}
/* /ROTATOR */



var itv;
function popup_show(winId, hideCloseBtn)
{
        hideCloseBtn = hideCloseBtn || false;

        $('#popup_window div.content>div').hide();
        $('#popup_window #' + winId).show();

        popup_window_w = $('#popup_window').width();
        popup_window_h = $('#popup_window').height();
        window_w = $(window).width();
        window_h = $(window).height();
        new_left = (window_w - popup_window_w)/2;
        if (new_left < 0) new_left = 10;
        var new_top = (window_h - popup_window_h)/2;
        if (new_top < 0) new_top = 10;
        $('#popup_window').css('left', new_left + 'px');
        $('#popup_window').css('top', new_top + 'px');

        if (hideCloseBtn) $('#popup_window .close_btn').hide();
        else $('#popup_window .close_btn').show();

		$('#black_layer').show();
        $('#popup_window').show();

	if ( winId == 'webcam1') { itv = setInterval('reloadWebCam1()', 15000); }
        if ( winId == 'webcam9') { itv = setInterval('reloadWebCam9()', 15000); }
        if ( winId == 'webcam4') { itv = setInterval('reloadWebCam4()', 15000); }
        if ( winId == 'webcam4a') { itv = setInterval('reloadWebCam4a()', 15000); }
        if ( winId == 'webcam10') { itv = setInterval('reloadWebCam10()', 15000); }
        if ( winId == 'webcam30') { itv = setInterval('reloadWebCam30()', 15000); }
        if ( winId == 'webcam10b') { itv = setInterval('reloadWebCam10b()', 15000); }
        if ( winId == 'webcam5') { itv = setInterval('reloadWebCam5()', 15000); }
        if ( winId == 'webcam7') { itv = setInterval('reloadWebCam7()', 15000); }
        if ( winId == 'webcam7a') { itv = setInterval('reloadWebCam7a()', 15000); }
        if ( winId == 'webcam21') { itv = setInterval('reloadWebCam21()', 15000); }
        if ( winId == 'webcam21a') { itv = setInterval('reloadWebCam21a()', 15000); }
        if ( winId == 'webcam31') { itv = setInterval('reloadWebCam31', 15000); }
}
function popup_hide()
{
        clearInterval(itv);
	$('#black_layer').hide();
        $('#popup_window').hide();
}

function reloadWebCam1()
{
	theDate = new Date();
	var url = 'http://81.25.172.208/cam2/show.php?nv=IPCam127441&dummy=';
	//var url = 'http://81.25.170.195/axis-cgi/jpg/image.cgi?resolution=480x360&compression=30&clock=1&date=1&dummy=';
	url +=  theDate.getTime().toString(10);
	$('#webcam1 .webcam img').attr('src', url);
}

function reloadWebCam9()
{
	theDate = new Date();
	var url = 'http://81.25.172.208/cam3/show.php?nv=NVS127419&dummy=';
	url +=  theDate.getTime().toString(10);
	$('#webcam9 .webcam img').attr('src', url);
}

function reloadWebCam4()
{
	theDate = new Date();
	var url = 'http://81.25.170.194:88/axis-cgi/jpg/image.cgi?resolution=480x360&compression=30&clock=1&date=1&dummy=';
	url +=  theDate.getTime().toString(10);
	$('#webcam4 .webcam img').attr('src', url);
}

function reloadWebCam4a()
{
	theDate = new Date();
	var url = 'http://81.25.170.194/axis-cgi/jpg/image.cgi?resolution=480x360&compression=30&clock=1&date=1&dummy=';
	url +=  theDate.getTime().toString(10);
	$('#webcam4a .webcam img').attr('src', url);
}

function reloadWebCam10()
{
	theDate = new Date();
	var url = 'http://46.21.68.138/axis-cgi/jpg/image.cgi?resolution=480x360&compression=30&clock=1&date=1&dummy=';
	url +=  theDate.getTime().toString(10);
	$('#webcam10 .webcam img').attr('src', url);
}
function reloadWebCam30()
{
	theDate = new Date();
	var url = 'http://46.21.68.138:88/axis-cgi/jpg/image.cgi?resolution=480x360&compression=30&clock=1&date=1&dummy=';
	url +=  theDate.getTime().toString(10);
	$('#webcam30 .webcam img').attr('src', url);
}
function reloadWebCam10b()
{
	theDate = new Date();
	var url = 'http://46.21.68.138:99/axis-cgi/jpg/image.cgi?resolution=480x360&compression=30&clock=1&date=1&dummy=';
	url +=  theDate.getTime().toString(10);
	$('#webcam10b .webcam img').attr('src', url);
}

function reloadWebCam5()
{
	theDate = new Date();
	//var url = 'http://92.255.200.43:1080/axis-cgi/jpg/image.cgi?resolution=480x360&compression=30&clock=1&date=1&dummy=';
	var url = 'http://46.52.175.210/axis-cgi/jpg/image.cgi?resolution=480x360&compression=30&clock=1&date=1&dummy=';
	url +=  theDate.getTime().toString(10);
	$('#webcam5 .webcam img').attr('src', url);
}

function reloadWebCam7()
{
	theDate = new Date();
	var url = 'http://81.25.170.193:1080/axis-cgi/jpg/image.cgi?resolution=480x360&compression=30&clock=1&date=1&dummy=';
	url +=  theDate.getTime().toString(10);
	$('#webcam7 .webcam img').attr('src', url);
}

function reloadWebCam7a()
{
	theDate = new Date();
	var url = 'http://81.25.170.193/axis-cgi/jpg/image.cgi?resolution=480x360&compression=30&clock=1&date=1&dummy=';
	url +=  theDate.getTime().toString(10);
	$('#webcam7a .webcam img').attr('src', url);
}

function reloadWebCam21()
{
	theDate = new Date();
	var url = 'http://46.21.68.254/axis-cgi/jpg/image.cgi?resolution=480x360&compression=30&clock=1&date=1&dummy=';
	url +=  theDate.getTime().toString(10);
	$('#webcam21 .webcam img').attr('src', url);
}

function reloadWebCam21a()
{
	theDate = new Date();
	var url = 'http://46.52.175.138/axis-cgi/jpg/image.cgi?resolution=480x360&compression=30&clock=1&date=1&dummy=';
	url +=  theDate.getTime().toString(10);
	$('#webcam21a .webcam img').attr('src', url);
}
function reloadWebCam31()
{
	theDate = new Date();
	var url = 'http://46.21.68.138:1090/axis-cgi/jpg/image.cgi?resolution=480x360&compression=30&clock=1&date=1&dummy=';
	url +=  theDate.getTime().toString(10);
	$('#webcam31 .webcam img').attr('src', url);
}
function popup_show2(winId, hideCloseBtn)
{
        hideCloseBtn = hideCloseBtn || false;

        $('#popup_window2 div.content2>div').hide();
        $('#popup_window2 #' + winId).show();

        popup_window_w = $('#popup_window2').width();
        popup_window_h = $('#popup_window2').height();
        window_w = $(window).width();
        window_h = $(window).height();
        new_left = (window_w - popup_window_w)/2;
        if (new_left < 0) new_left = 10;
        var new_top = (window_h - popup_window_h)/2;
        if (new_top < 0) new_top = 10;
        $('#popup_window2').css('left', new_left + 'px');
        $('#popup_window2').css('top', new_top + 'px');

        if (hideCloseBtn) $('#popup_window2 .close_btn2').hide();
        else $('#popup_window2 .close_btn2').show();

		$('#black_layer').show();
        $('#popup_window2').show();

		if ( winId == 'webcam') setInterval('reloadWebCam()', 15000);
}

function popup_hide2()
{
	$('#black_layer').hide();
    $('#popup_window2').hide();
    
}

function popup_show3(winId, hideCloseBtn)
{
        hideCloseBtn = hideCloseBtn || false;

        $('#popup_window3 div.content3>div').hide();
        $('#popup_window3 #' + winId).show();

        popup_window_w = $('#popup_window3').width();
        popup_window_h = $('#popup_window3').height();
        window_w = $(window).width();
        window_h = $(window).height();
        new_left = (window_w - popup_window_w)/2;
        if (new_left < 0) new_left = 10;
        var new_top = (window_h - popup_window_h)/2;
        if (new_top < 0) new_top = 10;
        $('#popup_window3').css('left', new_left + 'px');
        $('#popup_window3').css('top', new_top + 'px');

        if (hideCloseBtn) $('#popup_window3 .close_btn3').hide();
        else $('#popup_window3 .close_btn3').show();

		$('#black_layer').show();
        $('#popup_window3').show();

		if ( winId == 'webcam') setInterval('reloadWebCam()', 15000);
}

function popup_hide3()
{
	$('#black_layer').hide();
    $('#popup_window3').hide();
    
}


/* VIDEO ON MAIN PAGE */
$('#video_button a').click(function(){
	$(this).hide();
	$('#black_layer').show();
	$('#video').show();
	return false;
});
$('#video .close_btn a').click(function(){
	$('#black_layer').hide();
	$('#video').hide();
	return false;
});
/* / VIDEO ON MAIN PAGE */

$(document).ready(function(){
	$(window).bind('resize', function(){resizeBackground();});

	/* ROTATOR NAV */
	if ( rotator_exists && cont_height > cont_wrap_height )
	{
		$('.rotator .downscroller a').show();
	}
	/* / ROTATOR NAV */

	Cufon.replace('.right .infra h2, #site_wrapper .header .links a, .right .rotator h2, .myriad', {hover: 'true'});
	if ( $.browser.msie && parseInt($.browser.version) < 8 ) Cufon.now();

	/* Бронирование */
	if ( $('#ordertable').length > 0 )
	{
		$('#ordertable td.form a').click(function(){
			target = 'mian';
			if ( $(this).is('.uni') ) target = 'uni';
			active = false;
			if ( $(this).is('.active') ) active = true;
			if ( !active )
			{
				//$('#ordertable input[name="from_' + target + '"]').attr('checked', 'checked');
				//$(this).addClass('active');

				//$('#ordertable input[name="from_' + target + '"]').attr('checked', 'checked');
                                $('#ordertable input[name="from_' + target + '"]').attr('value', '1');
				$(this).addClass('active');
			}
			else
			{
				//$('#ordertable input[name="from_' + target + '"]').removeAttr('checked');
				//$(this).removeClass('active');

				//$('#ordertable input[name="from_' + target + '"]').removeAttr('checked');
                                $('#ordertable input[name="from_' + target + '"]').attr('value', '0');
				$(this).removeClass('active');
			}
		});
		
		$('#ordertable form').submit(function(){
			name = $('input[name="name"]').val();
			phone = $('input[name="phone"]').val();
			room = $('select[name="room"]').val();
			if ( name != '' && phone != '')
			{
				//$('#ordertable td.form').html('Спасибо, Ваша заявка принята!<br /> В ближайшее время с Вами свяжутся наши менеджеры.');
			}
			else
			{
				alert('Пожалуйста, заполните все поля!');
                                return false;
			}
			//alert($('input[name="from_uni"]').attr('checked') + "  " + $('input[name="from_mian"]').attr('checked'));
			//return false;
		});
	}
	/* / Бронирование */


	/* Контакты */
	if ( $('#contacttable').length > 0 )
	{
		$('#contacttable form').submit(function(){
			name = $('input[name="contactname"]').val();
			phone = $('input[name="phone"]').val();
			room = $('select[name="email"]').val();
			if ( name != '' && phone != '')
			{
				//$('#contacttable td.form').html('Спасибо, Ваша заявка принята!<br /> В ближайшее время с Вами свяжутся наши менеджеры.');
			}
			else
			{
				alert('Пожалуйста, заполните все поля!');
                                return false;
			}
			//return false;
		});
	}
	/* / Контакты */

	/* IE6 LOCKER */
	if ( $.browser.msie && parseInt($.browser.version) < 7 )
	{
		$('head').append($('<script >').attr('type', 'text/javascript').attr('src', 'jquery.reject.rus.min.js'));

		$.reject({
              display: ['firefox','chrome','opera','safari','msie'],
              close: false,
              paragraph2: 'Щёлкните по любой иконке, чтобы перейти на страницу загрузки браузера, загрузите и установите его, после чего зайдите на сайт через него.'
          });
	}
	
    
    
    
});
/* / FOR IE */
$(function(){
   
   if ( $.browser.msie)
    {
             
        $('#back-top').css('left','240px');
        
    }
    
    if ( $.browser.msie && parseInt($.browser.version) < 8 )
    {
      $('div.content div.menu a.nicelink2').removeClass('nicelink2').removeClass('active2').addClass('nicelink3');  
      
      $('a.shock').css('margin-top','-25px');
      
      $('span.ie_br').html('<br>');
      $('span#price_kv').css('margin-bottom','5px');
	  $('.w14').css('width','150px');
	  
	  
    }
    
    
    
});
/* / FOR IE */
$(document).mousedown(function(event){
	if ( $('#video').length > 0 && $('#video').is(':visible') && $(event.target).parents('#video').length === 0)
	{
		$('#video').hide();
		$('#black_layer').hide();
	}
	});

$(window).load(function(){
	/* растягиваем фон контента */
	if ( $('#content .static .content .text').length > 0 )
	{
		var wrapper = $('#content .static');
		var container = $('#content .static .content');
		var footer = $('#site_wrapper .footer');
		var container_h = $(container).outerHeight();
		var footerPos = $(footer).offset().top;
		var containerPos = $(container).offset().top;
		if ( footerPos - (containerPos + container_h)  > 30 )
		{
			var h_to_add = footerPos - (containerPos + container_h) - 70;//alert(h_to_add);
			$(container).css('min-height', ($(container).innerHeight() + h_to_add) + 'px');
		}
	}
});
