
$(document).ready(function() {
  cover = $('.banners').everyTime(5000, function () {
    if(!$(this).hasClass('mouse-is-over')) {
      $(this).changeBanner('slow');
    }
  }).mouseover(function () {
    $(this).addClass('mouse-is-over');
  }).mouseout(function () {
    $(this).removeClass('mouse-is-over');
  });
  cover.find('.back').bind('click', function (e) {
    $('.banners').changeBanner('slow', -1);
    return false;
  });
  cover.find('.next').bind('click', function (e) {
    $('.banners').changeBanner('slow', 1);
    return false;
  });
  cover.data('change_function', function (banners, banner) {
    // now let's update the switcher
    //remove all classes
    counter = $(banners).find('.banner-counter');
    for(i in banners.find('.bannerItem')) {
      counter.removeClass('selected'+i);
    }
    items = banners.find('.bannerItem');
    counter.addClass('total'+items.length).addClass('selected'+$.inArray(banner.get(0), items));
    counter.find('.bannerLink.active').removeClass('active');
    counter.find('.bannerLinkId'+($.inArray(banner.get(0), items)+1)).addClass('active');
  });
  cover.changeBanner('slow', 0);
});


$(document).ready(function() {
  $("a.target_blank").attr('target','_blank');
  $( "#bannerBox" ).tabs();
});


var speed=400;
var opened_class = 'active';
var current_active_menu_class = 'menu-active';
var menu_item_opened_class = 'opened';
$(document).ready(function() {
  if(typeof current_page_pid != "undefined")
    menu_set_active($('.menuitem-page-'+current_page_pid));
  $("#nav ul:has(li)").each(menu_hide);
  $("#nav li:has(ul)").mouseout(function () {
    $(this).removeClass(current_active_menu_class)
  });
  $("#nav li:has(ul)").each(function() {
    if(!$(this).hasClass(opened_class))
      my_slide(this);
  });
  $("#nav li:has(ul)").mouseover(function(event){
    $(this).addClass(current_active_menu_class);
    $(this).pause(200, function () {
      if($(this).hasClass(current_active_menu_class)) {
        if (this == event.target || this == event.target.parentNode) {
          var current = this;
          $("#nav li:has(ul)").each(function() {
            parents = $(current).parents('#nav li');
            for(i = 0; i< parents.length; i++) {
              if(parents[i] == this) {
                return ;
              }
            }
            parents = $(this).parents('#nav li');
            for(i = 0; i< parents.length; i++) {
              if(parents[i] == current) {
                return ;
              }
            }
            if (this != current && !$(this).hasClass(opened_class)) my_slide(this);//$(this).children().slideUp(speed);
          });
          $("ul:first", $(this)).slideDown(speed).parent().addClass(menu_item_opened_class);
        }
      }
    });
  });
});
function menu_set_active (object) {
  var obj = $(object);
  if(obj.length > 0) {
    if( obj.get(0).tagName != 'UL' || obj.get(0).id != "nav" ) {
      if(obj.get(0).tagName == 'LI')
        obj.addClass('active');
      menu_set_active(obj.parent());
    }
  }
}

function menu_hide () {
  if(!$($(this).parent()).hasClass('active')) {
    $(this).hide();
  }
}
function my_slide(obj) {
  $(obj).removeClass(menu_item_opened_class);
  $(obj).children().each(function() {

    if(this.tagName != "A" && this.tagName != "SPAN") {
      $(this).slideUp(speed);
    }
  })
}

function showMaterialsTop10(){
  // swap tabs
  tabTop10 = document.getElementById("tabMaterialsTop10");
  tabAll   = document.getElementById("tabMaterialsAll")
  if(tabTop10 && tabAll){
    tabTop10.className = '';
    tabAll.className = 'disabled';
  }
  // swap divs
  divTop10 = document.getElementById("materialsTop10");
  divAll = document.getElementById("materialsAll");
  if(divTop10 && divAll){
    divTop10.style.display = 'block';
    divAll.style.display   = 'none';
  }
  // enable / disable checkboxes
  inputs = divTop10.getElementsByTagName("input");
  for (var i=0; i <= (inputs.length-1); i++){
    inputs[i].disabled = false;
  }
  inputs = divAll.getElementsByTagName("input");
  for (var i=0; i <= (inputs.length-1); i++){
    inputs[i].disabled = true;
  }

  materials_input = document.getElementById('search_materials_input');
  if(materials_input)
    materials_input.value='top10';
  update_map_height();
}


