jQuery(function($) {

	if( $('div.insta-popup').size() > 0 ) {
    
		var html = '';
        
		$('div.insta-popup').each( function() {
			html = html + $(this).html();
			$(this).remove();
		});
        
		html = '<div class="content2" style="width: 800px;">'+html+'</div>';
        
		$('body').append('<div style="display: none;"><div id="insta-popup">&nbsp;</div><a href="#insta-popup" id="insta-popup-trigger" class="nod">&nbsp;</a></div>');
		$('a#insta-popup-trigger').fancybox({
			'padding' : 20,
			'hideOnContentClick' : false,
			'autoScale' : false,
			'titleShow' : false,
			'transitionIn' : 'none',
			'transitionOut' : 'none',
			'content' : html
		});
        
		$("a#insta-popup-trigger").trigger("click");
	}


	/* ----------------------------------------------------------------
    jQuery Tabs
  ---------------------------------------------------------------- */
	var tabContainers = $('div.tabs > div');
	tabContainers.hide().filter(':first').show();

	$('div.tabs ul.tabNavigation a').click(function () {
		tabContainers.hide();
		tabContainers.filter(this.hash).show();
		$('div.tabs ul.tabNavigation a').removeClass('selected');
		$(this).addClass('selected');
		// other color underlining
		if ($(this).hasClass('bordeau')) {
			$(this).parents('.tabNavigation').addClass('tab-bordeau');
		} else {
			$(this).parents('.tabNavigation').removeClass('tab-bordeau');
		}
		return false;
	}).filter(':first').click();
	


	/* ----------------------------------------------------------------
    external links
  ---------------------------------------------------------------- */
	$("a[href^=http]").not('.nonewpage').each(function() {
		if (this.href.indexOf(location.hostname) == -1) {
			$(this)
			.addClass('external')
			.click(function() {
				window.open(this.href);
				return false;
			});
		}
	});
  
	$("a[href^=/redirect.php]").each(function() {
		$(this)
		.addClass('external')
		.click(function() {
			window.open(this.href);
			return false;
		});
	});
  
  
	/* ----------------------------------------------------------------
    files
  ---------------------------------------------------------------- */
	$('a[href$=.pdf]').addClass('file');
	$('a[href$=.pdf]').addClass('pdf');
  
  	
	/* ----------------------------------------------------------------
    pdf do noveho okna
  ---------------------------------------------------------------- */    
	$("a[href$=pdf]").click(function(){
		window.open(this.href);
		return false;
	});
			
		
	/* ----------------------------------------------------------------
    toggle_container
  ---------------------------------------------------------------- */
	$(".toggle_container").hide();

	$("h2.h2-crumbs, h3.h3-crumbs, p.crumbs").click(function() {
	  if ($(this).next().is(':visible')) {
      $(this).removeClass("active");
      if (!$('body.no-more').length) {
        $(this).find('em').text('(více...)');
      }
    } else {
      $(this).addClass("active");
      if (!$('body.no-more').length) {
        $(this).find('em').text('');
      }
    }
    return false;
	});
	
	$("h2.h2-crumbs, h3.h3-crumbs, p.crumbs").click(function() {
	  if ($(this).next().is(':visible')) {
      $(this).next(".toggle_container").slideUp("slow,");
    } else {
      $(this).next(".toggle_container").slideDown("slow,");
    }
    return false;
	});

  if (!$('body.no-more').length) {
    $('h2.h2-crumbs a, h3.h3-crumbs a, p.crumbs a').after('&nbsp;&nbsp;<a href="#"><em>(více...)</em></a>');
  }
	
  if (document.location.hash) { // otevře se hash z adresy
    $(document.location.hash).show().prev().addClass("active").find('em').text('');
    $(document.location.hash).parents(".toggle_container").show().prev().addClass("active").find('em').text('');
    $.scrollTo(document.location.hash);
  }


  /* ----------------------------------------------------------------
    collapser
  ---------------------------------------------------------------- */
	$('.collapser').collapser({
		duration: 250,
		easing: 'swing'
	});
	 
	 
	/* ----------------------------------------------------------------
    lightboxes
  ---------------------------------------------------------------- */
	$('a.iframe').fancybox();
  
	/* other galleries */
	$('a[rel=lightbox]').fancybox({
		'transitionIn' : 'none',
		'transitionOut' : 'none',
		'titlePosition' : 'over'
	});

	//var linkForAjax = $('#office-list .contact').attr('href');
	$('#office-list .contact, #office-list2 .contact').each(function() {
		$(this).attr('href', $(this).attr('href') + '&t=iframe')
	});
	$('#office-list .contact, #office-list2 .contact').fancybox({
		type: 'iframe'
	});

  
	/* ----------------------------------------------------------------
    iE6 workaround (hover)
  ---------------------------------------------------------------- */
	if ($.browser.msie && $.browser.version == 6) {
		$('#menu a, #menu li').hover(function() {
			$(this).addClass('hover');
		},
		function() {
			$(this).removeClass('hover');
		});
	}

	/* Clicable whole table row */
	$('.office-tabs table tbody').css('cursor','hand').css('cursor','pointer');
	$('.office-tabs table').delegate('tr', 'click', function() {
		$('a', this).click();
	});


    $('ul.image-slider').each( function() {
        var images = $(this).children('li').size();
        if( images < 2 ) {
            $(this).children('li').addClass('visible');
            return true;
        }
        $(this).attr('count', images );
        
        var index = 0;
        $(this).children('li').each( function() {
            $(this).attr('index', index );
            index = index + 1;
        });
        
        $(this).children('li:first').addClass('visible');
        
        $(this).append('<li class="arrow ar-left"><a href="#"></a></li>');
        $(this).append('<li class="arrow ar-right"><a href="#"></a></li>');
        
        $(this).find('li.arrow > a').click( function() {
            var parent = $(this).parents('ul.image-slider')
            var max = parseInt( parent.attr('count'));
            var clickIndex = parseInt( parent.children('li.visible').attr('index'));
            
            if( $(this).parent('li').attr('class') == 'arrow ar-left' ) {
                nextIndex = clickIndex - 1;
            } else {
                nextIndex = clickIndex + 1;
            }
            
            if( nextIndex == max ) {
                nextIndex = 0;
            }
            
            if( nextIndex < 0 ) {
                nextIndex = images - 1;
            }

            parent.children('li.visible').removeClass('visible');
            parent.children('li[index='+nextIndex+']').addClass('visible');
            
            return false;
        });
    });  
});


