var hash = '#!/';
var pngfixOff = true;

$('a[rel]:not(a[rel=imagebox])').live('click',function() {
	if($(this).attr('rel')=='external') {
		this.target = "_blank";
		return true;	}
	else {
		var hash='#!'+$(this).attr('href');
		location.href='/'+hash;
	}
	return false;
});

function hashchange() { 
	if(location.hash!=hash) { 
		if(location.hash.length>0) { var hash=location.hash; } else { var hash="#!/"; }
		tmp=hash.split("#"); url=tmp[1].replace("!",""); tmp=url.split('/');
		$('a.sel').removeClass('sel');
		$('a[href='+tmp[0]+']').addClass('sel');
		$('div#maindiv div.content').load('null.php?action='+url,afterLoad); 
	} 
}

function showNextImg() {
	var all=$('#menudiv img.topimg');
	var last=$('#menudiv img.topimg:visible');
	if($('#menudiv img.topimg:visible').next().length==1) 
		$('#menudiv img.topimg:visible').next().fadeIn(1000,function(){ last.hide(); });
	else {
		$('#menudiv img.topimg:first').show();
		last.fadeOut(1000);
	}
}

function Linky() {
	if($('#menudiv .linka').length==0) $('#menudiv').append('<span class="linka"></span>');
	var x=(Math.random()-0.5)*100;
	if($('#menudiv .linka').css("margin-left")>1000 || $('#menudiv .linka').css("margin-left")<0) $('#menudiv .linka').css("margin-left","0px");
	$('#menudiv .linka').animate({"margin-left":x+"px"},1000);
}

$(document).ready(function() {
//	$('#menudiv ul').css({'margin-left':'-300px'}).animate({'margin-left':'0px'},1000);
	$('#menudiv img.topimg:first').show();
	$('#menudiv ul').hide().fadeIn(2000);
	$('#menudiv ul li a').hover(function(){ 
								    $(this).find("img.menuimg").stop().animate({"margin-right":"20px"},500);
								    $(this).find("img.hoverimg").fadeIn(500);
								},
								function(){
								    $(this).find("img.menuimg").stop().animate({"margin-right":"8px"},500);
								    $(this).find("img.hoverimg").fadeOut(500);
								});
	var timer1=setInterval(showNextImg,5000);
	var timer2=setInterval(Linky,1500);
	if(location.hash.length==0) afterLoad(); else hashchange();
	$(window).bind('hashchange', hashchange);
});


function afterLoad() {
	$(".autocomplete" ).combobox();
	parseHash(location.hash);
}

function parseHash(myhash) {
	url=myhash.split("#");
	if(url.length>2) setTimeout("offset=$('a[name="+url[2]+"]').offset(); $('body').scrollTop(offset.top);",200); else $('body').scrollTop(0);
	return '#'+url[1];
}

(function( $ ) {
		$.widget( "ui.combobox", {
			_create: function() {
				var self = this,
					select = this.element.hide(),
					selected = select.children( ":selected" ),
					value = "";
//					value = selected.val() ? selected.text() : "";
				var input = this.input = $( "<input>" )
					.insertAfter( select )
					.val( value )
					.attr('title','zadejte či vyhledejte adresu')
					.autocomplete({
						delay: 0,
						minLength: 0,
						source: function( request, response ) {
							var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
							response( select.find( "option" ).map(function() {
								var text = $( this ).text(); var label = text;
								if ( this.value && ( !request.term || matcher.test(text) ) ) {
									if(request.term) var label=label.replace(
											new RegExp(
												"(?![^&;]+;)(?!<[^<>]*)(" +
												$.ui.autocomplete.escapeRegex(request.term) +
												")(?![^<>]*>)(?![^&;]+;)", "gi"
											), "<strong>$1</strong>" );
									return {
										label: label,
										value: text,
										title: $(this).attr('title'),
										option: this
									};
								}
							}) );
						},
						select: function( event, ui ) {
							ui.item.option.selected = true;
							self._trigger( "selected", event, {
								item: ui.item.option
							});
							tmp=ui.item.option.value.split(';');
							$('.ui-autocomplete-input').val('');
							$('.technici .kontakt').hide();
							if(tmp[0]!='') $('.technici div.technik span.jmeno:contains('+tmp[0]+')').parent().show();
							if(tmp[1]!='') $('.technici div.referent span.jmeno:contains('+tmp[1]+')').parent().show();
							if(tmp[2]!='') $('.technici div.ucetni span.jmeno:contains('+tmp[2]+')').parent().show();
						},
						change: function( event, ui ) {
							if ( !ui.item ) {
								var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( $(this).val() ) + "$", "i" ),
									valid = false;
								select.find( "option" ).each(function() {
									if ( $( this ).text().match( matcher ) ) {
										this.selected = valid = true;
										return false;
									}
								});
								if ( !valid ) {
									// remove invalid value, as it didn't match anything
									$( this ).val( "" );
									select.val( "" );
									input.data( "autocomplete" ).term = "";
									return false;
								}
							}
						}
					})
					.addClass( "ui-widget ui-widget-content ui-corner-left" );

				input.data( "autocomplete" )._renderItem = function( ul, item ) {
					if($(ul).children('li').length % 2 == 0) var trida="odd"; else var trida="";
					return $( "<li></li>" )
						.addClass(trida)
						.data( "item.autocomplete", item )
						.append( "<a>" + item.label + "<span>" + item.title + "</span></a>" )
						.appendTo( ul );
				};

				this.button = $( "<button type='button'>&nbsp;</button>" )
					.attr( "tabIndex", -1 )
					.attr( "title", "Show All Items" )
					.insertAfter( input )
					.button({
						icons: {
							primary: "ui-icon-triangle-1-s"
						},
						text: false
					})
					.removeClass( "ui-corner-all" )
					.addClass( "ui-autocomplete-button" )
					.click(function() {
						// close if already visible
						if ( input.autocomplete( "widget" ).is( ":visible" ) ) {
							input.autocomplete( "close" );
							return;
						}

						// pass empty string as value to search for, displaying all results
						input.autocomplete( "search", "" );
						input.focus();
					});
			},

			destroy: function() {
				this.input.remove();
				this.button.remove();
				this.element.show();
				$.Widget.prototype.destroy.call( this );
			}
		});
	})( jQuery );