function showMaterialsAll(){
  tabTop10 = document.getElementById("tabMaterialsTop10");
  tabAll   = document.getElementById("tabMaterialsAll")
  if(tabTop10 && tabAll){
    tabTop10.className = 'disabled';
    tabAll.className = '';
  }
  divTop10 = document.getElementById("materialsTop10");
  divAll = document.getElementById("materialsAll");
  if(divTop10 && divAll){
    divTop10.style.display = 'none';
    divAll.style.display   = 'block';
  }
  // enable / disable checkboxes
  inputs = divTop10.getElementsByTagName("input");
  for (var i=0; i <= (inputs.length-1); i++){
    inputs[i].disabled = true;
  }
  inputs = divAll.getElementsByTagName("input");
  for (var i=0; i <= (inputs.length-1); i++){
    inputs[i].disabled = false;
  }
  materials_input = document.getElementById('search_materials_input');
  if(materials_input)
    materials_input.value='all';
  update_map_height();
}
function update_map_height(){ // martin@tvarwebu.cz 2010-05-12
  myamap = document.getElementById('map');
  myasearch = document.getElementById('facility_search');
  providedby = document.getElementById('providedByRepak');
  if(myamap && myasearch){
    mysize = -3;//providedby.offsetHeight == 0 ? -12 : -12; // from the past
    //mysize = providedby.offsetHeight == 0 ? -9 : 25;
    myamap.style.height = (myasearch.clientHeight ? myasearch.clientHeight : myasearch.offsetHeight)+mysize+'px'; // changed from +25 to -9 because of "End of Life Vehicle Recycling" was added
    google.maps.event.trigger(map, "resize");
    //map.checkResize();
  }
}


$(document).ready(function() {
  $('.footer-sponsors ul').everyTime(4000, 'sponsors_footer', function () {
    if($(this).hasClass('mouse-is-over'))
      return;
    $(this).find('.animating').remove()
    children = $(this).children();
    to_scroll = $(children.get(0));
    clone = to_scroll.clone(true);
    clone.appendTo(this);
    $(to_scroll).addClass('animating').find('img').animate({width: 0, 'padding-left':0,'padding-right':0}, 'slow', function () {
      $(this).parents('li:eq(0)').remove();
    });
  }).mouseover(function () {
    $(this).addClass('mouse-is-over');
  }).mouseout(function () {
    $(this).removeClass('mouse-is-over');
  });
});


