var autoScrollTimeout = 5000, autoTimer = null;
var rooms;

jQuery.preloadImages = function() 
{
	jQuery.each( arguments, function (e) {
		jQuery("<img>").attr("src", this);
	});
}


function mainMenuOn( e )
{
    offTimer();
    nms = $(this).attr('className').split(' ');
    $('.mmi').removeClass('ammi');
    $(this).addClass('ammi');
    po = $('#p_'+nms[1]);
    if( po.get() )
	{
	$('.smd').hide();
	offs = $(this).offset();
	po.css('left',offs.left);
	po.css('top',offs.top+$(this).height()+1);
	if( po.width() < $(this).width() )
	    po.width( $(this).width() + ($.support.boxModel?0:2));
	po.show();
	}
}

var turnOffTimer = null;
function mainMenuOff( e )
{
//    $(this).removeClass('ammi');
    turnOffTimer = setTimeout( 'clearMenu()', 300 );
}

function offTimer()
{
    if( turnOffTimer )
	{
	clearTimeout( turnOffTimer );
	turnOffTimer = null;
	}
}

function clearMenu()
{
    offTimer();
    $('.mmi').removeClass('ammi');
    $('.smd').hide();
}

function turnSM( e )
{
    $(this).toggleClass('smdia'); 
}

var autoPhotos = null, photosDelay = 6000;
function autoChangePhotos()
{
    clearTimeout( autoPhotos );
    if( photos.length <= 2 )
	return;
    idx = Math.floor(Math.random()*photos.length)+1;
    $('#ill'+idx).click();
    photosDelay = 6000;
    setPhotosTimeout();
}

function setPhotosTimeout()
{
    autoPhotos = setTimeout( 'autoChangePhotos()', photosDelay );
}

function init()
{
    $('.smib').fadeTo('slow',0.5);
    $('.smi img').click( switchSlides );
    $('.smfr img').click( changeRooms );
    $('.mmi').hover( mainMenuOn, mainMenuOff );
    $('.smd').hover( offTimer, mainMenuOff );
    $('.smdi').hover( turnSM, turnSM );
    $('.smgi .iimg').click( changePhotos );
    
    $('.mmi a').each( function(i,c) { $(this).parent().click( function() { window.location = c.href; } ); } );
    $('.smdi a').each( function(i,c) { $(this).parent().click( function() { window.location = c.href; } ); } );

    if( rooms )
	{
        for( i=1; i<rooms.length; i++ )
	    $.preloadImages( rooms[i].limg );
        $('#sl2').css('backgroundImage','url('+rooms[0].limg+')');	    
	$('#sl2 .msg').click( function() { window.location = rooms[0].url; } );	    
	}
    if( photos )
	{
        for( i=1; i<photos.length; i++ )
	    $.preloadImages( photos[i].limg, photos[i].simg );
        autoPhotos = setTimeout( 'autoChangePhotos()', photosDelay );    
	}

    autoScrollIt();
}

var currSlide = 1; var targetSlide = 1, animated = false;
function switchSlides( e )
{
	e.preventDefault();
	if( animated )
		return;
	targetSlide = $(this).parent().parent().parent().attr('id').substr(2);
	if( targetSlide == currSlide )
		return;
	
	clearAutoScroll();
	autoScrollTimeout = 20000;
	animateSlides();
}

function autoScroll()
{
	clearAutoScroll();
	autoScrollTimeout = 5000;
	targetSlide = Math.floor(currSlide)+1;
	if( targetSlide > 5 )
		targetSlide = 1;
	animateSlides();
}

function animateSlides()
{
	animated = true;
	for( i=1; i<=5; i++ )
		{
		opt = { left: 40*(i-1)+(i>1?1:0)*(i-1)+(i>targetSlide?736:0), width: (targetSlide==i?776:40) };
		if( i!=5 || $.support.opacity )
			$('#sl'+i).animate( opt, 'slow' );
		else
			$('#sl'+i).animate( opt, 'slow', swapSmall );
		}
	if( $.support.opacity )
		{
		$('#sl'+currSlide+' .smc').show('slow');
		$('#sl'+targetSlide+' .smc').hide('slow', swapSmall );
		}
	else
		$('#sl'+targetSlide+' .smc').hide();
}

