//var base = 'http://192.168.1.3/studio/1281_cipiuesse/last/';
var base = '/';

/*************************************
 * Funzioni della home
 ****************/

function setupHome() {

  var infiniteLoading = (function() {
    
    var winSize = window.innerHeight,
        isLoading = false,
        page = 1;
        pageElement = document.getElementById('centrato');
    
    function onScroll(e) {
      if(!isLoading && pageElement.offsetHeight - (scrollY + winSize) < 500 ) {
        isLoading = true;
        $.get(base+'web/inc/articles_home.php', { page: page++, lingua: lingua }, function(resp) {
          $('#corpo').append(resp);
          $('a.play_video').slimbox();
          isLoading = false;
        });
      }
    }
    
    $(window).bind('scroll.infload', onScroll);
    
  })();
  
  $('a.play_video').slimbox();
  
}


/*************************************
 * Funzioni della pagina di elenco
 ****************/

function setupElenco() {
  // hides subtitle if title is two lines
  //$('#corpo').showSubtitle();

  // bind load more...
  loadMoreContent.init();
  loadMoreContent.fragment = '#more';
  
  // bind secondary more...
  $('h2.link a').live('click', function(e) {
    e.preventDefault();
    var $p = $(this).parent(),
        $parent = $p.parent();
    
    $p.slideUp();
    $.get(this.href, function(resp) {
      loadMoreContent.add(resp, $parent);
    });
  });
  
}


function setupDettaglio() {
  
  loadMoreContent.init();
  
  //initAlbum();
  $('ul.img_list').find('a').slimbox();
  
  $('#d-nav').find('a').click(function() {
    var link = $(this).attr('href');
    nTo = $(link).offset().top - 100;
    $.scrollTo(nTo > 0 ? nTo : 0, 700);
  });
  
}


function setupPubblications() {
  
  $('#search_text').bind('keyup blur', function() {
    $(this).prev().toggle($(this).val().length === 0)
  });
  
  $('#search').bind('submit', function(e) {
    e.preventDefault();
    var string = $('#search_text').val().toLowerCase();
    
    if(string.length > 1) {
      $('ul.m-biblio').find('li').each(function() {
        $(this).toggle($(this).text().toLowerCase().indexOf(string) >= 0);
      });
    } else {
      $('ul.m-biblio').find('li').show();
    }
    
    $('ul.m-biblio li:visible:odd').removeClass('even').addClass('odd');
    $('ul.m-biblio li:visible:even').removeClass('odd').addClass('even');
  })
  
}


function setupPhilosophy() {
  
  $('#phil-link').click(function(e) {
    e.preventDefault();
    var link = $(this).attr('href');
    nTo = $(link).offset().top - 120;
    $.scrollTo(nTo > 0 ? nTo : 0, 500);
  });
  
  $('ul.album').svCarousel({
    transition_mode: "alone_out",
    set_height: true
  });
  
}



function setupContacts() {
  
}



/*************************************
 * Ajax load
 ****************/

var loadMoreContent = {
  
  init : function() {
    $('h2.title').find('a, span').click(function(e) {
      e.preventDefault();
      
      var $p = $(this).parent(),
          $next = $p.next();
      
      //$p.siblings('.open').removeClass('open').next().slideUp(1000);
      
      if(!$p.hasClass('open')) {
        if($next.find('article').length === 0) {
          var el = this;
          setTimeout(function() {
            $.get(el.href, function(resp) { loadMoreContent.add(resp, $next) });
          }, 500);
        } else {
          loadMoreContent.show($next);
        }
        $p.addClass('open');
      } else if($('#more').length < 1) {
        $p.removeClass('open');
        $next.slideUp(1000);
      } 
    });
  },
  
  add : function(resp, $el) {
    var $resp = $('html').hasClass('oldie') ? $(innerShiv(resp, false)) : $(resp);
    
    if(loadMoreContent.fragment) {
      $resp = $resp.find(loadMoreContent.fragment).children();
    }
    
    $resp.appendTo($el).each(function(i) {
      $(this).delay(100*i).queue(function(next) {
        $(this).addClass('fade-in'); //.showSubtitle();
        next();
      });
    });
  },
  
  show : function($el) {
    $el.find('figure[data-src]').html(function() {
      return '<img src="'+$(this).attr('data-src')+'">';
    })
    $el.slideDown(); //.showSubtitle();
  }
  
}


/*************************************
 * Funzione per l'album
 ****************/
  function initAlbum() {
    
    $('ul.album').svCarousel({
      transition_mode: "alone_out",
      set_height: true,
      
      controls: 2,
      controls_always: true,
      controls_labels: ["prev", "next", "Image "],
      
      autoplay: false,
      onload: function() {
        $(this).next('.svScorri').append('<p class="svTitle" />');
      }
    }, function() {
      $(this).parent().parent().find('p.svTitle').html($(this).find('[title]').attr('title'));
    }).find('a').slimbox();
    
    
  }



var backToTop = (function() {
  
  $('#top').bind('click', function(e) {
    e.preventDefault();
    $.scrollTo(0, 1000);
  });
  
})();


