//middle branding

var is_ie = jQuery.browser.msie;

(function($){
  $(document).ready(function(){
    
    if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
     var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
     if (ieversion <= 7) {
      alert("Nasza strona nie jest już obsługiwana przez Twoją przeglądarkę. Aby przeglądać zawartość serwisu należy pobrać nowczesną przeglądarkę wspieraną przez nasz system. Za chwilę zostaniesz przekierowany do strony z której będzie można pobrać przeglądarkę firefox - jedną ze wspieranych przez nas.");

      window.location="http://www.mozilla.org/pl/firefox/new/";
      }
    }
    
    
    $(".middle_branding").each(function(){
      var that = this;
      $(this).data("selected", 0);
      $(this).data("count", $(this).find(".core .image").length);
      
      $(this).find(".nav").css({opacity: 0});
      $(this).find(".nav a b").css({opacity: 0});
      $(this).hover(function(){
        $(this).find(".nav").stop().animate({opacity: 1});
      }, function(){
        $(this).find(".nav").stop().animate({opacity: 0});
      })
      
      $(this).find(".nav a").hover(function(){
        $(this).find("b").stop().animate({opacity: 1});
        $(this).find("span").stop().animate({color: "#ffffff"});
      }, function(){
        $(this).find("b").stop().animate({opacity: 0});
        $(this).find("span").stop().animate({color: "#2c2c2c"});
      })
      var active = $(this).find(".core .image").first();
      $(this).find(".core .image").not(active).css({opacity: 0});
      // prev

    })
  })
})($);

var GOOGLEMAP = null;
function initializeGoogleMap(lat, lng, zoom,type){
  var latlng = new google.maps.LatLng(lat, lng);
  var myOptions = {
    zoom: zoom,
    center: latlng,
    mapTypeId: type
  };
  GOOGLEMAP = new google.maps.Map(document.getElementById("googlemap"), myOptions);
}


var MARKER = null;
function addMarker(lat, lng, body){
  var position = new google.maps.LatLng(lat, lng);
  
  var marker = new google.maps.Marker({
      map: GOOGLEMAP,
      position: position,
      clickable: (body ? true : false),
      draggable: (body ? false : true)
  });
  MARKER = marker;
  if (body!=null){
    if (body.length > 0) {
      var infoWindow = new google.maps.InfoWindow({
        content: body,
        position: position
      });
      google.maps.event.addListener(marker, 'click', function(){
        infoWindow.open(GOOGLEMAP, marker);
      });
    }
  }
}