function autoScrollIt()
{
	if( autoTimer )
		clearTimeout( autoTimer );
	autoTimer = setTimeout( 'autoScroll()', autoScrollTimeout );
}

function clearAutoScroll()
{
	if( autoTimer )
		{
		clearTimeout( autoTimer );
		autoTimer = null;
		}
}

function swapSmall()
{
	$('#sl'+currSlide+' .smc').show();
	currSlide = targetSlide; 
	animated = false;

	autoScrollIt();
}

var newRoom = 0, roomsChanging = false; 
function changeRooms()
{
	if( roomsChanging )
		return;

	newRoom = $(this).parent().attr('id').substr(4);
	
	$('#sl2').append( '<div id="smlrg"><img src="'+rooms[newRoom].limg+'" width=785 height=350 border=0 alt=""></div>' );
	$('#sl2').append( '<div id="smsm"><img src="'+rooms[0].img+'" width=92 height=49 border=0 alt=""></div>' );

	o = $(this).parent().position();
	o.top = o.top+7; o.left = o.left+7;
	$('#smsm').css( 'top', o.top );
	$('#smsm').css( 'left', o.left );

	clearAutoScroll();
	roomsChanging = true;
	$('#smsm').fadeTo(450,1);
	$('#smlrg').fadeTo(500,1,function() 
		{
		$('#sl2').css('background','url('+rooms[newRoom].limg+') top left no-repeat');
		$('#sl2 .msg h1').html( rooms[newRoom].txt );
		$('img',$('#smsl'+newRoom)).attr('src',rooms[0].img);
		a = rooms[0];
		rooms[0] = rooms[newRoom];
		rooms[newRoom] = a;
		if( $.support.opacity )
			{
			$('#sl2 .msg').hide();
			$('#sl2 .msg').fadeTo('slow',1, function() { roomsChanging = false; } );
			}
		else
			roomsChanging = false;		
		$('#smlrg').remove();
		$('#smsm').remove();
		autoScrollIt();
		} 
	);
}

var changingPhotos = false;
var photos = null; var swPhoto = 0;
function changePhotos()
{
    if( changingPhotos )
	return;
	
    clearTimeout( autoPhotos );
    changingPhotos = true;
    swPhoto = $(this).attr('id').substr(3)*1;
    
    $('.lrgi').prepend( '<div class="nwi"><img src="'+photos[swPhoto].limg+'" width=392 height=300 border=0></div>' );
    $(this).parent().parent().prepend( '<div class="nwi"><img src="'+photos[0].simg+'" width=124 height=95 border=0></div>' );
    t = photos[0];
    photos[0] = photos[swPhoto];
    photos[swPhoto]=t;    
    
    $('.lrgi .nwi').animate( {top: 0}, 'normal' );    
    $(this).parent().animate( {top: -95}, 'normal', function() 
	{
	$('.lrgi .curr img').attr('src',photos[0].limg);
	$('.lrgi .curr').css('top',0);
	$('.lrgi .nwi').remove();
	$('img',$(this)).attr('src',photos[swPhoto].simg);
	$(this).css('top',0);
	$('.smgi .nwi').remove();
	changingPhotos = false;
	}
    );
}

eprts = Array( 'ua', 'com', 'praguehotel', '@' );
function putEmail( what )
{
    s = '';
    for( i=eprts.length; i>0; s += eprts[--i] + (i==eprts.length-1?'':'.') );
    s = what+s.substr(0,s.length-1);
    document.write( '<a class="email" href="mailto:'+s+'">'+s+'<\/a>' );
}

function initPrices()
{
    $('.currs').click( function() 
        {
        idx = $(this).attr('id').substr(4)-1;
        npr = Math.round(data.pr/data.curr[idx].coef)+' <span class="curr">'+data.curr[idx].sign+'</span>';
        $('.price .sum').html( npr );
        $('.currs').removeClass('ac');
        $(this).addClass('ac');
        }
    );
}
								
