$(function(){
    if($('#bests').length)
    {
        $('#bests div.img img').hide();
        $('#bests div.img img').each(function() {
            var img = $(this);
            img.removeAttr('width');
            img.removeAttr('height');
            if((img.width() >= 450 && img.height() >= 350) || (img.width() < 450 && img.height() < 350))
            {
                if(img.width() > img.height())
                {
                    img.attr('width', 450);
                }
                else
                {
                    img.attr('height', 350);
                }
            }
            else if(img.width() >= 450 && img.height() < 350)
            {
                img.attr('height', 350);
            }
            else if(img.width() < 450 && img.height() >= 350)
            {
                img.attr('width', 450);
            }
            img.show();
        });
        function showNextBest()
        {
            var act = $('#bests div.best:visible');
            //~ console.log(act);
            var next = act.next();
            if(next.length == 0)
            {
                next = $('#bests div.best:first');
            }
            act.fadeOut('fast', function() { next.fadeIn('fast'); });
            best_timer = setTimeout(function() { showNextBest(); }, 4000);
        }
        
        best_timer = setTimeout(function() { showNextBest(); }, 4000);
    }
	$('#termin-select a').click(function(){
        $('#order-box').html('<p style="text-align:center;margin:2em 0;font-weight:bold;">ověřuji ceny...<br /><br /><img src="/images/loader.gif" alt="nahrávám..." /></p>');
        var href = this.href;
		$('#order-box').load(href, function(response){
            $('#order-box #ubytovani_id').change(function() {
                var select = $(this);
                if(select.val() != '')
                {
                    $('#order-box').html('<p style="text-align:center;margin:2em 0;font-weight:bold;">ověřuji ceny...<br /><br /><img src="/images/loader.gif" alt="nahrávám..." /></p>');
                    $('#order-box').load(href + '&ubytovani_id=' + select.val(), function(response) { });
                }
            });
		});
		return false;
	});
    $('#order-box select.count').live('change', function() {
        var select = $(this);
        var price = select.parent().parent().find('span.price').html();
        var price_sum = price * select.val();
        select.parent().parent().find('span.sum').html(price_sum);
        var sum = 0;
        $('#order-box td span.sum').each(function() {
            sum += parseInt($(this).html());
        });
        $('#order-box th span.sum').html(sum);
    });
    $('#order-box #ubytovani_id').live('change', function() {
        
    });
    $('#cestujici select[name=adult]').change(function() {
        var table = $('#cestujici table');
        var select = $(this);
        var ecount = select.val();
        var count = $('tr.adult', table).length;
        if(ecount > count)
        {
            var last_adult = $('tr.adult:last', table);
            for(var i = count; i < ecount; i++)
            {
                var last_adult = $('<tr class="adult"><td><input type="text" name="firstname[' + i + ']" /></td><td><input type="text" name="surname[' + i + ']" /></td><td>Dospělý<input type="hidden" name="tarif[' + i + ']" value="adult" /></td><td><input type="text" name="rc[' + i + ']" /></td>        <td><input type="text" name="birthdate[' + i + ']" /></td></tr>').insertAfter(last_adult);
            }
        }
        else
        {
            $('tr.adult', table).slice(ecount).remove();
        }
    });
    $('#cestujici select[name=child]').change(function() {
        var table = $('#cestujici table');
        var select = $(this);
        var ecount = select.val();
        var count = $('tr.child', table).length;
        if(ecount > count)
        {
            for(var i = count; i < ecount; i++)
            {
                table.append('<tr class="child"><td><input type="text" name="firstname[1' + i + ']" /></td><td><input type="text" name="surname[1' + i + ']" /></td><td>Dítě<input type="hidden" name="tarif[1' + i + ']" value="child" /></td><td><input type="text" name="rc[1' + i + ']" /></td>        <td><input type="text" name="birthdate[1' + i + ']" /></td></tr>');
            }
        }
        else
        {
            $('tr.child', table).slice(ecount).remove();
        }
    });
	var tapi = $('#hotel-info .tabs').tabs('#hotel-info div.tab',{
		tabs: 'li',
		effect: 'fade',
		current: 'tabs-selected',
        api: true
	});
    $('#allphotos').click(function() { tapi.next();return false; });
	
	$('#thumbs li').click(function(){
		
		$('#thumbs li.selected').removeClass('selected');
		$(this).addClass('selected');
		
		var src   = $(this).find('img').attr('src');
		var regx = /^(.+)_[0-9x]+(\.[a-z]+)$/;
		regx = regx.exec(src);
		
		if (regx && regx[1] && regx[2])
		{
			$('#photo img.main').attr('src', regx[1] + regx[2]);
		}
        return false;

	});
});