// jquery.rcz.hp.js v0.1, last update: 26.6.2010 1:10:37
// Base library of Reality.CZ Web2011
// Copyright (c) 2010 Jiri Jezdinsky, <jezdinsky@reality.cz>
// Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
//
// Dependencies: jquery-1.4.2, jquery-ui-1.8.2, jquery.rcz

RCZ = RCZ0.extend({
	init: function(options) {
		this._super(options);
		this.hlfrminit($("form#hlfrm09"), true);
		var self = this;
//		$('#inpobec').focus();
		$('.rolo').infiniteCarousel();
		$('#playstop').click(function () {
			self.playPause(self.autoscrolling ? 0 : true);
		});
		$('#hprollertabs').find('ul.tabs a').click(function () {
			self.playPause(0);
		}).end().find('.wrapper ul li').click(self.divClick);
		$('#hproller').mouseover(function () {
			if (self.autoscrolling) self.playPause(false);
		}).mouseout(function () {
			if (self.autoscrolling === false) self.playPause(true);
		});
		setInterval(function () {
			if (self.autoscrolling) $('.rolo').trigger('next');
		}, 7000);
		var $ul = $('ul', $('#hproller'));
		var vyska = $ul.height()+5;
		$ul.children('li').height(vyska);
		$('#hproller').height(vyska+8).children().first().height(vyska+3);
		vyska = (RCZ_IE && RCZ_IE8) ? 48 : 44;
		$('#hpmakler2').height($('#hprollertabs').height()-$('#hpmakler1').height()-vyska);
		self.playPause(true);
		var i = 1;
		$('.rolo li').each(function(){
			if (i++ % 3 == 1) $(this).addClass('bcFF');
		});	
		if (RCZ_IE) {
			$("#hpmakler1 div.hpmaklp").cornerz({radius: 4});
			$('ul.tabs li a').cornerz({
				radius: 4,
				corners: "tl tr"
			});
		}
	},
	playPause: function(value) {
		this.autoscrolling = value;
		if (value)
			$('#playstop').removeClass('stopped').attr('title', 'Zastavit přetáčení');
		else
			$('#playstop').addClass('stopped').attr('title', 'Znovu spustit přetáčení');
	},
	highlightAutocomplete: function(value, term) {
		return value.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + term.replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi, "\\$1") + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<strong>$1</strong>");
	},
	initAutocompleteOrig: function(self) {
//		if ($('#inpobec').val()=='') $('#inpobec').val(this.options.lokalita);
		var opts=this.options;
		$('#inpobec').focus(function( event ) {
			if (this.value == opts.lokalita) this.value = '';
			this.name = 'x'+Math.random(); // Firefox hotfix, against empty input
		}).blur(function( event ) {
		})
		this.inheritance('autocomplete');
		$('#inpobec').autocomplete({
			minLength: 2,
//			highlight: true, 
			source: function(request, response) {
				$.ajax({
					url: '/auto',
					dataType: "json",
					data: request,
					success: function( data ) {
						response( data );
					},
					error: function( data ) {
						response( [{"value":"", "label":data.responseText, "kod":"", "okres":""}] );
					}
				});
			},
			focus: function(event, ui) {
				$('#inpobec').val(ui.item.label);
//				return false;
			},
			select: function(event, ui) {
//			(oldcolor.match(/(bo?ld)/)) col += ' '+RegExp.$1
				//$('#o').val(ui.item.kod);
//				$('#oblkod').val(ui.item.value.toAscii());
//				$('#vyhledat').attr({'src': "/images/design/hled09.png", 'title': ''});
//				return false;
			}
		})
		.autocomplete('instead','_renderItem', function( ul, item ) {
			var a = $( "<a>" + self.highlightAutocomplete(item.label, this.term) + (item.okres ? ' <span class="fss">(' + item.okres + ")</span>" : "") + "</a>" );
			if (item.kod == '') a.addClass('cor bld');
			return li = $( "<li></li>" )
				.data( "item.autocomplete", item )
				.append( a )
				.appendTo( ul );
		});
		$('#inpobec').removeAttr('autocomplete');
	}
});


