(function($){
  

  $.fn.equalHeight = function() {  
    var tallest = 0;

    var result = this.each(function() {        
      var thisHeight = $(this).height();
      if(thisHeight > tallest) {
        tallest = thisHeight;
      }
    });

    this.height(tallest);
    return result;
  };



  $.fn.valign = function(options) {
    return this.each(function(i){
      var ah = $(this).height();
      var ph = $(this).parent().height();
      var mh = (ph - ah) / 2;
      $(this).css(options.absolute ? 'top' : 'padding-top', mh);
    });
  };
})(jQuery);

function show_kraj(i_rel,fast){
    $('.i-cont').hide(150);
    i_rel = i_rel.split("_");
    i_rel = i_rel[0];
    $('.i-'+i_rel).show(fast?0:400);
    if($('#export-pdf-link').attr('href')){
      novy = $('#export-pdf-link').attr('href').replace(/%23.*/gi,"") + "%23" + i_rel;
      $('#export-pdf-link').attr('href',novy);
    }
  }

jQuery(function($) {

  if(!$(".je-cena").html()) $('.prices-s').hide();

  if($("#header").css("display")=="none"){   // nefunkcni predelavani fotek za originaly pro pdf...
    $("[src$='.jpg']").each(function(){
      if($(this).attr("src").indexOf("_w")!=-1){
        new_src = $(this).attr("src").substr(0,$(this).attr("src").indexOf("_w"))+".jpg";
        //$(this).attr("src",new_src);
      }
    })
  }
  
  //videa
  if(typeof(videa)!="undefined"){
    var html = "";
    for (var key in videa) {  
      if(videa[key][0]){
        $('#videa').html($('#videa').html() + "<a class='fvideo' href='#' onclick='f_video(\""+ videa[key][1] +"\",\""+ (typeof(videa[key][3])!="undefined"?videa[key][3]:"") +"\"); return false;'><img src='"+ videa[key][0] +"' alt='' /><br />"+ (typeof(videa[key][2])!="undefined"?"<span>"+videa[key][2]+"</span>":"") +"&nbsp;</a>");
      }
    }
  }

  // uložení pdf
  $("[href$='.pdf'], .save_as").each(function(){
      /// get basename
      b = $(this).attr("href").replace(/^.*[\/\\]/g, '');
      if (typeof(suffix) == 'string' && b.substr(b.length - suffix.length) == suffix) {
        b = b.substr(0, b.length - suffix.length);
      }
      ///
    $(this)//.attr("target","blank")
    .attr("rel","nofollow")
    .attr("href","http://www.alcaplast.info/pdf?file_url=" + ($(this).attr("href").indexOf("ttp://")!=-1 ? $(this).attr("href"): global_url+$(this).attr("href")) + "&file=" + b +  "&end=p"); //$(this).attr("title")
  })
    
 
  $("#instalateri").css("border","none");

  $("#menu a span").each(function(){
    text = $(this).html();
    if(text.length>20){
      $(this).addClass("xsmaller");
    }
  });
  
  // kontakt form
  $("#contact-form").submit(function(){
    var vreturn = true;
    $(".invalid-text").hide();
    $("#contact-form input, #contact-form textarea").removeClass("invalid");
    if($("#contact-form #name").val() == ""){
      $("#contact-form #name").addClass("invalid");
      vreturn = false;
    }
    if($("#contact-form #email").val() == ""){
      $("#contact-form #email").addClass("invalid");
      vreturn = false;
    }
    if($("#contact-form #body").val() == ""){
      $("#contact-form #body").addClass("invalid");
      vreturn = false;
    }
    if(!vreturn){
      $(".invalid-text").show();
      return vreturn;
    }
  });

  //po startu
  var hash = location.hash;
  hash = hash.replace(/^#/, '');
  if(hash!="") show_kraj(hash,true);
  $('.instalateri a').click(function(){
    var i_rel = $(this).attr('rel');
    show_kraj(i_rel);
    return false;
  });
  $(window).bind( "hashchange", function(){
    hash = location.hash;
    hash = hash.replace(/^#/, '');
    if(hash.indexOf("_") === -1){
      show_kraj(hash);
    }
  });

  // skrytí většinou obrázkových příloh
  $("[title='hide']").attr("href","").hide();
  $("[title='hide'] a").attr("href","");

  $('#menu > ul > li').hover(function() { $(this).find('ul').show(); }, function() { $(this).find('ul').hide(); });
  $('#menu > ul > li:has(li.current)').addClass('current');

  $('#product-text, #konfigurator-wrap').equalHeight();  

  $('#product-categories h2').each(function() {
    var div = $(this);
    if (div.height() == 13) { div.css('padding-top', '9px'); }
  });

  var anchor = $.url.attr('anchor');
  if (anchor) {
    $('.part-panel').hide();
    $('#'+anchor).show();
  }

  $('ul.tabs a').click(function() {
    var link = $(this);
    $('.part-panel').hide();
    $('ul.tabs a').removeClass('selected');
    $(link.attr('href')).show();
    link.addClass('selected');

    var exportLink = $('#export-pdf-link');
    exportLink.attr('href', exportLink.attr('href').split('?')[0] + '?selected=' + link.attr('href').substr(1));

    return false;
  });

  $('#menu > ul > li > a').attr('href', 'javascript:void(0)');

  var fragments = window.location.pathname.replace(/^\//,'').replace(/\/$/,'').split('/');

  var pathnames = [];

  for(var i=0; i < fragments.length; i++){ pathnames.push('/'+fragments.slice(0,i + 1).join('/')+'/');}

  $('#menu a[href="' + pathnames[1] + '"]').parent('li').parents('li').addClass('current');

  var current_pathname = '';

  while(pathnames.length > 0) {
    current_pathname = pathnames.pop();
    
    if($('#submenu a[href="' + current_pathname + '"], #submenu a[href="' + current_pathname.substr(0,current_pathname.length-1) + '"]').not('.redirecting-link').parent('li').addClass('current').size() > 0)
    {
      pathnames = [];
    }
  };  

  $('#submenu li:has(ul)').each(function() {
    var li = $(this);

    if (li.hasClass('current') || li.has('li.current').length > 0) {
      li.prepend('<img src="/images/bullet_open.png" class="bullet" />');
    } else {
      li.find('ul').hide();
      li.prepend('<img src="/images/bullet_close.png" class="bullet" />');
    }
  });

  $('#submenu img.bullet').click(function() {
    var bullet = $(this);
    var subUl = bullet.siblings('ul');
    
    if (subUl.is(':hidden')) {
      bullet.attr('src', '/images/bullet_open.png');
      subUl.slideDown();
    } else {
      bullet.attr('src', '/images/bullet_close.png');
      subUl.slideUp();
    }
  });

  $('#product-list li.item').click(function() {
    window.location = $(this).find('a').attr('href');
  });

  $('#submenu a[href^="http://skola.alcaplast.cz"]').attr('target', '_blank');

$("[href$='.gif'],[href$='.jpg'],[href$='.png'],[href$='.JPG']").attr('rel', 'page_image')
//$("#product-text a ").attr('rel', 'product_image')
$("#fotogalerie-part a").attr('rel', 'foto_image')
$("#prislusenstvi-part a").attr('rel', 'prislusenstvi_image')
$("#podrobnosti-part a").attr('rel', 'podrobnosti_image')
$("#montaz-part a").attr('rel', 'montaz_image')
$("#nahradni_dily-part a").attr('rel', 'nahradni_dily_image')
$("#navody-part a").attr('rel', 'navody')

$("[href*='.gif'],[href*='.jpg'],[href*='.png'],[href*='.JPG']").not("[href*='.jpg&file']").fancybox({
  'overlayOpacity': 0.1,
  'titlePosition': 'inside',
  'transitionIn'	:	'fade',
  'transitionOut'	:	'fade',
  'speedIn'		:	600, 
  'speedOut'		:	400
});


});



/*$('#menu ul ul').each(function() {
    var ul = $(this);
    var widest = 0;
      
    ul.find('a').each(function() {        
      var thisWidth = $(this).width();
      if(thisWidth > widest) {
        widest = thisWidth;
      }
    });

    ul.width(widest + 30);
    ul.find('a').css('display', 'block');
  });

  $('#menu ul ul').hide();*/

function f_video(url,image){
  $.fancybox({
        'overlayOpacity'      : 0.1,
	'padding'		: 0,
	'autoScale'		: false,
	'transitionIn'	        : 'none',
	'transitionOut'	        : 'none',
	'width'		        : 604,
	'height'		: 453,
	'href'			: '/flash/mediaplayer.swf',
	'type'			: 'swf',
	'swf'			: {
	   	'wmode'	           : 'transparent',
		'allowfullscreen'  : 'true',
                'flashvars'        : 'file='+ url +'&amp;controlbar=over&amp;image='+ image
	}
  });
}



