$(function(){
	//Skryvani doplnku na detailu
	$('.sel-toggle').click(function() {
			$(this).next('.sel-hidden').toggle(250);
			return false;
	});

	//Rolovatko znacek
	$('#choice-mark .sel-toggle').click(function() {
		if($(this).hasClass('link-active')) {
			$(this).next('.box').slideUp(250, function() {
				$('#choice-mark .sel-toggle')
				.removeClass('link-active')
				.addClass('link');
			});
		}
		else {
			$(this).removeClass('link').addClass('link-active');
			$(this).next('.box').slideDown(250);
		}
		return false;
	});

	$(':not(#choice-mark .sel-toggle)').click( function(){
		$('#choice-mark .box').slideUp(250, function() {
			$('#choice-mark .sel-toggle')
			.removeClass('link-active')
			.addClass('link');
		});
	});

	//Zalozky s formulari na HP (katalog, konfigurator, objednavka)
	$('#search-box a.sel-toggle').click(function() {
		$('#search-box').children('.box').hide();
		if($(this).hasClass('sel-active')) {
			$(this).removeClass('sel-active');
		}
		else {
			$('#search-box a.sel-toggle').removeClass('sel-active');
			$(this).next('.box').fadeIn(250);
			$(this).addClass('sel-active');
		}
		return false;
	});

	//Zobrazuje/skryva detailni parametry vyhledavani ve formulari pro hledani v katalogu
	$('form#search-form a.sel-property-show').click(function() {
		var property = $(this).attr('href');
		property = property.substr(1, property.length - 1);
		$( '#sel-' + property).fadeIn( 250);
		$( this).fadeOut( 250);
	});
	$('form#search-form a.sel-property-hide').click(function() {
		var property = $(this).attr('href');
		property = property.substr(1, property.length - 1);
		$( '#sel-' + property).fadeOut( 250);
		$('form#search-form a.sel-property-show[href=#'+property+']').fadeIn( 250);
	});

	//Zobrazeni poctu dostupnych vozidel podle aktualniho zadani ve formulari
	$('#search-box form#search-form input, form#search-form select').change(function() {
		var _data = $(this).parents('form').serialize();
		$('#sel-count-results').html( '...');
		$.getJSON('/hledani/format/json/', _data, function( json) {
			$('#sel-count-results').html( json.count);
		})
	});

	//Skryje/zobrazi defaultni text v input[type=text]
	$('.sel-toggle-text').focus( function(){
		if( $(this).attr('title') == $(this).val()) {
			$(this).val("");
		}
	}).blur( function() {
		if(  $(this).val() == '') {
			$(this).val( $(this).attr('title'));
		}
	});

	//Pridavadi/odebirani vozidel k porovnani
	$('.sel-add-garage-input').change(function( e){
		var _url = null;
		if(this.checked) {
			_url = $(this).parents('form').attr('action');
		}
		else {
			_url = $(this).siblings('input[name=sel-remove-url]').attr('value');
		}
		var _spinner = new Spinner;
		_spinner.show();
		$.getJSON( _url, new Array, function(json) {
			if(json) {
				for( var name in json.snippets) {
					_updateSnippet( name, json.snippets[ name]);
				}
			}
			 
			_spinner.hide();				
		});
	});

	//Automaticke odeslani formuare pri zmene klice razeni
	$('#frmcatalog-orderItemIndex').change( function() {
		$('#search-form').submit();
	});

	if(jQuery().lightBox) {
		$('.sel-lightbox').lightBox({
			imageLoading: '/images/lightbox-ico-loading.gif',
			imageBtnClose: '/images/lightbox-btn-close.gif',
			imageBtnPrev: '/images/lightbox-btn-prev.gif',
			imageBtnNext: '/images/lightbox-btn-next.gif',
			imageBlank: '/images/lightbox-blank.gif',
		});
	}	

	var ch = new Changer;
	ch.init();
});