$(document).ready(function() {
  readMore();
  
  $(".news_line").each(function(index) {
    var that = this;
    var images = $(this).find("img");
    var elements = $(this).find(".news_element");
    var wrapper = $(this);
    if (images.length > 0) {
      var loaded = 0;
      images.load(function(){
        loaded += 1;
        if (loaded == images.length) {
          elements.height(wrapper.height());
        };
      })
    }else{
      elements.height(wrapper.height());
    };
  });
  
  
  
  $(".fancy_block").each(function(index){
    $(this).hover(function() {
      $(this).find('.fancy_block_wrapper').stop().animate({top: -50});
      t = $($(".fb_tooltips .fb_tooltip")[$(this).index()]);
      is_ie ? t.show().stop().css({opacity: 1}) : t.show().stop().animate({opacity: 1});
      
    }, function() {
      if (is_ie) {
        $(this).find('.fancy_block_wrapper').stop().css({top: 0});
        t = $($(".fb_tooltips .fb_tooltip")[$(this).index()]);
        t.stop().css({opacity: 0}).hide();
      }else{
        $(this).find('.fancy_block_wrapper').stop().animate({top: 0});
        t = $($(".fb_tooltips .fb_tooltip")[$(this).index()]);
        t.stop().animate({opacity: 0}, {complete: function(){$(this).hide()}});
      };
    });
  });
  
  $(".fb_tooltips .fb_tooltip").each(function(index) {
    $(this).css({opacity: 0}).hide();
    var top = 176*$(this).index();
    if (top+$(this).height() > ($(".transparent_sidebar").height()-85)) {
      var dif = top+$(this).height() - ($(".transparent_sidebar").height()-85);
      $(this).find('.arrow').css({top: 30+dif})
      $(this).css({top: top-dif})
    }else{
      $(this).css({top: top})
    };
  });
  
  $('.witness_box').each(function(index) {
    $(this).find('.action').css({width: 0, opacity: 0});
    $(this).hover(function() {
      $(this).find('.action').stop().animate({width: 131, opacity: 1});
    }, function(){
      $(this).find('.action').stop().animate({width: 0, opacity: 0});
    });
  });
  
  $('.witness_list').each(function() {
    var items = $(this).find(".slides a");
    var slider = $(this).find(".slides");
    if (items.length < 13) {$(this).find("a.nav_link").hide();};
    var current_item = 0;
    $(this).find("a.prev").click(function(){goPrev();});
    $(this).find("a.next").click(function(){goNext();});
    function goNext () {
      current_item += 12;
      if (current_item > (items.length-1)-12) {current_item=items.length-1-12};
      slider.stop().animate({left: -$(items[current_item]).position().left});
      return false;
    }
    function goPrev () {
      current_item -= 12;
      if (current_item <= 0) {current_item=0};
      slider.stop().animate({left: -$(items[current_item]).position().left})
    }
    $(this).find('a.photo.unactive').css({opacity: 0.5}).hover(function() {
      $(this).stop().animate({opacity: 1});
    }, function() {
      $(this).stop().animate({opacity: 0.5});
    });
  });
  
  
  
  $(".timeline_wrapper").each(function(index) {
    var wrapper = $(this);
    var slider_container = wrapper.find(".slider");
    var pages = wrapper.find(".slider .timeline_element");
    var labels = wrapper.find(".navigation_slides .labels");
    var total_pages_width = 0;
    pages.each(function(index) {total_pages_width += $(this).width();});
    var label_scale = labels.width()/total_pages_width;
    pages.each(function(index) {
      var name = $(this).data().name;
      var width = parseInt($(this).width()*label_scale);
      labels.append("<div class='label_"+index%6+"' style='width: "+width+"px'><p></p><label>"+name+"</label></div>");
      labels.find("label").each(function(index) {
        $(this).css({left: -$(this).width()/2});
      });
    });
    var ui_slider = wrapper.find(".ui_slider");
    ui_slider.slider({
      max: total_pages_width,
      slide: function(event, ui){
        slider_container.css({left: -ui.value});
      },
      change: function(event, ui){
        slider_container.css({left: -ui.value});
      }
    });
    
    var start_drag = 0;
    
    slider_container.draggable({
      axis: "x",
      cancel: ".element",
      drag: function(event, ui){
        value = -ui.position.left;
        if (value <= ui_slider.slider("option", "max") && value >= ui_slider.slider("option", "min")) {
          ui_slider.slider("option", "value", value);
        };
      },
      helper: function(){return $("<div></div>");}
    });
    
    wrapper.mousewheel(function(event, delta, deltaX, deltaY) {
      var value = ui_slider.slider("option", "value");
      value = value + deltaX*30;
      if (value <= ui_slider.slider("option", "max") && value >= ui_slider.slider("option", "min")) {
        ui_slider.slider("option", "value", value);
      };
    });
    
    wrapper.find("a.prev").click(function() {
      var value = ui_slider.slider("option", "value");
      value = value - 30;
      if (value >= ui_slider.slider("option", "min")) {
        ui_slider.slider("option", "value", value);
      }else{
        ui_slider.slider("option", "value", ui_slider.slider("option", "min"));
      };
      return false;
    });
    
    wrapper.find("a.next").click(function() {
      var value = ui_slider.slider("option", "value");
      value = value + 30;
      if (value <= ui_slider.slider("option", "max")) {
        ui_slider.slider("option", "value", value);
      }else{
        ui_slider.slider("option", "value", ui_slider.slider("option", "max"));
      };
      return false;
      return false;
    });
  });
  
  
  
  $('.white_block').each(function(index) {
    $(this).hover(function() {
      $(this).find(".block_wrapper").stop().animate({top: 0});
    }, function() {
      $(this).find(".block_wrapper").stop().animate({top: -63});
    });
  });
  
  
  
  $('.user_footer').add(".sticky_push").height($('.user_footer .inner_footer').outerHeight());
  $('.sticky_wrapper').css({"margin-bottom": -$('.user_footer .inner_footer').outerHeight()});
  
  
  $('a.fancy_btn').fancybox({
    padding: 24,
    overlayOpacity: 0.7,
    overlayColor: "#000000",
    onComplete: function(){
      $("#fancybox-close").html($("#content_close").text());
    }
  });
  
  $('a.fancy_btn_in_timeline').fancybox({
    width: "550px",
    padding: 24,
    overlayOpacity: 0.7,
    overlayColor: "#000000",
    onComplete: function(){
      $("#fancybox-close").html($("#content_close").text());
      $('#fancybox-content').each(function(index) {
        // $(this).css({"border-right-width": 0});
        if ($(this).find(".scroll_pane").height() < $(this).find(".scroll_wrapper").height()) {
          $(this).find(".scroll_pane").jScrollPane();
        };
      });
      
      // 507px
    }
  });
  
  
  $('.circles a.circle span').each(function(index) {
    if ($(this).height() > 20){
      $(this).parent().css({height: 34, padding: "34px 0"});
    }
  });
  
  
  $(".suggested_line").each(function(index) {
    var wrapper = $(this).find(".sliding_wrapper");
    var slider = $(this).find(".slider")
    var current = slider.find(".suggestion").first();
    
    var max_left = 0
    $(this).find(".suggestion").each(function(index) {
      max_left += $(this).width()+50;
    });
    max_left = max_left - wrapper.width()+70;
    function prevSlide(){
      if (current.prev().length > 0) {
        current = current.prev();
        goToElement(current);
      };
    }
    
    function nextSlide(){
      if (current.next().length > 0 && current.next().position().left < max_left) {
        current = current.next();
        goToElement(current);
      };
    }
    
    function goToElement(element){
      var left = element.position().left;
      slider.stop().animate({left: -left});
    }
    
    $(this).find(".prev").click(function(){prevSlide(); return false;});
    $(this).find(".next").click(function(){nextSlide(); return false;});
  });
  
  
  $('.inner_footer .circles .element').each(function(index) {
    var tooltip = $(this).find('.tooltip');
    tooltip.css({top: (-tooltip.height()-27), opacity: 0}).hide();
    var timeoutv = null
    $(this).hover(function() {
      if (is_ie) {
        // if (timeoutv) {
        //   clearTimeout(timeoutv)
        // };
        tooltip.show().stop().css({opacity: 1});
      }else{
        tooltip.show().stop().animate({opacity: 1});  
      };
    }, function() {
      if (is_ie) {
        // timeoutv = setTimeout(1000, function(){
          tooltip.stop().css({opacity: 0}).hide();
        // })
      }else{
        tooltip.stop().animate({opacity: 0}, {complete: function(){$(this).hide()}});
      };
    });
  });
  
  $('.content_slides').each(function(index){
    var slides = $(this).find(".slide");
    var navs = $(this).find(".nav a");
    slides.css({opacity: 0});
    selectElement(navs.first());
    navs.click(function() {
      selectElement($(this));
      return false;
    });
    
    function selectElement(element){
      navs.not(element).removeClass("active");
      element.addClass("active");
      
      selected_slide = $(slides[element.index()]);
      slides.not(selected_slide).stop().animate({opacity: 0}, {complete: function(){$(this).hide()}});
      selected_slide.show().stop().animate({opacity: 1});
    }
  });
  
  jQuery("a.data_remote").live('click', function(){
    var ajax_options = {
      url: jQuery(this).attr("href"),
      type: "GET",
      beforeSend: function(xhr){
        jQuery.fancybox.showActivity();
        xhr.setRequestHeader('Accept', 'text/javascript');
      },
      complete: function(){jQuery.fancybox.hideActivity();}
    };
    if (jQuery(this).attr("data-confirm")) {
      if (confirm(jQuery(this).attr("data-confirm"))) {
        jQuery.ajax(ajax_options);
      };
    }else{
      jQuery.ajax(ajax_options);
    };
    return false;
  });
  
  $('.calendar .below').add(".inline_calendar .below").each(function(index) {
    prepareCalendar($(this));
  });
  
  function hideNotVisibleNewsOnMainPage(){
    $('.liquid_main_page .news_wrapper').each(function(index) {
      var wrapper = $(this);
      wrapper.find(".news_element").each(function(index) {
        if ($(this).offset().top + $(this).height() > wrapper.height()){
          $(this).css({opacity: 0});
        }else{
          $(this).css({opacity: 1});
        };
      });
    });
  }
  
  setTimeout(function(){hideNotVisibleNewsOnMainPage()},1000);
  
  $(window).load(function() {
    hideNotVisibleNewsOnMainPage();
  });
  
  hideNotVisibleNewsOnMainPage();
  $(window).resize(function() {
    hideNotVisibleNewsOnMainPage();
  });
  
  $(".total_branding").each(function(index) {
    var tooltips = $(this).find(".twrap .tooltip_on_main")
    if (is_ie) {
      tooltips.hide();
    }else{
      tooltips.css({opacity: 0}).show();
    };
    var links = $(this).find(".links a");
    links.hover(function() {
      if (is_ie) {
        $(tooltips[$(this).index()]).show()
      }else{
        $(tooltips[$(this).index()]).stop().animate({opacity: 1});
      };
      
    }, function() {
      if (is_ie) {
        $(tooltips[$(this).index()]).hide();
      }else{
        $(tooltips[$(this).index()]).stop().animate({opacity: 0});
      };
      
    });
  });
  
  $('.liquid_main_page').each(function(index) {
    var tooltip = $(this).find('.news_tooltip');
    var wrapper = $(this).find(".news_wrapper");
    tooltip.css({opacity: 0}).hide();
    tooltip.hover(function() {
      is_ie ? $(this).show() : $(this).stop().show().animate({opacity: 1});
    }, function() {
      is_ie ? $(this).hide() : $(this).stop().animate({opacity: 0}, {complete: function(){$(this).hide();}});
    });
    $(this).find('.normal_element h4').hover(function(){
      var parent = $(this).parents(".normal_element");
      if (parent.css("opacity")!=0) {
        is_ie ? tooltip.show().css({opacity: 1}) : tooltip.stop().show().animate({opacity: 1});
        var c = $(this).next();
        tooltip.find(".opener .content .variable").html(c.html());
        var top = $(this).offset().top - 90;
        if (top+tooltip.height() > wrapper.height()) {
          var dif = top+tooltip.height() - (wrapper.height()-5);
          tooltip.find('.arrow').stop().animate({top: 30+dif});
          tooltip.css({top: top-dif});
        }else{
          tooltip.css({top: top});
          tooltip.find('.arrow').stop().animate({top: 30});
        };
        readMore(tooltip.find(".read_more a"));
      };
    }, function() {
      is_ie ? tooltip.hide() : tooltip.stop().animate({opacity: 0}, {complete: function(){$(this).hide();}});
    });
  });
  
  $('#header .menu ul li.languages').hover(function(){
    $(this).addClass("hovered");
  }, function() {
    $(this).removeClass("hovered");
  });
  
  
  $('.main_baner .content').data("start_height", $('.main_baner .content').height());
  
  if ($.cookie('baner_state') == "hidden") {
    if ($('.main_baner').parents(".sticky_wrapper").length > 0) {
      $('.main_baner').addClass("baner_hidden").find(".content").css({height: 0, "margin-bottom": 0});
    }else{
      $('.main_baner').addClass("baner_hidden").css({top: -1*$('.main_baner').height()});
    };
    
  };  
  
  $('.main_baner a.hider').click(function() {
    if ($('.main_baner').hasClass("baner_hidden")) {
      $.cookie('baner_state', "show");
      if ($(this).parents(".sticky_wrapper").length > 0) {
        $('.main_baner .content').stop().animate({height: $('.main_baner .content').data("start_height"), "margin-bottom": 10},{complete: function(){$('.main_baner').removeClass("baner_hidden");}});
      }else{
        $('.main_baner').stop().animate({top: 0},{complete: function(){$(this).removeClass("baner_hidden");}});
      };
      
      $('.main_baner').find("a.hider").animate({bottom: 3});
    }else{
      $.cookie('baner_state', "hidden");
      if ($(this).parents(".sticky_wrapper").length > 0) {
        $('.main_baner .content').stop().animate({height: 0, "margin-bottom": 0},{complete: function(){$('.main_baner').addClass("baner_hidden");}});
      }else{
        $('.main_baner').stop().animate({top: -1*$('.main_baner').height()},{complete: function(){$(this).addClass("baner_hidden");}});
      }
      $('.main_baner').find("a.hider").animate({bottom: -23});
    };
    
    return false;
  });
  
  $('input#q').each(function(index) {
    selfClearInput($(this));
  });
  
  $('.with_sidebar .sidebar .sidebar_block').each(function(index) {
    $(this).find(".wrapper").css({top: -$(this).find("span").outerHeight()});
    $(this).hover(function() {
      $(this).find(".wrapper").stop().animate({top: 0});
    }, function() {
      $(this).find(".wrapper").stop().animate({top: -$(this).find("span").outerHeight()});
    });
  });
});





