$j=jQuery.noConflict();



$j("document").ready(function(){
  var template_url=$j("#template_url").text();
  $j.loadImages([template_url+"/img/bg_body.jpg"],window.showContent);
});

showContent=function(){
  var template_url=$j("#template_url").text();
  //------------------- tweaking
  
  //-------------- preparem blockquote i imatge
  $entry=$j(".entry").eq(0);
  $blockquote=$entry.find("blockquote");
  $tweak_blockquote=$j("#menu-servicios,#content.cursos,#content.calendario,#content.enviar-texto");
  var $content=$j("#content");
  $menu=$content.find(".menu");
  $post=$content.find(".post");
  $enviarFirma=$content.hasClass("enviar-texto");
  $img=$entry.find("img").eq(0);  
  
  if($tweak_blockquote.length>0){
    $entry.prepend("<div class='entry-upper'></div>");
    
    $upper=$entry.find(".entry-upper");
    
    $post.css("padding-top","90px");
    
      if(!$enviarFirma){
        $upper
          .append($img)
      }
      $upper
        .append($blockquote)
        .css("padding-left",$menu.closest("div").width()-20);
    $upper_h=$upper.height();
    
    if($upper_h<$menu.closest("div").height()+20){
      $upper.height($menu.closest("div").height()+20);
    }
  }
  
  //border bottom del menu dreta, noticies i calendari
  $j("#menu-dreta li.menu-item:last-child")
    .add("#content .event:last-child")
    .add("#content .noticia:last-child")
    .css("border-bottom","0");
    
  //logo
  $j("#sidebar-2 h2.widgettitle")
    .html("<a href='"+$j("#wpurl").text()+"'><img src='"+$j("#template_url").text()+"/img/logo.png' /></a>");
  
  
  if($j("body").hasClass("page-template-noticies-php") || $j("body").hasClass("single-noticias") ){ // si la pàgina és noticies
    $j("div.navigation a[rel=next]")
      .css("text-align","right");
    $j("#menu-principal a[href$='noticias/']")
      .closest("li")
      .addClass("current-menu-item");
  }
  
  
  //
  /*if($j("#content").hasClass("test-proyectivos")){
    $j("div.entry").scrollable();
  }*/
  
  
  
  
};

//--------------------------------------------------------------------------------LOAD

$j(window).load(function(){

  var $post=$j(".post");
  var $content=$j("#content");
  var $page=$j("#page");
  
  //----------------------------------------------------------------- signatures
  if($j("body").hasClass("page-template-firmas-php")){
    
      
    var $imatges=$j(".ty_firmas img");
    
    var $foto=$imatges.eq(0);
    $foto.addClass("foto");
    
    var $signatura=$imatges.eq(1);
    $signatura.addClass("signatura");
    
    var $titol=$content.find(".post-titol");
    
    $content
      .prepend("<ul class='div-info'><li class='div-foto'/><li class='div-signatura' /></ul>");
      
    var $divInfo=$content.find(".div-info");
    
    $divInfo.find(".div-signatura").append($titol).append($signatura);
    $divInfo.find(".div-foto").append($foto);
    

    $signatura.imgResize();
    $foto.imgResize();
      
    var $divInfo_h=$foto.height();
    if($signatura.height()>$foto.height()){
      $divInfo_h=$signatura.height();
    }
    $divInfo
      .add($divInfo.find("li"))
      .height($divInfo_h+20);

    $signatura.vAlign();
    
  }  

  //----------------------------------------------------------- allarguem pàgina
  if(570<$post.outerHeight()+20){
    //ajustem alçades
    $content.add($j(".menu-principal-container")).height($post.outerHeight()+50);
    $page.prepend("<div id='fake-bg'></div>")
    $j("#fake-bg").height($post.outerHeight()+40-570);
    $page.add($j("html")).height($content.height());
    $j(window).resize();
  }
  var $fakeBg=$j("#fake-bg");

  //---------------------------------------------------------------------png fix
  if($j.browser.msie && $j.browser.version>=6 && $j.browser.version<7){
    $j("body").find("img[src$=.png]").addClass("png");
    $page.addClass("png");
    $content.addClass("png");
    $fakeBg.addClass("png");
    $page.find("blockquote").addClass("png");
    DD_belatedPNG.fix('.png');
  }

  //------------------------------resize
  $j(window).resize(function(){
    $j(".backstretch-wrap").remove();
    $j.backstretch($j("#template_url").text()+"/img/bg_body.jpg",{hideUntilReady:!$j.browser.msie});
    $j(".backstretch-wrap").addClass("fixme");
    
    //treiem overlay
    $j("#preload-overlay:visible").fadeOut(100);      
  }).resize();
  


  if($j("#content").hasClass("donde-estamos")){

    //var myOptions = {
    //  zoom: 14,
    //  mapTypeId: google.maps.MapTypeId.ROADMAP
    //};
    //var map = new google.maps.Map($("#map_canvas")[0], myOptions);
    //
    //var address = "Plaça Doctor Letamendi 35, 08007 Barcelona";
    //
    //var geocoder = new google.maps.Geocoder();
    //geocoder.geocode( { 'address': address}, function(results, status) {
    //  if (status == google.maps.GeocoderStatus.OK) {
    //    map.setCenter(results[0].geometry.location);
    //    var marker = new google.maps.Marker({
    //        map: map, 
    //        position: results[0].geometry.location
    //    });
    //  } else {
    //    alert("Geocode was not successful for the following reason: " + status);
    //  }
    //});
    
  }
  
});