// *****************
// * Tracking clicks within the page
// *****************
$(document).ready(function() {
  $('a').live('click', function () { // track events (external link click, mailto click, pdf click)
    if(typeof settings_google_analytics_code != 'undefined' && typeof _gat != 'undefined'){
      //_gaq.push(['_trackPageview'], $(this).attr('href'));
      var filetypes_track = /\.(zip|exe|pdf|doc*|xls*|ppt*|mp3|jpg|png|gif)$/i;
      var pageTracker = _gat._getTracker(settings_google_analytics_code);
      var href = $(this).attr('href');
      if ((href.match(/^https?\:/i)) && (!href.match(document.domain))){
        var extLink = href.replace(/^https?\:\/\//i, '');
        pageTracker._trackEvent('External', 'Click - external link', extLink);
      }else if (href.match(/^mailto\:/i)){
        var mailLink = href.replace(/^mailto\:/i, '');
        pageTracker._trackEvent('Email', 'Click - mailto', mailLink);
      }else if (href.match(filetypes_track)){
        var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
        filePath = href.replace(new RegExp('^https?\:\/\/'+document.domain+'\/', "i"), '/');
        pageTracker._trackEvent('Download', 'Click - ' + extension, href);
      }
    }
  });
});

//**********************
//* menu autoresizing
//**********************
$(document).ready(function () {
  margin_left_and_right = 0; // margin-left + margin-right of .main_menu
  if((container = $('.main_menu').filter(":eq(0)")).length > 0) {
    if(container.width() < $('.page_menu').width()-margin_left_and_right-1) // 40 = margin-left + margin-right of .main_menu + 1px?
      total_w = $('.page_menu').width()-margin_left_and_right-1; // IE6 must rely on menu ul container
    else
      total_w = container.width();
    children_width_new = container.getChildrenWidth() + 0;

    addPadding = Math.floor((total_w - children_width_new) / (container.children().length * 2));
    children = container.children();
    for(i=0;i<children.length;i++) {
      $(children[i]).find('A').each(function () {
        if($(this).parents('.submenu').length == 0) {
          padd = {paddingLeft: parseInt($(this).css('paddingLeft')) + addPadding, paddingRight: parseInt($(this).css('paddingRight'))+addPadding};
          $(this).css(padd);
        }
      });
    }

    children_width_new = container.getChildrenWidth();
    to_add = total_w - children_width_new;
    child = children[0];
    paddingPlace = 'paddingLeft';
    while(to_add > 0 && child) {
      $(child).children().filter('A').each(function () {
        if($(this).parents('.submenu').length == 0) {
          if(to_add > 1) {
            $(this).css(paddingPlace, parseInt($(this).css(paddingPlace)) + 1);
          }
          to_add --;
        }
      });
      child = $(child).next();
      if(child == null || typeof child == 'object' && typeof child.length != 'undefined' && child.length == 0) {
        paddingPlace = 'paddingRight';
        child = children[0];
      }
    }
  }
});

$.extend($.fn, {
  getChildrenWidth: function () {
    children = $(this).children();
    total = 0;
    for(i=0;i<children.length;i++) {
      total += $(children[i]).width()+myParseInt($(children[i]).css('marginLeft'))+myParseInt($(children[i]).css('marginRight'))+myParseInt($(children[i]).css('paddingLeft'))+myParseInt($(children[i]).css('paddingRight'));
    }
    return total;
  }
});

function myParseInt(i) {
  i=parseInt(i);
  if(isNaN(i))
    return 0;
  return i;
}
// ***********************
// * Universal functions *
// ***********************
/**
  * parseClassToObject(): parses element's class and returns known elements as array
  * @param String prefix: class prefix
  * example:
  *  elements class = 'blah--name--value blah--name2--val'
  *  out = $(element).parseClassToObject('blah')
  *  output: object:{name: 'value', name2: 'val'};
*/
(function(cash) {$.extend($.fn, {
  parseClassToObject: function (prefix) {
    out = {};
    cl = $(this).attr('class');
    if(typeof cl == 'undefined')
      cl = $(this).attr('className');
    if(typeof cl != 'undefined' && cl.length > 0) {
      classes = cl.split(' ');
      reg=/^\-\-(.*)$/;
      for(i=0;i<classes.length;i++) {
        cl = $.trim(classes[i]);
        if(cl.indexOf(prefix) == 0){
          cl = cl.substr(prefix.length);
          if((preg = reg.exec(cl)) && preg[1].length > 0) {
            pos = preg[1].indexOf('--');
            if(pos > 0) {
              out[unescape((''+preg[1]).substr(0, pos))] = unescape((''+preg[1]).substr(pos+2));
            } else {
              out[out.length] = unescape(preg[1]);
            }
          }
        }
      }
    }
    return out;
  },
  changeBanner: function (speed, where, absolute) {
    if(typeof where == 'undefined' || where == null)
      where = 1;
    if(typeof absolute == 'undefined')
      absolute = null;
    cur_i = 0;
    cont = $(this).find('.bannerItem.item-shown').parent();
    children = cont.children();
    n = $('.bannerItem.item-shown');
    if(where > 0) {
      n = $(this).find('.bannerItem.item-shown').next('.bannerItem');
      if(n.length == 0 && $(this).find('.bannerItem.item-shown').length > 0) {
        for(i=$.inArray($(this).find('.bannerItem.item-shown').get(0), children)+1;i<children.length;i++) {
          if($(children[i]).hasClass('bannerItem') && ++cur_i == where){
            n = $(children[i]);
            break;
          }
        }
      }
      if(typeof n == 'undefined' || n == null || n.length == 0) {
        n = $(this).children('.bannerItem').filter(':eq(0)');
      }
    } else if (where < 0) {
      n = $(this).find('.bannerItem.item-shown').prev('.bannerItem');
      if(n.length == 0 && $(this).find('.bannerItem.item-shown').length > 0) {
        for(i=$.inArray($(this).find('.bannerItem.item-shown').get(0), children)-1;i>=0;i--) {
          if($(children[i]).hasClass('bannerItem') && --cur_i == where){
            n = $(children[i]);
            break;
          }
        }
      }
      if(typeof n == 'undefined' || n == null || n.length == 0) {
        n = $(this).children('.bannerItem').filter(':last');
      }
    } else if (where == 0 && absolute != null){
      n = $(this).find('.bannerItem:eq('+(absolute-1)+')');
    } else if (n.length == 0){
      n = $(this).find('.bannerItem:eq(0)');
    }
    sibl=$(this).find('.bannerItem.item-shown')
    if ($(this).find('.bannerItem').length > 1 || $('.bannerItem.item-shown').length == 0) {
      sibl.fadeOut(speed).removeClass('item-shown');
      n.stop(true, true).addClass('item-shown').fadeIn(speed);
      items = $(this).children('.bannerItem');
      change_function = $(this).data('change_function');
      if(typeof change_function == 'function') {
        change_function(this, n);
      }
      //$(this).find('.page_number').html(($.inArray(n.get(0), items)+1) + '/' + items.length);
    }
    if(!n.hasClass('item-shown')) {
      n.addClass('item-shown');
    }
    return this;
  }
})}) (jQuery);
function add_param_to_url(href,myvar,myvalue){
  return href + ((href.indexOf('?')>0) ? '&' : '?')+myvar+"="+escape(myvalue);
}
if(typeof $.fn.pause == "undefined") {
  $.fn.pause = function(duration, fnc) {
    $(this).animate({ dummy: 1 }, duration, fnc);
    return this;
  };
}



