var SERIALIZED_DATA = "";
var TOGGLE_PERIOD = 8000;

jQuery(document).load(function(){
  setBlocksHeight();
});

jQuery(document).ready(function(){

  jQuery("a.fancy_btn").fancybox({
    titleShow: false, 
    padding: 5, 
    scrolling: "no", 
    autoDimensions: true,
    onComplete: function(){
      jQuery.fancybox.resize();
    }
  });
  
  
  //auto clear search
  jQuery("#q").focus(function(){
    if (jQuery(this).val() == jQuery(this).attr("title")) {jQuery(this).val("").addClass("active");}
  });
  
  jQuery("#q").blur(function(){
    if (jQuery(this).val() == "") {jQuery(this).val(jQuery(this).attr("title")).removeClass('active');}
  });
  
  setBlocksHeight();
  
  
  
  
  jQuery(".big_branding").each(function(element){
    var parent = jQuery(this);
    if (jQuery(this).find("div.element").length > 1){
      toogleBrandingElement(parent.find("div.element:first").attr("id").split("_")[2], parent);
      parent.data("timerInterval", setInterval(function(){toogleSlowBrandingElement(null, parent);}, TOGGLE_PERIOD));
    }else{
      parent.find("div.nav").css({"display": "none"});
    };
    
    parent.find(".nav div").click(function(){
      if (parent.data("timerInterval")) {
        clearInterval(parent.data("timerInterval"));
      }
      toogleSlowBrandingElement(jQuery(this).attr("id").split("_")[3], parent);
      parent.data("timerInterval",setInterval(function(){toogleSlowBrandingElement(null, parent);}, TOGGLE_PERIOD));
    });
  });
});


function setBlocksHeight(){
  var h = 0;
  jQuery("#top_footer .block .content").each(function(){
    if (h <= jQuery(this).height()) {
      h = jQuery(this).height();
    };
  });
  jQuery("#top_footer .block .content").height(h);
  h = 0;
  jQuery("#center_footer .block .content").each(function(){
    if (h <= jQuery(this).height()) {
      h = jQuery(this).height();
    };
  });
  jQuery("#center_footer .block .content").height(h);
}

function toogleBrandingElement(nr, parent){
  var nnr = prepareNumberForToogleBranding(nr, parent);
  if ($.browser.msie) {
    parent.find(".branding_element_"+nnr).show();
    parent.find("div.element").not(".branding_element_"+nnr).hide();
  }else{
    parent.find(".branding_element_"+nnr).css({"opacity" :1}).show();
    parent.find("div.element").not(".branding_element_"+nnr).css({"opacity" :0}).hide();
  }
  
  parent.find(".nav div.branding_element_nav_"+nnr).addClass("active");
  parent.find(".nav div").not(".branding_element_nav_"+nnr).removeClass("active");
}



function toogleSlowBrandingElement(nr, parent){
  if ($.browser.msie) {
    toogleBrandingElement(nr, parent);  //problem z przeźroczystością przycisków
  }else{
    var nnr = prepareNumberForToogleBranding(nr, parent);
    parent.find(".branding_element_"+nnr).show().css({opacity: 0});
    parent.find(".branding_element_"+nnr).animate({opacity: 1});
    parent.find("div.element").not(".branding_element_"+nnr).animate({opacity: 0}, 300, function(){jQuery(this).hide()});

    parent.find(".nav div.branding_element_nav_"+nnr).addClass("active");
    parent.find(".nav div").not(".branding_element_nav_"+nnr).removeClass("active");
  };
}

function prepareNumberForToogleBranding(nr, parent){
  var nnr = nr;
  if(!nnr){
    var active = parent.find(".nav div.active");
    var next = null;
    if (active.prev().length > 0) {
      next = active.prev();
    }else{
      next = parent.find(".nav div:last");
    };
    nnr = next.attr("id").split("_")[3];
  }
  return nnr;
}

function toggleSubmenu(id){
  jQuery("."+id).toggleClass("hidden");
  jQuery(".toggle_"+id).find("span").toggleClass("active");
}


//google map

var GOOGLEMAP = null;
var GEOCODER = null;
function initializeGoogleMap(lat, lng, zoom,type){
  GEOCODER = new google.maps.Geocoder();
  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);
      });
    }
  }
}
