jQuery().ready(function(){
    $('#city, #info_city').change(function() {
        var city = $(this);
        var quarter = $('#quarter');
        if (city.attr('id') == 'info_city') quarter = $('#info_quarter');
        var ziro_element = $('option:first', quarter).html();

        quarter.html('<option value="0">' + ziro_element + '</option>');

        var value = parseInt(city.val());
        if (value > 0)
        {
            $.get( _site_root + 'application/get_quarters', { city_id: value }, function(data){
                if (data != '')
                {
                    for(i in data)
                    {
                        if (city.attr('id') == 'info_city')
                        {
                            quarter.append('<option value="' + data[i].title + '">' + data[i].title + '</option>');
                        }
                        else
                        {
                            quarter.append('<option value="' + data[i].id + '">' + data[i].title + '</option>');
                        }
                    }
                }
            }, 'json');
        }
    });

    $('.details dd').css('display', 'none');
    $('.details dd:first').css('display', 'block');

    $('.details dt:first').addClass('left');
    $('.details dt:last').addClass('right');

	var elements = jQuery('.details dt');
	elements.click(function(){
		var height=0;
		$('.details dd').fadeOut('fast');
		$('.details dt').removeClass('active');
		$(this).addClass('active');
		$(this).next().fadeIn('fast');
		height = $('.details dt').height() + $(this).next().height();
		$('.details').css({height: height});

	});
	jQuery(elements[0]).trigger('click');

	// Galleries

	window.onload = galleryWidth;

	function galleryWidth() {
	var galleryWidth = $('#img').width();
		$('#more_images .jcarousel-container').css('width', galleryWidth+2);
	    $('#more_images .jcarousel-clip').css('width', galleryWidth-37-37);
	};

    var resizeTimer = null;
    $(window).bind('resize', function() {
       if (resizeTimer) clearTimeout(resizeTimer);
       resizeTimer = setTimeout(galleryWidth, 100);
    });


	if ($('#more_images .list').length)
    {
        $('#more_images .list').jcarousel({});
        $('#more_images .list li a').click(function() {
            if (!$('#more_images.no_big_image').length)
            {
                var put_here = $('#big_image');
                var link_to_image = $(this).attr('href');

                put_here.attr('src', link_to_image);
                return false;
            }
        });


    }

    // Tables
    $('#object_childs tbody tr:odd td').each(function(){
        $(this).css('background', '#fff');
    });

    // Galleries
    $("a[rel^='prettyPhoto']").prettyPhoto({
        animationSpeed: 'fast', /* fast/slow/normal */
        padding: 40, /* padding for each side of the picture */
        opacity: 0.35, /* Value betwee 0 and 1 */
        showTitle: false, /* true/false */
        allowresize: true, /* true/false */
        counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
        theme: 'dark_square' /* light_rounded / dark_rounded / light_square / dark_square */
    });
});

// Load Flash function
function loadFlash( fname, w, h, params )
{
	var add_params = '';

	if ( params )
	{
		/*for( param in params ) {
			add_params += '<param name="'+param+'" value="'+params[param]+'" />';
		}*/
		add_params = params;
	}

	var Flash = ' \
		<object type="application/x-shockwave-flash" data="' + fname + '" width="' + w + '" height="' + h + '"> \
		<param name="quality" value="high" /> \
		<param name="scale" value="noscale" /> \
		<param name="wmode" value="transparent" /> \
		' + add_params + ' \
		<param name="movie" value="' + fname + '" /> \
		</object> \
	';

	document.write( Flash );
}