/* shows subtitle if title is one line
$.fn.showSubtitle = function() {
  $(this).find('h3').each(function() {
    var $h3 = $(this);
    if($h3.prev().innerHeight() < 30) {
      $h3.css('visibility','visible');
    }
  });
}
*/

/*************************************
 * Funzione per il menu
 ****************/
/*
var menuScroller = (function(w, $els) {
  
  var t, inTheMiddle = false, currentY,
      offsetTop = 0, height = 100+20;
  
  function onScroll(e) {
    currentY = scrollY;
    clearTimeout(t);
    if(inTheMiddle) {
      $els.removeClass('slide-in').fadeOut('fast', function() {
        $els.css('top', offsetTop).show();
      })
      inTheMiddle = false;
    }
    t = setTimeout(endScroll, 500);
  }
  
  function endScroll() {
    if(currentY > 100) {
      inTheMiddle = true;
      $els
        .css('top', currentY-height).delay(200)
        .queue(function(next) {
          $els.addClass('slide-in').css('top', currentY+offsetTop);
          next();
        });
    }
  }
  
  $(w).bind('scroll.menu', onScroll);
  
})(window, $('#intestazione'));
*/


/**
 * jQuery.ScrollTo - Easy element scrolling using jQuery.
 * Copyright (c) 2007-2009 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * @author Ariel Flesler
 * @version 1.4.2
 */
;(function(d){var k=d.scrollTo=function(a,i,e){d(window).scrollTo(a,i,e)};k.defaults={axis:'xy',duration:parseFloat(d.fn.jquery)>=1.3?0:1};k.window=function(a){return d(window)._scrollable()};d.fn._scrollable=function(){return this.map(function(){var a=this,i=!a.nodeName||d.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!i)return a;var e=(a.contentWindow||a).document||a.ownerDocument||a;return d.browser.safari||e.compatMode=='BackCompat'?e.body:e.documentElement})};d.fn.scrollTo=function(n,j,b){if(typeof j=='object'){b=j;j=0}if(typeof b=='function')b={onAfter:b};if(n=='max')n=9e9;b=d.extend({},k.defaults,b);j=j||b.speed||b.duration;b.queue=b.queue&&b.axis.length>1;if(b.queue)j/=2;b.offset=p(b.offset);b.over=p(b.over);return this._scrollable().each(function(){var q=this,r=d(q),f=n,s,g={},u=r.is('html,body');switch(typeof f){case'number':case'string':if(/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(f)){f=p(f);break}f=d(f,this);case'object':if(f.is||f.style)s=(f=d(f)).offset()}d.each(b.axis.split(''),function(a,i){var e=i=='x'?'Left':'Top',h=e.toLowerCase(),c='scroll'+e,l=q[c],m=k.max(q,i);if(s){g[c]=s[h]+(u?0:l-r.offset()[h]);if(b.margin){g[c]-=parseInt(f.css('margin'+e))||0;g[c]-=parseInt(f.css('border'+e+'Width'))||0}g[c]+=b.offset[h]||0;if(b.over[h])g[c]+=f[i=='x'?'width':'height']()*b.over[h]}else{var o=f[h];g[c]=o.slice&&o.slice(-1)=='%'?parseFloat(o)/100*m:o}if(/^\d+$/.test(g[c]))g[c]=g[c]<=0?0:Math.min(g[c],m);if(!a&&b.queue){if(l!=g[c])t(b.onAfterFirst);delete g[c]}});t(b.onAfter);function t(a){r.animate(g,j,b.easing,a&&function(){a.call(this,n,b)})}}).end()};k.max=function(a,i){var e=i=='x'?'Width':'Height',h='scroll'+e;if(!d(a).is('html,body'))return a[h]-d(a)[e.toLowerCase()]();var c='client'+e,l=a.ownerDocument.documentElement,m=a.ownerDocument.body;return Math.max(l[h],m[h])-Math.min(l[c],m[c])};function p(a){return typeof a=='object'?a:{top:a,left:a}}})(jQuery);



/**
 * innerShiv
 * makes HTML5shim work on innerHTML & jQuery
 * http://bit.ly/ishiv | WTFPL License
 */
window.innerShiv=function(){function h(c,e,b){return/^(?:area|br|col|embed|hr|img|input|link|meta|param)$/i.test(b)?c:e+"></"+b+">"}var c,e=document,j,g="abbr article aside audio canvas datalist details figcaption figure footer header hgroup mark meter nav output progress section summary time video".split(" ");return function(d,i){if(!c&&(c=e.createElement("div"),c.innerHTML="<nav></nav>",j=c.childNodes.length!==1)){for(var b=e.createDocumentFragment(),f=g.length;f--;)b.createElement(g[f]);b.appendChild(c)}d=d.replace(/^\s\s*/,"").replace(/\s\s*$/,"").replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi,"").replace(/(<([\w:]+)[^>]*?)\/>/g,h);c.innerHTML=(b=d.match(/^<(tbody|tr|td|col|colgroup|thead|tfoot)/i))?"<table>"+d+"</table>":d;b=b?c.getElementsByTagName(b[1])[0].parentNode:c;if(i===!1)return b.childNodes;for(var f=e.createDocumentFragment(),k=b.childNodes.length;k--;)f.appendChild(b.firstChild);return f}}();