/* ---------------------------------------------------------------- 
    Table rowizer
  ---------------------------------------------------------------- */
function makeRows( elemntID, startRow, rowClass ) {
	if ( elemntID == null || typeof( elemntID ) == "undefined" ) {
		return;
	}
	if ( startRow == null || typeof( startRow ) == "undefined" ) {
		startRow = 2;
	} // by default we start w 2rd row
	if ( rowClass == null || typeof( rowClass ) == "undefined" ) {
		rowClass = "bgcolor";
	}
	
	var tabulka = document.getElementById( elemntID );
	if ( !tabulka || typeof tabulka == "undefined" ) {
		return;
	}
	
	var typ = tabulka.nodeName;
	if ( typ != "TABLE" ) {
		return;
	} // We need to have a table
	
	var radky = tabulka.getElementsByTagName("tr");
	if ( radky.length < startRow ) {
		return;
	} // Not needed
	
	for( i = startRow; i < radky.length; i = i + 2 )
	{
		radky[i].className = rowClass;
	}
}
  
/* --------------------------------------------------------------------
    Seznam smluvnich zarizeni pro Zdravotni asistencni sluzbu Medikompas
  --------------------------------------------------------------------- */ 
function showThisRegion(region){
	$("#service-department h2, #service-department .fLeft").show();
	$("table.sdresult").hide();
	$("#where h2.regionView").html($("#r"+region).html());
	$("table.i"+region).show();
}
function viewNameRegion(region,view){
	if (view=="show"){
		$("#where h2.regionViewShow").show();
		$("#where h2.regionViewShow").html($("#r"+region).html());
	}
	else{
		$("#where h2.regionViewShow").html("");
	}
}


/* --------------------------------------------------------------------
    We need this for closing hp flash, dont delete it, ffs
  --------------------------------------------------------------------- */ 

function hideLayer(id) {
	document.getElementById(id).style.display = "none";
}