function prepareCalendar(wrapper){
  var elements = wrapper.find(".elements").show().css({opacity: 0});
  var nominal_left = parseInt(elements.css("left"));
  var nominal_arrow = parseInt(elements.find(".arrow").css("left"));
  
  if (elements.length > 0) {
    var diff = 0;
    if (elements.offset().left < 0) {
      diff = -1*elements.offset().left;
    };
    
    if ((elements.offset().left + elements.width()) > $(window).width()) {
      diff = -1*(elements.offset().left + elements.width()-$(window).width());
    };
    elements.css({left: nominal_left+diff});
    elements.find(".arrow").css({left: nominal_arrow-diff});
    
    elements.hide().css({opacity: 0});
    wrapper.parents(".day").hover(function() {
      elements.removeClass("reverted_elements")
      if (is_ie) {
        elements.show().stop().css({opacity: 0.01}).css({opacity: 1});
      }else{
        elements.show().stop().css({opacity: 0.01}).animate({opacity: 1});
      };
      
      if (elements.offset().top < 50) {
        elements.addClass("reverted_elements");
      };
    }, function() {
      if (is_ie) {
        elements.hide().css({opacity: 0});
      }else{
        elements.stop().animate({opacity: 0}, {complete: function(){elements.hide()}});
      };
      
    });
  };
}

function readMore(selector){
  if(!selector){
    selector = $(".read_more a");
  }
  selector.each(function(index) {
    $(this).find("span").css({width: "auto"});
    var base_width = $(this).find("span").width();
    $(this).find("span").css({width: 0});
    $(this).find("span").stop().animate({width: 0});
    $(this).hover(function() {
      $(this).find("span").stop().animate({width: base_width});
    }, function() {
      $(this).find("span").stop().animate({width: 0});
    });
  })
}

function selfClearInput (element) {
  element.focus(function() {
    if (element.val()==element.attr('title')) {
      element.val("");
    }
  }).blur(function(){
    if (element.val()=="") {
      element.val($(this).attr('title'));
    }
  })
}

