var SERIALIZED_DATA = "";
var TOGGLE_PERIOD = 8000;

jQuery(document).ready(function(){  
  jQuery("a.level_2").hover(function(){
    jQuery(this).find("span").stop().animate({"paddingLeft": 5});
  }, function(){
    jQuery(this).find("span").stop().animate({"paddingLeft": 0});
  })
  
  jQuery("#q").focus(function(){
    if (jQuery(this).val() == jQuery(this).attr("title")) {;jQuery(this).val("")};
  });
  
  jQuery("#q").blur(function(){
    if (jQuery(this).val() == "") {jQuery(this).val(jQuery(this).attr("title"))};
  });
  
  jQuery("a.fancy_btn").fancybox({
    titleShow: false, 
    scrolling: "no", 
    autoDimensions: true,
    onComplete: function(){
      jQuery.fancybox.resize();
    }
  });
  
  jQuery('.scrollable').scrollable().navigator();
  
  jQuery(".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));
    });
  });
  
  jQuery(".menu_container li.menu_element").hover(function(){
    if (jQuery(this).find(".submenu").length > 0) {
      jQuery(this).addClass("hovered");
      jQuery(this).find(".submenu").show();
    };
  }, function(){
    if (jQuery(this).find(".submenu").length > 0) {
      jQuery(this).removeClass("hovered");
      jQuery(this).find(".submenu").hide();
    };
  });
})


//google map

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);
}

function addMarker(lat, lng, body){
  var position = new google.maps.LatLng(lat, lng);
  var marker = new google.maps.Marker({
      map: GOOGLEMAP,
      position: position,
      clickable: true
  });
  var infoWindow = new google.maps.InfoWindow({
    content: body,
    position: position
  });
  
  google.maps.event.addListener(marker, 'click', function(){
    infoWindow.open(GOOGLEMAP, marker);
  });
}



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.next().length > 0) {
      next = active.next();
    }else{
      next = parent.find(".nav div:first");
    };
    nnr = next.attr("id").split("_")[3];
  }
  return nnr;
}