$( document ).ready(function(){

	/*
	$( 'div.gallery-page' ).live( 'pageshow', function( e ) {
		$( 'div.gallery a', e.target ).photoSwipe();
		return true;
	});
	*/

});

// página de pesquisa
$( '#pesquisa' ).live( 'pageshow', function() {

	$( '.sliderFieldHolder' ).each( function( index, element ) {
		$( element ).children( '.ui-slider-input' ).css( 'display', 'none' );
		$( element ).children( 'div.ui-slider' ).append( '<div class="slider-bg"></div><div class="range-holder"></div><div class="range"></div>' );
		$( element ).children( '.ui-slider-input' ).bind( 'change', function( a ) {
			var handler = $( element ).find( 'a.ui-slider-handle' ).css( 'left' );
			$( element ).find( '.range' ).css( 'width', handler );
			$( element ).find( '.price_to .value' ).html( $( a.target ).val() );
		});
		$( element ).find( '.range' ).css( 'width', $( element ).find( 'a.ui-slider-handle' ).css( 'left' ) );
	});

	var categories = $( '.carTypes ul li a' );
	categories.each( function( index, item ) {
		$( item ).click( function() {
			$( '.carTypes ul li a .icon.active' ).removeClass( 'active' );
			$( item ).find( '.icon' ).addClass( 'active' );
			$( '#categoria' ).val( index );
		});
	});

	// ajax marcas/modelos
	$( '#marca' ).change( function() {
		var marca = $( '#marca' ).val();
		$.get( '_ajax/search.ajax.php?getModels=true&marca=' + marca , function( data ) {
			$( '#modelo' ).html( data );
			$( '#modelo' ).removeAttr( 'disabled' );
			$( '#modelo' ).selectmenu( 'refresh', true );
		});
	});

});

$( '#detalhes' ).live( 'pageshow', function() {

	// calcular a largura

	var imgs = $( '.carImagesHolder img' );
	if( imgs.length > 0 ) {
		var imgsObject = [];
		$.each( imgs, function( index, item ) {
			imgsObject[index] = new Object();
			imgsObject[index].image = item.src;
		});
		var carousel = new $.widgets.Carousel({
			uuid: 'carousel',
			args: {
				'scrollInterval' : 500,
				'itemWidth': 640
			},
			value: imgsObject
		});
		$( '.carImagesHolder' ).remove();
		$( '#carousel_template' ).remove();
	}

});
